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 (§6 M-4). 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"); }