Skip to main content

vcs_testkit/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(rustdoc::broken_intra_doc_links)]
3//! `vcs-testkit` — throwaway git/jj sandboxes (and a bare remote) for
4//! integration tests.
5//!
6//! Hands a `#[test]` a real repository to drive: a unique self-cleaning
7//! [`TempDir`], a configured [`GitSandbox`] / [`JjSandbox`] to build scenarios
8//! in, and a seeded [`BareRemote`] to clone/fetch/push against. It is
9//! **dependency-free** (not even the wrapper crates, so it can be a
10//! dev-dependency of any of them without a cycle), **synchronous** (test setup
11//! needs no runtime — it shells out with `std::process::Command`, not the async
12//! client under test), and **panics on failure** (a broken fixture should fail
13//! loudly at the call site, not thread `Result`s through scenario code).
14//!
15//! Built for `#[test]` / `#[ignore]` integration tests that need a *real* repo:
16//! the helpers run the actual `git` / `jj` on `PATH`, so gate any test that
17//! touches one behind `#[ignore = "requires the git binary"]` — a hermetic CI
18//! with no binaries installed then stays green, and `cargo test -- --ignored`
19//! runs them locally. Every sandbox is isolated from the host's VCS config (no
20//! system/global config, no `init.templateDir` hook leakage, a deterministic
21//! identity even on the commit `jj git init` creates) — see `command`.
22//!
23//! # The surface
24//!
25//! - **[`TempDir`]** — a unique temporary directory, removed on drop.
26//!   Uniqueness without a temp-dir crate: pid + a process-wide monotonic
27//!   counter, so parallel tests in a run never collide. Every fixture owns one.
28//! - **[`GitSandbox`]** — a throwaway **git** repo on branch `main` with a
29//!   deterministic identity. Build scenarios through the convenience steps
30//!   ([`commit_file`](GitSandbox::commit_file), [`branch`](GitSandbox::branch),
31//!   [`checkout`](GitSandbox::checkout), [`rev_parse`](GitSandbox::rev_parse))
32//!   plus the raw [`git`](GitSandbox::git) escape hatch for anything unmodelled.
33//! - **[`JjSandbox`]** — the same shape for a **jj** (git-backed) workspace:
34//!   [`describe`](JjSandbox::describe), [`new_change`](JjSandbox::new_change),
35//!   [`bookmark`](JjSandbox::bookmark), and the raw [`jj`](JjSandbox::jj) hatch.
36//! - **[`BareRemote`]** — a populated **bare** git repo, a local
37//!   clone/fetch/push source with no network. [`BareRemote::seeded`] gives one
38//!   commit on `main` containing `seed.txt`; [`url`](BareRemote::url) yields a
39//!   string remote URL.
40//! - **[`configure_identity`]** — stamp a git repo with a deterministic
41//!   identity and byte-stable behaviour (`user.*`, `commit.gpgsign=false`,
42//!   `core.autocrlf=false`). Standalone, for tests whose *subject* is `init`.
43//! - **Raw steps [`git`] / [`jj`]** — run one command in any `dir`, panicking
44//!   on failure: for scenario steps in directories no sandbox owns (linked
45//!   worktrees, fresh clones, repos the code under test initialised).
46//!
47//! # Recipes
48//!
49//! These are sync — no async wrapper, no `Result` (fixtures panic). They are
50//! `no_run`: they really create temp dirs and shell out to `git`/`jj`, so they
51//! compile here but only run under a binary-equipped `#[test]`.
52//!
53//! Build a git scenario — write + stage + commit is one step:
54//!
55//! ```no_run
56//! use vcs_testkit::GitSandbox;
57//! # fn demo() {
58//! let repo = GitSandbox::init("scenario");
59//! repo.commit_file("a.txt", "one\n", "first");   // write + add -A + commit
60//! repo.branch("feature");
61//! repo.checkout("feature");
62//! repo.commit_file("sub/b.txt", "two\n", "second");
63//!
64//! let head = repo.rev_parse("HEAD");
65//! assert_eq!(head.len(), 40);
66//! assert_ne!(head, repo.rev_parse("main"));       // feature has diverged
67//! # }
68//! ```
69//!
70//! Seed a bare remote and fetch from it — drop to raw `git` for the remote wiring:
71//!
72//! ```no_run
73//! use vcs_testkit::{BareRemote, GitSandbox};
74//! # fn demo() {
75//! let repo = GitSandbox::init("local");
76//! repo.commit_file("a.txt", "one\n", "first");
77//!
78//! let remote = BareRemote::seeded("origin");
79//! repo.git(&["remote", "add", "origin", remote.url().as_str()]);
80//! repo.git(&["fetch", "-q", "origin"]);
81//! assert_eq!(repo.rev_parse("origin/main").len(), 40); // seed commit fetched
82//! # }
83//! ```
84//!
85//! # In-depth guide
86//!
87//! Beyond this page, this crate ships a full how-to guide — rendered on docs.rs
88//! from `docs/`. See the [`guide`] module (and its cross-cutting
89//! [`testing`](crate::guide::testing) sub-guide on the trait / mock / runner
90//! seams that let most tests skip real binaries entirely).
91
92use std::path::{Path, PathBuf};
93use std::process::Command;
94use std::sync::atomic::{AtomicU64, Ordering};
95
96static COUNTER: AtomicU64 = AtomicU64::new(0);
97
98/// A unique temporary directory, removed on drop.
99///
100/// Unique without a temp-dir crate: process id + a process-wide monotonic
101/// counter, so parallel tests within a run never collide. The name is kept
102/// deliberately short — jj's `op_store` paths are deep, and a long prefix here
103/// can tip a nested `.jj/repo/op_store/operations/<id>` path over Windows'
104/// `MAX_PATH` (260) limit.
105pub struct TempDir(PathBuf);
106
107impl TempDir {
108    /// Create `%TEMP%/vcs-testkit-<tag>-<pid>-<n>`. Panics when the directory
109    /// cannot be created.
110    pub fn new(tag: &str) -> Self {
111        let path = std::env::temp_dir().join(format!(
112            "vcs-testkit-{tag}-{}-{}",
113            std::process::id(),
114            COUNTER.fetch_add(1, Ordering::Relaxed)
115        ));
116        std::fs::create_dir_all(&path).expect("create temp dir");
117        TempDir(path)
118    }
119
120    /// The directory's path.
121    pub fn path(&self) -> &Path {
122        &self.0
123    }
124}
125
126impl Drop for TempDir {
127    fn drop(&mut self) {
128        // Best-effort: a leaked temp dir must not fail the test run.
129        let _ = std::fs::remove_dir_all(&self.0);
130    }
131}
132
133/// Build an isolated [`Command`] for `binary` in `cwd`.
134///
135/// **Every** git/jj invocation the testkit makes routes through here so the
136/// sandbox is hermetic — it must not inherit the host user's VCS config. A
137/// host-global `init.templateDir` / `core.hooksPath` (git) or `[user]` block
138/// (jj) would otherwise leak in: a templateDir hook gets copied into the
139/// sandbox's `.git/hooks` and *executes* during sandbox commits, and a host
140/// jj identity stamps the init-created working-copy commit.
141///
142/// The redirect-config env vars point at a guaranteed-nonexistent path; git
143/// and jj both treat a missing config file as empty, so no temp file is
144/// needed and the free [`git`]/[`jj`] helpers (which own no sandbox dir) get
145/// the same isolation as the sandbox methods.
146fn command(binary: &str, cwd: &Path) -> Command {
147    // A path that cannot exist: a child of *this* binary's own path (a file,
148    // so it can have no children). Resolved per call to stay self-contained.
149    let nonexistent = std::env::current_exe()
150        .unwrap_or_else(|_| PathBuf::from("vcs-testkit-no-such"))
151        .join("vcs-testkit-nonexistent-config");
152    let mut cmd = Command::new(binary);
153    cmd.current_dir(cwd);
154    match binary {
155        "git" => {
156            // Ignore system config; redirect global/system config at a
157            // nonexistent file (defeats a host-set GIT_CONFIG_GLOBAL too);
158            // and never block on a credential prompt. Scrub any inherited
159            // GIT_DIR-style vars that would otherwise point git elsewhere.
160            cmd.env("GIT_CONFIG_NOSYSTEM", "1")
161                .env("GIT_CONFIG_GLOBAL", &nonexistent)
162                .env("GIT_CONFIG_SYSTEM", &nonexistent)
163                .env("GIT_TERMINAL_PROMPT", "0")
164                .env_remove("GIT_CONFIG_PARAMETERS")
165                .env_remove("GIT_CONFIG")
166                .env_remove("GIT_DIR")
167                .env_remove("GIT_COMMON_DIR")
168                .env_remove("GIT_WORK_TREE")
169                .env_remove("GIT_INDEX_FILE")
170                .env_remove("GIT_OBJECT_DIRECTORY")
171                .env_remove("GIT_NAMESPACE");
172        }
173        "jj" => {
174            // Read config exclusively from a nonexistent file (no host
175            // config), and stamp a deterministic identity on *every* commit
176            // — including the working-copy commit `jj git init` creates,
177            // which a later `config set --repo user.*` cannot retroactively
178            // re-author.
179            cmd.env("JJ_CONFIG", &nonexistent)
180                .env("JJ_USER", "test")
181                .env("JJ_EMAIL", "test@example.com");
182        }
183        _ => {}
184    }
185    cmd
186}
187
188/// Run a binary in `cwd`, panicking (with the command line in the message) on
189/// a spawn failure or non-zero exit. The fixture contract: fail loudly.
190fn run(binary: &str, cwd: &Path, args: &[&str]) {
191    let status = command(binary, cwd)
192        .args(args)
193        .status()
194        .unwrap_or_else(|e| panic!("failed to run `{binary} {args:?}`: {e}"));
195    assert!(status.success(), "`{binary} {args:?}` exited with {status}");
196}
197
198/// Like [`run`] but capturing trimmed stdout.
199fn run_capture(binary: &str, cwd: &Path, args: &[&str]) -> String {
200    let out = command(binary, cwd)
201        .args(args)
202        .output()
203        .unwrap_or_else(|e| panic!("failed to run `{binary} {args:?}`: {e}"));
204    assert!(
205        out.status.success(),
206        "`{binary} {args:?}` exited with {}: {}",
207        out.status,
208        String::from_utf8_lossy(&out.stderr)
209    );
210    String::from_utf8_lossy(&out.stdout).trim_end().to_string()
211}
212
213/// Run `git <args>` in `dir`, panicking on failure — for scenario steps in
214/// directories not owned by a [`GitSandbox`] (linked worktrees, fresh clones,
215/// repos initialised by the code under test).
216pub fn git(dir: &Path, args: &[&str]) {
217    run("git", dir, args);
218}
219
220/// Run `jj <args>` in `dir`, panicking on failure (see [`git`]).
221pub fn jj(dir: &Path, args: &[&str]) {
222    run("jj", dir, args);
223}
224
225/// Give the git repository at `dir` a deterministic identity and byte-stable
226/// behaviour: `user.name`/`user.email`, `commit.gpgsign=false` (no keychain
227/// prompts), and `core.autocrlf=false` (no CRLF rewriting under content
228/// assertions on Windows).
229///
230/// Deliberately does NOT touch `core.hooksPath`: host-config hook leakage is
231/// neutralised at the source instead — `command`'s env redirect keeps a host
232/// global/system config (a `core.hooksPath` or `init.templateDir`) out of
233/// every testkit-run git, and `--template=` on `init` keeps template hooks
234/// from being copied into `.git/hooks`. Disabling hooks in the repo's *local*
235/// config would also disable hooks a test itself installs on purpose (e.g.
236/// the hardened-profile suppression test).
237///
238/// Standalone (not folded into [`GitSandbox::init`] only) for tests whose
239/// *subject* is repository initialisation itself — they run their own `init`
240/// and only need the identity applied afterwards.
241pub fn configure_identity(dir: &Path) {
242    for (key, val) in [
243        ("user.name", "Test"),
244        ("user.email", "test@example.com"),
245        ("commit.gpgsign", "false"),
246        ("core.autocrlf", "false"),
247    ] {
248        run("git", dir, &["config", key, val]);
249    }
250}
251
252/// A throwaway **git** repository: owns its [`TempDir`], initialised on
253/// branch `main` with a deterministic identity (see [`configure_identity`]).
254///
255/// Scenario-building goes through the raw [`git`](GitSandbox::git) escape
256/// hatch plus the convenience methods — the sandbox deliberately does not
257/// depend on the typed wrapper crates, so it can be a dev-dependency of any
258/// of them.
259pub struct GitSandbox {
260    dir: TempDir,
261}
262
263impl GitSandbox {
264    /// Create and initialise a repository (`git init -b main` — git ≥ 2.28,
265    /// comfortably below the wrappers' documented floor).
266    ///
267    /// `--template=` (empty) makes the new repo skip *any* init template,
268    /// so a host-global `init.templateDir` cannot seed hooks into
269    /// `.git/hooks` — the version-portable complement to the config
270    /// isolation in `command`.
271    pub fn init(tag: &str) -> Self {
272        let dir = TempDir::new(tag);
273        run(
274            "git",
275            dir.path(),
276            &["init", "-q", "-b", "main", "--template="],
277        );
278        configure_identity(dir.path());
279        GitSandbox { dir }
280    }
281
282    /// The repository's working-tree path.
283    pub fn path(&self) -> &Path {
284        self.dir.path()
285    }
286
287    /// Run `git <args>` in the repository, panicking on failure.
288    pub fn git(&self, args: &[&str]) {
289        run("git", self.path(), args);
290    }
291
292    /// Write `content` to the repo-relative `path` (creating parent dirs).
293    pub fn write(&self, path: &str, content: &str) {
294        let full = self.path().join(path);
295        if let Some(parent) = full.parent() {
296            std::fs::create_dir_all(parent).expect("create parent dirs");
297        }
298        std::fs::write(full, content).expect("write file");
299    }
300
301    /// Stage everything (`git add -A`).
302    pub fn add_all(&self) {
303        self.git(&["add", "-A"]);
304    }
305
306    /// Commit the staged changes (`git commit -qm <message>`).
307    pub fn commit(&self, message: &str) {
308        self.git(&["commit", "-qm", message]);
309    }
310
311    /// Write + stage + commit one file — the everyday scenario step.
312    pub fn commit_file(&self, path: &str, content: &str, message: &str) {
313        self.write(path, content);
314        self.add_all();
315        self.commit(message);
316    }
317
318    /// Create a branch at HEAD without switching (`git branch <name>`).
319    pub fn branch(&self, name: &str) {
320        self.git(&["branch", "-q", name]);
321    }
322
323    /// Switch to a branch (`git checkout <name>`).
324    pub fn checkout(&self, name: &str) {
325        self.git(&["checkout", "-q", name]);
326    }
327
328    /// Resolve a revision to a full hash (`git rev-parse <rev>`).
329    pub fn rev_parse(&self, rev: &str) -> String {
330        run_capture("git", self.path(), &["rev-parse", rev])
331    }
332}
333
334/// A populated **bare** git repository — a local clone/fetch/push source for
335/// integration tests (no network). Seeded with one commit on `main`
336/// containing `seed.txt`.
337pub struct BareRemote {
338    dir: TempDir,
339    bare: PathBuf,
340}
341
342impl BareRemote {
343    /// Build the seeded bare repository.
344    pub fn seeded(tag: &str) -> Self {
345        let dir = TempDir::new(tag);
346        let work = dir.path().join("seed-work");
347        let bare = dir.path().join("remote.git");
348        std::fs::create_dir_all(&work).expect("create work dir");
349        std::fs::create_dir_all(&bare).expect("create bare dir");
350        run("git", &work, &["init", "-q", "-b", "main", "--template="]);
351        configure_identity(&work);
352        std::fs::write(work.join("seed.txt"), "seed\n").expect("write seed");
353        run("git", &work, &["add", "-A"]);
354        run("git", &work, &["commit", "-qm", "seed"]);
355        run(
356            "git",
357            &bare,
358            &["init", "-q", "--bare", "-b", "main", "--template="],
359        );
360        run(
361            "git",
362            &work,
363            &["push", "-q", bare.to_str().expect("utf8 path"), "main:main"],
364        );
365        BareRemote { dir, bare }
366    }
367
368    /// The bare repository's path (use as a local remote URL).
369    pub fn path(&self) -> &Path {
370        &self.bare
371    }
372
373    /// The path as a `String` — convenient for argv slices.
374    pub fn url(&self) -> String {
375        self.bare.to_str().expect("utf8 path").to_string()
376    }
377
378    /// The owning temp dir (kept alive as long as the remote is used).
379    pub fn temp_dir(&self) -> &Path {
380        self.dir.path()
381    }
382}
383
384/// A throwaway **jj** repository (git-backed) with a repo-scoped identity.
385pub struct JjSandbox {
386    dir: TempDir,
387}
388
389impl JjSandbox {
390    /// Create and initialise the repository (`jj git init` + repo-scoped
391    /// `user.name`/`user.email`).
392    ///
393    /// The identity is supplied to *every* jj invocation as `JJ_USER` /
394    /// `JJ_EMAIL` env (see `command`), so the working-copy commit that
395    /// `jj git init` creates is authored deterministically — a later
396    /// `config set --repo user.*` only affects *future* commits and so cannot
397    /// fix the init commit on its own. The repo-scoped config is kept anyway
398    /// as belt-and-braces for any tool path that reads config over the env.
399    pub fn init(tag: &str) -> Self {
400        let dir = TempDir::new(tag);
401        run("jj", dir.path(), &["git", "init"]);
402        run(
403            "jj",
404            dir.path(),
405            &["config", "set", "--repo", "user.name", "Test"],
406        );
407        run(
408            "jj",
409            dir.path(),
410            &["config", "set", "--repo", "user.email", "test@example.com"],
411        );
412        JjSandbox { dir }
413    }
414
415    /// The workspace root path.
416    pub fn path(&self) -> &Path {
417        self.dir.path()
418    }
419
420    /// Run `jj <args>` in the workspace, panicking on failure.
421    pub fn jj(&self, args: &[&str]) {
422        run("jj", self.path(), args);
423    }
424
425    /// Write `content` to the workspace-relative `path` (creating parents).
426    pub fn write(&self, path: &str, content: &str) {
427        let full = self.path().join(path);
428        if let Some(parent) = full.parent() {
429            std::fs::create_dir_all(parent).expect("create parent dirs");
430        }
431        std::fs::write(full, content).expect("write file");
432    }
433
434    /// Describe the working-copy change (`jj describe -m <message>`).
435    pub fn describe(&self, message: &str) {
436        self.jj(&["describe", "-m", message]);
437    }
438
439    /// Start a new change on top (`jj new -m <message>`).
440    pub fn new_change(&self, message: &str) {
441        self.jj(&["new", "-m", message]);
442    }
443
444    /// Create a bookmark at `@` (`jj bookmark create <name> -r @`).
445    pub fn bookmark(&self, name: &str) {
446        self.jj(&["bookmark", "create", name, "-r", "@"]);
447    }
448}
449
450#[cfg(test)]
451mod tests {
452    use super::*;
453
454    // Hermetic: uniqueness and cleanup need no binaries.
455    #[test]
456    fn temp_dirs_are_unique_and_removed_on_drop() {
457        let a = TempDir::new("unique");
458        let b = TempDir::new("unique");
459        assert_ne!(a.path(), b.path());
460        assert!(a.path().exists() && b.path().exists());
461        let kept = a.path().to_path_buf();
462        drop(a);
463        assert!(!kept.exists(), "removed on drop");
464    }
465
466    // Real-binary round-trips; ignored so hermetic CI stays green.
467    #[test]
468    #[ignore = "requires the git binary"]
469    fn git_sandbox_builds_scenarios() {
470        let repo = GitSandbox::init("sandbox");
471        repo.commit_file("a.txt", "one\n", "first");
472        repo.branch("feature");
473        repo.checkout("feature");
474        repo.commit_file("sub/b.txt", "two\n", "second");
475        let head = repo.rev_parse("HEAD");
476        assert_eq!(head.len(), 40);
477        assert_ne!(head, repo.rev_parse("main"));
478
479        let remote = BareRemote::seeded("remote");
480        repo.git(&["remote", "add", "origin", remote.url().as_str()]);
481        repo.git(&["fetch", "-q", "origin"]);
482        assert_eq!(
483            run_capture("git", repo.path(), &["show", "origin/main:seed.txt"]),
484            "seed"
485        );
486    }
487
488    // Isolation: `--template=` plus the config env keep a host-global
489    // `init.templateDir` from seeding hooks, so the sandbox's `.git/hooks`
490    // holds no live hook. (A real host hook firing is what the reviewer hit;
491    // here we assert the precondition — no enabled hook files — which holds
492    // regardless of the host's config.)
493    #[test]
494    #[ignore = "requires the git binary"]
495    fn git_sandbox_has_no_leaked_hooks() {
496        let repo = GitSandbox::init("hooks");
497        repo.commit_file("a.txt", "one\n", "first");
498        let hooks = repo.path().join(".git").join("hooks");
499        let enabled: Vec<_> = std::fs::read_dir(&hooks)
500            .into_iter()
501            .flatten()
502            .flatten()
503            .map(|e| e.file_name().to_string_lossy().into_owned())
504            // git ships `*.sample` hooks (inert); only non-sample files run.
505            .filter(|name| !name.ends_with(".sample"))
506            .collect();
507        assert!(
508            enabled.is_empty(),
509            "sandbox should have no live hooks, found {enabled:?}"
510        );
511        // Note `core.hooksPath` is deliberately NOT pinned in the local config —
512        // a test may install its own hook on purpose (see `configure_identity`);
513        // the isolation lives in `command`'s env + `--template=` instead.
514    }
515
516    #[test]
517    #[ignore = "requires the jj binary"]
518    fn jj_sandbox_builds_scenarios() {
519        let repo = JjSandbox::init("sandbox");
520        repo.write("a.txt", "one\n");
521        repo.describe("base");
522        repo.bookmark("mark");
523        repo.new_change("next");
524        // The described change and the bookmark are visible to jj.
525        let out = run_capture(
526            "jj",
527            repo.path(),
528            &[
529                "log",
530                "-r",
531                "::@",
532                "--no-graph",
533                "-T",
534                "description.first_line() ++ \"\\n\"",
535                "--color",
536                "never",
537            ],
538        );
539        assert!(out.contains("base"), "got {out:?}");
540    }
541
542    // Isolation: the working-copy commit `jj git init` creates is authored
543    // deterministically from the `JJ_USER`/`JJ_EMAIL` env, *not* from the
544    // host's jj config (which `config set --repo` could not retroactively
545    // re-author). `root()+` is the first non-root commit — the init commit.
546    #[test]
547    #[ignore = "requires the jj binary"]
548    fn jj_sandbox_init_commit_has_deterministic_author() {
549        let repo = JjSandbox::init("identity");
550        let email = run_capture(
551            "jj",
552            repo.path(),
553            &[
554                "log",
555                "-r",
556                "root()+",
557                "--no-graph",
558                "-T",
559                "author.email()",
560                "--color",
561                "never",
562            ],
563        );
564        assert_eq!(email, "test@example.com", "init commit author.email");
565    }
566}
567
568// Long-form how-to guides, rendered from this crate's docs/*.md on docs.rs.
569#[doc = include_str!("../docs/testkit.md")]
570#[allow(rustdoc::broken_intra_doc_links)]
571pub mod guide {
572    #[doc = include_str!("../docs/testing.md")]
573    #[allow(rustdoc::broken_intra_doc_links)]
574    pub mod testing {}
575}