use super::*;
use crate::test_environment::Environment;
#[test]
fn test20107_local_state_is_filed_by_place_and_never_in_the_checkout() {
let prj = tempfile::tempdir().expect("a workshop");
let machine = tempfile::tempdir().expect("a machine folder");
let elsewhere = tempfile::tempdir().expect("a folder outside the workshop");
for folder in ["alpha/main-worktree", "alpha/fix-worktree", "alpha/notes"] {
std::fs::create_dir_all(prj.path().join(folder)).unwrap();
}
let outside_first = elsewhere.path().join("first");
let outside_second = elsewhere.path().join("second");
std::fs::create_dir_all(&outside_first).unwrap();
std::fs::create_dir_all(&outside_second).unwrap();
let workshop = crate::platform::canonicalize(prj.path()).unwrap();
let machine_dir = machine.path().to_string_lossy().into_owned();
let mut env = Environment::set(&[
("PRJ", &workshop.to_string_lossy()),
(crate::daemon::MACHINE_DIR_ENV, &machine_dir),
]);
let fix = LocalState::of(&workshop.join("alpha/fix-worktree")).expect("a worktree's state");
assert_eq!(fix.dir(), workshop.join(".state/workspaces/alpha/worktrees/fix"));
assert_eq!(fix.key(), "alpha/fix");
assert_eq!(
LocalState::of(&workshop.join("alpha/main-worktree")).unwrap().dir(),
workshop.join(".state/workspaces/alpha/worktrees/main")
);
let loose = LocalState::of(&workshop.join("alpha/notes")).expect_err("a folder that is no worktree");
assert!(loose.to_string().contains("none of its worktrees"), "{loose}");
let first = LocalState::of(&outside_first).expect("an outside checkout's state");
assert_eq!(first.dir().parent(), Some(machine.path().join("outside").as_path()));
assert_eq!(first.key(), format!("outside/{}", first.dir().file_name().unwrap().to_string_lossy()));
assert_ne!(first, LocalState::of(&outside_second).unwrap(), "two checkouts share one folder");
#[cfg(unix)]
{
let link = elsewhere.path().join("link");
std::os::unix::fs::symlink(&outside_first, &link).unwrap();
assert_eq!(LocalState::of(&link).unwrap(), first, "a symlinked spelling found other state");
}
env.unset("PRJ");
let without = LocalState::of(&workshop.join("alpha/fix-worktree")).expect("state with no $PRJ");
assert!(without.dir().starts_with(machine.path().join("outside")), "{}", without.dir().display());
}