use super::*;
struct ReposRootGuard(Option<std::ffi::OsString>);
impl ReposRootGuard {
fn set(dir: &Path) -> Self {
let prev = std::env::var_os(super::super::inproject::REPOS_ROOT_ENV);
unsafe { std::env::set_var(super::super::inproject::REPOS_ROOT_ENV, dir) };
Self(prev)
}
}
impl Drop for ReposRootGuard {
fn drop(&mut self) {
unsafe {
match self.0.take() {
Some(v) => std::env::set_var(super::super::inproject::REPOS_ROOT_ENV, v),
None => std::env::remove_var(super::super::inproject::REPOS_ROOT_ENV),
}
}
}
}
fn gh() -> GithubPath {
trusty_common::github_path::parse_github_path("https://github.com/an-owner/a-repo")
.expect("fixture URL parses")
}
const ORIGIN: &str = "https://github.com/an-owner/a-repo";
#[test]
fn managed_checkout_is_the_base_clone_path() {
let _lock = crate::core::trusty_tools_config::env_test_lock();
let tmp = tempfile::TempDir::new().expect("temp dir");
let _env = ReposRootGuard::set(tmp.path());
assert_eq!(
managed_checkout_for(&gh()),
tmp.path().join("an-owner").join("a-repo"),
"the managed checkout is <workspace-root>/<owner>/<repo>"
);
}
#[test]
fn managed_launch_dir_is_left_alone() {
let _lock = crate::core::trusty_tools_config::env_test_lock();
let tmp = tempfile::TempDir::new().expect("temp dir");
let _env = ReposRootGuard::set(tmp.path());
let managed = tmp.path().join("an-owner").join("a-repo");
let placement =
resolve_placement(&managed, &gh(), ORIGIN).expect("equality branch never fails");
assert_eq!(placement, managed);
assert!(
!managed.exists(),
"the equality branch must not provision anything: {}",
managed.display()
);
}
#[test]
fn unmanaged_launch_dir_redirects_to_managed() {
let _lock = crate::core::trusty_tools_config::env_test_lock();
let tmp = tempfile::TempDir::new().expect("temp dir");
let _env = ReposRootGuard::set(tmp.path());
let managed = tmp.path().join("an-owner").join("a-repo");
std::fs::create_dir_all(managed.join(".git")).expect("seed a base clone");
let unmanaged = tempfile::TempDir::new().expect("unmanaged temp dir");
let placement = resolve_placement(unmanaged.path(), &gh(), ORIGIN).expect("redirect succeeds");
assert_eq!(
placement, managed,
"an unmanaged launch directory must switch to the managed checkout"
);
assert_eq!(
std::fs::read_dir(unmanaged.path())
.expect("read unmanaged dir")
.count(),
0,
"the unmanaged launch directory must never be written to"
);
}
#[test]
fn provisioning_failure_is_an_error_not_a_fallback() {
let _lock = crate::core::trusty_tools_config::env_test_lock();
let tmp = tempfile::TempDir::new().expect("temp dir");
let _env = ReposRootGuard::set(tmp.path());
let managed = tmp.path().join("an-owner").join("a-repo");
std::fs::create_dir_all(managed.join(".worktrees")).expect("seed protected state");
let unmanaged = tempfile::TempDir::new().expect("unmanaged temp dir");
let err = resolve_placement(unmanaged.path(), &gh(), ORIGIN)
.expect_err("a base clone that cannot be established must fail the launch");
assert!(
err.contains(&unmanaged.path().display().to_string()),
"the refusal must name the unmanaged directory it will not run in: {err}"
);
}
#[test]
fn deny_worktree_fallback_errors_when_a_worktree_was_requested() {
let id = ManagedSessionId::new();
let err = deny_worktree_fallback(&id, true, "base clone unavailable")
.expect_err("an unhonourable worktree request must abort the spawn");
assert!(err.contains("base clone unavailable"), "{err}");
assert!(
err.contains(&id.to_string()),
"the error must name the session: {err}"
);
}
#[test]
fn deny_worktree_fallback_permits_the_fallback_when_none_was_requested() {
let id = ManagedSessionId::new();
assert!(
deny_worktree_fallback(&id, false, "base clone unavailable").is_ok(),
"no worktree was requested, so the local-path fallback stays available"
);
}