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]
This commit is contained in:
andy.boot
2020-08-03 22:00:37 +01:00
parent 1546cf2eba
commit e118814684
+31 -11
View File
@@ -1,5 +1,8 @@
use assert_cmd::Command; use assert_cmd::Command;
use std::str; use std::str;
use std::sync::Once;
static INIT: Once = Once::new();
mod tests_symlinks; mod tests_symlinks;
@@ -7,6 +10,17 @@ mod tests_symlinks;
/// Copy to /tmp dir - we assume that the formatting of the /tmp partition /// Copy to /tmp dir - we assume that the formatting of the /tmp partition
/// is consistent. If the tests fail your /tmp filesystem probably differs /// is consistent. If the tests fail your /tmp filesystem probably differs
fn copy_test_data(dir: &str) { 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::<String>();
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() { match Command::new("cp").arg("-r").arg(dir).arg("/tmp/").ok() {
Ok(_) => {} Ok(_) => {}
Err(err) => { 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 // We can at least test the file names are there
#[test] #[test]
pub fn test_basic_output() { 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)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_main_basic() { pub fn test_main_basic() {
copy_test_data("src/test_dir");
// -c is no color mode - This makes testing much simpler // -c is no color mode - This makes testing much simpler
initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let assert = cmd.arg("-c").arg("/tmp/test_dir/").unwrap().stdout; let assert = cmd.arg("-c").arg("/tmp/test_dir/").unwrap().stdout;
let output = str::from_utf8(&assert).unwrap(); let output = str::from_utf8(&assert).unwrap();
@@ -57,8 +79,7 @@ pub fn test_main_basic() {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_main_multi_arg() { pub fn test_main_multi_arg() {
copy_test_data("src/test_dir"); initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let assert = cmd let assert = cmd
.arg("-c") .arg("-c")
@@ -103,8 +124,7 @@ fn main_output() -> String {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_main_long_paths() { pub fn test_main_long_paths() {
copy_test_data("src/test_dir"); initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let assert = cmd let assert = cmd
.arg("-c") .arg("-c")
@@ -148,8 +168,7 @@ fn main_output_long_paths() -> String {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_apparent_size() { pub fn test_apparent_size() {
copy_test_data("src/test_dir"); initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let assert = cmd.arg("-c").arg("-s").arg("src/test_dir").unwrap().stdout; let assert = cmd.arg("-c").arg("-s").arg("src/test_dir").unwrap().stdout;
let output = str::from_utf8(&assert).unwrap(); let output = str::from_utf8(&assert).unwrap();
@@ -187,6 +206,7 @@ fn output_apparent_size() -> String {
#[test] #[test]
pub fn test_reverse_flag() { pub fn test_reverse_flag() {
initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd.arg("-c").arg("-r").arg("src/test_dir/").unwrap().stdout; let output = cmd.arg("-c").arg("-r").arg("src/test_dir/").unwrap().stdout;
let output = str::from_utf8(&output).unwrap(); let output = str::from_utf8(&output).unwrap();
@@ -199,6 +219,7 @@ pub fn test_reverse_flag() {
#[test] #[test]
pub fn test_d_flag_works() { pub fn test_d_flag_works() {
initialize();
// We should see the top level directory but not the sub dirs / files: // We should see the top level directory but not the sub dirs / files:
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd let output = cmd
@@ -216,8 +237,7 @@ pub fn test_d_flag_works() {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_substring_of_names() { pub fn test_substring_of_names() {
copy_test_data("src/test_dir2"); initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd.arg("-c").arg("/tmp/test_dir2").unwrap().stdout; let output = cmd.arg("-c").arg("/tmp/test_dir2").unwrap().stdout;
let output = str::from_utf8(&output).unwrap(); let output = str::from_utf8(&output).unwrap();
@@ -260,8 +280,7 @@ fn no_substring_of_names_output() -> String {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_unicode_directories() { pub fn test_unicode_directories() {
copy_test_data("src/test_dir3"); initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd.arg("-c").arg("/tmp/test_dir3").unwrap().stdout; let output = cmd.arg("-c").arg("/tmp/test_dir3").unwrap().stdout;
let output = str::from_utf8(&output).unwrap(); 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 // Check against directories and files whos names are substrings of each other
#[test] #[test]
pub fn test_ignore_dir() { pub fn test_ignore_dir() {
initialize();
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd let output = cmd
.arg("-c") .arg("-c")