use crate::context::TestContext; use anyhow::Result; use assert_cmd::Command; use std::{ fs, path::{Path, PathBuf}, }; pub fn create_clone(c: &TestContext, repo_dir: &Path, relative_name: &str) -> PathBuf { Command::new("git") .arg("clone") .arg(repo_dir) .arg(relative_name) .current_dir(&c.workdir) .timeout(std::time::Duration::from_secs(3)) .assert() .success(); c.workdir.as_ref().join(relative_name) } pub fn create_commit(repo_dir: &Path) -> Result { let test_file_name = repo_dir.join("some_file"); fs::write(test_file_name, "Some content or something")?; Command::new("git") .args(["add", "--all"]) .current_dir(&repo_dir) .timeout(std::time::Duration::from_secs(3)) .assert() .success(); Command::new("git") .args(["commit", "-m", "A commit message"]) .current_dir(&repo_dir) .timeout(std::time::Duration::from_secs(3)) .assert() .success(); let commit_hash = String::from_utf8( Command::new("git") .args(["rev-parse", "HEAD"]) .current_dir(&repo_dir) .timeout(std::time::Duration::from_secs(3)) .output()? .stdout, )?; Ok(commit_hash) } pub fn verify_repo_exists(repo_dir: &Path) { Command::new("git") .arg("rev-list") .arg("--all") .current_dir(repo_dir) .assert() .success() .stdout(""); } pub fn verify_repo_does_not_exist(repo_dir: &Path) { assert!(!repo_dir.exists()); } pub fn verify_current_branch(repo_dir: &Path, expected_ref: &str) { Command::new("git") .arg("symbolic-ref") .arg("HEAD") .current_dir(repo_dir) .assert() .success() .stdout(format!("{expected_ref}\n")); } pub fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Option<&str>) { let assert = Command::new("git") .args(["config", "--local", config_key]) .current_dir(repo_dir) .assert(); match config_value { Some(value) => { assert.success().stdout(format!("{value}\n")); } None => { assert.failure().code(1); } } }