//! Helpers shared by both packages' integration tests: isolated git, scratch folders, and a //! stand-in for ssh that runs `gitv serve`. Each package's `tests/common/mod.rs` includes this //! file; the web tests add their own server and HTTP client. #![allow(dead_code)] use std::fs; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; use std::sync::LazyLock; /// The `gitv` binary. A test can only be told where its own package's binaries are, so the /// web tests find it beside their own executable, where a workspace build puts it. pub static GITV: LazyLock = LazyLock::new(|| binary(option_env!("CARGO_BIN_EXE_gitv"), "gitv")); /// The `gitv-web` binary, found the same way. pub static GITV_WEB: LazyLock = LazyLock::new(|| binary(option_env!("CARGO_BIN_EXE_gitv-web"), "gitv-web")); fn binary(own: Option<&str>, name: &str) -> String { if let Some(path) = own { return path.to_string(); } // The binaries sit in target/. Test executables are somewhere below it: in // target//deps, or deeper in newer Cargo's build directory layout. So look in each // folder above this test for a file with the binary's name. let exe = std::env::current_exe().unwrap(); exe.ancestors() .skip(1) .map(|dir| dir.join(name)) .find(|path| path.is_file()) .map(|path| path.to_string_lossy().into_owned()) .unwrap_or_else(|| { panic!("{name} isn't built near {}; run the tests from the workspace root with `cargo test`", exe.display()) }) } /// The hostname every test installation is configured with. pub const TEST_HOST: &str = "git.example.test"; /// A configuration file that doesn't exist, so the machine's own /etc/gitv/gitv.conf never applies. pub const NO_CONFIG: &str = "/nonexistent/gitv.conf"; /// A fresh, empty directory under Cargo's per-target temporary directory. pub fn scratch(name: &str) -> PathBuf { let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(name); let _ = fs::remove_dir_all(&dir); fs::create_dir_all(&dir).unwrap(); dir } /// Git with no user or system config and a fixed identity and clock, so runs are repeatable. pub fn git_cmd(dir: &Path) -> Command { let mut command = Command::new("git"); command .current_dir(dir) .env("HOME", dir) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") .env("GIT_AUTHOR_NAME", "Ada Lovelace") .env("GIT_AUTHOR_EMAIL", "ada@example.com") .env("GIT_COMMITTER_NAME", "Ada Lovelace") .env("GIT_COMMITTER_EMAIL", "ada@example.com") .env("GIT_AUTHOR_DATE", "2026-01-02T03:04:05Z") .env("GIT_COMMITTER_DATE", "2026-01-02T03:04:05Z") .env("GIT_SSH_VARIANT", "simple") .stdin(Stdio::null()); command } /// Run git in `dir`, panic with its output on failure, and return trimmed stdout. pub fn git(dir: &Path, args: &[&str]) -> String { let output = git_cmd(dir).args(args).output().unwrap(); assert!(output.status.success(), "git {args:?} failed: {}", String::from_utf8_lossy(&output.stderr)); String::from_utf8_lossy(&output.stdout).trim().to_string() } /// A GITv installation in a scratch directory: repositories, a keys file, and a fake `ssh` /// that hands git's command to `gitv serve` as an owner's key would. pub struct Host { pub dir: PathBuf, pub repos: PathBuf, pub keys: PathBuf, } impl Host { pub fn new(name: &str) -> Host { let dir = scratch(name); let repos = dir.join("repos"); fs::create_dir_all(&repos).unwrap(); let keys = dir.join("authorized_keys"); let host = Host { dir, repos, keys }; // With GIT_SSH_VARIANT=simple git runs: @host . let script = format!( "#!/bin/sh\nowner=${{1%%@*}}\nSSH_ORIGINAL_COMMAND=\"$2\" GITV_REPOS='{}' GITV_LOG='{}' GITV_HOST={TEST_HOST} GITV_CONFIG={NO_CONFIG} exec '{}' serve \"$owner\"\n", host.repos.display(), host.log().display(), *GITV ); let ssh = host.ssh_script(); fs::write(&ssh, script).unwrap(); fs::set_permissions(&ssh, fs::Permissions::from_mode(0o755)).unwrap(); host } /// Where `gitv` writes its operator log in tests. pub fn log(&self) -> PathBuf { self.dir.join("operator.log") } /// The operator log so far, empty if nothing was logged. pub fn logged(&self) -> String { fs::read_to_string(self.log()).unwrap_or_default() } pub fn ssh_script(&self) -> PathBuf { self.dir.join("fake-ssh") } /// `ssh git@host ` as `owner`'s key. pub fn ssh(&self, owner: &str, command: &str) -> Output { Command::new(&*GITV) .args(["serve", owner]) .env("SSH_ORIGINAL_COMMAND", command) .env("GITV_HOST", TEST_HOST) .env("GITV_CONFIG", NO_CONFIG) .env("GITV_REPOS", &self.repos) .env("GITV_LOG", self.log()) .stdin(Stdio::null()) .output() .unwrap() } /// `gitv ` as the administrator, pointed at this host. pub fn admin(&self, args: &[&str]) -> Output { Command::new(&*GITV) .args(args) .env("GITV_HOST", TEST_HOST) .env("GITV_CONFIG", NO_CONFIG) .env("GITV_REPOS", &self.repos) .env("GITV_KEYS", &self.keys) .env("GITV_LOG", self.log()) .stdin(Stdio::null()) .output() .unwrap() } /// Git in `dir`, with remotes reached through this host's fake ssh. pub fn git(&self, dir: &Path, args: &[&str]) -> Output { git_cmd(dir).env("GIT_SSH_COMMAND", self.ssh_script()).args(args).output().unwrap() } pub fn repo(&self, owner: &str, name: &str) -> PathBuf { self.repos.join(owner).join(format!("{name}.git")) } } pub fn stdout(output: &Output) -> String { String::from_utf8_lossy(&output.stdout).into_owned() } pub fn stderr(output: &Output) -> String { String::from_utf8_lossy(&output.stderr).into_owned() } /// A syntactically valid OpenSSH Ed25519 public key built from `seed`. pub fn ed25519_key(seed: u8) -> String { use base64::Engine; let mut blob = 11u32.to_be_bytes().to_vec(); blob.extend_from_slice(b"ssh-ed25519"); blob.extend_from_slice(&32u32.to_be_bytes()); blob.extend_from_slice(&[seed; 32]); format!("ssh-ed25519 {} test@example\n", base64::engine::general_purpose::STANDARD.encode(blob)) }