use std::fs;
use std::path::{Path, PathBuf};
use openmoat_hosts::{Host, ProjectFile, ProjectOff};
use super::{Off, turned_off};
fn project() -> (tempfile::TempDir, PathBuf, PathBuf) {
let dir = tempfile::tempdir().unwrap();
let home = dir.path().join("home");
let project = home.join("proj");
fs::create_dir_all(project.join(".git")).unwrap();
(dir, home, project)
}
fn write(root: &Path, file: &ProjectFile, text: &str) {
let path = root.join(file.path);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
fn examples() -> Vec<(Host, ProjectFile)> {
Host::ALL
.into_iter()
.filter_map(|host| match host.spec().hook_off.project {
ProjectOff::Files { example, .. } => Some((host, example)),
ProjectOff::Nothing { .. } => None,
})
.collect()
}
fn found(host: Host, cwd: &Path, home: &Path) -> Vec<Off> {
let keys = host
.spec()
.hook_off
.session_override
.as_ref()
.map_or(&[][..], |o| o.keys);
turned_off(cwd, home, host, &home.join("hook-file"), keys)
}
#[test]
fn each_hosts_example_turns_its_hook_off_where_the_host_loads_it() {
let hosts = examples();
assert!(hosts.len() >= 7, "{hosts:?}");
for (host, example) in hosts {
let (_dir, home, project) = project();
write(&project, &example, example.text);
let off = found(host, &project, &home);
assert_eq!(off.len(), 1, "{host:?}: {off:?}");
assert!(off[0].path.ends_with(example.path), "{host:?}: {off:?}");
assert!(!off[0].what.is_empty(), "{host:?}");
let sub = project.join("src/deep");
fs::create_dir_all(&sub).unwrap();
assert!(
!found(host, &sub, &home).is_empty(),
"{host:?}: the root's file"
);
let overridable = host.spec().hook_off.session_override.is_some();
assert_eq!(off[0].overridden, overridable, "{host:?}");
}
}
#[test]
fn files_that_turn_nothing_off_or_are_not_loaded_pass() {
for (host, example) in examples() {
let (_dir, home, project) = project();
let empty = if Path::new(example.path)
.extension()
.is_some_and(|e| e == "toml")
{
"[features]\nhooks = true\n"
} else {
"{}\n"
};
write(&project, &example, empty);
let elsewhere = ProjectFile::new("fixtures/repo", "");
let copy = project.join(elsewhere.path).join(example.path);
fs::create_dir_all(copy.parent().unwrap()).unwrap();
fs::write(©, example.text).unwrap();
assert_eq!(found(host, &project, &home), Vec::new(), "{host:?}");
write(&project, &example, "{ not json\n[[");
assert_eq!(found(host, &project, &home), Vec::new(), "{host:?}");
}
}
#[test]
fn an_override_covers_only_its_own_keys() {
for (host, example) in examples() {
let Some(over) = &host.spec().hook_off.session_override else {
continue;
};
let (_dir, home, project) = project();
write(&project, &example, example.text);
assert!(found(host, &project, &home)[0].overridden, "{host:?}");
let text: serde_json::Value = serde_json::from_str(example.text).unwrap();
let mut rest = text.as_object().unwrap().clone();
rest.retain(|key, _| !over.keys.contains(&key.as_str()));
write(&project, &example, &serde_json::to_string(&rest).unwrap());
assert_eq!(found(host, &project, &home), Vec::new(), "{host:?}");
}
}
#[test]
fn substituted_text_and_folders_between_count() {
for (host, example) in examples() {
let (_dir, home, root) = project();
let deep = root.join("a/b");
fs::create_dir_all(&deep).unwrap();
write(&root.join("a"), &example, example.text);
assert_eq!(
found(host, &deep, &home).len(),
1,
"{host:?}: the middle folder"
);
let markers = host
.spec()
.project_settings
.as_ref()
.unwrap()
.reader
.substitutions();
for marker in markers {
let (_dir, home, root) = project();
write(
&root,
&example,
&format!("{{\"model\": \"{marker}X}}\"}}\n"),
);
let off = found(host, &root, &home);
assert_eq!(off.len(), 1, "{host:?} {marker}: {off:?}");
}
}
}
#[test]
fn the_home_directory_is_not_a_project() {
for (host, example) in examples() {
let (_dir, home, _project) = project();
write(&home, &example, example.text);
assert_eq!(found(host, &home, &home), Vec::new(), "{host:?}");
}
}