From a9bd7f2399b6e679e52b502332a9efe250dec9fd Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Mon, 13 Jan 2025 13:00:04 -0800 Subject: [PATCH] fix: remove repo path on failed install --- src/utils.rs | 9 ++++++++- tests/cli_install_subcommand_tests.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 79a9555..0edc941 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -62,7 +62,14 @@ pub fn git_clone(repo_url: &str, target_dir: &Path, revision: Option<&str>) -> R .with_context(|| format!("Failed to clone repository from {}", repo_url))?; let revision_str = revision.unwrap_or("main"); - return git_to_revision(target_dir, "origin", revision_str); + let result = git_to_revision(target_dir, "origin", revision_str); + if let Err(e) = result { + // Cleanup! If we cannot checkout the revision, remove the directory. + fs::remove_dir_all(target_dir) + .with_context(|| format!("Failed to remove directory {}", target_dir.display()))?; + return Err(e); + } + Ok(()) } pub fn git_update(repo_path: &Path, repo_url: &str, revision: Option<&str>) -> Result<()> { diff --git a/tests/cli_install_subcommand_tests.rs b/tests/cli_install_subcommand_tests.rs index a584ebf..a7b54d7 100644 --- a/tests/cli_install_subcommand_tests.rs +++ b/tests/cli_install_subcommand_tests.rs @@ -331,6 +331,10 @@ revision = "invalid-revision" "##; write_to_file(&config_path, config_content)?; + let mut repo_path = repo_path.clone(); + repo_path.push("repos"); + repo_path.push("tinted-jqp"); + // --- // Act // --- @@ -339,11 +343,20 @@ revision = "invalid-revision" // ------ // Assert // ------ + let path_exists = repo_path.exists(); cleanup()?; assert!( stderr.contains("cannot resolve invalid-revision"), "Expected revision not found", ); + assert!( + !path_exists, + "Expected repo path {} to not exist", + repo_path.display(), + ); + + + Ok(()) }