From 62db5899dc60833b013331bfed2cc874628a3297 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 7 Aug 2024 15:39:28 +0100 Subject: test(git): make drop GitTestRepo optional so that we can inspect the state of a test repo --- test_utils/src/git.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'test_utils/src') diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs index 058eb31..f299b40 100644 --- a/test_utils/src/git.rs +++ b/test_utils/src/git.rs @@ -17,6 +17,7 @@ use crate::generate_repo_ref_event; pub struct GitTestRepo { pub dir: PathBuf, pub git_repo: git2::Repository, + pub delete_dir_on_drop: bool, } impl Default for GitTestRepo { @@ -56,12 +57,22 @@ impl GitTestRepo { Ok(Self { dir: path, git_repo, + delete_dir_on_drop: true, }) } pub fn without_repo_in_git_config() -> Self { Self::new("main").unwrap() } + pub fn open(path: &PathBuf) -> Result { + let git_repo = git2::Repository::open(path)?; + Ok(Self { + dir: path.clone(), + git_repo, + delete_dir_on_drop: true, + }) + } + pub fn duplicate(existing_repo: &GitTestRepo) -> Result { let path = current_dir()?.join(format!("tmpgit-{}", rand::random::())); // function source: https://stackoverflow.com/a/65192210 @@ -86,6 +97,7 @@ impl GitTestRepo { Ok(Self { dir: path, git_repo, + delete_dir_on_drop: true, }) } @@ -120,6 +132,7 @@ impl GitTestRepo { Ok(Self { dir: path, git_repo, + delete_dir_on_drop: true, }) } @@ -129,6 +142,7 @@ impl GitTestRepo { Ok(Self { dir: path, git_repo, + delete_dir_on_drop: true, }) } @@ -263,7 +277,9 @@ impl GitTestRepo { impl Drop for GitTestRepo { fn drop(&mut self) { - let _ = fs::remove_dir_all(&self.dir); + if self.delete_dir_on_drop { + let _ = fs::remove_dir_all(&self.dir); + } } } pub fn joe_signature() -> Signature<'static> { -- cgit v1.2.3