diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fa8a4a489b..c874c9fc73 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -141,6 +141,11 @@ repos: # cargo clippy -p e2e-cucumber --test e2e -- -D warnings # cargo test --workspace --all-targets --exclude e2e-cucumber # and `cargo xtask pre-push-check --base ` runs one by hand. + # A step that already passed on the same tree (same cargo command, toolchain + # and environment, clean working tree) is skipped and says so; a green run + # records the pass under the git directory. `ROCM_PRE_PUSH_RERUN=1` (or + # `--rerun` by hand) ignores recorded passes, and `SKIP=` still + # skips a hook. - id: cargo-clippy name: cargo clippy entry: cargo xtask pre-push-check clippy diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a3091b821f..8704da4e68 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -48,7 +48,7 @@ prek install -t pre-push # heavier checks on push (clippy + tests of the crat `prek` runs the same checks locally that CI enforces: `cargo fmt`, `clippy`, `cargo test`, `ruff` (Python), `shellcheck` (shell), PowerShell syntax, license headers (`hawkeye`), markdown links (`lychee`), and the generated manifests (`MANIFEST.md`, `THIRD_PARTY_NOTICES.txt`). -The pre-push hooks (`cargo clippy`, the e2e harness clippy and `cargo test`) check only the crates your push affects: the crates whose files changed plus every crate that depends on them, selected the way CI's `cargo xtask affected` does. They check the whole workspace when a change cannot be confined to specific crates (the lockfile, the root manifest, the toolchain file, workflow files, a doc an xtask test reads, or a path no crate owns), and when the affected crates cannot be determined at all, for example because the remote branch tip is not fetched locally or the pushed range lists no changed files. They also add the crates whose tests read files from outside their own crate: any Rust file adds `xtask`, whose contract tests scan every source file, and `README.md`, `docs/` and `skills/` add `rocm` and `xtask`. Each step prints the scope it chose and why. A push that changes no Rust file runs none of the cargo steps, whatever else it changes (a lockfile-only push included). A green hook is an early warning and not the gate: on pull requests CI lints the whole workspace, but its Linux test job runs only the affected crates, and the whole workspace's tests run in the Windows debug job, in the merge queue and on `main`. To see what a push would check without compiling anything, run `cargo xtask pre-push-check test --dry-run --base origin/main` (the steps are `clippy`, `clippy-e2e` and `test`). +The pre-push hooks (`cargo clippy`, the e2e harness clippy and `cargo test`) check only the crates your push affects: the crates whose files changed plus every crate that depends on them, selected the way CI's `cargo xtask affected` does. They check the whole workspace when a change cannot be confined to specific crates (the lockfile, the root manifest, the toolchain file, workflow files, a doc an xtask test reads, or a path no crate owns), and when the affected crates cannot be determined at all, for example because the remote branch tip is not fetched locally or the pushed range lists no changed files. They also add the crates whose tests read files from outside their own crate: any Rust file adds `xtask`, whose contract tests scan every source file, and `README.md`, `docs/` and `skills/` add `rocm` and `xtask`. Each step prints the scope it chose and why. A step that already passed on this exact content is skipped, and says so: after a green run it records a stamp keyed on the git tree hash of `HEAD`, the exact cargo command (so a pass for a narrow selection never covers a wider one), the toolchain and the `CARGO_*` and `RUST*` environment, under the clone's `.git` directory where every worktree shares it, so a retried push, a manual run before the push or an amend that only rewords the message does not pay for the same checks twice. Stamps are only used and written when the working tree is clean (cargo checks the working tree, not the pushed commit, and an untracked file can hide a missing `git add`), expire after seven days, and do not cover your user-level cargo configuration, installed tools, files git ignores or flaky tests. A skip shows that this tree passed those checks earlier, not that they ran during this push; the `cargo test --workspace` and `cargo clippy --workspace` commands you run by hand are unaffected, and `--rerun` gives a fresh run when you want proof the checks ran for this push. `cargo xtask pre-push-check --rerun`, or `ROCM_PRE_PUSH_RERUN=1` for the hook, ignores them, drops this step's earlier pass so a failed rerun cannot leave it to skip the next run, and records a fresh pass when the step succeeds; `SKIP=` still skips a hook outright, and the commit-signature hook runs on every push. A push that changes no Rust file runs none of the cargo steps, whatever else it changes (a lockfile-only push included). A green hook is an early warning and not the gate: on pull requests CI lints the whole workspace, but its Linux test job runs only the affected crates, and the whole workspace's tests run in the Windows debug job, in the merge queue and on `main`. To see what a push would check without compiling anything, run `cargo xtask pre-push-check test --dry-run --base origin/main` (the steps are `clippy`, `clippy-e2e` and `test`). The manifest hooks only run when you change the dependency graph. They *rewrite* the generated file instead of only reporting that it is stale. When that happens, the commit stops so you can re-stage the refreshed file. diff --git a/docs/testing.md b/docs/testing.md index 4dbd6cc1ac..0f3cd0b810 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -448,14 +448,61 @@ Known limits, unchanged by the narrowing: the steps check the working tree rathe commit; under prek 0.4.5 a push of several refs, a tag or a deletion runs no hooks; and `-p` builds can resolve Cargo features differently from `--workspace` ones. -Preview what a push would check without compiling: +### Pass stamps + +A step that already passed on this exact content is skipped. After a step's cargo command exits 0, +[prepush_stamp.rs](../xtask/src/prepush_stamp.rs) records a stamp, and the next run of the same step +looks for it first. The stamp stands for: + +- the step and its exact cargo arguments, so a pass recorded for `-p xtask` never lets a later run + over `--workspace` skip (the selection is part of the key, not a separate check); +- the git *tree* hash of `HEAD`, not the commit hash, so an amend that only rewords the message, or + the same content pushed to a second remote, still hits; +- the compiler and cargo identity (`rustc -vV`, `cargo -vV`) and a digest of the `CARGO_*` and + `RUST*` environment (never the values), minus a short list of per-run variables such as + `CARGO_TARGET_DIR` and `CARGO_TERM_COLOR`. The environment is read as raw bytes, so a variable + whose value is not valid Unicode never stops the hook: an unrelated one is ignored and a + relevant one is hashed as the bytes it is. + +The working tree has to be clean for a stamp to be used or written, because cargo checks the working +tree, not the pushed commit: with uncommitted changes the step runs and says why, and an untracked +file counts because it can hide a missing `git add`. The tree is probed again after the step, and +nothing is recorded if cargo changed it (a rewritten `Cargo.lock`, for example). A pass is only +written after cargo succeeded, per step, so a failed `test` does not erase the `clippy` pass that +the retry then skips. + +Stamps live in `rocm-cli-prepush-passes` under the git common directory, so every worktree of a +clone shares them, nothing lands in the work tree and `cargo clean` does not erase them. Each write +drops stamps older than seven days and keeps at most 64. A write goes through a scratch file and a +rename, and removes the scratch file itself when the rename fails; a process killed mid-write +leaves its scratch file behind until a later write prunes it, once it is more than an hour old (a +younger one may belong to a concurrent writer). Any error locating, reading or verifying a +stamp means the step runs (git is run for an explicit directory with `GIT_DIR` and the other +repository-selecting variables removed, which git and prek export to a hook run from a linked +worktree); a stamp that does not match its key exactly, is dated in the future or is +unreadable is ignored (the step names it); a failed write is reported and never fails the hook. + +The skip message says what a stamp does not cover: user-level cargo configuration, installed +tools, files git ignores (the one thing a clean `git status` cannot see) and flaky tests. Force +a run with `--rerun` or `ROCM_PRE_PUSH_RERUN=1` (the hook has no flags, so use the variable +there), or delete the directory the message names. `SKIP=` still skips a hook outright, +and `verify-commits` is a separate hook that checks commit signatures on every push. A skip +proves that this tree passed these checks earlier, not that they ran during this push; for a +fresh run before relying on the result, use `--rerun`. A rerun replaces the recorded pass: it +drops this step's earlier pass before running, so if the rerun fails the next ordinary run runs +the step instead of skipping on the pass it was meant to double-check, and a successful rerun +records a fresh one. Other steps' and other selections' passes are left alone, and `--dry-run` +never touches a stamp. + +Preview what a push would check without compiling (a dry run also says when a real run would skip): ```bash cargo xtask pre-push-check test --dry-run --base origin/main ``` `--base` replaces the range's base; `clippy` and `clippy-e2e` take the same flags. The selection -logic is unit-tested in [prepush.rs](../xtask/src/prepush.rs); as dev tooling, with no change to +logic and the stamps are unit-tested in [prepush.rs](../xtask/src/prepush.rs) and +[prepush_stamp.rs](../xtask/src/prepush_stamp.rs); as dev tooling, with no change to what the `rocm` binary does, it has no Gherkin scenario. The smoke path is the cross-platform local no-fallback acceptance surface. It diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 403afef349..de223062de 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -29,6 +29,7 @@ mod package; mod paths; mod powershell; mod prepush; +mod prepush_stamp; mod release_readiness; mod report_mailbox; mod report_rollup; @@ -42,7 +43,7 @@ mod workflow_contract; use std::path::PathBuf; use std::process::ExitCode; -use anyhow::Result; +use anyhow::{Result, bail}; use clap::{Parser, Subcommand}; #[derive(Parser)] @@ -118,6 +119,10 @@ pub(crate) enum Command { /// cannot be determined, and prints why. It is skipped only when the /// selection is known and has nothing for the step to check. CI always /// checks the whole workspace. + /// + /// A step that already passed on this exact tree (same cargo command, + /// toolchain and environment, clean working tree) is skipped, and a pass is + /// recorded after a green run; see `xtask/src/prepush_stamp.rs`. PrePushCheck { /// Which step to run. #[arg(value_enum)] @@ -129,6 +134,10 @@ pub(crate) enum Command { /// Print the scope and the cargo command without running it. #[arg(long)] dry_run: bool, + /// Run the step even when this tree already passed it, and record the + /// new result. `ROCM_PRE_PUSH_RERUN=1` does the same, for the hook. + #[arg(long)] + rerun: bool, }, /// Fail if any first-party crate has gained a normal/build dependency /// edge outside the allowlist pinned in `xtask/src/crate_edges.rs`. @@ -402,11 +411,7 @@ fn run() -> Result<()> { } => signing::verify(public_key.as_deref(), &input, &signature)?, Command::VerifyPinnedKeys => verify_pinned_keys::run()?, Command::Affected { base } => affected::run(base)?, - Command::PrePushCheck { - check, - base, - dry_run, - } => prepush::run(check, base, dry_run)?, + command @ Command::PrePushCheck { .. } => run_pre_push_check(command, prepush::run)?, Command::CheckCrateEdges => crate_edges::run()?, Command::CheckModuleSize => module_size::run()?, Command::CheckModuleSizeAdvisory => module_size::run_advisory()?, @@ -487,6 +492,25 @@ fn run() -> Result<()> { Ok(()) } +/// The `pre-push-check` arm of [`run`]: hand each argument to `run` as parsed. +/// `run` is a parameter so a test sees exactly what arrives, in particular that +/// `--rerun` is not dropped on the way to the stamp logic. +fn run_pre_push_check( + command: Command, + run: impl FnOnce(prepush::Check, Option, bool, bool) -> Result<()>, +) -> Result<()> { + let Command::PrePushCheck { + check, + base, + dry_run, + rerun, + } = command + else { + bail!("not a pre-push-check command"); + }; + run(check, base, dry_run, rerun) +} + fn main() -> ExitCode { match run() { Ok(()) => ExitCode::SUCCESS, @@ -496,3 +520,67 @@ fn main() -> ExitCode { } } } + +#[cfg(test)] +mod tests { + use clap::Parser; + + use super::{Cli, Command}; + + fn rerun(args: &[&str]) -> bool { + let cli = Cli::try_parse_from(args).expect("the arguments parse"); + let Command::PrePushCheck { rerun, .. } = cli.command else { + panic!("expected pre-push-check"); + }; + rerun + } + + /// What the dispatch hands to `prepush::run` for these arguments: the real + /// clap parse, then the `pre-push-check` arm, with `run` recording its input. + fn handed_to_run(args: &[&str]) -> (String, Option, bool, bool) { + let cli = Cli::try_parse_from(args).expect("the arguments parse"); + let mut seen = None; + super::run_pre_push_check(cli.command, |check, base, dry_run, rerun| { + seen = Some((format!("{check:?}"), base, dry_run, rerun)); + Ok(()) + }) + .expect("the dispatch succeeds"); + seen.expect("the arm calls run") + } + + #[test] + fn the_dispatch_hands_every_flag_to_run_unchanged() { + assert_eq!( + handed_to_run(&["xtask", "pre-push-check", "test"]), + ("Test".to_string(), None, false, false) + ); + assert_eq!( + handed_to_run(&[ + "xtask", + "pre-push-check", + "clippy", + "--base", + "origin/main", + "--dry-run", + "--rerun", + ]), + ( + "Clippy".to_string(), + Some("origin/main".to_string()), + true, + true + ) + ); + // `--rerun` alone, and `--dry-run` alone, are not mixed up with each other. + assert!(handed_to_run(&["xtask", "pre-push-check", "test", "--rerun"]).3); + assert!(!handed_to_run(&["xtask", "pre-push-check", "test", "--rerun"]).2); + assert!(handed_to_run(&["xtask", "pre-push-check", "test", "--dry-run"]).2); + assert!(!handed_to_run(&["xtask", "pre-push-check", "test", "--dry-run"]).3); + } + + #[test] + fn pre_push_check_takes_rerun_and_defaults_to_off() { + assert!(rerun(&["xtask", "pre-push-check", "test", "--rerun"])); + assert!(!rerun(&["xtask", "pre-push-check", "test"])); + } +} diff --git a/xtask/src/prepush.rs b/xtask/src/prepush.rs index 403bb4f3e5..9ac9fec066 100644 --- a/xtask/src/prepush.rs +++ b/xtask/src/prepush.rs @@ -31,18 +31,27 @@ //! manual run, it diffs `origin/main...HEAD` like `cargo xtask affected` does. //! `--base` overrides the from-ref. //! +//! A step that already passed on this exact tree is skipped, and a green run is +//! recorded: see [`crate::prepush_stamp`] for what a pass is keyed on (the +//! step's cargo arguments, so a narrow pass never covers a wider run; the tree +//! hash of `HEAD`; the toolchain; the environment) and why a dirty working tree +//! neither uses nor writes one. +//! //! As in [`crate::affected`], the decisions ([`push_from_env`], [`plan`], //! [`hook_selection`], [`resolve_selection`]) are pure and unit-tested; running -//! git and cargo is confined to [`run`], and [`execute`] takes both as -//! parameters so its own behavior (the range it diffs, `--dry-run`, a failing -//! cargo) is tested too. +//! git and cargo is confined to [`run`], and [`execute`] takes both, the stamps +//! and the output as parameters so its own behavior (the range it diffs, +//! `--dry-run`, a failing cargo, a skipped or recorded pass, and what it prints +//! about each) is tested too. use std::collections::BTreeSet; +use std::io::Write; use std::process::Command; use anyhow::{Context, Result, bail}; use crate::affected::{self, DEFAULT_BASE, Graph, Selection}; +use crate::prepush_stamp::{self, Lookup, Stamps}; /// The e2e harness crate. `cargo test` excludes it (its scenarios run through /// `cargo xtask e2e`), and its harness target is linted by its own step. @@ -357,39 +366,94 @@ impl Env { } } +/// What one invocation asks for. +#[derive(Debug, Clone, Default)] +pub struct Request { + /// Replaces the range's base (`--base`). + pub base: Option, + /// Print the plan without running or recording anything (`--dry-run`). + pub dry_run: bool, + /// The refs prek exported. + pub env: Env, +} + /// Run one step: resolve the range (an explicit `base` wins over prek's /// from-ref), pick the affected crates, print the scope, and hand the planned -/// cargo arguments to `run_cargo` unless the step is skipped or `dry_run`. +/// cargo arguments to `run_cargo` unless the step is skipped, `dry_run`, or a +/// pass of exactly this command on this tree is on record (`stamps`). /// -/// `load_graph`, `changed_files` (given the range's base and tip) and -/// `run_cargo` are parameters so the behavior around them is unit-tested. +/// A pass is recorded only after `run_cargo` succeeded. `load_graph`, +/// `changed_files` (given the range's base and tip), `run_cargo` and `out` are +/// parameters so the behavior around them is unit-tested. pub fn execute( check: Check, - base: Option, - dry_run: bool, - env: Env, + request: Request, + stamps: &Stamps<'_>, load_graph: impl FnOnce() -> Result, changed_files: impl FnOnce(&str, &str) -> Result>, mut run_cargo: impl FnMut(&[String]) -> Result<()>, + out: &mut impl Write, ) -> Result<()> { + let label = check.label(); + let Request { base, dry_run, env } = request; let push = push_from_env(base.or(env.from_ref), env.to_ref); let (selection, fallback) = resolve_selection(load_graph, || changed_files(push.base(), &push.to)); - println!("{}", banner(check, &push, &selection, fallback.as_deref())); + writeln!( + out, + "{}", + banner(check, &push, &selection, fallback.as_deref()) + )?; let args = match plan(check, &selection) { Plan::Skip(reason) => { - println!("pre-push-check {}: skipped, {reason}", check.label()); + writeln!(out, "pre-push-check {label}: skipped, {reason}")?; return Ok(()); } Plan::Cargo(args) => args, }; - println!("pre-push-check {}: cargo {}", check.label(), args.join(" ")); + writeln!(out, "pre-push-check {label}: cargo {}", args.join(" "))?; + let key = match stamps.lookup(label, &args) { + Lookup::Hit { name, age_secs } => { + if dry_run { + writeln!( + out, + "pre-push-check {label}: a real run would skip this step, it passed {} ago", + prepush_stamp::age_text(age_secs) + )?; + } else { + let location = stamps.location(); + for line in prepush_stamp::skip_lines(label, &args, age_secs, &name, &location) { + writeln!(out, "{line}")?; + } + } + return Ok(()); + } + Lookup::Miss { key, note } => { + if let Some(note) = note { + writeln!(out, "pre-push-check {label}: {note}")?; + } + key + } + }; if dry_run { return Ok(()); } - run_cargo(&args) + + // `--rerun` replaces the recorded pass: drop it first, so a run that fails + // leaves nothing for the next ordinary run to skip on, and write a fresh one + // only once cargo succeeded. + if stamps.rerun + && let Some(note) = key.as_ref().and_then(|key| stamps.invalidate(key)) + { + writeln!(out, "pre-push-check {label}: {note}")?; + } + run_cargo(&args)?; + if let Some(note) = key.and_then(|key| stamps.record(&key)) { + writeln!(out, "pre-push-check {label}: {note}")?; + } + Ok(()) } /// Run `cargo` with `args`, failing when it does. @@ -405,16 +469,91 @@ fn spawn_cargo(args: &[String]) -> Result<()> { Ok(()) } +/// A process environment variable as text. A value that is not valid Unicode is +/// still a set value (`std::env::var` reports it as an error, which `.ok()` would +/// read as unset, so a non-Unicode `ROCM_PRE_PUSH_RERUN` would be ignored). +fn process_env(name: &str) -> Option { + std::env::var_os(name).map(|value| value.to_string_lossy().into_owned()) +} + +/// What the `pre-push-check` command line asked for. +pub struct Flags { + pub base: Option, + pub dry_run: bool, + pub rerun: bool, +} + +/// The world one invocation runs against: where passes are kept, how the tree +/// and toolchain are probed, the clock, and how a process variable is read. +pub struct World<'a> { + pub probe: &'a dyn prepush_stamp::Probe, + pub store: &'a dyn prepush_stamp::Store, + /// Seconds since the epoch. + pub now: u64, + pub env: &'a dyn Fn(&str) -> Option, +} + /// Entry point for the `pre-push-check` subcommand. -pub fn run(check: Check, base: Option, dry_run: bool) -> Result<()> { - execute( +pub fn run(check: Check, base: Option, dry_run: bool, rerun: bool) -> Result<()> { + let store: Box = match prepush_stamp::FsStore::in_git_common_dir(None) + { + Ok(store) => Box::new(store), + Err(error) => Box::new(prepush_stamp::UnavailableStore(format!("{error:#}"))), + }; + let probe = prepush_stamp::SystemProbe::default(); + run_in( check, - base, - dry_run, - Env::from_lookup(|name| std::env::var(name).ok()), + Flags { + base, + dry_run, + rerun, + }, + &World { + probe: &probe, + store: store.as_ref(), + now: prepush_stamp::now_secs(), + env: &process_env, + }, affected::load_graph, affected::changed_files_between, spawn_cargo, + &mut std::io::stdout().lock(), + ) +} + +/// [`run`] with the world injected: this is where `--rerun` and +/// `ROCM_PRE_PUSH_RERUN` reach the stamp logic, so a test drives it with a fake +/// store and sees whether a recorded pass is honored. +pub fn run_in( + check: Check, + flags: Flags, + world: &World<'_>, + load_graph: impl FnOnce() -> Result, + changed_files: impl FnOnce(&str, &str) -> Result>, + run_cargo: impl FnMut(&[String]) -> Result<()>, + out: &mut impl Write, +) -> Result<()> { + let Flags { + base, + dry_run, + rerun, + } = flags; + let stamps = Stamps::new(world.probe, world.store, world.now, rerun, |name| { + (world.env)(name) + }); + let request = Request { + base, + dry_run, + env: Env::from_lookup(|name| (world.env)(name)), + }; + execute( + check, + request, + &stamps, + load_graph, + changed_files, + run_cargo, + out, ) } @@ -423,6 +562,10 @@ mod tests { use std::collections::{BTreeMap, BTreeSet}; use super::*; + use crate::prepush_stamp::fakes::{FakeProbe, MemStore}; + + /// A fixed clock for the stamp tests. + const NOW: u64 = 1_800_000_000; fn packages(names: &[&str]) -> Selection { Selection::Packages(names.iter().map(|name| (*name).to_string()).collect()) @@ -836,24 +979,32 @@ mod tests { } } - /// Run a step against canned inputs; `files` of `None` makes the diff fail. - /// Returns the result, every `(base, tip)` the diff was asked for, and the - /// argument lines handed to cargo. - fn run_step( + /// Everything one `execute` call did. + struct StepRun { + result: Result<()>, + ranges: Ranges, + /// The argument lines handed to cargo. + cargo: Vec, + /// What the step printed. + out: String, + } + + /// Run a step against canned inputs, with the given stamps; `files` of + /// `None` makes the diff fail. + fn run_step_with( check: Check, - base: Option<&str>, - dry_run: bool, - env: Env, + request: Request, + stamps: &Stamps<'_>, files: Option<&[&str]>, cargo_fails: bool, - ) -> (Result<()>, Ranges, Vec) { + ) -> StepRun { let ranges = std::cell::RefCell::new(Vec::new()); let cargo = std::cell::RefCell::new(Vec::new()); + let mut out = Vec::new(); let result = execute( check, - base.map(String::from), - dry_run, - env, + request, + stamps, || Ok(graph()), |base, tip| { ranges @@ -871,8 +1022,44 @@ mod tests { } Ok(()) }, + &mut out, ); - (result, ranges.into_inner(), cargo.into_inner()) + StepRun { + result, + ranges: ranges.into_inner(), + cargo: cargo.into_inner(), + out: String::from_utf8(out).expect("the step prints UTF-8"), + } + } + + /// Run a step with stamps that never apply (the working tree reads as dirty), + /// so these tests see only the range, `--dry-run` and cargo's result. + /// Returns the result, every `(base, tip)` the diff was asked for, and the + /// argument lines handed to cargo. + fn run_step( + check: Check, + base: Option<&str>, + dry_run: bool, + env: Env, + files: Option<&[&str]>, + cargo_fails: bool, + ) -> (Result<()>, Ranges, Vec) { + let probe = FakeProbe::clean("tree"); + *probe.tree.borrow_mut() = Err("the working tree is dirty".to_string()); + let store = MemStore::default(); + let stamps = Stamps { + probe: &probe, + store: &store, + now: NOW, + rerun: false, + }; + let request = Request { + base: base.map(String::from), + dry_run, + env, + }; + let run = run_step_with(check, request, &stamps, files, cargo_fails); + (run.result, run.ranges, run.cargo) } const XTASK_ONLY: Option<&[&str]> = Some(&["xtask/src/main.rs"]); @@ -982,6 +1169,943 @@ mod tests { assert_eq!(Env::from_lookup(|_| None), Env::default()); } + // ---- pass stamps: skip a step that already passed on this exact tree ---- + + const XTASK_TEST: &str = "test -p xtask --all-targets"; + const WIDE_TEST: &str = "test --workspace --all-targets --exclude e2e-cucumber"; + + fn stamps<'a>(probe: &'a FakeProbe, store: &'a MemStore, now: u64, rerun: bool) -> Stamps<'a> { + Stamps { + probe, + store, + now, + rerun, + } + } + + fn run_at( + stamps: &Stamps<'_>, + check: Check, + files: &[&str], + dry_run: bool, + cargo_fails: bool, + ) -> StepRun { + let request = Request { + dry_run, + ..Request::default() + }; + run_step_with(check, request, stamps, Some(files), cargo_fails) + } + + #[test] + fn a_green_run_is_recorded_and_the_next_identical_run_is_skipped_with_the_reason() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let first = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + first.result.expect("the first run"); + assert_eq!(first.cargo, [XTASK_TEST]); + assert_eq!(store.files.borrow().len(), 1, "a green run is recorded"); + + let second = run_at( + &stamps(&probe, &store, NOW + 90, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + second.result.expect("the skipped run"); + assert!( + second.cargo.is_empty(), + "the step must not run again: {:?}", + second.cargo + ); + assert!( + second + .out + .contains(&format!("pre-push-check test: skipped, this tree already passed `cargo {XTASK_TEST}` 1 min ago")), + "{}", + second.out + ); + // The skip names what it does not cover and how to force a run. + assert!( + second.out.contains("user-level cargo configuration"), + "{}", + second.out + ); + assert!(second.out.contains("--rerun"), "{}", second.out); + assert!(second.out.contains("files git ignores"), "{}", second.out); + assert!(second.out.contains("installed tools"), "{}", second.out); + assert!(second.out.contains("flaky tests"), "{}", second.out); + assert!( + second.out.contains(prepush_stamp::RERUN_VAR), + "{}", + second.out + ); + assert!(second.out.contains(""), "{}", second.out); + assert_eq!(store.files.borrow().len(), 1, "a skip records nothing"); + } + + /// A pushed range that lists no changed files means the range is wrong, not + /// that nothing changed, so on the stamped path too it runs the whole + /// workspace: it is never skipped as "no Rust crate", an earlier narrow pass + /// does not cover it, and it is skipped only by its own earlier pass. + #[test] + fn an_empty_changed_file_list_runs_the_whole_workspace_on_the_stamped_path() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let narrow = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert_eq!(narrow.cargo, [XTASK_TEST]); + + let empty = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &[], + false, + false, + ); + empty.result.expect("the empty-range run"); + assert_eq!(empty.cargo, [WIDE_TEST], "{}", empty.out); + assert!( + empty + .out + .contains("the pushed range lists no changed files"), + "{}", + empty.out + ); + assert!(!empty.out.contains("skipped"), "{}", empty.out); + assert_eq!(store.files.borrow().len(), 2, "the wide pass is recorded"); + + let again = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Test, + &[], + false, + false, + ); + assert!(again.cargo.is_empty(), "{:?}", again.cargo); + assert!( + again + .out + .contains(&format!("this tree already passed `cargo {WIDE_TEST}`")), + "{}", + again.out + ); + + // The lint step falls back the same way and is not skipped as "no crate". + let clippy = run_at( + &stamps(&probe, &store, NOW + 15, false), + Check::Clippy, + &[], + false, + false, + ); + assert_eq!( + clippy.cargo, + ["clippy --workspace --all-targets -- -D warnings"], + "{}", + clippy.out + ); + } + + #[test] + fn a_narrow_pass_never_lets_a_wider_selection_skip() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let narrow = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert_eq!(narrow.cargo, [XTASK_TEST]); + + // The same tree, but the push now also touches the lockfile: the whole + // workspace. The earlier `-p xtask` pass says nothing about that. + let wide = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &["Cargo.lock"], + false, + false, + ); + assert_eq!(wide.cargo, [WIDE_TEST], "{}", wide.out); + assert!(!wide.out.contains("skipped"), "{}", wide.out); + assert_eq!(store.files.borrow().len(), 2, "both passes are on record"); + + // And each is skipped on its own next run. + let again = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert!(again.cargo.is_empty(), "{}", again.out); + } + + #[test] + fn a_different_tree_runs_again() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ) + .result + .expect("first"); + *probe.tree.borrow_mut() = Ok("tree-2".to_string()); + let second = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert_eq!(second.cargo, [XTASK_TEST], "{}", second.out); + assert_eq!(store.files.borrow().len(), 2); + } + + #[test] + fn a_different_toolchain_or_environment_runs_again() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ) + .result + .expect("first"); + + *probe.toolchain.borrow_mut() = Ok("rustc 1.97.0\ncargo 1.97.0\n".to_string()); + let toolchain = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert_eq!( + toolchain.cargo, + [XTASK_TEST], + "a new toolchain must not reuse the pass" + ); + + *probe.env.borrow_mut() = "env-b".to_string(); + let env = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + assert_eq!( + env.cargo, + [XTASK_TEST], + "a new RUSTFLAGS and the like must not reuse the pass" + ); + } + + #[test] + fn a_failed_step_records_nothing_and_a_passed_sibling_still_skips_on_retry() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Clippy, + &files, + false, + false, + ) + .result + .expect("clippy passes"); + let failed = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &files, + false, + true, + ); + failed.result.expect_err("test fails"); + assert_eq!( + store.files.borrow().len(), + 1, + "only the clippy pass is on record" + ); + assert!( + store + .files + .borrow() + .keys() + .all(|name| name.starts_with("clippy-")), + "{:?}", + store.files.borrow().keys() + ); + + // The retry skips the step that passed and re-runs the one that failed. + let clippy = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Clippy, + &files, + false, + false, + ); + assert!(clippy.cargo.is_empty(), "{}", clippy.out); + let test = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Test, + &files, + false, + false, + ); + assert_eq!(test.cargo, [XTASK_TEST]); + assert_eq!(store.files.borrow().len(), 2); + } + + #[test] + fn a_dirty_working_tree_neither_skips_nor_records_and_says_so() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("clean run"); + assert_eq!(store.files.borrow().len(), 1); + + *probe.tree.borrow_mut() = + Err("the working tree has 2 uncommitted or untracked path(s)".to_string()); + let dirty = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &files, + false, + false, + ); + assert_eq!( + dirty.cargo, + [XTASK_TEST], + "a recorded pass is not honored on a dirty tree" + ); + assert!( + dirty + .out + .contains("pass stamps are not used: the working tree has 2 uncommitted"), + "{}", + dirty.out + ); + assert_eq!(*store.writes.borrow(), 1, "and no pass is recorded from it"); + } + + #[test] + fn a_tree_the_run_changed_is_not_recorded() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let stamps = stamps(&probe, &store, NOW, false); + let mut out = Vec::new(); + execute( + Check::Test, + Request::default(), + &stamps, + || Ok(graph()), + |_, _| Ok(vec!["xtask/src/main.rs".to_string()]), + |_| { + // cargo rewrote a tracked file (`Cargo.lock`, say) while it ran. + *probe.tree.borrow_mut() = + Err("the working tree has 1 uncommitted path".to_string()); + Ok(()) + }, + &mut out, + ) + .expect("the step itself passed"); + let out = String::from_utf8(out).expect("utf-8"); + assert!( + out.contains("pass not recorded: the working tree has 1 uncommitted"), + "{out}" + ); + assert!( + store.files.borrow().is_empty(), + "nothing is recorded for a tree cargo changed" + ); + } + + /// `--rerun` is used when something the key omits may have changed (user-level + /// cargo configuration, an installed tool). If that rerun fails, the earlier + /// pass must not survive to skip the next ordinary run. + #[test] + fn a_failed_rerun_does_not_leave_the_earlier_pass_to_skip_the_next_ordinary_run() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("the first, passing run"); + assert_eq!(store.files.borrow().len(), 1); + + let failed = run_at( + &stamps(&probe, &store, NOW + 100, true), + Check::Test, + &files, + false, + true, + ); + assert!(failed.result.is_err(), "the rerun fails"); + assert_eq!(failed.cargo, [XTASK_TEST], "--rerun must run the step"); + assert!( + store.files.borrow().is_empty(), + "the earlier pass is dropped before the rerun and nothing replaces it" + ); + + let retry = run_at( + &stamps(&probe, &store, NOW + 200, false), + Check::Test, + &files, + false, + false, + ); + retry.result.expect("the ordinary retry"); + assert_eq!( + retry.cargo, + [XTASK_TEST], + "the ordinary retry must run the step: {}", + retry.out + ); + assert!(!retry.out.contains("skipped"), "{}", retry.out); + assert_eq!( + store.files.borrow().len(), + 1, + "the passing retry is recorded" + ); + } + + #[test] + fn a_failed_rerun_keeps_the_passes_of_other_steps_and_other_selections() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + for (check, files) in [ + (Check::Test, &["xtask/src/main.rs"][..]), + (Check::Test, &["Cargo.lock"][..]), + (Check::Clippy, &["xtask/src/main.rs"][..]), + ] { + run_at( + &stamps(&probe, &store, NOW, false), + check, + files, + false, + false, + ) + .result + .expect("a recorded pass"); + } + assert_eq!(store.files.borrow().len(), 3); + + let failed = run_at( + &stamps(&probe, &store, NOW + 100, true), + Check::Test, + &["xtask/src/main.rs"], + false, + true, + ); + assert!(failed.result.is_err()); + assert_eq!( + store.files.borrow().len(), + 2, + "only the stamp the rerun replaces is dropped" + ); + // The sibling passes still skip. + for (check, files) in [ + (Check::Test, &["Cargo.lock"][..]), + (Check::Clippy, &["xtask/src/main.rs"][..]), + ] { + let again = run_at( + &stamps(&probe, &store, NOW + 200, false), + check, + files, + false, + false, + ); + assert!(again.cargo.is_empty(), "{:?}: {}", again.cargo, again.out); + } + } + + #[test] + fn a_dry_run_rerun_leaves_every_stamp_in_place() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("the first run"); + let before = store.files.borrow().clone(); + + let dry = run_at( + &stamps(&probe, &store, NOW + 100, true), + Check::Test, + &files, + true, + false, + ); + dry.result.expect("the dry run"); + assert!( + dry.cargo.is_empty(), + "a dry run runs nothing: {:?}", + dry.cargo + ); + assert_eq!( + *store.files.borrow(), + before, + "a dry run neither removes nor rewrites a stamp" + ); + } + + #[test] + fn a_passing_rerun_replaces_the_pass_with_a_fresh_one() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("the first run"); + run_at( + &stamps(&probe, &store, NOW + 100, true), + Check::Test, + &files, + false, + false, + ) + .result + .expect("the passing rerun"); + assert_eq!( + store.files.borrow().len(), + 1, + "one pass, replaced not added" + ); + let after = run_at( + &stamps(&probe, &store, NOW + 130, false), + Check::Test, + &files, + false, + false, + ); + assert!(after.cargo.is_empty(), "{}", after.out); + assert!( + after.out.contains( + "this tree already passed `cargo test -p xtask --all-targets` under a minute ago" + ), + "the fresh pass is the one that skips: {}", + after.out + ); + } + + #[test] + fn rerun_ignores_a_recorded_pass_and_records_a_fresh_one() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("first"); + let forced = run_at( + &stamps(&probe, &store, NOW + 100, true), + Check::Test, + &files, + false, + false, + ); + assert_eq!(forced.cargo, [XTASK_TEST], "--rerun must run the step"); + assert!( + forced.out.contains("recorded passes are ignored"), + "{}", + forced.out + ); + assert!( + forced.out.contains(prepush_stamp::RERUN_VAR), + "{}", + forced.out + ); + let recorded: Vec = store + .files + .borrow() + .values() + .map(|(text, _)| text.clone()) + .collect(); + assert_eq!(recorded.len(), 1, "the new pass replaces the old one"); + assert!( + recorded[0].contains(&format!("passed-at: {}", NOW + 100)), + "{recorded:?}" + ); + } + + #[test] + fn a_dry_run_reports_a_hit_and_never_runs_or_writes() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + let miss = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + true, + false, + ); + assert!(miss.cargo.is_empty()); + assert!(store.files.borrow().is_empty(), "a dry run records nothing"); + assert!(!miss.out.contains("would skip"), "{}", miss.out); + + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("real run"); + let writes = *store.writes.borrow(); + let hit = run_at( + &stamps(&probe, &store, NOW + 3600, false), + Check::Test, + &files, + true, + false, + ); + assert!(hit.cargo.is_empty()); + assert!( + hit.out + .contains("a real run would skip this step, it passed 1 h ago"), + "{}", + hit.out + ); + assert_eq!(*store.writes.borrow(), writes); + } + + #[test] + fn a_failed_write_is_reported_and_never_fails_the_step() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + *store.fail_writes.borrow_mut() = true; + let run = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ); + run.result + .expect("a stamp that cannot be written must not fail a green hook"); + assert_eq!(run.cargo, [XTASK_TEST]); + assert!( + run.out.contains( + "pass not recorded: could not write it under (read-only file system)" + ), + "{}", + run.out + ); + } + + #[test] + fn a_store_that_cannot_be_set_up_still_runs_the_step_and_says_why() { + let probe = FakeProbe::clean("tree-1"); + let store = prepush_stamp::UnavailableStore("not a git repository".to_string()); + let stamps = Stamps { + probe: &probe, + store: &store, + now: NOW, + rerun: false, + }; + let run = run_at(&stamps, Check::Test, &["xtask/src/main.rs"], false, false); + run.result.expect("the step runs"); + assert_eq!(run.cargo, [XTASK_TEST]); + assert!( + run.out + .contains("could not read the pass stamp (not a git repository)"), + "{}", + run.out + ); + assert!( + run.out + .contains("pass not recorded: could not write it under the stamp directory"), + "{}", + run.out + ); + } + + #[test] + fn a_corrupted_stamp_is_ignored_and_replaced_by_a_real_one() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let files = ["xtask/src/main.rs"]; + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &files, + false, + false, + ) + .result + .expect("first"); + let name = store.files.borrow().keys().next().expect("a stamp").clone(); + store + .files + .borrow_mut() + .insert(name.clone(), ("not a stamp".to_string(), NOW)); + + let second = run_at( + &stamps(&probe, &store, NOW + 5, false), + Check::Test, + &files, + false, + false, + ); + assert_eq!( + second.cargo, + [XTASK_TEST], + "garbage under the right name must not skip" + ); + assert!( + second + .out + .contains(&format!("ignoring the pass stamp {name}")), + "{}", + second.out + ); + let third = run_at( + &stamps(&probe, &store, NOW + 10, false), + Check::Test, + &files, + false, + false, + ); + assert!( + third.cargo.is_empty(), + "the replacement is a real stamp: {}", + third.out + ); + } + + #[test] + fn a_skipped_selection_neither_looks_for_nor_writes_a_stamp() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let run = run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["CHANGELOG.md"], + false, + false, + ); + assert!(run.cargo.is_empty()); + assert!( + run.out.contains("skipped, no Rust crate is affected"), + "{}", + run.out + ); + assert_eq!(*store.writes.borrow(), 0); + } + + #[test] + fn a_head_that_moved_during_the_step_is_not_recorded_and_the_note_names_head() { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let stamps = stamps(&probe, &store, NOW, false); + let mut out = Vec::new(); + execute( + Check::Test, + Request::default(), + &stamps, + || Ok(graph()), + |_, _| Ok(vec!["xtask/src/main.rs".to_string()]), + |_| { + // HEAD is a different commit by the time the step has finished. + *probe.tree.borrow_mut() = Ok("tree-2".to_string()); + Ok(()) + }, + &mut out, + ) + .expect("the step itself passed"); + let out = String::from_utf8(out).expect("utf-8"); + assert!( + out.contains("pass not recorded: HEAD moved while the step ran"), + "{out}" + ); + assert!( + store.files.borrow().is_empty(), + "nothing is written for a tree that was not checked" + ); + assert_eq!(*store.writes.borrow(), 0); + } + + /// A stamp for `xtask`'s test step on `tree-1`, written by a normal run. + fn with_a_recorded_pass() -> (FakeProbe, MemStore) { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + run_at( + &stamps(&probe, &store, NOW, false), + Check::Test, + &["xtask/src/main.rs"], + false, + false, + ) + .result + .expect("the first run"); + assert_eq!(store.files.borrow().len(), 1); + (probe, store) + } + + // ---- run_in: the flag and the variable reach the stamp logic ---- + + /// Drive the real wiring below `main`: `run_in` builds the stamps from the + /// flag and the environment, here over a fake store. With `recorded_pass` + /// a first plain run records a pass for the tree; the run that follows is + /// the one measured, and the result says whether it ran cargo. + fn measured_run_ran_cargo(rerun: bool, rerun_var: Option<&str>, recorded_pass: bool) -> bool { + let (probe, store) = (FakeProbe::clean("tree-1"), MemStore::default()); + let env = |name: &str| { + if name == prepush_stamp::RERUN_VAR { + rerun_var.map(String::from) + } else { + None + } + }; + let ran = std::cell::Cell::new(false); + let run_once = |now: u64, rerun: bool| { + ran.set(false); + run_in( + Check::Test, + Flags { + base: None, + dry_run: false, + rerun, + }, + &World { + probe: &probe, + store: &store, + now, + env: &env, + }, + || Ok(graph()), + |_, _| Ok(vec!["xtask/src/main.rs".to_string()]), + |_| { + ran.set(true); + Ok(()) + }, + &mut Vec::new(), + ) + .expect("the step"); + ran.get() + }; + if recorded_pass { + // Nothing is on record yet, so this run runs cargo and records a pass. + assert!(run_once(NOW, false), "the first run runs the step"); + assert_eq!(store.files.borrow().len(), 1, "and records a pass"); + } + run_once(NOW + 60, rerun) + } + + #[test] + fn without_the_flag_or_the_variable_a_recorded_pass_skips_the_step() { + assert!(!measured_run_ran_cargo(false, None, true)); + assert!( + !measured_run_ran_cargo(false, Some("0"), true), + "0 means off" + ); + assert!( + !measured_run_ran_cargo(false, Some(""), true), + "empty means off" + ); + } + + #[test] + fn the_flag_reaches_the_stamp_logic_and_reruns_despite_a_recorded_pass() { + assert!(measured_run_ran_cargo(true, None, true)); + } + + #[test] + fn the_variable_reaches_the_stamp_logic_and_reruns_despite_a_recorded_pass() { + assert!(measured_run_ran_cargo(false, Some("1"), true)); + } + + #[test] + fn with_no_recorded_pass_the_step_runs_either_way() { + assert!(measured_run_ran_cargo(false, None, false)); + assert!(measured_run_ran_cargo(true, None, false)); + } + + #[test] + fn the_rerun_flag_forces_a_run_despite_a_matching_stamp() { + let (probe, store) = with_a_recorded_pass(); + let forced = Stamps::new(&probe, &store, NOW + 5, true, |_| None); + let run = run_at(&forced, Check::Test, &["xtask/src/main.rs"], false, false); + assert_eq!(run.cargo, [XTASK_TEST], "{}", run.out); + assert!( + run.out.contains("recorded passes are ignored"), + "{}", + run.out + ); + assert!(!run.out.contains("skipped"), "{}", run.out); + } + + #[test] + fn the_rerun_variable_forces_a_run_despite_a_matching_stamp() { + let (probe, store) = with_a_recorded_pass(); + let lookup = |name: &str| (name == prepush_stamp::RERUN_VAR).then(|| "1".to_string()); + let forced = Stamps::new(&probe, &store, NOW + 5, false, lookup); + let run = run_at(&forced, Check::Test, &["xtask/src/main.rs"], false, false); + assert_eq!(run.cargo, [XTASK_TEST], "{}", run.out); + assert!( + run.out.contains("recorded passes are ignored"), + "{}", + run.out + ); + } + + #[test] + fn without_the_flag_or_with_the_variable_off_the_same_stamp_still_skips() { + let (probe, store) = with_a_recorded_pass(); + let off = |name: &str| (name == prepush_stamp::RERUN_VAR).then(|| "0".to_string()); + for stamps in [ + Stamps::new(&probe, &store, NOW + 5, false, |_| None), + Stamps::new(&probe, &store, NOW + 5, false, off), + ] { + let run = run_at(&stamps, Check::Test, &["xtask/src/main.rs"], false, false); + assert!(run.cargo.is_empty(), "{}", run.out); + assert!( + run.out.contains("skipped, this tree already passed"), + "{}", + run.out + ); + } + } + // ---- drift guard: every cross-crate read must be covered ---- use std::path::{Path, PathBuf}; diff --git a/xtask/src/prepush_stamp.rs b/xtask/src/prepush_stamp.rs new file mode 100644 index 0000000000..46d5d0290a --- /dev/null +++ b/xtask/src/prepush_stamp.rs @@ -0,0 +1,1618 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Pass stamps for `cargo xtask pre-push-check`: remember that a cargo step +//! already passed on exactly this content, and skip it the next time. +//! +//! The pre-push hook has no memory, so a push retried after a dropped +//! connection, a manual run before the push, an amend that only rewords the +//! message, or the same tree pushed to a second remote re-ran every step on a +//! tree it had just checked. A stamp is written after a step's cargo command +//! exits 0 and is looked up before the next run of that step. +//! +//! **What a stamp stands for.** One step (`clippy`, `clippy-e2e` or `test`), the +//! exact cargo arguments it ran, the git *tree* hash of `HEAD` (not the commit +//! hash, so an amend or a re-commit with identical content still hits), the +//! compiler and cargo identity (`rustc -vV`, `cargo -vV`), and a digest of the +//! `CARGO_*`/`RUST*` environment. Because the arguments are part of the key, a +//! pass recorded for a narrow selection can never let a later, wider selection +//! skip: `-p xtask` and `--workspace` are different stamps. +//! +//! **The working tree must be clean.** Cargo checks the working tree, not the +//! pushed commit, so a stamp keyed on `HEAD`'s tree is only true when the +//! working tree *is* that tree. With uncommitted changes or untracked files +//! (an untracked `mod` target can hide a missing `git add`) a stamp is neither +//! honored nor written, and the step says why. After the run the tree is checked +//! again: if cargo changed it (a rewritten `Cargo.lock`, say), nothing is +//! recorded. +//! +//! **Fail safe.** Every error while locating the store, reading, parsing or +//! verifying a stamp means the step runs; a stamp that does not match its key +//! byte for byte, is unreadable, belongs to a different format, is dated in the +//! future or is older than [`MAX_AGE_SECS`] is ignored; a failed write is +//! reported and never fails the hook. A stamp is only written after cargo +//! succeeded. +//! +//! **Where.** Under the git *common* directory, so every linked worktree of a +//! clone shares the stamps, nothing lands in the work tree (no ignore rule, no +//! chance of committing one), and `cargo clean` does not erase them. Each write +//! prunes stamps older than [`MAX_AGE_SECS`] and keeps at most [`MAX_STAMPS`]. +//! +//! **Not covered**, and said in the skip message: user-level cargo +//! configuration (`~/.cargo/config.toml`), installed tools, files git ignores, +//! network-dependent or flaky tests. `--rerun` or [`RERUN_VAR`] ignores recorded +//! passes: it drops this step's earlier pass before running, so a rerun that +//! fails leaves nothing for the next ordinary run to skip on, and it records a +//! fresh pass when the step succeeds. `SKIP=` still skips a hook +//! outright. The `verify-commits` hook is a separate hook and runs every time. +//! +//! As elsewhere in this crate the decisions are pure and the world is injected: +//! git and the toolchain sit behind [`Probe`], the filesystem behind [`Store`], +//! and the clock is a parameter, so the rules above are unit-tested without +//! cargo, git or a disk. + +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::{SystemTime, UNIX_EPOCH}; + +use anyhow::{Context, Result, bail}; +use sha2::{Digest, Sha256}; + +use crate::affected; + +/// Setting this to anything but empty or `0` ignores recorded passes. +pub const RERUN_VAR: &str = "ROCM_PRE_PUSH_RERUN"; + +/// Directory name under the git common directory. +const STAMP_DIR: &str = "rocm-cli-prepush-passes"; +const STAMP_EXT: &str = ".stamp"; +/// Scratch files a write goes through before it is renamed into place. +const TMP_EXT: &str = ".tmp"; +const FORMAT_LINE: &str = "rocm-cli pre-push pass stamp v1"; + +/// The revision whose hash identifies the checked content. +const HEAD_TREE: &str = "HEAD^{tree}"; + +/// A pass older than this is ignored and pruned: toolchains, dependencies and +/// the registry move on, and a week is longer than any realistic retry loop. +pub const MAX_AGE_SECS: u64 = 7 * 24 * 60 * 60; + +/// At most this many stamps are kept; the oldest go first. +pub const MAX_STAMPS: usize = 64; + +/// A write killed mid-way leaves its scratch file behind until a later write +/// prunes it, once it is older than this: an older scratch file belongs to a +/// write that died, a younger one may be a concurrent writer's and is left alone. +pub const TMP_MAX_AGE_SECS: u64 = 60 * 60; + +/// A stamp dated this far ahead of the clock is treated as corrupt. +const FUTURE_SLACK_SECS: u64 = 5 * 60; + +/// Environment variable prefixes that can change what a cargo step does. +/// Over-including only costs a re-run; leaving one out could skip a step that +/// would now fail. +const ENV_PREFIXES: &[&str] = &["CARGO_", "RUST"]; + +/// Variables under those prefixes that differ between runs without changing the +/// outcome: where artifacts go, terminal styling, and what `cargo run` sets for +/// the `xtask` binary itself. +const ENV_IGNORED: &[&str] = &[ + "CARGO_TARGET_DIR", + "CARGO_MAKEFLAGS", + "CARGO_MANIFEST_DIR", + "CARGO_MANIFEST_PATH", + "CARGO_BIN_NAME", + "CARGO_CRATE_NAME", + "CARGO_PRIMARY_PACKAGE", + "RUST_BACKTRACE", + "RUST_LIB_BACKTRACE", +]; +const ENV_IGNORED_PREFIXES: &[&str] = &["CARGO_TERM_", "CARGO_PKG_", "CARGO_BIN_EXE_"]; + +fn hex_digest(text: &str) -> String { + hex_digest_bytes(text.as_bytes()) +} + +fn hex_digest_bytes(bytes: &[u8]) -> String { + format!("{:x}", Sha256::digest(bytes)) +} + +/// Whether `--rerun` or [`RERUN_VAR`] asks for recorded passes to be ignored. +pub fn rerun_requested(flag: bool, lookup: impl Fn(&str) -> Option) -> bool { + flag || lookup(RERUN_VAR).is_some_and(|value| { + let value = value.trim(); + !value.is_empty() && value != "0" + }) +} + +/// Digest of the environment variables that can change a cargo step, in a +/// stable order. Only the digest is kept, never a value. +pub fn env_digest(vars: impl IntoIterator) -> String +where + K: AsRef, + V: AsRef, +{ + // The process environment is read as `OsString`s: a variable holding bytes + // that are not valid Unicode (on Linux, any variable at all) must not abort + // an optional check, so nothing here needs a `String`. Names are matched as + // text, which is enough because every prefix and exempt name is ASCII, and + // values are hashed as the bytes they are. + let mut relevant: Vec<(Vec, Vec)> = vars + .into_iter() + .filter(|(name, _)| { + let name = name.as_ref().to_string_lossy(); + ENV_PREFIXES.iter().any(|prefix| name.starts_with(prefix)) + && !ENV_IGNORED.contains(&name.as_ref()) + && !ENV_IGNORED_PREFIXES + .iter() + .any(|prefix| name.starts_with(prefix)) + }) + .map(|(name, value)| { + ( + name.as_ref().as_encoded_bytes().to_vec(), + value.as_ref().as_encoded_bytes().to_vec(), + ) + }) + .collect(); + relevant.sort(); + let mut bytes = Vec::new(); + for (name, value) in relevant { + bytes.extend_from_slice(&name); + bytes.push(b'='); + bytes.extend_from_slice(&value); + bytes.push(b'\n'); + } + hex_digest_bytes(&bytes) +} + +/// Everything one pass depends on. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Key { + step: String, + args: String, + tree: String, + toolchain: String, + env: String, +} + +impl Key { + fn canonical(&self) -> String { + format!( + "step: {}\ncargo: {}\ntree: {}\ntoolchain: {}\nenv: {}\n", + self.step, self.args, self.tree, self.toolchain, self.env + ) + } + + fn file_name(&self) -> String { + let digest = hex_digest(&self.canonical()); + format!("{}-{}{STAMP_EXT}", self.step, &digest[..32]) + } + + fn stamp_text(&self, passed_at: u64) -> String { + format!( + "{FORMAT_LINE}\n{}passed-at: {passed_at}\n", + self.canonical() + ) + } +} + +/// What git and the toolchain say about the state being checked. +pub trait Probe { + /// The tree hash of `HEAD`, only when the working tree is exactly that tree. + /// `Err` carries the reason stamps cannot be trusted right now. + fn clean_tree(&self) -> std::result::Result; + /// A description of the compiler and cargo that will run the step. + fn toolchain(&self) -> std::result::Result; + /// [`env_digest`] of the current environment. + fn env_digest(&self) -> String; +} + +/// Where stamps are kept. Names are file names inside the store. +pub trait Store { + fn read(&self, name: &str) -> Result>; + fn write(&self, name: &str, contents: &str) -> Result<()>; + /// Every stamp as `(name, modified time in seconds since the epoch)`. + fn list(&self) -> Result>; + fn remove(&self, name: &str) -> Result<()>; + /// Where the stamps are, for messages. + fn location(&self) -> String; +} + +/// The outcome of looking for a recorded pass. +#[derive(Debug, PartialEq, Eq)] +pub enum Lookup { + /// A matching pass is on record: skip the step. + Hit { name: String, age_secs: u64 }, + /// Run the step. `key` is `Some` when a pass can be recorded afterwards, and + /// `note` explains anything the user should know (a dirty tree, a rejected + /// stamp, `--rerun`). + Miss { + key: Option, + note: Option, + }, +} + +/// Stamp handling for one invocation, with the world injected. +pub struct Stamps<'a> { + pub probe: &'a dyn Probe, + pub store: &'a dyn Store, + /// Seconds since the epoch. + pub now: u64, + /// Ignore recorded passes (but still record a new one). + pub rerun: bool, +} + +impl<'a> Stamps<'a> { + /// The stamps for one invocation. `rerun_flag` is `--rerun`; `lookup` reads + /// the environment for [`RERUN_VAR`], so tests need no process environment. + pub fn new( + probe: &'a dyn Probe, + store: &'a dyn Store, + now: u64, + rerun_flag: bool, + lookup: impl Fn(&str) -> Option, + ) -> Self { + Self { + probe, + store, + now, + rerun: rerun_requested(rerun_flag, lookup), + } + } +} + +impl Stamps<'_> { + fn key(&self, step: &str, args: &[String]) -> std::result::Result { + let tree = self.probe.clean_tree()?; + let toolchain = hex_digest(&self.probe.toolchain()?); + Ok(Key { + step: step.to_string(), + args: args.join(" "), + tree, + toolchain, + env: self.probe.env_digest(), + }) + } + + /// Look for a pass of `step` run with `args` on the current tree. + pub fn lookup(&self, step: &str, args: &[String]) -> Lookup { + let key = match self.key(step, args) { + Ok(key) => key, + Err(why) => { + return Lookup::Miss { + key: None, + note: Some(format!("pass stamps are not used: {why}")), + }; + } + }; + if self.rerun { + return Lookup::Miss { + key: Some(key), + note: Some(format!( + "recorded passes are ignored because --rerun or {RERUN_VAR} is set" + )), + }; + } + let name = key.file_name(); + let text = match self.store.read(&name) { + Ok(Some(text)) => text, + Ok(None) => { + return Lookup::Miss { + key: Some(key), + note: None, + }; + } + Err(error) => { + return Lookup::Miss { + key: Some(key), + note: Some(format!( + "could not read the pass stamp ({error:#}); running the step" + )), + }; + } + }; + match self.verify(&key, &text) { + Ok(Some(age_secs)) => Lookup::Hit { name, age_secs }, + Ok(None) => Lookup::Miss { + key: Some(key), + note: None, + }, + Err(why) => Lookup::Miss { + key: Some(key), + note: Some(format!("ignoring the pass stamp {name}: {why}")), + }, + } + } + + /// `Ok(Some(age))` for a valid, fresh stamp, `Ok(None)` for an expired one, + /// `Err(reason)` for one that is not what its name says. + fn verify(&self, key: &Key, text: &str) -> std::result::Result, String> { + let expected = format!("{FORMAT_LINE}\n{}", key.canonical()); + let rest = text + .strip_prefix(&expected) + .ok_or("it does not match this step, tree, toolchain and environment")?; + let passed_at: u64 = rest + .strip_prefix("passed-at: ") + .and_then(|value| value.trim().parse().ok()) + .ok_or("its time stamp is unreadable")?; + if passed_at > self.now.saturating_add(FUTURE_SLACK_SECS) { + return Err("it is dated in the future".to_string()); + } + let age = self.now.saturating_sub(passed_at); + Ok((age <= MAX_AGE_SECS).then_some(age)) + } + + /// Drop the recorded pass for exactly this key, before a rerun that will + /// replace it. A rerun exists because something the key does not cover may + /// have changed (user-level cargo configuration, an installed tool), so if it + /// fails the earlier pass must not survive to skip the next ordinary run. + /// Other steps' and other trees' stamps are different files and stay. + /// Returns a note when the stamp could not be removed. + pub fn invalidate(&self, key: &Key) -> Option { + self.store.remove(&key.file_name()).err().map(|error| { + format!( + "could not remove the earlier pass stamp ({error:#}); if this run fails, the \ + next ordinary run may still skip the step" + ) + }) + } + + /// Record that the step passed. Returns a note when nothing was recorded. + /// + /// The tree is probed again first: a stamp is only true when the working + /// tree is still the one the key was computed for, and a step that rewrote + /// it (`Cargo.lock`, a generated file) did not check `HEAD`'s tree. + pub fn record(&self, key: &Key) -> Option { + match self.probe.clean_tree() { + Ok(tree) if tree == key.tree => {} + Ok(_) => return Some("pass not recorded: HEAD moved while the step ran".to_string()), + Err(why) => return Some(format!("pass not recorded: {why}")), + } + if let Err(error) = self + .store + .write(&key.file_name(), &key.stamp_text(self.now)) + { + return Some(format!( + "pass not recorded: could not write it under {} ({error:#})", + self.store.location() + )); + } + self.prune(); + None + } + + /// Drop stamps past [`MAX_AGE_SECS`], then the oldest beyond [`MAX_STAMPS`], + /// and scratch files left by a write that died ([`TMP_MAX_AGE_SECS`]). + /// Best effort: a file that cannot be removed is only wasted space. + fn prune(&self) { + let Ok(mut entries) = self.store.list() else { + return; + }; + entries.retain(|(name, modified)| { + let age = self.now.saturating_sub(*modified); + let expired = if name.ends_with(TMP_EXT) { + age > TMP_MAX_AGE_SECS + } else { + age > MAX_AGE_SECS + }; + if expired { + let _ = self.store.remove(name); + } + !expired + }); + // A scratch file is not a pass: it does not count toward the cap. + entries.retain(|(name, _)| !name.ends_with(TMP_EXT)); + entries.sort_by_key(|(_, modified)| std::cmp::Reverse(*modified)); + for (name, _) in entries.iter().skip(MAX_STAMPS) { + let _ = self.store.remove(name); + } + } + + /// Where the stamps live, for the skip message. + pub fn location(&self) -> String { + self.store.location() + } +} + +/// `3 min`, `2 h`, `1 d`: how long ago a pass was recorded. +pub fn age_text(secs: u64) -> String { + match secs { + 0..=59 => "under a minute".to_string(), + 60..=3599 => format!("{} min", secs / 60), + 3600..=86_399 => format!("{} h", secs / 3600), + _ => format!("{} d", secs / 86_400), + } +} + +/// The lines printed when a step is skipped because its pass is on record. +pub fn skip_lines( + step: &str, + args: &[String], + age_secs: u64, + name: &str, + location: &str, +) -> Vec { + vec![ + format!( + "pre-push-check {step}: skipped, this tree already passed `cargo {}` {} ago \ + (stamp {name})", + args.join(" "), + age_text(age_secs) + ), + format!( + "pre-push-check {step}: that does not cover your user-level cargo configuration, \ + installed tools, files git ignores or flaky tests; run it again with --rerun \ + or {RERUN_VAR}=1, or delete {location}" + ), + ] +} + +// ---- the real world: git, the toolchain, the filesystem ---- + +/// A `git` command for the repository in `dir`. With an explicit directory the +/// repository-selecting variables are removed ([`affected::scrub_git_env`]): git +/// and prek export an absolute `GIT_DIR` to a pre-push hook run from a linked +/// worktree, and `git -C ` would otherwise still act on the repository +/// being pushed. With `None` the environment is kept, because in the hook that +/// inherited `GIT_DIR` correctly names the repository being pushed. +fn git_command(dir: Option<&Path>) -> Command { + let mut git = Command::new("git"); + if let Some(dir) = dir { + affected::scrub_git_env(&mut git).arg("-C").arg(dir); + } + git +} + +/// Run `git` with `args` (in `dir`, or the current directory) and return its +/// trimmed stdout. +fn git_stdout(dir: Option<&Path>, args: &[&str]) -> std::result::Result { + let output = git_command(dir) + .args(args) + .output() + .map_err(|error| format!("could not run git: {error}"))?; + if !output.status.success() { + return Err(format!( + "`git {}` failed: {}", + args.join(" "), + String::from_utf8_lossy(&output.stderr).trim() + )); + } + Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) +} + +/// ` -vV`, or the reason it could not run. +fn version_text(tool: &std::ffi::OsStr) -> std::result::Result { + let output = Command::new(tool) + .arg("-vV") + .output() + .map_err(|error| format!("could not run {}: {error}", tool.to_string_lossy()))?; + if !output.status.success() { + return Err(format!("`{} -vV` failed", tool.to_string_lossy())); + } + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) +} + +/// [`Probe`] backed by the `git` and toolchain on `PATH`, for the repository in +/// `dir` (the current directory when `None`). +#[derive(Default)] +pub struct SystemProbe { + pub dir: Option, +} + +impl Probe for SystemProbe { + fn clean_tree(&self) -> std::result::Result { + let dir = self.dir.as_deref(); + let status = git_stdout(dir, &["status", "--porcelain", "--untracked-files=normal"])?; + let dirty = status + .lines() + .filter(|line| !line.trim().is_empty()) + .count(); + if dirty > 0 { + return Err(format!( + "the working tree has {dirty} uncommitted or untracked path(s), and cargo checks \ + the working tree rather than HEAD; commit or remove them (`git status` lists them)" + )); + } + git_stdout(dir, &["rev-parse", HEAD_TREE]) + } + + fn toolchain(&self) -> std::result::Result { + let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()); + Ok(format!( + "{}\n{}", + version_text(std::ffi::OsStr::new("rustc"))?, + version_text(&cargo)? + )) + } + + fn env_digest(&self) -> String { + env_digest(std::env::vars_os()) + } +} + +/// Stamps in a directory on disk. +pub struct FsStore { + dir: PathBuf, +} + +impl FsStore { + /// The stamp directory under the git common directory of the repository in + /// `dir` (the current directory when `None`), shared by every worktree of + /// that clone. + pub fn in_git_common_dir(dir: Option<&Path>) -> Result { + let common = git_stdout(dir, &["rev-parse", "--git-common-dir"]) + .map_err(|why| anyhow::anyhow!(why))?; + let common = PathBuf::from(common); + // git prints the path relative to where it ran when it is inside there. + let common = if common.is_absolute() { + common + } else { + let base = match dir { + Some(dir) => dir.to_path_buf(), + None => std::env::current_dir().context("could not read the current directory")?, + }; + base.join(common) + }; + Ok(Self { + dir: common.join(STAMP_DIR), + }) + } + + fn path(&self, name: &str) -> Result { + if name.is_empty() || name == "." || name == ".." || name.contains(['/', '\\']) { + bail!("refusing the stamp name {name:?}"); + } + Ok(self.dir.join(name)) + } +} + +impl Store for FsStore { + fn read(&self, name: &str) -> Result> { + match std::fs::read_to_string(self.path(name)?) { + Ok(text) => Ok(Some(text)), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(error) => Err(error).context("failed to read the stamp"), + } + } + + fn write(&self, name: &str, contents: &str) -> Result<()> { + std::fs::create_dir_all(&self.dir) + .with_context(|| format!("failed to create {}", self.dir.display()))?; + let target = self.path(name)?; + // Write beside the target and rename, so a reader never sees half a stamp. + let scratch = self + .dir + .join(format!(".{name}.{}{TMP_EXT}", std::process::id())); + let result = std::fs::write(&scratch, contents) + .context("failed to write the stamp") + .and_then(|()| { + std::fs::rename(&scratch, &target).context("failed to move the stamp into place") + }); + if result.is_err() { + // Do not leave the scratch file behind; `prune` only sees what a + // later successful write lists. + let _ = std::fs::remove_file(&scratch); + } + result + } + + fn list(&self) -> Result> { + let mut entries = Vec::new(); + let read_dir = match std::fs::read_dir(&self.dir) { + Ok(read_dir) => read_dir, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(entries), + Err(error) => return Err(error).context("failed to list the stamps"), + }; + for entry in read_dir.flatten() { + let name = entry.file_name().to_string_lossy().into_owned(); + if !name.ends_with(STAMP_EXT) && !name.ends_with(TMP_EXT) { + continue; + } + let modified = entry + .metadata() + .and_then(|meta| meta.modified()) + .ok() + .and_then(|time| time.duration_since(UNIX_EPOCH).ok()) + .map_or(0, |since| since.as_secs()); + entries.push((name, modified)); + } + Ok(entries) + } + + fn remove(&self, name: &str) -> Result<()> { + match std::fs::remove_file(self.path(name)?) { + Ok(()) => Ok(()), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(error).context("failed to remove the stamp"), + } + } + + fn location(&self) -> String { + self.dir.display().to_string() + } +} + +/// A store that could not be set up: every read and write reports why, so the +/// step runs and says so. +pub struct UnavailableStore(pub String); + +impl Store for UnavailableStore { + fn read(&self, _name: &str) -> Result> { + bail!("{}", self.0) + } + fn write(&self, _name: &str, _contents: &str) -> Result<()> { + bail!("{}", self.0) + } + fn list(&self) -> Result> { + bail!("{}", self.0) + } + fn remove(&self, _name: &str) -> Result<()> { + bail!("{}", self.0) + } + fn location(&self) -> String { + "the stamp directory".to_string() + } +} + +/// Seconds since the epoch, or 0 when the clock is before it (which makes every +/// stamp look dated in the future, so none is honored). +pub fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |since| since.as_secs()) +} + +#[cfg(test)] +pub(crate) mod fakes { + use std::cell::RefCell; + use std::collections::BTreeMap; + + use anyhow::{Result, bail}; + + use super::{Probe, Store}; + + /// A probe whose answers a test sets, and which counts its calls. + pub struct FakeProbe { + pub tree: RefCell>, + pub toolchain: RefCell>, + pub env: RefCell, + } + + impl FakeProbe { + pub fn clean(tree: &str) -> Self { + Self { + tree: RefCell::new(Ok(tree.to_string())), + toolchain: RefCell::new(Ok("rustc 1.96.0\ncargo 1.96.0\n".to_string())), + env: RefCell::new("env-a".to_string()), + } + } + } + + impl Probe for FakeProbe { + fn clean_tree(&self) -> std::result::Result { + self.tree.borrow().clone() + } + fn toolchain(&self) -> std::result::Result { + self.toolchain.borrow().clone() + } + fn env_digest(&self) -> String { + self.env.borrow().clone() + } + } + + /// An in-memory store; `fail_writes` makes every write error. + #[derive(Default)] + pub struct MemStore { + pub files: RefCell>, + pub fail_writes: RefCell, + pub writes: RefCell, + } + + impl Store for MemStore { + fn read(&self, name: &str) -> Result> { + Ok(self.files.borrow().get(name).map(|(text, _)| text.clone())) + } + fn write(&self, name: &str, contents: &str) -> Result<()> { + if *self.fail_writes.borrow() { + bail!("read-only file system"); + } + *self.writes.borrow_mut() += 1; + // The mtime is the stamp's own `passed-at`, as on a real disk. + let modified = contents + .lines() + .find_map(|line| line.strip_prefix("passed-at: ")) + .and_then(|value| value.parse().ok()) + .unwrap_or(0); + self.files + .borrow_mut() + .insert(name.to_string(), (contents.to_string(), modified)); + Ok(()) + } + fn list(&self) -> Result> { + Ok(self + .files + .borrow() + .iter() + .map(|(name, (_, modified))| (name.clone(), *modified)) + .collect()) + } + fn remove(&self, name: &str) -> Result<()> { + self.files.borrow_mut().remove(name); + Ok(()) + } + fn location(&self) -> String { + "".to_string() + } + } +} + +#[cfg(test)] +mod tests { + use super::fakes::{FakeProbe, MemStore}; + use super::*; + + const NOW: u64 = 1_800_000_000; + + fn args(text: &str) -> Vec { + text.split_whitespace().map(String::from).collect() + } + + fn stamps<'a>(probe: &'a FakeProbe, store: &'a MemStore, now: u64) -> Stamps<'a> { + Stamps { + probe, + store, + now, + rerun: false, + } + } + + /// Record a pass of `test -p xtask` and return the stamp's file name. + fn record_pass(probe: &FakeProbe, store: &MemStore, now: u64) -> String { + let stamps = stamps(probe, store, now); + let Lookup::Miss { key: Some(key), .. } = stamps.lookup("test", &args("test -p xtask")) + else { + panic!("expected a miss with a key"); + }; + assert_eq!(stamps.record(&key), None); + key.file_name() + } + + // ---- the key ---- + + fn base_key() -> Key { + Key { + step: "test".to_string(), + args: "test -p xtask".to_string(), + tree: "tree".to_string(), + toolchain: "toolchain".to_string(), + env: "env".to_string(), + } + } + + #[test] + fn every_part_of_the_key_changes_the_stamp_name() { + let base = base_key(); + let variants = [ + Key { + step: "clippy".into(), + ..base.clone() + }, + Key { + args: "test --workspace".into(), + ..base.clone() + }, + Key { + tree: "other".into(), + ..base.clone() + }, + Key { + toolchain: "other".into(), + ..base.clone() + }, + Key { + env: "other".into(), + ..base.clone() + }, + ]; + for variant in &variants { + assert_ne!(variant.file_name(), base.file_name(), "{variant:?}"); + } + assert_eq!( + base.file_name(), + base_key().file_name(), + "the name is stable" + ); + let name = base.file_name(); + assert!( + name.starts_with("test-") && name.strip_suffix(".stamp").is_some(), + "{name}" + ); + } + + // ---- age: expiry and a clock that disagrees ---- + + #[test] + fn a_pass_is_honored_up_to_the_maximum_age_and_not_a_second_longer() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + record_pass(&probe, &store, NOW); + let at = |now| stamps(&probe, &store, now).lookup("test", &args("test -p xtask")); + assert!(matches!(at(NOW + MAX_AGE_SECS), Lookup::Hit { .. })); + // Expired: a quiet miss that is allowed to record a fresh pass. + match at(NOW + MAX_AGE_SECS + 1) { + Lookup::Miss { key, note } => { + assert!(key.is_some()); + assert_eq!(note, None); + } + Lookup::Hit { .. } => panic!("an expired pass must not skip the step"), + } + } + + #[test] + fn a_stamp_dated_in_the_future_is_ignored_and_says_so() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + let name = record_pass(&probe, &store, NOW + 10 * 60); + match stamps(&probe, &store, NOW).lookup("test", &args("test -p xtask")) { + Lookup::Miss { note, .. } => { + let note = note.expect("a note"); + assert!( + note.contains(&name) && note.contains("dated in the future"), + "{note}" + ); + } + Lookup::Hit { .. } => panic!("a future-dated stamp must not skip the step"), + } + // A little clock skew is tolerated. + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + record_pass(&probe, &store, NOW + 60); + assert!(matches!( + stamps(&probe, &store, NOW).lookup("test", &args("test -p xtask")), + Lookup::Hit { .. } + )); + } + + #[test] + fn a_stamp_from_another_format_or_with_a_bad_time_is_ignored() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + let name = record_pass(&probe, &store, NOW); + let good = store.files.borrow()[&name].0.clone(); + for (broken, why) in [ + ( + good.replace("pass stamp v1", "pass stamp v2"), + "does not match", + ), + (good.replace("tree: t", "tree: u"), "does not match"), + ( + good.replace(&format!("passed-at: {NOW}"), "passed-at: soon"), + "time stamp is unreadable", + ), + ( + good.replace(&format!("passed-at: {NOW}\n"), ""), + "time stamp is unreadable", + ), + ] { + store.files.borrow_mut().insert(name.clone(), (broken, NOW)); + match stamps(&probe, &store, NOW + 1).lookup("test", &args("test -p xtask")) { + Lookup::Miss { note, .. } => { + let note = note.expect("a note"); + assert!(note.contains(why), "{note}"); + } + Lookup::Hit { .. } => panic!("a broken stamp must not skip the step: {why}"), + } + } + } + + // ---- expiry and the size cap ---- + + #[test] + fn recording_prunes_expired_stamps_and_keeps_the_newest_up_to_the_cap() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + { + let mut files = store.files.borrow_mut(); + files.insert( + "old-a.stamp".into(), + (String::new(), NOW - MAX_AGE_SECS - 10), + ); + files.insert( + "old-b.stamp".into(), + (String::new(), NOW - MAX_AGE_SECS - 20), + ); + for index in 0..(MAX_STAMPS + 5) { + files.insert( + format!("fresh-{index:03}.stamp"), + (String::new(), NOW - 1000 + index as u64), + ); + } + } + record_pass(&probe, &store, NOW); + let files = store.files.borrow(); + assert_eq!( + files.len(), + MAX_STAMPS, + "{:?}", + files.keys().collect::>() + ); + assert!(!files.contains_key("old-a.stamp") && !files.contains_key("old-b.stamp")); + assert!( + !files.contains_key("fresh-000.stamp"), + "the oldest beyond the cap go first" + ); + assert!( + files.contains_key(&format!("fresh-{:03}.stamp", MAX_STAMPS + 4)), + "the newest stay" + ); + assert!( + files.keys().any(|name| name.starts_with("test-")), + "and so does the new pass" + ); + } + + #[test] + fn expired_stamps_are_pruned_even_when_the_store_is_far_below_the_cap() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + { + let mut files = store.files.borrow_mut(); + files.insert("old.stamp".into(), (String::new(), NOW - MAX_AGE_SECS - 1)); + files.insert("edge.stamp".into(), (String::new(), NOW - MAX_AGE_SECS)); + files.insert("fresh.stamp".into(), (String::new(), NOW - 60)); + } + record_pass(&probe, &store, NOW); + let files = store.files.borrow(); + assert!( + !files.contains_key("old.stamp"), + "{:?}", + files.keys().collect::>() + ); + assert!( + files.contains_key("edge.stamp"), + "a stamp exactly at the limit is still honored" + ); + assert!(files.contains_key("fresh.stamp")); + } + + // ---- the environment ---- + + fn vars(pairs: &[(&str, &str)]) -> Vec<(String, String)> { + pairs + .iter() + .map(|(k, v)| ((*k).to_string(), (*v).to_string())) + .collect() + } + + #[test] + fn the_environment_digest_follows_cargo_and_rust_settings_only() { + let base = env_digest(vars(&[("PATH", "/bin"), ("RUSTFLAGS", "-D warnings")])); + // Settings that can change a step's outcome change the digest ... + assert_ne!( + base, + env_digest(vars(&[("RUSTFLAGS", "-C target-cpu=native")])) + ); + assert_ne!( + base, + env_digest(vars(&[ + ("RUSTFLAGS", "-D warnings"), + ("CARGO_BUILD_TARGET", "x") + ])) + ); + // ... unrelated or per-run variables do not, whatever their order. + assert_eq!( + base, + env_digest(vars(&[ + ("HOME", "/home/a"), + ("CARGO_TARGET_DIR", "/elsewhere"), + ("CARGO_TERM_COLOR", "always"), + ("CARGO_PKG_NAME", "xtask"), + ("CARGO_MANIFEST_DIR", "/repo/xtask"), + ("RUST_BACKTRACE", "1"), + ("RUSTFLAGS", "-D warnings"), + ])) + ); + assert_eq!( + env_digest(vars(&[("RUSTFLAGS", "a"), ("CARGO_INCREMENTAL", "0")])), + env_digest(vars(&[("CARGO_INCREMENTAL", "0"), ("RUSTFLAGS", "a")])), + ); + } + + #[test] + fn no_environment_value_ends_up_in_a_stamp() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + *probe.env.borrow_mut() = env_digest(vars(&[("CARGO_REGISTRY_TOKEN", "hunter2-secret")])); + let name = record_pass(&probe, &store, NOW); + let text = store.files.borrow()[&name].0.clone(); + assert!( + !text.contains("hunter2") && !text.contains("CARGO_REGISTRY_TOKEN"), + "{text}" + ); + } + + #[test] + fn rerun_comes_from_the_flag_or_a_non_empty_non_zero_variable() { + let var = |value: Option<&'static str>| { + move |name: &str| { + (name == RERUN_VAR) + .then(|| value.map(String::from)) + .flatten() + } + }; + assert!(rerun_requested(true, var(None))); + assert!(rerun_requested(false, var(Some("1")))); + assert!(rerun_requested(false, var(Some("yes")))); + assert!(!rerun_requested(false, var(Some("0")))); + assert!(!rerun_requested(false, var(Some("")))); + assert!(!rerun_requested(false, var(Some(" ")))); + assert!(!rerun_requested(false, var(None))); + } + + #[test] + fn ages_read_as_minutes_hours_or_days() { + assert_eq!(age_text(5), "under a minute"); + assert_eq!(age_text(90), "1 min"); + assert_eq!(age_text(2 * 3600 + 5), "2 h"); + assert_eq!(age_text(3 * 86_400), "3 d"); + } + + // ---- the real store on a real disk ---- + + #[test] + fn the_file_store_round_trips_lists_only_stamps_and_removes_idempotently() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = FsStore { + dir: dir.path().join("passes"), + }; + assert_eq!( + store.read("a.stamp").expect("read"), + None, + "a missing stamp is not an error" + ); + assert!( + store.list().expect("list").is_empty(), + "nor is a missing directory" + ); + + store.write("a.stamp", "one").expect("write"); + store + .write("a.stamp", "two") + .expect("an overwrite replaces it"); + assert_eq!(store.read("a.stamp").expect("read").as_deref(), Some("two")); + std::fs::write(dir.path().join("passes/notes.txt"), "x").expect("a foreign file"); + + let listed = store.list().expect("list"); + assert_eq!(listed.len(), 1, "{listed:?}"); + assert_eq!(listed[0].0, "a.stamp"); + assert!( + listed[0].1 > 1_000_000_000, + "the modified time is read: {listed:?}" + ); + let leftovers = std::fs::read_dir(dir.path().join("passes")) + .expect("dir") + .count(); + assert_eq!( + leftovers, 2, + "no scratch file is left behind (the stamp and the foreign file)" + ); + + store.remove("a.stamp").expect("remove"); + store.remove("a.stamp").expect("removing it again is fine"); + assert_eq!(store.read("a.stamp").expect("read"), None); + } + + #[test] + fn the_file_store_refuses_names_that_leave_its_directory() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = FsStore { + dir: dir.path().join("passes"), + }; + for name in ["../escape.stamp", "a/b.stamp", "a\\b.stamp", "..", ".", ""] { + // The guard itself refuses, not whatever the filesystem then does. + let refusals = [ + store.write(name, "x").unwrap_err(), + store.read(name).unwrap_err(), + store.remove(name).unwrap_err(), + ]; + for error in refusals { + assert!( + format!("{error:#}").contains("refusing the stamp name"), + "{name}: {error:#}" + ); + } + } + assert!(!dir.path().join("escape.stamp").exists()); + } + + // ---- git: the tree hash, the dirty check and the shared location ---- + + fn git_in(dir: &Path, args: &[&str]) -> String { + let output = git_command(Some(dir)) + .args([ + "-c", + "user.name=t", + "-c", + "user.email=t@example.com", + "-c", + "commit.gpgsign=false", + ]) + .args(args) + .output() + .expect("run git"); + assert!( + output.status.success(), + "git {args:?} failed in {}", + dir.display() + ); + String::from_utf8_lossy(&output.stdout).trim().to_string() + } + + /// A repository with one commit, in a fresh directory. + fn repo() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + git_in(dir.path(), &["init", "-q"]); + std::fs::write(dir.path().join("a.txt"), "one\n").expect("write"); + git_in(dir.path(), &["add", "a.txt"]); + git_in(dir.path(), &["commit", "-q", "-m", "first"]); + dir + } + + fn probe_for(dir: &Path) -> SystemProbe { + SystemProbe { + dir: Some(dir.to_path_buf()), + } + } + + #[test] + fn a_clean_tree_is_keyed_by_its_tree_hash_which_an_amend_does_not_change() { + let repo = repo(); + let probe = probe_for(repo.path()); + let tree = probe.clean_tree().expect("a clean tree"); + assert_eq!(tree, git_in(repo.path(), &["rev-parse", HEAD_TREE])); + + let commit = git_in(repo.path(), &["rev-parse", "HEAD"]); + git_in(repo.path(), &["commit", "-q", "--amend", "-m", "reworded"]); + assert_ne!( + git_in(repo.path(), &["rev-parse", "HEAD"]), + commit, + "the commit hash changed" + ); + assert_eq!( + probe.clean_tree().expect("still clean"), + tree, + "the tree hash did not" + ); + + std::fs::write(repo.path().join("a.txt"), "two\n").expect("write"); + git_in(repo.path(), &["commit", "-q", "-am", "second"]); + assert_ne!( + probe.clean_tree().expect("clean"), + tree, + "new content is a new tree" + ); + } + + #[test] + fn uncommitted_or_untracked_files_make_the_tree_unusable_for_stamps() { + let repo = repo(); + let probe = probe_for(repo.path()); + probe.clean_tree().expect("clean to start with"); + + std::fs::write(repo.path().join("a.txt"), "changed\n").expect("write"); + let why = probe.clean_tree().expect_err("a modified tracked file"); + assert!(why.contains("1 uncommitted or untracked path"), "{why}"); + assert!( + why.contains("git status"), + "the reason says how to fix it: {why}" + ); + git_in(repo.path(), &["checkout", "--", "a.txt"]); + probe.clean_tree().expect("clean again"); + + // An untracked file can hide a missing `git add`, so it counts too. + std::fs::write(repo.path().join("new.rs"), "mod x;\n").expect("write"); + assert!(probe.clean_tree().is_err()); + } + + #[test] + fn linked_worktrees_share_one_stamp_directory_outside_every_work_tree() { + let repo = repo(); + let linked = tempfile::tempdir().expect("tempdir"); + let linked_path = linked.path().join("wt"); + git_in( + repo.path(), + &[ + "worktree", + "add", + "-q", + linked_path.to_str().expect("utf-8"), + "-b", + "side", + ], + ); + + let main = FsStore::in_git_common_dir(Some(repo.path())).expect("store"); + let side = FsStore::in_git_common_dir(Some(&linked_path)).expect("store"); + let canonical = |store: &FsStore| { + std::fs::canonicalize(store.dir.parent().expect("parent")).expect("exists") + }; + assert_eq!( + canonical(&main), + canonical(&side), + "one directory for the whole clone" + ); + assert!(main.dir.ends_with(STAMP_DIR)); + assert!(canonical(&main).ends_with(".git"), "{:?}", canonical(&main)); + + // A stamp written from one worktree is read from the other, and writing + // it does not dirty either work tree. + main.write("t.stamp", "shared").expect("write"); + assert_eq!( + side.read("t.stamp").expect("read").as_deref(), + Some("shared") + ); + probe_for(repo.path()) + .clean_tree() + .expect("the main work tree is still clean"); + probe_for(&linked_path) + .clean_tree() + .expect("so is the linked one"); + } + + #[test] + fn a_directory_git_does_not_know_has_no_store_and_no_usable_tree() { + let dir = tempfile::tempdir().expect("tempdir"); + // The temp directory is not normally inside a repository; if it were, + // git would answer and there would be nothing to assert. + if git_stdout(Some(dir.path()), &["rev-parse", "--git-dir"]).is_err() { + assert!(probe_for(dir.path()).clean_tree().is_err()); + assert!(FsStore::in_git_common_dir(Some(dir.path())).is_err()); + } + } + + #[test] + fn the_toolchain_is_identified_from_rustc_and_cargo() { + let text = SystemProbe::default() + .toolchain() + .expect("rustc and cargo run in the test environment"); + assert!(text.contains("rustc") && text.contains("cargo"), "{text}"); + } + + // ---- hermetic git: a hostile ambient GIT_DIR must not redirect any of it ---- + + const HOSTILE_CHILD_VAR: &str = "ROCM_STAMP_TEST_HOSTILE_CHILD"; + + #[test] + fn an_explicit_directory_drops_the_repository_selecting_environment() { + let with_dir = git_command(Some(Path::new("scratch"))); + let envs: Vec<_> = with_dir.get_envs().collect(); + for var in [ + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_INDEX_FILE", + "GIT_COMMON_DIR", + ] { + assert!( + envs.iter() + .any(|(name, value)| name.to_str() == Some(var) && value.is_none()), + "{var} must be removed for a command pointed at a directory" + ); + } + // Without a directory the hook's own environment is kept: there GIT_DIR + // correctly names the repository being pushed. + assert_eq!(git_command(None).get_envs().count(), 0); + } + + /// Does nothing on its own. The test below re-runs this binary with a hostile + /// git environment and this variable set, and this body then drives the real + /// probe, store and test helpers against a scratch repository. + #[test] + fn hostile_environment_child() { + let Some(dir) = std::env::var_os(HOSTILE_CHILD_VAR) else { + return; + }; + let dir = PathBuf::from(dir); + assert!( + std::env::var_os("GIT_DIR").is_some(), + "the child is supposed to run under a hostile GIT_DIR" + ); + let probe = probe_for(&dir); + let tree = git_in(&dir, &["rev-parse", HEAD_TREE]); + assert_eq!( + probe.clean_tree().expect("the scratch repository's tree"), + tree + ); + + // Committing through the helper lands in the scratch repository. + std::fs::write(dir.join("b.txt"), "two\n").expect("write"); + git_in(&dir, &["add", "b.txt"]); + git_in(&dir, &["commit", "-q", "-m", "second"]); + assert_ne!(probe.clean_tree().expect("clean"), tree); + + let store = FsStore::in_git_common_dir(Some(&dir)).expect("store"); + let scratch = std::fs::canonicalize(&dir).expect("canonical"); + let stamp_parent = + std::fs::canonicalize(store.dir.parent().expect("parent")).expect("exists"); + assert!( + stamp_parent.starts_with(&scratch), + "the store must be in the scratch repository, not the outer one: {stamp_parent:?}" + ); + println!("hostile-child-ran"); + } + + #[test] + fn git_driven_behaviour_ignores_a_hostile_git_environment() { + let outer = repo(); + let inner = repo(); + let outer_git = outer.path().join(".git"); + let outer_head = git_in(outer.path(), &["rev-parse", "HEAD"]); + + let output = Command::new(std::env::current_exe().expect("this test binary")) + .args([ + "--exact", + "prepush_stamp::tests::hostile_environment_child", + "--nocapture", + "--test-threads=1", + ]) + .env(HOSTILE_CHILD_VAR, inner.path()) + .env("GIT_DIR", &outer_git) + .env("GIT_WORK_TREE", outer.path()) + .env("GIT_INDEX_FILE", outer_git.join("index")) + .env("GIT_COMMON_DIR", &outer_git) + .output() + .expect("re-run the test binary"); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(output.status.success(), "{stdout}\n{stderr}"); + assert!( + stdout.contains("hostile-child-ran"), + "the child did not run its body: {stdout}" + ); + + // The outer repository is exactly as it was: no stray commit, nothing staged. + assert_eq!(git_in(outer.path(), &["rev-parse", "HEAD"]), outer_head); + assert_eq!(git_in(outer.path(), &["rev-list", "--count", "HEAD"]), "1"); + assert_eq!(git_in(outer.path(), &["status", "--porcelain"]), ""); + } + + // ---- non-Unicode environment variables must never abort the optional stamp ---- + + #[cfg(unix)] + fn non_unicode(prefix: &str, tail: &[u8]) -> std::ffi::OsString { + use std::os::unix::ffi::OsStringExt; + let mut bytes = prefix.as_bytes().to_vec(); + bytes.extend_from_slice(tail); + std::ffi::OsString::from_vec(bytes) + } + + /// Unicode values hash to the same digest they always did, so stamps recorded + /// before the digest read the environment as bytes stay valid. + #[test] + fn the_environment_digest_of_unicode_values_is_the_digest_of_the_text() { + assert_eq!( + env_digest(vars(&[("RUSTFLAGS", "-D warnings"), ("PATH", "/bin")])), + hex_digest("RUSTFLAGS=-D warnings\n") + ); + } + + #[cfg(unix)] + #[test] + fn an_unrelated_variable_with_non_unicode_bytes_is_ignored_and_does_not_panic() { + let base = env_digest(vars(&[("RUSTFLAGS", "-D warnings")])); + let with_noise = env_digest([ + ( + std::ffi::OsString::from("RUSTFLAGS"), + std::ffi::OsString::from("-D warnings"), + ), + ( + std::ffi::OsString::from("PATH"), + non_unicode("/bin", &[0xff]), + ), + (non_unicode("HOME_", &[0xfe]), std::ffi::OsString::from("x")), + ( + std::ffi::OsString::from("LANG"), + non_unicode("", &[0x80, 0x81]), + ), + ]); + assert_eq!( + with_noise, base, + "unrelated variables never reach the digest" + ); + } + + #[cfg(unix)] + #[test] + fn a_relevant_variable_with_non_unicode_bytes_is_hashed_as_the_bytes_it_is() { + let digest = |value: &[u8]| { + env_digest([( + std::ffi::OsString::from("RUSTFLAGS"), + non_unicode("", value), + )]) + }; + let absent = env_digest(Vec::<(String, String)>::new()); + assert_ne!(digest(&[0xff]), absent, "the variable counts"); + // A lossy conversion would turn both bytes into U+FFFD and collapse them. + assert_ne!( + digest(&[0xff]), + digest(&[0xfe]), + "the bytes are hashed, not a lossy text" + ); + assert_eq!(digest(&[0xff]), digest(&[0xff]), "and the digest is stable"); + + // The same holds when the invalid bytes are in the name, after a prefix. + let named = |tail: &[u8]| { + env_digest([(non_unicode("CARGO_X", tail), std::ffi::OsString::from("1"))]) + }; + assert_ne!(named(&[0xff]), absent); + assert_ne!(named(&[0xff]), named(&[0xfe])); + } + + const NON_UNICODE_CHILD_VAR: &str = "ROCM_STAMP_TEST_NON_UNICODE_CHILD"; + + /// Does nothing on its own. The test below re-runs this binary with + /// non-Unicode bytes in unrelated and in relevant variables, and this body + /// then drives the real probe: `std::env::vars()` would panic here. + #[test] + fn non_unicode_environment_child() { + if std::env::var_os(NON_UNICODE_CHILD_VAR).is_none() { + return; + } + let probe = SystemProbe::default(); + let first = Probe::env_digest(&probe); + assert_eq!(first, Probe::env_digest(&probe), "the digest is stable"); + assert_eq!(first.len(), 64, "a SHA-256 digest: {first}"); + println!("non-unicode-child-ran {first}"); + } + + #[cfg(unix)] + #[test] + fn the_real_probe_computes_its_digest_under_non_unicode_environment_variables() { + let run = |rustflags: &[u8]| { + let output = Command::new(std::env::current_exe().expect("this test binary")) + .args([ + "--exact", + "prepush_stamp::tests::non_unicode_environment_child", + "--nocapture", + "--test-threads=1", + ]) + .env(NON_UNICODE_CHILD_VAR, "1") + .env("ROCM_STAMP_TEST_UNRELATED", non_unicode("v", &[0xff, 0xfe])) + .env(non_unicode("UNRELATED_", &[0xff]), "x") + .env("RUSTFLAGS", non_unicode("", rustflags)) + .output() + .expect("re-run the test binary"); + let stdout = String::from_utf8_lossy(&output.stdout).into_owned(); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(output.status.success(), "{stdout}\n{stderr}"); + stdout + .split_once("non-unicode-child-ran ") + .and_then(|(_, rest)| rest.split_whitespace().next()) + .unwrap_or_else(|| panic!("the child did not run its body: {stdout}")) + .to_string() + }; + let one = run(&[0xff]); + // A relevant non-Unicode value changes the digest, losslessly. + assert_ne!(one, run(&[0xfe])); + assert_eq!(one, run(&[0xff])); + } + + // ---- scratch files left by a write that died ---- + + #[test] + fn a_write_that_cannot_be_moved_into_place_leaves_no_scratch_file() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = FsStore { + dir: dir.path().join("passes"), + }; + // A non-empty directory where the stamp should go makes the rename fail. + std::fs::create_dir_all(store.dir.join("a.stamp")).expect("mkdir"); + std::fs::write(store.dir.join("a.stamp").join("inner"), "x").expect("write"); + + let error = store + .write("a.stamp", "x") + .expect_err("a directory is in the way"); + assert!( + format!("{error:#}").contains("failed to move the stamp into place"), + "{error:#}" + ); + let names: Vec = std::fs::read_dir(&store.dir) + .expect("dir") + .flatten() + .map(|entry| entry.file_name().to_string_lossy().into_owned()) + .collect(); + assert_eq!( + names, + ["a.stamp"], + "no scratch file is left behind: {names:?}" + ); + } + + #[test] + fn stale_scratch_files_are_pruned_and_fresh_ones_kept_and_none_count_toward_the_cap() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + { + let mut files = store.files.borrow_mut(); + files.insert( + ".a.stamp.1.tmp".into(), + (String::new(), NOW - TMP_MAX_AGE_SECS - 1), + ); + files.insert( + ".b.stamp.2.tmp".into(), + (String::new(), NOW - TMP_MAX_AGE_SECS), + ); + files.insert(".c.stamp.3.tmp".into(), (String::new(), NOW - 60)); + for index in 0..MAX_STAMPS { + files.insert( + format!("fresh-{index:03}.stamp"), + (String::new(), NOW - 1000 + index as u64), + ); + } + } + record_pass(&probe, &store, NOW); + let files = store.files.borrow(); + let names: Vec<&String> = files.keys().collect(); + assert!( + !files.contains_key(".a.stamp.1.tmp"), + "a dead writer's scratch file goes: {names:?}" + ); + assert!( + files.contains_key(".b.stamp.2.tmp"), + "one exactly at the bound stays" + ); + assert!( + files.contains_key(".c.stamp.3.tmp"), + "a young one may be a live writer's" + ); + let stamps = files + .keys() + .filter(|name| name.ends_with(STAMP_EXT)) + .count(); + assert_eq!( + stamps, MAX_STAMPS, + "scratch files do not push stamps out: {names:?}" + ); + } + + #[test] + fn a_real_store_prunes_a_dead_writers_scratch_file() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = FsStore { + dir: dir.path().join("passes"), + }; + std::fs::create_dir_all(&store.dir).expect("mkdir"); + let now = now_secs(); + let age_of = |name: &str, secs: u64| { + let path = store.dir.join(name); + std::fs::write(&path, "x").expect("write"); + std::fs::File::options() + .write(true) + .open(&path) + .expect("open") + .set_modified(SystemTime::now() - std::time::Duration::from_secs(secs)) + .expect("set the modified time"); + path + }; + let dead = age_of(".a.stamp.1.tmp", 2 * 60 * 60); + let live = age_of(".b.stamp.2.tmp", 60); + + let probe = FakeProbe::clean("t"); + let stamps = Stamps::new(&probe, &store, now, false, |_| None); + let Lookup::Miss { key: Some(key), .. } = stamps.lookup("test", &args("test -p xtask")) + else { + panic!("expected a miss with a key"); + }; + assert_eq!(stamps.record(&key), None); + assert!( + !dead.exists(), + "the dead writer's scratch file is removed from disk" + ); + assert!(live.exists(), "the young one is left alone"); + } + + // ---- Stamps::new: the --rerun flag and the variable reach the lookup ---- + + #[test] + fn stamps_new_reads_the_flag_and_the_variable() { + let (probe, store) = (FakeProbe::clean("t"), MemStore::default()); + let var = + |value: &'static str| move |name: &str| (name == RERUN_VAR).then(|| value.to_string()); + assert!(Stamps::new(&probe, &store, NOW, true, |_| None).rerun); + assert!(Stamps::new(&probe, &store, NOW, false, var("1")).rerun); + assert!(!Stamps::new(&probe, &store, NOW, false, var("0")).rerun); + assert!(!Stamps::new(&probe, &store, NOW, false, |_| None).rerun); + } +}