//! The production surface cannot see the test scaffolding (§6 M-4, M-9). //! //! The shim harness is load-bearing: the whole step machine is tested by driving //! the real binaries with fakes for `makepkg`, `mkarchroot`, `gpg` and friends. //! That is fine. What is not fine is production code reaching into it — the //! publish path once read a package's name through the shim module's copy of the //! `.PKGINFO` parser, which meant the bytes we signed and the bytes the tests //! believed were parsed by two pieces of code that could drift apart. //! //! The compile-time half of this rule is real: `shims` is compiled out unless the //! `test-support` feature is on, and only this crate's own dev-dependency turns //! it on. This test covers the half the compiler cannot: a module in `src/` //! naming the scaffolding directly. 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('\\', "/"); // The harness itself, and the shim binaries that are its front ends, are // allowed to know about it. 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() { // §6 M-5: two mutexes in `publish.rs`, each documented as *the* "one publish // per repo" guarantee, with `withdraw_package` taking only one of them, is // worse than either answer — a reader cannot tell whether the guarantee is // redundant or incomplete. There is one lock now, and this keeps it that way. 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"); // And both writers of the served index take it, not just one of them. 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() { // §6 M-9: fault injection in the publish hot path, shim environment // variables and clock pinning were part of the production API surface — // `Config` advertised `publish_fault` next to `signing_key_id`, and the // branch that reads it sat in the middle of every real publish. 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", ] { // Each appears exactly once, inside `TestHooks`, never on `Config`. 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() { // The two fault-injection points in `publish()` are the ones that ran on // every real publish. Each must sit behind the feature gate. 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() { // The state every real process runs in: nothing hooked, and nothing written // to config.json about hooks. 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() { // §6 m-2: `hosts` existed twice, verbatim, and the "did the executable part // of the recipe change" rule existed twice with *different* semantics — the // diff panel compared function header lines, the risk scorer compared the // bodies. One question, one answer, in the module that owns the subject. 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() { // §6 m-4: three review sites hand-built `AuditEntry` literals, each with its // own `detail` shape and its own spelling of `actor_kind`. 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"); }