Share test helpers within each crate, and say where unit tests write
Browse filesThe repository tests' two fixtures each wrote out the same fast-import setup, and gitv's unit tests built their scratch folders by hand four times; each crate now has one helper. The README said every test writes only to target/tmp, but Cargo names that folder only to integration tests, so unit tests use the system's temporary folder; it now says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 22:17
- Commit
- d087586f23e9
- Parent
- 0fb1e46
4 files changed +33−37
+1−1
| @@ -91,7 +91,7 @@ git clone um9i@local:um9i/myrepo.git | ||
| cargo test | ||
| ``` | ||
| Run them from the workspace root, so both binaries are built: the web tests use `gitv` to set up their repositories. `crates/gitv/tests/ssh.rs` runs `gitv serve` the way a key does over SSH, pushing and cloning real repositories through a stand-in for ssh, and `crates/gitv/tests/backup.rs` runs the backup and restore scripts. `crates/gitv-web/tests/web.rs` builds a fixture repository and checks every page of a running `gitv-web` over HTTP, signed in, including escaping and the requests it must refuse. `crates/gitv-web/tests/auth.rs` covers signing in, two-factor codes, lockout, sessions and changing the password. None needs an SSH server or network access; all write only to `target/tmp`. Helpers both packages use are in `testing/host.rs`. | ||
| Run them from the workspace root, so both binaries are built: the web tests use `gitv` to set up their repositories. `crates/gitv/tests/ssh.rs` runs `gitv serve` the way a key does over SSH, pushing and cloning real repositories through a stand-in for ssh, and `crates/gitv/tests/backup.rs` runs the backup and restore scripts. `crates/gitv-web/tests/web.rs` builds a fixture repository and checks every page of a running `gitv-web` over HTTP, signed in, including escaping and the requests it must refuse. `crates/gitv-web/tests/auth.rs` covers signing in, two-factor codes, lockout, sessions and changing the password. None needs an SSH server or network access. The integration tests write only to `target/tmp`; the unit tests use the system's temporary folder, since Cargo names `target/tmp` only to integration tests. Helpers both packages use are in `testing/host.rs`. | ||
| `.github/workflows/ci.yml` runs the same checks on every push and pull request (`cargo fmt --check`, clippy with warnings as errors, `cargo test`), plus `cargo audit` against the RustSec advisory database, also weekly so new advisories surface without a push. | ||
| @@ -548,8 +548,7 @@ mod tests { | ||
| /// A bare repository holding one blob of `size` bytes, and the blob's id. | ||
| fn repo_with_blob(name: &str, size: usize) -> (RepoDir, String) { | ||
| let dir = std::env::temp_dir().join(format!("gitv-lib-{name}-{}", std::process::id())); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| let dir = scratch(name); | ||
| assert!(git_base().args(["init", "-q", "--bare"]).arg(&dir).status().unwrap().success()); | ||
| let dir = RepoDir::at(&dir).unwrap(); | ||
| let mut child = git_command(&dir) | ||
| @@ -399,13 +399,27 @@ mod tests { | ||
| } | ||
| } | ||
| /// A bare repository with 1000 commits on main, 3000 blobs and an annotated tag on the tip, | ||
| /// enough that 4-digit prefixes collide: commit with blob, and commit with commit. | ||
| fn crowded_repository() -> (Reader, std::path::PathBuf) { | ||
| /// A bare repository built from a `git fast-import` stream, and its folder. | ||
| fn imported(name: &str, stream: &str) -> (Reader, std::path::PathBuf) { | ||
| use std::io::Write as _; | ||
| let dir = std::env::temp_dir().join(format!("gitv-web-crowded-{}", std::process::id())); | ||
| let dir = std::env::temp_dir().join(format!("gitv-web-{name}-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| assert!(gitv_core::git_base().args(["init", "-q", "--bare"]).arg(&dir).status().unwrap().success()); | ||
| let mut import = gitv_core::git_base() | ||
| .arg("--git-dir") | ||
| .arg(&dir) | ||
| .args(["fast-import", "--quiet"]) | ||
| .stdin(std::process::Stdio::piped()) | ||
| .spawn() | ||
| .unwrap(); | ||
| import.stdin.take().unwrap().write_all(stream.as_bytes()).unwrap(); | ||
| assert!(import.wait().unwrap().success()); | ||
| (Reader::new(RepoDir::at(&dir).unwrap()), dir) | ||
| } | ||
| /// A bare repository with 1000 commits on main, 3000 blobs and an annotated tag on the tip, | ||
| /// enough that 4-digit prefixes collide: commit with blob, and commit with commit. | ||
| fn crowded_repository() -> (Reader, std::path::PathBuf) { | ||
| let mut stream = String::new(); | ||
| for i in 0..3000 { | ||
| let data = format!("blob {i}\n"); | ||
| @@ -420,16 +434,7 @@ mod tests { | ||
| ); | ||
| } | ||
| stream += "tag v1\nfrom refs/heads/main\ntagger A <a@example.com> 1800000000 +0000\ndata 3\nv1\n"; | ||
| let mut import = gitv_core::git_base() | ||
| .arg("--git-dir") | ||
| .arg(&dir) | ||
| .args(["fast-import", "--quiet"]) | ||
| .stdin(std::process::Stdio::piped()) | ||
| .spawn() | ||
| .unwrap(); | ||
| import.stdin.take().unwrap().write_all(stream.as_bytes()).unwrap(); | ||
| assert!(import.wait().unwrap().success()); | ||
| (Reader::new(RepoDir::at(&dir).unwrap()), dir) | ||
| imported("crowded", &stream) | ||
| } | ||
| #[test] | ||
| @@ -473,10 +478,6 @@ mod tests { | ||
| #[test] | ||
| fn one_walk_finds_entries_changed_long_ago() { | ||
| use std::io::Write as _; | ||
| let dir = std::env::temp_dir().join(format!("gitv-web-walk-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| assert!(gitv_core::git_base().args(["init", "-q", "--bare"]).arg(&dir).status().unwrap().success()); | ||
| // old.txt is written once, then busy.txt changes in each of 30 later commits. | ||
| let mut stream = String::from( | ||
| "commit refs/heads/main\ncommitter A <a@example.com> 1700000000 +0000\ndata 6\nfirst\nM 644 inline old.txt\ndata 4\nold\n\n", | ||
| @@ -491,16 +492,7 @@ mod tests { | ||
| content.len() | ||
| ); | ||
| } | ||
| let mut import = gitv_core::git_base() | ||
| .arg("--git-dir") | ||
| .arg(&dir) | ||
| .args(["fast-import", "--quiet"]) | ||
| .stdin(std::process::Stdio::piped()) | ||
| .spawn() | ||
| .unwrap(); | ||
| import.stdin.take().unwrap().write_all(stream.as_bytes()).unwrap(); | ||
| assert!(import.wait().unwrap().success()); | ||
| let reader = Reader::new(RepoDir::at(&dir).unwrap()); | ||
| let (reader, dir) = imported("walk", &stream); | ||
| let tip = reader.git(&["rev-parse", "main"]).unwrap().trim().to_string(); | ||
| let first = reader.git(&["rev-list", "--max-parents=0", "main"]).unwrap().trim().to_string(); | ||
| let names = [b"old.txt".as_slice(), b"busy.txt"]; | ||
| @@ -822,14 +822,21 @@ mod tests { | ||
| use nix::fcntl::{OFlag, open}; | ||
| use std::os::unix::fs::PermissionsExt; | ||
| /// A fresh, empty directory for one test. | ||
| fn scratch(name: &str) -> PathBuf { | ||
| let dir = std::env::temp_dir().join(format!("gitv-{name}-{}", std::process::id())); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| fs::create_dir_all(&dir).unwrap(); | ||
| dir | ||
| } | ||
| fn open_path(path: &Path) -> OwnedFd { | ||
| open(path, OFlag::O_DIRECTORY | OFlag::O_CLOEXEC, Mode::empty()).unwrap() | ||
| } | ||
| #[test] | ||
| fn published_files_are_relabelled_only_where_selinux_is_on() { | ||
| let dir = std::env::temp_dir().join(format!("gitv-relabel-{}", std::process::id())); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| let dir = scratch("relabel"); | ||
| fs::create_dir_all(dir.join("selinuxfs")).unwrap(); | ||
| let keys = dir.join("authorized_keys"); | ||
| fs::write(&keys, "").unwrap(); | ||
| @@ -892,8 +899,7 @@ mod tests { | ||
| #[test] | ||
| fn a_failed_build_never_removes_what_it_did_not_make() { | ||
| let dir = std::env::temp_dir().join(format!("gitv-build-{}", std::process::id())); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| let dir = scratch("build"); | ||
| // A staging directory that already holds someone's `repo`, as it would if the staging | ||
| // name had been swapped for another private directory before it was opened. | ||
| fs::create_dir_all(dir.join("owner/.staging/repo")).unwrap(); | ||
| @@ -909,8 +915,7 @@ mod tests { | ||
| #[test] | ||
| fn a_failed_build_cleans_up_what_it_made() { | ||
| let dir = std::env::temp_dir().join(format!("gitv-build-clean-{}", std::process::id())); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| let dir = scratch("build-clean"); | ||
| fs::create_dir_all(dir.join("owner/.staging")).unwrap(); | ||
| // The target is taken, so publication fails after the repository was built. | ||
| fs::create_dir_all(dir.join("owner/new.git")).unwrap(); | ||