use std::path::{Path, PathBuf}; fn read(rel: &str) -> String { std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join(rel)).expect("read source") } fn rust_files(dir: &Path, out: &mut Vec) { for entry in std::fs::read_dir(dir).expect("read_dir") { let path = entry.expect("entry").path(); if path.is_dir() { rust_files(&path, out); } else if path.extension().is_some_and(|e| e == "rs") { out.push(path); } } } #[test] fn no_production_module_references_the_shim_harness() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); let mut files = Vec::new(); rust_files(&root, &mut files); let mut offenders: Vec = Vec::new(); for file in files { let rel = file.strip_prefix(&root).unwrap_or(&file).to_string_lossy().replace('\\', "/"); if rel.starts_with("shims") || rel.starts_with("bin/") { continue; } let text = std::fs::read_to_string(&file).expect("read source"); for (no, line) in text.lines().enumerate() { let t = line.trim_start(); if t.starts_with("//") { continue; } if line.contains("shims::") { offenders.push(format!("{rel}:{}: {t}", no + 1)); } } } assert!( offenders.is_empty(), "production code must not depend on the test harness. Parse with `pkginfo`, not with the shim:\n{}", offenders.join("\n") ); } #[test] fn the_served_index_has_exactly_one_serialization_lock() { let src = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/pipeline/publish.rs")) .expect("read publish.rs"); let mutexes = src.lines().filter(|l| l.contains("static") && l.contains("Mutex<")).collect::>(); assert_eq!(mutexes.len(), 1, "publish.rs must declare exactly one serialization mutex, found {mutexes:?}"); assert!(!src.contains("PUBLISH_LOCK"), "the second lock must stay deleted"); let acquisitions = src.lines().filter(|l| l.contains("lock_repo()") && !l.trim_start().starts_with("fn ")).count(); assert!( acquisitions >= 2, "publish and withdraw_package must both take the repo lock; found {acquisitions} acquisitions" ); } #[test] fn the_shim_binaries_are_test_only() { let manifest = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml")).expect("read Cargo.toml"); for tool in ["mkarchroot", "makechrootpkg", "makepkg", "repo-add", "gpg", "pacman"] { let block = manifest .split("[[bin]]") .find(|b| b.contains(&format!("name = \"{tool}\""))) .unwrap_or_else(|| panic!("no [[bin]] for {tool}")); assert!( block.contains("required-features = [\"test-support\"]"), "the {tool} shim is a test binary and must not be built for a production target" ); } } #[test] fn the_test_hooks_are_a_gated_submodule_not_config_fields() { let cfg_src = read("src/config.rs"); for field in [ "pub path_prepend", "pub shim_manifest", "pub shim_dir", "pub publish_fault", "pub shim_fail", "pub shim_kill_parent", "pub shim_plant_link", ] { let hits = cfg_src.matches(field).count(); assert_eq!(hits, 1, "{field} must be declared once, on TestHooks — not on Config"); } assert!(cfg_src.contains("pub struct TestHooks"), "the hooks must live in one named submodule"); assert!( cfg_src.contains("#[cfg(feature = \"test-support\")]"), "the hooks must be compiled out without the test-support feature" ); } #[test] fn the_publish_hot_path_has_no_ungated_test_branch() { let src = read("src/pipeline/publish.rs"); let lines: Vec<&str> = src.lines().collect(); let mut gated = 0; for (i, line) in lines.iter().enumerate() { if line.contains("hooks.publish_fault") { let window = lines[i.saturating_sub(3)..i].join("\n"); assert!( window.contains("#[cfg(feature = \"test-support\")]"), "fault injection at publish.rs:{} is compiled into production builds", i + 1 ); gated += 1; } } assert_eq!(gated, 2, "expected the two known fault points, found {gated}"); } #[test] fn a_default_config_has_no_hooks_attached() { let cfg = aur_build::config::Config::default(); assert!(cfg.hooks.is_empty()); let json = serde_json::to_string(&cfg).unwrap(); assert!(!json.contains("publish_fault"), "hooks leaked into the serialized config: {json}"); } #[test] fn the_review_modules_do_not_keep_private_copies_of_the_rules() { for (file, forbidden) in [ ("src/review/diff.rs", ["fn hosts(", "fn body_changed("]), ("src/review/risk.rs", ["fn hosts(", "fn body_changed("]), ] { let src = read(file); for f in forbidden { assert!(!src.contains(f), "{file} still defines {f} — use recipe::bodies_changed / urlutil::hosts"); } } assert!(read("src/review/diff.rs").contains("urlutil::hosts")); assert!(read("src/review/risk.rs").contains("urlutil::hosts")); assert!(read("src/review/diff.rs").contains("recipe::bodies_changed")); assert!(read("src/review/risk.rs").contains("recipe::bodies_changed")); } #[test] fn audit_rows_are_built_by_the_constructors_not_by_hand() { let src = read("src/review/approve.rs"); assert!(!src.contains("db::AuditEntry {"), "audit rows must be built by AuditEntry::human/system"); assert_eq!(src.matches("AuditEntry::human(").count(), 3, "approve, reject and withdraw"); } #[test] fn the_virtual_clock_is_compiled_out_without_test_support() { let src = read("src/time.rs"); assert!( src.contains("#[cfg(feature = \"test-support\")]"), "the virtual clock must be behind the test-support feature" ); let fallback = src .split("#[cfg(not(feature = \"test-support\"))]") .nth(1) .expect("without test-support there must be a clock module that is always real"); assert!( fallback.contains("pub fn virtual_sleep() -> bool") && fallback.contains("false"), "the fallback must report a real wait so the seam compiles to a plain sleep" ); assert!(!fallback.contains("Mutex"), "nothing may be locked on the wait path of a production build"); } #[test] fn control_loops_wait_through_the_clock_seam() { for file in ["src/pipeline/dispatch.rs", "src/watch/scheduler.rs", "src/http/sse.rs", "src/sandbox/remote.rs"] { let src = read(file); assert!( !src.contains("std::thread::sleep"), "{file} waits on the wall clock; go through crate::time so a test can drive it" ); assert!( src.contains("crate::time::wait") || src.contains("crate::time::sleep"), "{file} never reaches the clock seam" ); } let agent = read("src/sandbox/agent.rs"); assert!(agent.contains("crate::time::sleep"), "the watchdog must use the seam"); assert_eq!( agent.matches("std::thread::sleep").count(), 1, "only the hub connect retry may sleep on the wall clock: it runs in the agent binary, which has no test-support to virtualize" ); }