testing_conventions/e2e.rs
1//! `e2e attest` / `e2e verify` — the e2e attestation nudge.
2//!
3//! `attest` runs the e2e suite locally and records that it ran against the
4//! current commit; `verify` confirms in CI that the latest
5//! code commit is attested. The point is to *nudge* agents to run e2e locally —
6//! CI never runs e2e, it only checks the committed attestation.
7
8use std::path::{Path, PathBuf};
9use std::process::Command;
10use std::time::{SystemTime, UNIX_EPOCH};
11
12use anyhow::{bail, Context, Result};
13use serde::{Deserialize, Serialize};
14
15/// Where the committed attestation lives, relative to the repo root.
16pub const ATTESTATION_PATH: &str = "e2e-attestation.json";
17
18/// A record of one local e2e run — written to disk and committed by [`attest`].
19///
20/// `commit` is the SHA of the code commit the run was made against (HEAD at
21/// attest time); [`verify`](crate::e2e) checks it against the latest code
22/// commit. The rest is information for humans — nothing is gated on it.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct Attestation {
25 /// The command that was run (e.g. `pnpm run e2e`).
26 pub command: String,
27 /// When it ran, as a Unix timestamp (seconds).
28 pub ran_at: u64,
29 /// The command's exit code — recorded, never gated on.
30 pub exit_code: i32,
31 /// The commit the run was made against (HEAD at attest time).
32 pub commit: String,
33}
34
35/// Run `command` in `repo`, write an [`Attestation`] naming the current HEAD to
36/// `repo`/[`ATTESTATION_PATH`], and commit it on top. Returns the attestation.
37///
38/// Writes regardless of the command's exit code — this forces a *run*, not a
39/// *pass*.
40pub fn attest(repo: &Path, command: &str) -> Result<Attestation> {
41 let commit = git_capture(repo, &["rev-parse", "HEAD"])
42 .context("resolving HEAD — `e2e attest` must run inside a git repo with a commit")?;
43
44 // Run the e2e command via the shell, streaming its output through.
45 let status = Command::new("sh")
46 .arg("-c")
47 .arg(command)
48 .current_dir(repo)
49 .status()
50 .with_context(|| format!("running e2e command `{command}`"))?;
51 let exit_code = status.code().unwrap_or(-1);
52
53 let ran_at = SystemTime::now()
54 .duration_since(UNIX_EPOCH)
55 .map(|d| d.as_secs())
56 .unwrap_or(0);
57
58 let attestation = Attestation {
59 command: command.to_string(),
60 ran_at,
61 exit_code,
62 commit: commit.clone(),
63 };
64
65 // Write the attestation, then commit just that file on top — it names the
66 // code commit it was run against (a commit can't name its own SHA).
67 let path = repo.join(ATTESTATION_PATH);
68 let json = serde_json::to_string_pretty(&attestation).context("serializing the attestation")?;
69 std::fs::write(&path, format!("{json}\n"))
70 .with_context(|| format!("writing {}", path.display()))?;
71
72 git_run(repo, &["add", ATTESTATION_PATH])?;
73 let short = &commit[..commit.len().min(7)];
74 let message = format!("e2e attestation for {short}");
75 // A plain commit that inherits the repo's signing policy: a repo requiring
76 // verified signatures gets a signed (mergeable) attestation, instead of the
77 // unsigned commit a forced `commit.gpgsign=false` would leave behind.
78 git_run(repo, &["commit", "-q", "-m", message.as_str()])?;
79
80 Ok(attestation)
81}
82
83/// The outcome of [`verify`] — whether the committed attestation names the latest
84/// code commit, and if not, why.
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub enum Verification {
87 /// The attestation names the latest code commit — the gate passes.
88 Fresh,
89 /// No attestation file is present — the gate fails.
90 Missing,
91 /// An attestation is present but names an older commit than the latest code
92 /// commit (code changed since it was attested) — the gate fails.
93 Stale {
94 /// The commit the attestation names.
95 attested: String,
96 /// The latest code commit (newest one touching a non-attestation path).
97 latest: String,
98 },
99}
100
101/// Verify that the committed attestation names the latest code commit — the
102/// CI side of the nudge. Reads only the committed attestation: never runs e2e,
103/// never inspects the recorded exit code or output.
104///
105/// Equivalent to [`verify_scoped`] with `scope` set to `repo` — the freshness
106/// walk covers everything under `repo`.
107pub fn verify(repo: &Path) -> Result<Verification> {
108 verify_scoped(repo, repo)
109}
110
111/// Verify the committed attestation at `repo`, scoping the "latest code commit"
112/// walk to `scope` instead of all of `repo`.
113///
114/// `repo` and `scope` serve different roles: `repo` is where
115/// `e2e-attestation.json` lives (the package root — a manifest's own natural
116/// home), while `scope` is what counts as "code" for freshness (the directory a
117/// `path`-scoped call actually named, which can be narrower — a package root
118/// commonly also holds `tests/`, docs, and config files that aren't the
119/// attestable source). `scope` must be `repo` or a descendant of it.
120///
121/// Equivalent to [`verify_since`] with `base` set to `None` — the walk covers
122/// all reachable history.
123pub fn verify_scoped(repo: &Path, scope: &Path) -> Result<Verification> {
124 verify_since(repo, scope, None)
125}
126
127/// Equivalent to [`verify_extra_scoped`] with no extra roots and no excludes —
128/// the freshness walk covers only `scope`.
129pub fn verify_since(repo: &Path, scope: &Path, base: Option<&str>) -> Result<Verification> {
130 verify_extra_scoped(repo, scope, base, &[], &[])
131}
132
133/// Verify the committed attestation at `repo`, joining **extra freshness roots**
134/// outside `scope` into the "latest code commit" walk.
135///
136/// `extra_scopes` are **repo-root-relative** directories — a shared source tree
137/// that is a sibling of the package (a native core bound into several language
138/// bindings), which no `scope` at-or-below the attestation directory can reach.
139/// Their commits count as code just like commits under `scope`, so a change to
140/// the shared tree stales an attestation whose own subtree it doesn't touch.
141/// `excludes` are repo-root-relative directories carved back out of that union —
142/// a feature-gated subtree (a core `cli/` compiled out of the bindings) whose
143/// changes must not stale them. Because the roots may lie outside the package
144/// subtree — that's the point — the descendant constraint stays on `scope` only.
145///
146/// The freshness rule is unchanged: the attestation must name the newest in-range
147/// commit touching the **union** of `scope` and `extra_scopes`, minus `excludes`.
148/// `base` diff-scopes the walk exactly as in [`verify_since`]. Empty
149/// `extra_scopes` and `excludes` are byte-identical to [`verify_since`].
150pub fn verify_extra_scoped(
151 repo: &Path,
152 scope: &Path,
153 base: Option<&str>,
154 extra_scopes: &[PathBuf],
155 excludes: &[PathBuf],
156) -> Result<Verification> {
157 let path = repo.join(ATTESTATION_PATH);
158 let Ok(contents) = std::fs::read_to_string(&path) else {
159 return Ok(Verification::Missing);
160 };
161 let attestation: Attestation =
162 serde_json::from_str(&contents).context("parsing the attestation")?;
163
164 match latest_code_commit(repo, scope, base, extra_scopes, excludes)? {
165 // With `--base`, an empty walk means this branch introduced no scoped
166 // code commit — there is nothing to re-attest, so it's fresh by
167 // construction (mirrors the changed-line coverage/mutation gates, which
168 // pass on an empty diff). This is what keeps the gate squash-safe: a
169 // stale-on-base attestation never reds a PR that didn't touch the source.
170 // Without `--base` the walk covers all history and only comes back empty
171 // for a scope with no commits at all, where an equality check against the
172 // attested SHA (below) is the right answer — same as before this flag.
173 None if base.is_some() => Ok(Verification::Fresh),
174 latest => {
175 let latest = latest.unwrap_or_default();
176 if attestation.commit == latest {
177 Ok(Verification::Fresh)
178 } else {
179 Ok(Verification::Stale {
180 attested: attestation.commit,
181 latest,
182 })
183 }
184 }
185 }
186}
187
188/// The newest commit that changed any path other than the attestation file,
189/// under the union of `scope` and `extra_scopes` minus `excludes` — the "latest
190/// code commit" the attestation must name to be fresh. Uses an `:(exclude)`
191/// pathspec so the attestation's own commit never counts as code. `extra_scopes`
192/// and `excludes` are repo-root-relative (`:(top)` pathspec magic), so they match
193/// from the top of the tree regardless of `repo` being a subdirectory cwd — that
194/// is how a sibling tree outside `scope` joins the walk. When `base` is
195/// `Some`, the walk is restricted to the commits this branch introduced
196/// (`<base>..HEAD`); `None` when that range holds no such commit. Without `base`
197/// the walk covers all reachable history.
198fn latest_code_commit(
199 repo: &Path,
200 scope: &Path,
201 base: Option<&str>,
202 extra_scopes: &[PathBuf],
203 excludes: &[PathBuf],
204) -> Result<Option<String>> {
205 let pathspec = relative_pathspec(repo, scope);
206 let mut args: Vec<String> = vec!["log".into(), "-1".into(), "--format=%H".into()];
207 // `<base>..HEAD` — commits reachable from HEAD but not `base`, i.e. the ones
208 // this branch introduced. A commit that also landed on `base` (a squash of an
209 // earlier PR) is reachable from `base`, so it's excluded here — that's the
210 // whole point.
211 if let Some(base) = base {
212 args.push(format!("{base}..HEAD"));
213 }
214 args.push("--".into());
215 // Positive pathspecs: the package's own `scope`, plus each extra root as a
216 // repo-root-relative `:(top)` pathspec so it matches from the tree top rather
217 // than relative to `repo` (the package cwd git runs in).
218 args.push(pathspec);
219 for extra in extra_scopes {
220 args.push(format!(":(top){}", extra.display()));
221 }
222 // Negative pathspecs: the attestation itself never counts as code, and each
223 // excluded (feature-gated) subtree of an extra root is carved back out.
224 args.push(format!(":(exclude){ATTESTATION_PATH}"));
225 for exclude in excludes {
226 args.push(format!(":(top,exclude){}", exclude.display()));
227 }
228 let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
229 let out = git_capture(repo, &arg_refs)?;
230 Ok((!out.is_empty()).then_some(out))
231}
232
233/// `scope` as a pathspec relative to `repo` (git resolves pathspecs relative to
234/// the invocation's cwd, which is always `repo` here). `.` when `scope` is
235/// `repo` itself.
236fn relative_pathspec(repo: &Path, scope: &Path) -> String {
237 if scope == repo {
238 return ".".to_string();
239 }
240 match scope.strip_prefix(repo) {
241 Ok(rel) if !rel.as_os_str().is_empty() => rel.to_string_lossy().into_owned(),
242 _ => scope.to_string_lossy().into_owned(),
243 }
244}
245
246/// Run `git` with `args` in `repo`, returning trimmed stdout; errors if git fails.
247fn git_capture(repo: &Path, args: &[&str]) -> Result<String> {
248 let out = Command::new("git")
249 .args(args)
250 .current_dir(repo)
251 .output()
252 .with_context(|| format!("running `git {}`", args.join(" ")))?;
253 if !out.status.success() {
254 bail!(
255 "`git {}` failed: {}",
256 args.join(" "),
257 String::from_utf8_lossy(&out.stderr).trim()
258 );
259 }
260 Ok(String::from_utf8(out.stdout)?.trim().to_string())
261}
262
263/// Run `git` with `args` in `repo` for its side effect; errors if git fails.
264fn git_run(repo: &Path, args: &[&str]) -> Result<()> {
265 let status = Command::new("git")
266 .args(args)
267 .current_dir(repo)
268 .status()
269 .with_context(|| format!("running `git {}`", args.join(" ")))?;
270 if !status.success() {
271 bail!("`git {}` failed", args.join(" "));
272 }
273 Ok(())
274}