crates/kernel/ents-anchor/src/fixture.rs
fixture.rshistorycomment on this file
| 1 | //! Test-only git fixtures: a throwaway repository and the plumbing helpers |
| 2 | //! this crate's test suites drive it with (ported from `pre-redo`'s |
| 3 | //! `git-store::test_support`). |
| 4 | //! |
| 5 | //! Compiled only under `cfg(test)`. When a second crate needs these |
| 6 | //! helpers, they move to the shared `ents-testutil` dev-dependency crate |
| 7 | //! the workspace test strategy calls for; extracting them now would create |
| 8 | //! a crate no second consumer exists for yet. |
| 9 | |
| 10 | #![allow(clippy::unwrap_used, reason = "test fixture")] |
| 11 | |
| 12 | use std::path::Path; |
| 13 | use std::process::Command; |
| 14 | |
| 15 | /// A fresh temporary directory holding an initialized git repository. |
| 16 | #[must_use] |
| 17 | pub(crate) fn repo() -> tempfile::TempDir { |
| 18 | let dir = tempfile::tempdir().unwrap(); |
| 19 | let status = Command::new("git") |
| 20 | .arg("-C") |
| 21 | .arg(dir.path()) |
| 22 | .args(["init", "-q"]) |
| 23 | .status() |
| 24 | .unwrap(); |
| 25 | assert!(status.success()); |
| 26 | for (key, value) in [("user.email", "test@example.com"), ("user.name", "test")] { |
| 27 | let status = Command::new("git") |
| 28 | .arg("-C") |
| 29 | .arg(dir.path()) |
| 30 | .args(["config", key, value]) |
| 31 | .status() |
| 32 | .unwrap(); |
| 33 | assert!(status.success()); |
| 34 | } |
| 35 | dir |
| 36 | } |
| 37 | |
| 38 | /// Stage everything in `dir` and commit it as `message` under the fixed |
| 39 | /// test identity. |
| 40 | pub(crate) fn commit_all(dir: &Path, message: &str) { |
| 41 | let status = Command::new("git") |
| 42 | .arg("-C") |
| 43 | .arg(dir) |
| 44 | .args(["add", "-A"]) |
| 45 | .status() |
| 46 | .unwrap(); |
| 47 | assert!(status.success()); |
| 48 | let status = Command::new("git") |
| 49 | .arg("-C") |
| 50 | .arg(dir) |
| 51 | .args([ |
| 52 | "-c", |
| 53 | "user.name=test", |
| 54 | "-c", |
| 55 | "user.email=test@example.com", |
| 56 | "commit", |
| 57 | "-q", |
| 58 | "-m", |
| 59 | message, |
| 60 | ]) |
| 61 | .status() |
| 62 | .unwrap(); |
| 63 | assert!(status.success()); |
| 64 | } |
| 65 | |
| 66 | /// The full hex id of `dir`'s `HEAD` commit. |
| 67 | #[must_use] |
| 68 | pub(crate) fn head(dir: &Path) -> String { |
| 69 | let output = Command::new("git") |
| 70 | .arg("-C") |
| 71 | .arg(dir) |
| 72 | .args(["rev-parse", "HEAD"]) |
| 73 | .output() |
| 74 | .unwrap(); |
| 75 | assert!(output.status.success()); |
| 76 | String::from_utf8(output.stdout).unwrap().trim().to_owned() |
| 77 | } |
| 78 | |
| 79 | /// `range.map(|n| "line {n}\n")` concatenated — the numbered fixture file |
| 80 | /// every projection test edits. |
| 81 | #[must_use] |
| 82 | pub(crate) fn numbered(range: std::ops::RangeInclusive<u32>) -> String { |
| 83 | range.map(|n| format!("line {n}\n")).collect() |
| 84 | } |