use super::*;
#[test]
fn test9960_git_and_wsb_can_never_be_carried() {
for refused in [
".git",
".git/config",
".git/modules/capdag/HEAD",
"cartridges/.git",
"cartridges/mlx/.git/objects/ab/cd",
".wsb",
".wsb/secrets.wsbs",
"wsb/.wsb/state.json",
] {
assert!(
is_refused(std::path::Path::new(refused)),
"{refused} was not refused"
);
}
for allowed in ["src/main.rs", "git/notes.md", "wsb/src/lib.rs", ".github/workflows/ci.yml"] {
assert!(
!is_refused(std::path::Path::new(allowed)),
"{allowed} was refused"
);
}
}
#[test]
fn test9961_a_peer_cannot_name_a_path_outside_the_tree() {
let root = std::path::Path::new("/workspace");
for hostile in [
"../outside",
"a/../../outside",
"/etc/passwd",
".git/hooks/pre-commit",
".wsb/secrets.wsbs",
"src\\windows.rs",
"",
] {
assert!(
local_path(root, hostile).is_err(),
"{hostile:?} resolved to a path"
);
}
let resolved = local_path(root, "src/peer/mod.rs").expect("an ordinary path");
assert_eq!(resolved, root.join("src/peer/mod.rs"));
assert!(resolved.starts_with(root));
}
#[test]
fn test9962_the_set_is_tracked_plus_untracked_minus_ignored() {
let temp = tempfile::tempdir().expect("tempdir");
let dir = temp.path();
run(dir, &["init", "-q", "-b", "main"]);
run(dir, &["config", "user.name", "Test"]);
run(dir, &["config", "user.email", "test@example.com"]);
std::fs::write(dir.join(".gitignore"), "target/\n*.log\n").expect("write");
std::fs::create_dir_all(dir.join("src")).expect("mkdir");
std::fs::create_dir_all(dir.join("target/debug")).expect("mkdir");
std::fs::write(dir.join("src/main.rs"), "fn main() {}\n").expect("write");
run(dir, &["add", "."]);
run(dir, &["commit", "-qm", "one"]);
std::fs::write(dir.join("src/main.rs"), "fn main() { work() }\n").expect("write");
std::fs::write(dir.join("src/new.rs"), "// in progress\n").expect("write");
std::fs::write(dir.join("target/debug/binary"), "ELF").expect("write");
std::fs::write(dir.join("run.log"), "noise").expect("write");
std::fs::write(dir.join(".wsb-not-refused"), "ordinary").expect("write");
let set = read(dir, &Policy::default()).expect("a set");
let paths: Vec<&str> = set.keys().map(String::as_str).collect();
assert!(paths.contains(&"src/main.rs"), "{paths:?}");
assert!(paths.contains(&".gitignore"), "{paths:?}");
assert!(paths.contains(&"src/new.rs"), "an untracked file is work in progress: {paths:?}");
assert!(!paths.contains(&"target/debug/binary"), "{paths:?}");
assert!(!paths.contains(&"run.log"), "{paths:?}");
assert!(paths.contains(&".wsb-not-refused"), "{paths:?}");
let entry = set.get("src/main.rs").expect("the modified file");
assert_eq!(
entry.hash,
crate::peer::converge::hash_bytes(b"fn main() { work() }\n")
);
}
#[test]
fn test9963_policy_is_declared_compared_and_bounded() {
let temp = tempfile::tempdir().expect("tempdir");
let dir = temp.path();
run(dir, &["init", "-q", "-b", "main"]);
run(dir, &["config", "user.name", "Test"]);
run(dir, &["config", "user.email", "test@example.com"]);
std::fs::write(dir.join(".gitignore"), ".env\nbuild/\n").expect("write");
std::fs::write(dir.join("kept.rs"), "x\n").expect("write");
run(dir, &["add", "."]);
run(dir, &["commit", "-qm", "one"]);
std::fs::write(dir.join(".env"), "TOKEN=abc\n").expect("write");
std::fs::create_dir_all(dir.join("build")).expect("mkdir");
std::fs::write(dir.join("build/out"), "big").expect("write");
let plain = read(dir, &Policy::default()).expect("a set");
assert!(!plain.contains_key(".env"));
let opted = Policy {
include_ignored: vec![".env".into()],
exclude: vec![],
};
let with_env = read(dir, &opted).expect("a set");
assert!(with_env.contains_key(".env"), "an explicit include must be carried");
assert!(!with_env.contains_key("build/out"), "only what was named");
let excluded = Policy {
include_ignored: vec![],
exclude: vec!["kept.rs".into()],
};
assert!(!read(dir, &excluded).expect("a set").contains_key("kept.rs"));
let hostile = Policy {
include_ignored: vec![".git".into(), ".wsb".into(), ".git/config".into()],
exclude: vec![],
};
let set = read(dir, &hostile).expect("a set");
assert!(
set.keys().all(|path| !is_refused(std::path::Path::new(path))),
"a declaration re-admitted a refused name: {:?}",
set.keys().collect::<Vec<_>>()
);
let one = Policy {
include_ignored: vec![".env".into(), "fixtures/".into()],
exclude: vec![],
};
let same = Policy {
include_ignored: vec!["fixtures".into(), "/.env".into()],
exclude: vec![],
};
one.agrees_with(&same).expect("the same policy, spelled differently");
let different = Policy {
include_ignored: vec![".env".into()],
exclude: vec!["src".into()],
};
let error = one.agrees_with(&different).expect_err("a real difference");
assert!(error.contains("exclude"), "{error}");
}
fn run(dir: &std::path::Path, args: &[&str]) {
let output = crate::git_backend::git_command()
.args(args)
.current_dir(dir)
.output()
.unwrap_or_else(|e| panic!("git {args:?}: {e}"));
assert!(
output.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test10121_a_scan_stops_at_a_nested_repository() {
let temp = tempfile::tempdir().expect("tempdir");
let dir = temp.path();
run(dir, &["init", "-q", "-b", "main"]);
run(dir, &["config", "user.name", "Test"]);
run(dir, &["config", "user.email", "test@example.com"]);
std::fs::write(dir.join("own.txt"), "the parent's\n").expect("write");
let inner = dir.join("inner");
std::fs::create_dir_all(inner.join("src")).expect("mkdir");
std::fs::write(inner.join(".git"), "gitdir: ../.git/modules/inner\n").expect("write");
std::fs::write(inner.join("src/deep.rs"), "// the submodule's\n").expect("write");
std::fs::write(inner.join("top.txt"), "also the submodule's\n").expect("write");
let set = read(dir, &Policy::default()).expect("a set");
let paths: Vec<&str> = set.keys().map(String::as_str).collect();
assert!(paths.contains(&"own.txt"), "the parent's own file: {paths:?}");
for claimed in ["inner/src/deep.rs", "inner/top.txt", "inner/.git"] {
assert!(
!paths.contains(&claimed),
"the parent claimed {claimed}, which belongs to the repository at inner/: {paths:?}"
);
}
std::fs::create_dir_all(dir.join("plain")).expect("mkdir");
std::fs::write(dir.join("plain/ordinary.txt"), "still mine\n").expect("write");
let set = read(dir, &Policy::default()).expect("a set");
assert!(
set.keys().any(|path| path == "plain/ordinary.txt"),
"an ordinary subdirectory stopped being walked: {:?}",
set.keys().collect::<Vec<_>>()
);
}
#[test]
fn test10122_a_nested_checkout_stops_the_walk_at_any_depth() {
let temp = tempfile::tempdir().expect("tempdir");
let dir = temp.path();
run(dir, &["init", "-q", "-b", "main"]);
run(dir, &["config", "user.name", "Test"]);
run(dir, &["config", "user.email", "test@example.com"]);
let nested = dir.join("vendor").join("thing");
std::fs::create_dir_all(nested.join(".git")).expect("mkdir");
std::fs::create_dir_all(nested.join("src")).expect("mkdir");
std::fs::write(nested.join("src/lib.rs"), "// theirs\n").expect("write");
std::fs::write(dir.join("vendor/notes.md"), "mine\n").expect("write");
let set = read(dir, &Policy::default()).expect("a set");
let paths: Vec<&str> = set.keys().map(String::as_str).collect();
assert!(paths.contains(&"vendor/notes.md"), "{paths:?}");
assert!(
!paths.contains(&"vendor/thing/src/lib.rs"),
"a clone inside the tree was claimed as this repository's: {paths:?}"
);
}