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_DIR")
166 .env_remove("GIT_WORK_TREE")
167 .env_remove("GIT_INDEX_FILE");
168 }
169 "jj" => {
170 // Read config exclusively from a nonexistent file (no host
171 // config), and stamp a deterministic identity on *every* commit
172 // — including the working-copy commit `jj git init` creates,
173 // which a later `config set --repo user.*` cannot retroactively
174 // re-author.
175 cmd.env("JJ_CONFIG", &nonexistent)
176 .env("JJ_USER", "test")
177 .env("JJ_EMAIL", "test@example.com");
178 }
179 _ => {}
180 }
181 cmd
182}
183
184/// Run a binary in `cwd`, panicking (with the command line in the message) on
185/// a spawn failure or non-zero exit. The fixture contract: fail loudly.
186fn run(binary: &str, cwd: &Path, args: &[&str]) {
187 let status = command(binary, cwd)
188 .args(args)
189 .status()
190 .unwrap_or_else(|e| panic!("failed to run `{binary} {args:?}`: {e}"));
191 assert!(status.success(), "`{binary} {args:?}` exited with {status}");
192}
193
194/// Like [`run`] but capturing trimmed stdout.
195fn run_capture(binary: &str, cwd: &Path, args: &[&str]) -> String {
196 let out = command(binary, cwd)
197 .args(args)
198 .output()
199 .unwrap_or_else(|e| panic!("failed to run `{binary} {args:?}`: {e}"));
200 assert!(
201 out.status.success(),
202 "`{binary} {args:?}` exited with {}: {}",
203 out.status,
204 String::from_utf8_lossy(&out.stderr)
205 );
206 String::from_utf8_lossy(&out.stdout).trim_end().to_string()
207}
208
209/// Run `git <args>` in `dir`, panicking on failure — for scenario steps in
210/// directories not owned by a [`GitSandbox`] (linked worktrees, fresh clones,
211/// repos initialised by the code under test).
212pub fn git(dir: &Path, args: &[&str]) {
213 run("git", dir, args);
214}
215
216/// Run `jj <args>` in `dir`, panicking on failure (see [`git`]).
217pub fn jj(dir: &Path, args: &[&str]) {
218 run("jj", dir, args);
219}
220
221/// Give the git repository at `dir` a deterministic identity and byte-stable
222/// behaviour: `user.name`/`user.email`, `commit.gpgsign=false` (no keychain
223/// prompts), and `core.autocrlf=false` (no CRLF rewriting under content
224/// assertions on Windows).
225///
226/// Deliberately does NOT touch `core.hooksPath`: host-config hook leakage is
227/// neutralised at the source instead — `command`'s env redirect keeps a host
228/// global/system config (a `core.hooksPath` or `init.templateDir`) out of
229/// every testkit-run git, and `--template=` on `init` keeps template hooks
230/// from being copied into `.git/hooks`. Disabling hooks in the repo's *local*
231/// config would also disable hooks a test itself installs on purpose (e.g.
232/// the hardened-profile suppression test).
233///
234/// Standalone (not folded into [`GitSandbox::init`] only) for tests whose
235/// *subject* is repository initialisation itself — they run their own `init`
236/// and only need the identity applied afterwards.
237pub fn configure_identity(dir: &Path) {
238 for (key, val) in [
239 ("user.name", "Test"),
240 ("user.email", "test@example.com"),
241 ("commit.gpgsign", "false"),
242 ("core.autocrlf", "false"),
243 ] {
244 run("git", dir, &["config", key, val]);
245 }
246}
247
248/// A throwaway **git** repository: owns its [`TempDir`], initialised on
249/// branch `main` with a deterministic identity (see [`configure_identity`]).
250///
251/// Scenario-building goes through the raw [`git`](GitSandbox::git) escape
252/// hatch plus the convenience methods — the sandbox deliberately does not
253/// depend on the typed wrapper crates, so it can be a dev-dependency of any
254/// of them.
255pub struct GitSandbox {
256 dir: TempDir,
257}
258
259impl GitSandbox {
260 /// Create and initialise a repository (`git init -b main` — git ≥ 2.28,
261 /// comfortably below the wrappers' documented floor).
262 ///
263 /// `--template=` (empty) makes the new repo skip *any* init template,
264 /// so a host-global `init.templateDir` cannot seed hooks into
265 /// `.git/hooks` — the version-portable complement to the config
266 /// isolation in `command`.
267 pub fn init(tag: &str) -> Self {
268 let dir = TempDir::new(tag);
269 run(
270 "git",
271 dir.path(),
272 &["init", "-q", "-b", "main", "--template="],
273 );
274 configure_identity(dir.path());
275 GitSandbox { dir }
276 }
277
278 /// The repository's working-tree path.
279 pub fn path(&self) -> &Path {
280 self.dir.path()
281 }
282
283 /// Run `git <args>` in the repository, panicking on failure.
284 pub fn git(&self, args: &[&str]) {
285 run("git", self.path(), args);
286 }
287
288 /// Write `content` to the repo-relative `path` (creating parent dirs).
289 pub fn write(&self, path: &str, content: &str) {
290 let full = self.path().join(path);
291 if let Some(parent) = full.parent() {
292 std::fs::create_dir_all(parent).expect("create parent dirs");
293 }
294 std::fs::write(full, content).expect("write file");
295 }
296
297 /// Stage everything (`git add -A`).
298 pub fn add_all(&self) {
299 self.git(&["add", "-A"]);
300 }
301
302 /// Commit the staged changes (`git commit -qm <message>`).
303 pub fn commit(&self, message: &str) {
304 self.git(&["commit", "-qm", message]);
305 }
306
307 /// Write + stage + commit one file — the everyday scenario step.
308 pub fn commit_file(&self, path: &str, content: &str, message: &str) {
309 self.write(path, content);
310 self.add_all();
311 self.commit(message);
312 }
313
314 /// Create a branch at HEAD without switching (`git branch <name>`).
315 pub fn branch(&self, name: &str) {
316 self.git(&["branch", "-q", name]);
317 }
318
319 /// Switch to a branch (`git checkout <name>`).
320 pub fn checkout(&self, name: &str) {
321 self.git(&["checkout", "-q", name]);
322 }
323
324 /// Resolve a revision to a full hash (`git rev-parse <rev>`).
325 pub fn rev_parse(&self, rev: &str) -> String {
326 run_capture("git", self.path(), &["rev-parse", rev])
327 }
328}
329
330/// A populated **bare** git repository — a local clone/fetch/push source for
331/// integration tests (no network). Seeded with one commit on `main`
332/// containing `seed.txt`.
333pub struct BareRemote {
334 dir: TempDir,
335 bare: PathBuf,
336}
337
338impl BareRemote {
339 /// Build the seeded bare repository.
340 pub fn seeded(tag: &str) -> Self {
341 let dir = TempDir::new(tag);
342 let work = dir.path().join("seed-work");
343 let bare = dir.path().join("remote.git");
344 std::fs::create_dir_all(&work).expect("create work dir");
345 std::fs::create_dir_all(&bare).expect("create bare dir");
346 run("git", &work, &["init", "-q", "-b", "main", "--template="]);
347 configure_identity(&work);
348 std::fs::write(work.join("seed.txt"), "seed\n").expect("write seed");
349 run("git", &work, &["add", "-A"]);
350 run("git", &work, &["commit", "-qm", "seed"]);
351 run(
352 "git",
353 &bare,
354 &["init", "-q", "--bare", "-b", "main", "--template="],
355 );
356 run(
357 "git",
358 &work,
359 &["push", "-q", bare.to_str().expect("utf8 path"), "main:main"],
360 );
361 BareRemote { dir, bare }
362 }
363
364 /// The bare repository's path (use as a local remote URL).
365 pub fn path(&self) -> &Path {
366 &self.bare
367 }
368
369 /// The path as a `String` — convenient for argv slices.
370 pub fn url(&self) -> String {
371 self.bare.to_str().expect("utf8 path").to_string()
372 }
373
374 /// The owning temp dir (kept alive as long as the remote is used).
375 pub fn temp_dir(&self) -> &Path {
376 self.dir.path()
377 }
378}
379
380/// A throwaway **jj** repository (git-backed) with a repo-scoped identity.
381pub struct JjSandbox {
382 dir: TempDir,
383}
384
385impl JjSandbox {
386 /// Create and initialise the repository (`jj git init` + repo-scoped
387 /// `user.name`/`user.email`).
388 ///
389 /// The identity is supplied to *every* jj invocation as `JJ_USER` /
390 /// `JJ_EMAIL` env (see `command`), so the working-copy commit that
391 /// `jj git init` creates is authored deterministically — a later
392 /// `config set --repo user.*` only affects *future* commits and so cannot
393 /// fix the init commit on its own. The repo-scoped config is kept anyway
394 /// as belt-and-braces for any tool path that reads config over the env.
395 pub fn init(tag: &str) -> Self {
396 let dir = TempDir::new(tag);
397 run("jj", dir.path(), &["git", "init"]);
398 run(
399 "jj",
400 dir.path(),
401 &["config", "set", "--repo", "user.name", "Test"],
402 );
403 run(
404 "jj",
405 dir.path(),
406 &["config", "set", "--repo", "user.email", "test@example.com"],
407 );
408 JjSandbox { dir }
409 }
410
411 /// The workspace root path.
412 pub fn path(&self) -> &Path {
413 self.dir.path()
414 }
415
416 /// Run `jj <args>` in the workspace, panicking on failure.
417 pub fn jj(&self, args: &[&str]) {
418 run("jj", self.path(), args);
419 }
420
421 /// Write `content` to the workspace-relative `path` (creating parents).
422 pub fn write(&self, path: &str, content: &str) {
423 let full = self.path().join(path);
424 if let Some(parent) = full.parent() {
425 std::fs::create_dir_all(parent).expect("create parent dirs");
426 }
427 std::fs::write(full, content).expect("write file");
428 }
429
430 /// Describe the working-copy change (`jj describe -m <message>`).
431 pub fn describe(&self, message: &str) {
432 self.jj(&["describe", "-m", message]);
433 }
434
435 /// Start a new change on top (`jj new -m <message>`).
436 pub fn new_change(&self, message: &str) {
437 self.jj(&["new", "-m", message]);
438 }
439
440 /// Create a bookmark at `@` (`jj bookmark create <name> -r @`).
441 pub fn bookmark(&self, name: &str) {
442 self.jj(&["bookmark", "create", name, "-r", "@"]);
443 }
444}
445
446#[cfg(test)]
447mod tests {
448 use super::*;
449
450 // Hermetic: uniqueness and cleanup need no binaries.
451 #[test]
452 fn temp_dirs_are_unique_and_removed_on_drop() {
453 let a = TempDir::new("unique");
454 let b = TempDir::new("unique");
455 assert_ne!(a.path(), b.path());
456 assert!(a.path().exists() && b.path().exists());
457 let kept = a.path().to_path_buf();
458 drop(a);
459 assert!(!kept.exists(), "removed on drop");
460 }
461
462 // Real-binary round-trips; ignored so hermetic CI stays green.
463 #[test]
464 #[ignore = "requires the git binary"]
465 fn git_sandbox_builds_scenarios() {
466 let repo = GitSandbox::init("sandbox");
467 repo.commit_file("a.txt", "one\n", "first");
468 repo.branch("feature");
469 repo.checkout("feature");
470 repo.commit_file("sub/b.txt", "two\n", "second");
471 let head = repo.rev_parse("HEAD");
472 assert_eq!(head.len(), 40);
473 assert_ne!(head, repo.rev_parse("main"));
474
475 let remote = BareRemote::seeded("remote");
476 repo.git(&["remote", "add", "origin", remote.url().as_str()]);
477 repo.git(&["fetch", "-q", "origin"]);
478 assert_eq!(
479 run_capture("git", repo.path(), &["show", "origin/main:seed.txt"]),
480 "seed"
481 );
482 }
483
484 // Isolation: `--template=` plus the config env keep a host-global
485 // `init.templateDir` from seeding hooks, so the sandbox's `.git/hooks`
486 // holds no live hook. (A real host hook firing is what the reviewer hit;
487 // here we assert the precondition — no enabled hook files — which holds
488 // regardless of the host's config.)
489 #[test]
490 #[ignore = "requires the git binary"]
491 fn git_sandbox_has_no_leaked_hooks() {
492 let repo = GitSandbox::init("hooks");
493 repo.commit_file("a.txt", "one\n", "first");
494 let hooks = repo.path().join(".git").join("hooks");
495 let enabled: Vec<_> = std::fs::read_dir(&hooks)
496 .into_iter()
497 .flatten()
498 .flatten()
499 .map(|e| e.file_name().to_string_lossy().into_owned())
500 // git ships `*.sample` hooks (inert); only non-sample files run.
501 .filter(|name| !name.ends_with(".sample"))
502 .collect();
503 assert!(
504 enabled.is_empty(),
505 "sandbox should have no live hooks, found {enabled:?}"
506 );
507 // Note `core.hooksPath` is deliberately NOT pinned in the local config —
508 // a test may install its own hook on purpose (see `configure_identity`);
509 // the isolation lives in `command`'s env + `--template=` instead.
510 }
511
512 #[test]
513 #[ignore = "requires the jj binary"]
514 fn jj_sandbox_builds_scenarios() {
515 let repo = JjSandbox::init("sandbox");
516 repo.write("a.txt", "one\n");
517 repo.describe("base");
518 repo.bookmark("mark");
519 repo.new_change("next");
520 // The described change and the bookmark are visible to jj.
521 let out = run_capture(
522 "jj",
523 repo.path(),
524 &[
525 "log",
526 "-r",
527 "::@",
528 "--no-graph",
529 "-T",
530 "description.first_line() ++ \"\\n\"",
531 "--color",
532 "never",
533 ],
534 );
535 assert!(out.contains("base"), "got {out:?}");
536 }
537
538 // Isolation: the working-copy commit `jj git init` creates is authored
539 // deterministically from the `JJ_USER`/`JJ_EMAIL` env, *not* from the
540 // host's jj config (which `config set --repo` could not retroactively
541 // re-author). `root()+` is the first non-root commit — the init commit.
542 #[test]
543 #[ignore = "requires the jj binary"]
544 fn jj_sandbox_init_commit_has_deterministic_author() {
545 let repo = JjSandbox::init("identity");
546 let email = run_capture(
547 "jj",
548 repo.path(),
549 &[
550 "log",
551 "-r",
552 "root()+",
553 "--no-graph",
554 "-T",
555 "author.email()",
556 "--color",
557 "never",
558 ],
559 );
560 assert_eq!(email, "test@example.com", "init commit author.email");
561 }
562}
563
564// Long-form how-to guides, rendered from this crate's docs/*.md on docs.rs.
565#[doc = include_str!("../docs/testkit.md")]
566#[allow(rustdoc::broken_intra_doc_links)]
567pub mod guide {
568 #[doc = include_str!("../docs/testing.md")]
569 #[allow(rustdoc::broken_intra_doc_links)]
570 pub mod testing {}
571}