diff --git a/.gitignore b/.gitignore index 185ca35..aa7171b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target .idea + +test_runs/ diff --git a/tests/cli.rs b/tests/cli.rs index 30c6348..ef7244d 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -3,9 +3,10 @@ use dcp::VERSION; use predicates::prelude::*; use rand::{thread_rng, Rng}; use std::error::Error; +use std::fs::remove_dir_all; const PRG: &str = "dcp"; -const TEST_CONTENT_DIR: &str = "/tmp/dcp_test_run"; +const TEST_CONTENT_DIR: &str = "./test_runs"; const DEFAULT_IMAGE: &str = "quay.io/tyslaton/sample-catalog:v0.0.4"; const IMAGE_NO_TAG: &str = "quay.io/tyslaton/sample-catalog"; const SCRATCH_BASE_IMAGE: &str = "quay.io/tflannag/bundles:resolveset-v0.0.2"; @@ -14,7 +15,22 @@ const SCRATCH_BASE_IMAGE: &str = "quay.io/tflannag/bundles:resolveset-v0.0.2"; // returns a new string with an appended 5 digit string fn generate_temp_path() -> String { let random_string = thread_rng().gen_range(10000..99999); - return format!("{}_{}", TEST_CONTENT_DIR, random_string); + format!("{}/{}", TEST_CONTENT_DIR, random_string) +} + +// clean_up_test_dir removes the testing directory specified completely. +// +// WARNING: This function is deleting directories recursively, if you are +// using it, be absolutely sure you know what you're doing. +fn clean_up_test_dir(path: &str) { + if path.starts_with(TEST_CONTENT_DIR) { + match remove_dir_all(path) { + Ok(_) => {} + Err(e) => { + eprintln!("failed to delete testing dir {}:{}", path, e); + } + } + } } type TestResult = Result<(), Box>; @@ -49,6 +65,8 @@ fn accepts_download_path() -> TestResult { // verify that content was written to the desired download_path assert_eq!(std::path::Path::new(path).exists(), true); + clean_up_test_dir(path); + Ok(()) } @@ -69,6 +87,8 @@ fn accepts_content_path() -> TestResult { let specific_content = &format!("{}/{}", path, content_path); assert_eq!(std::path::Path::new(specific_content).exists(), true); + clean_up_test_dir(path); + Ok(()) } @@ -87,6 +107,8 @@ fn fails_invalid_content_path() -> TestResult { .assert() .failure(); + clean_up_test_dir(path); + Ok(()) } @@ -108,6 +130,8 @@ fn accepts_image() -> TestResult { .assert() .failure(); + clean_up_test_dir(path); + Ok(()) } @@ -123,6 +147,8 @@ fn defaults_tag_to_latest() -> TestResult { .assert() .success(); + clean_up_test_dir(path); + Ok(()) } @@ -138,6 +164,8 @@ fn fails_on_just_tag() -> TestResult { .assert() .failure(); + clean_up_test_dir(path); + Ok(()) } @@ -154,5 +182,7 @@ fn accepts_scratch_base_images() -> TestResult { .assert() .success(); + clean_up_test_dir(path); + Ok(()) }