From e118814684671f697a95c6d941c3c3d5fbf6e30a Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 3 Aug 2020 22:00:37 +0100 Subject: [PATCH] Tests: Remove any entries in existing tmp dir Delete the existing dir before copying the new dir over it incase its contents have changed. Delete & Copy directories before tests are run. On test runners the tests run in parallel so we can't write and clean up at the start and end of each test. Unless each test copys the data to a unique dir name. [This may be a better thing to do in the long run] --- tests/tests.rs | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index eef46c9..6270c4a 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,5 +1,8 @@ use assert_cmd::Command; use std::str; +use std::sync::Once; + +static INIT: Once = Once::new(); mod tests_symlinks; @@ -7,6 +10,17 @@ mod tests_symlinks; /// Copy to /tmp dir - we assume that the formatting of the /tmp partition /// is consistent. If the tests fail your /tmp filesystem probably differs fn copy_test_data(dir: &str) { + // First remove the existing directory - just incase it is there and has incorrect data + let last_slash = dir.rfind('/').unwrap(); + let last_part_of_dir = dir.chars().skip(last_slash).collect::(); + match Command::new("rm") + .arg("-rf") + .arg("/tmp/".to_owned() + &*last_part_of_dir) + .ok() + { + Ok(_) => {} + Err(_) => {} + }; match Command::new("cp").arg("-r").arg(dir).arg("/tmp/").ok() { Ok(_) => {} Err(err) => { @@ -15,6 +29,14 @@ fn copy_test_data(dir: &str) { }; } +pub fn initialize() { + INIT.call_once(|| { + copy_test_data("src/test_dir"); + copy_test_data("src/test_dir2"); + copy_test_data("src/test_dir3"); + }); +} + // We can at least test the file names are there #[test] pub fn test_basic_output() { @@ -46,8 +68,8 @@ pub fn test_output_no_bars_means_no_excess_spaces() { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_basic() { - copy_test_data("src/test_dir"); // -c is no color mode - This makes testing much simpler + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let assert = cmd.arg("-c").arg("/tmp/test_dir/").unwrap().stdout; let output = str::from_utf8(&assert).unwrap(); @@ -57,8 +79,7 @@ pub fn test_main_basic() { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_multi_arg() { - copy_test_data("src/test_dir"); - + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let assert = cmd .arg("-c") @@ -103,8 +124,7 @@ fn main_output() -> String { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_long_paths() { - copy_test_data("src/test_dir"); - + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let assert = cmd .arg("-c") @@ -148,8 +168,7 @@ fn main_output_long_paths() -> String { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_apparent_size() { - copy_test_data("src/test_dir"); - + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let assert = cmd.arg("-c").arg("-s").arg("src/test_dir").unwrap().stdout; let output = str::from_utf8(&assert).unwrap(); @@ -187,6 +206,7 @@ fn output_apparent_size() -> String { #[test] pub fn test_reverse_flag() { + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd.arg("-c").arg("-r").arg("src/test_dir/").unwrap().stdout; let output = str::from_utf8(&output).unwrap(); @@ -199,6 +219,7 @@ pub fn test_reverse_flag() { #[test] pub fn test_d_flag_works() { + initialize(); // We should see the top level directory but not the sub dirs / files: let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd @@ -216,8 +237,7 @@ pub fn test_d_flag_works() { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_substring_of_names() { - copy_test_data("src/test_dir2"); - + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd.arg("-c").arg("/tmp/test_dir2").unwrap().stdout; let output = str::from_utf8(&output).unwrap(); @@ -260,8 +280,7 @@ fn no_substring_of_names_output() -> String { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_unicode_directories() { - copy_test_data("src/test_dir3"); - + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd.arg("-c").arg("/tmp/test_dir3").unwrap().stdout; let output = str::from_utf8(&output).unwrap(); @@ -299,6 +318,7 @@ fn unicode_dir() -> String { // Check against directories and files whos names are substrings of each other #[test] pub fn test_ignore_dir() { + initialize(); let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd .arg("-c")