BUGFIX: CreateDirectory now properly prunes when it should (#181)

* BUGFIX: CreateDirectory now properly prunes when it should

* in a module
This commit is contained in:
Ana Hobden 2023-01-11 13:56:29 -08:00 committed by GitHub
parent 7ce0ae5391
commit 11e4d64c76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -173,7 +173,8 @@ impl Action for CreateDirectory {
.read_dir()
.map_err(|e| ActionError::Read(path.clone(), e))?
.next()
.is_some();
.is_none();
match (is_empty, force_prune_on_revert) {
(true, _) | (false, true) => remove_dir_all(path.clone())
.await
@ -186,3 +187,64 @@ impl Action for CreateDirectory {
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[tokio::test]
async fn creates_and_deletes_empty_directory() -> eyre::Result<()> {
let temp_dir = tempdir::TempDir::new("nix_installer_tests_create_file")?;
let test_dir = temp_dir.path().join("creates_and_deletes_empty_directory");
let mut action = CreateDirectory::plan(test_dir.clone(), None, None, None, false).await?;
action.try_execute().await?;
action.try_revert().await?;
assert!(!test_dir.exists(), "Folder should have been deleted");
Ok(())
}
#[tokio::test]
async fn creates_and_deletes_populated_directory_if_prune_true() -> eyre::Result<()> {
let temp_dir = tempdir::TempDir::new("nix_installer_tests_create_file")?;
let test_dir = temp_dir
.path()
.join("creates_and_deletes_populated_directory_if_prune_true");
let mut action = CreateDirectory::plan(test_dir.clone(), None, None, None, true).await?;
action.try_execute().await?;
let stub_file = test_dir.as_path().join("stub");
tokio::fs::write(stub_file, "More content").await?;
action.try_revert().await?;
assert!(!test_dir.exists(), "Folder should have been deleted");
Ok(())
}
#[tokio::test]
async fn creates_and_leaves_populated_directory_if_prune_false() -> eyre::Result<()> {
let temp_dir = tempdir::TempDir::new("nix_installer_tests_create_file")?;
let test_dir = temp_dir
.path()
.join("creates_and_leaves_populated_directory_if_prune_false");
let mut action = CreateDirectory::plan(test_dir.clone(), None, None, None, false).await?;
action.try_execute().await?;
let stub_file = test_dir.as_path().join("stub");
tokio::fs::write(&stub_file, "More content").await?;
action.try_revert().await?;
assert!(test_dir.exists(), "Folder should not have been deleted");
assert!(stub_file.exists(), "Folder should not have been deleted");
Ok(())
}
}