better git testing
This commit is contained in:
parent
b6787224c3
commit
ba47a4bcd6
2 changed files with 25 additions and 6 deletions
|
|
@ -7,6 +7,7 @@ edition = "2024"
|
||||||
anyhow = "1.0.95"
|
anyhow = "1.0.95"
|
||||||
clap = { version = "4.5.26", features = ["derive"] }
|
clap = { version = "4.5.26", features = ["derive"] }
|
||||||
mktemp = "0.5.1"
|
mktemp = "0.5.1"
|
||||||
|
rand = "0.9.1"
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
thiserror = "2.0.11"
|
thiserror = "2.0.11"
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
|
|
|
||||||
30
src/git.rs
30
src/git.rs
|
|
@ -23,8 +23,7 @@ pub fn checkout_branch_in_worktree(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_worktree(worktree_dir: &std::path::Path) -> Result<(), GitError> {
|
pub fn git_worktree_prune() -> Result<(), GitError> {
|
||||||
std::fs::remove_dir_all(worktree_dir)?;
|
|
||||||
let result = std::process::Command::new("git")
|
let result = std::process::Command::new("git")
|
||||||
.args(["worktree", "prune"])
|
.args(["worktree", "prune"])
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
@ -36,6 +35,18 @@ pub fn remove_worktree(worktree_dir: &std::path::Path) -> Result<(), GitError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn git_remove_branch(branch: &str) -> Result<(), GitError> {
|
||||||
|
let result = std::process::Command::new("git")
|
||||||
|
.args(["branch", "-D", branch])
|
||||||
|
.output()?;
|
||||||
|
if !result.status.success() {
|
||||||
|
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
||||||
|
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
|
||||||
|
return Err(GitError::Oops);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_orphan_branch(branch: &str) -> Result<(), GitError> {
|
pub fn create_orphan_branch(branch: &str) -> Result<(), GitError> {
|
||||||
{
|
{
|
||||||
let tmp_worktree = mktemp::Temp::new_path();
|
let tmp_worktree = mktemp::Temp::new_path();
|
||||||
|
|
@ -108,13 +119,20 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_worktree() {
|
fn test_worktree() {
|
||||||
let worktree_dir = std::path::Path::new("/tmp/boo");
|
{
|
||||||
checkout_branch_in_worktree("origin/main", worktree_dir).unwrap();
|
let temp_worktree = mktemp::Temp::new_path();
|
||||||
remove_worktree(worktree_dir).unwrap();
|
checkout_branch_in_worktree("origin/main", temp_worktree.as_path()).unwrap();
|
||||||
|
// The temporary worktree directory is removed when the Temp variable is dropped.
|
||||||
|
}
|
||||||
|
git_worktree_prune().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_orphan_branch() {
|
fn test_create_orphan_branch() {
|
||||||
create_orphan_branch("bloppers").unwrap();
|
let rnd: u128 = rand::random();
|
||||||
|
let mut branch = std::string::String::from("entomologist-test-branch-");
|
||||||
|
branch.push_str(&format!("{:0x}", rnd));
|
||||||
|
create_orphan_branch(&branch).unwrap();
|
||||||
|
git_remove_branch(&branch).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue