From 87ecb30800e532da28cee3e30d603b22d0b9749b Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Thu, 5 Aug 2021 15:41:00 +0100 Subject: [PATCH 1/4] Tests: Refactor: More code reuse --- tests/test_exact_output.rs | 97 ++++++++++++++------------------------ 1 file changed, 36 insertions(+), 61 deletions(-) diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 17500ad1..29c3db4a 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -1,4 +1,5 @@ use assert_cmd::Command; +use std::ffi::OsStr; use std::str; use std::sync::Once; @@ -37,7 +38,7 @@ fn copy_test_data(dir: &str) { }; } -pub fn initialize() { +fn initialize() { INIT.call_once(|| { copy_test_data("tests/test_dir"); copy_test_data("tests/test_dir2"); @@ -45,40 +46,44 @@ pub fn initialize() { }); } +fn run_dust_with>(params: Vec) -> String { + let mut cmd = Command::cargo_bin("dust").unwrap(); + let mut a = &mut cmd; + for p in params { + a = a.arg(p); + } + str::from_utf8(&a.unwrap().stdout).unwrap().into() +} + +fn check_dust_output(output: String, func: fn() -> Vec) { + let mut we_match = false; + for mo in func() { + we_match = we_match || output.contains(&mo); + } + assert!(we_match); +} + // "windows" result data can vary by host (size seems to be variable by one byte); fix code vs test and re-enable #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_basic() { // -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(); - let mut we_match = false; - for mo in main_output() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec!["-c", "/tmp/test_dir/"]); + check_dust_output(output, main_output); } #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_multi_arg() { initialize(); - let mut cmd = Command::cargo_bin("dust").unwrap(); - let assert = cmd - .arg("-c") - .arg("/tmp/test_dir/many/") - .arg("/tmp/test_dir") - .arg("/tmp/test_dir") - .unwrap() - .stdout; - let output = str::from_utf8(&assert).unwrap(); - let mut we_match = false; - for mo in main_output() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec![ + "-c", + "/tmp/test_dir/many/", + "/tmp/test_dir", + "/tmp/test_dir", + ]); + check_dust_output(output, main_output); } fn main_output() -> Vec { @@ -109,20 +114,8 @@ fn main_output() -> Vec { #[test] pub fn test_main_long_paths() { initialize(); - let mut cmd = Command::cargo_bin("dust").unwrap(); - let assert = cmd - .arg("-c") - .arg("-p") - .arg("/tmp/test_dir/") - .unwrap() - .stdout; - let output = str::from_utf8(&assert).unwrap(); - - let mut we_match = false; - for mo in main_output_long_paths() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec!["-c", "-p", "/tmp/test_dir/"]); + check_dust_output(output, main_output_long_paths); } fn main_output_long_paths() -> Vec { @@ -149,14 +142,8 @@ fn main_output_long_paths() -> Vec { #[test] pub fn test_apparent_size() { initialize(); - let mut cmd = Command::cargo_bin("dust").unwrap(); - let assert = cmd.arg("-c").arg("-s").arg("/tmp/test_dir").unwrap().stdout; - let output = str::from_utf8(&assert).unwrap(); - let mut we_match = false; - for mo in output_apparent_size() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec!["-c", "-s", "/tmp/test_dir"]); + check_dust_output(output, output_apparent_size); } fn output_apparent_size() -> Vec { @@ -172,14 +159,8 @@ fn output_apparent_size() -> Vec { #[test] pub fn test_substring_of_names_and_long_names() { 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(); - let mut we_match = false; - for mo in no_substring_of_names_output() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec!["-c", "/tmp/test_dir2"]); + check_dust_output(output, no_substring_of_names_output); } fn no_substring_of_names_output() -> Vec { @@ -213,14 +194,8 @@ fn no_substring_of_names_output() -> Vec { #[test] pub fn test_unicode_directories() { initialize(); - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd.arg("-c").arg("/tmp/test_dir_unicode").unwrap().stdout; - let output = str::from_utf8(&output).unwrap(); - let mut we_match = false; - for mo in unicode_dir() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + let output = run_dust_with(vec!["-c", "/tmp/test_dir_unicode"]); + check_dust_output(output, unicode_dir); } fn unicode_dir() -> Vec { From 98882c30f99d76fd9aae7ec258e5b657aa90a33f Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Thu, 5 Aug 2021 16:22:07 +0100 Subject: [PATCH 2/4] Tests: Refactor: Neaten --- tests/test_exact_output.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 29c3db4a..1e7b0cb1 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -56,11 +56,9 @@ fn run_dust_with>(params: Vec) -> String { } fn check_dust_output(output: String, func: fn() -> Vec) { - let mut we_match = false; - for mo in func() { - we_match = we_match || output.contains(&mo); - } - assert!(we_match); + assert!(func() + .iter() + .fold(false, |sum, i| sum || output.contains(i))); } // "windows" result data can vary by host (size seems to be variable by one byte); fix code vs test and re-enable From 1ed2469f6e56a365d0df2780c4a79323b508c7f9 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Thu, 5 Aug 2021 17:13:11 +0100 Subject: [PATCH 3/4] Tests: Refactor: Improve exact output tests Move more shared code into single function --- tests/test_exact_output.rs | 46 ++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 1e7b0cb1..62f6db57 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -46,17 +46,16 @@ fn initialize() { }); } -fn run_dust_with>(params: Vec) -> String { - let mut cmd = Command::cargo_bin("dust").unwrap(); - let mut a = &mut cmd; - for p in params { +fn exact_output_test>(valid_outputs: Vec, command_args : Vec) { + initialize(); + + let mut a = &mut Command::cargo_bin("dust").unwrap(); + for p in command_args { a = a.arg(p); } - str::from_utf8(&a.unwrap().stdout).unwrap().into() -} + let output : String = str::from_utf8(&a.unwrap().stdout).unwrap().into(); -fn check_dust_output(output: String, func: fn() -> Vec) { - assert!(func() + assert!(valid_outputs .iter() .fold(false, |sum, i| sum || output.contains(i))); } @@ -66,22 +65,19 @@ fn check_dust_output(output: String, func: fn() -> Vec) { #[test] pub fn test_main_basic() { // -c is no color mode - This makes testing much simpler - initialize(); - let output = run_dust_with(vec!["-c", "/tmp/test_dir/"]); - check_dust_output(output, main_output); + exact_output_test(main_output(), vec!["-c", "/tmp/test_dir/"]) } #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_multi_arg() { - initialize(); - let output = run_dust_with(vec![ + let command_args = vec![ "-c", "/tmp/test_dir/many/", "/tmp/test_dir", "/tmp/test_dir", - ]); - check_dust_output(output, main_output); + ]; + exact_output_test(main_output(), command_args); } fn main_output() -> Vec { @@ -111,9 +107,8 @@ fn main_output() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_long_paths() { - initialize(); - let output = run_dust_with(vec!["-c", "-p", "/tmp/test_dir/"]); - check_dust_output(output, main_output_long_paths); + let command_args = vec!["-c", "-p", "/tmp/test_dir/"]; + exact_output_test(main_output_long_paths(), command_args); } fn main_output_long_paths() -> Vec { @@ -139,9 +134,8 @@ fn main_output_long_paths() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_apparent_size() { - initialize(); - let output = run_dust_with(vec!["-c", "-s", "/tmp/test_dir"]); - check_dust_output(output, output_apparent_size); + let command_args = vec!["-c", "-s", "/tmp/test_dir"]; + exact_output_test(output_apparent_size(), command_args); } fn output_apparent_size() -> Vec { @@ -156,9 +150,8 @@ fn output_apparent_size() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_substring_of_names_and_long_names() { - initialize(); - let output = run_dust_with(vec!["-c", "/tmp/test_dir2"]); - check_dust_output(output, no_substring_of_names_output); + let command_args = vec!["-c", "/tmp/test_dir2"]; + exact_output_test(no_substring_of_names_output(), command_args); } fn no_substring_of_names_output() -> Vec { @@ -191,9 +184,8 @@ fn no_substring_of_names_output() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_unicode_directories() { - initialize(); - let output = run_dust_with(vec!["-c", "/tmp/test_dir_unicode"]); - check_dust_output(output, unicode_dir); + let command_args = vec!["-c", "/tmp/test_dir_unicode"]; + exact_output_test(unicode_dir(), command_args); } fn unicode_dir() -> Vec { From 039b8db13faae5f1513fb29d8577ddf0946d2823 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Fri, 6 Aug 2021 10:41:54 +0100 Subject: [PATCH 4/4] Tests: Refactor Move apparent size test to 'test_flags' file. We can no longer test the exact output of this test as it changes too much on different filesystems Move common code to shared initialization function in test_flags.rs --- tests/test_exact_output.rs | 19 +------ tests/test_flags.rs | 101 +++++++++++++------------------------ 2 files changed, 37 insertions(+), 83 deletions(-) diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 62f6db57..401c13a9 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -46,14 +46,14 @@ fn initialize() { }); } -fn exact_output_test>(valid_outputs: Vec, command_args : Vec) { +fn exact_output_test>(valid_outputs: Vec, command_args: Vec) { initialize(); let mut a = &mut Command::cargo_bin("dust").unwrap(); for p in command_args { a = a.arg(p); } - let output : String = str::from_utf8(&a.unwrap().stdout).unwrap().into(); + let output: String = str::from_utf8(&a.unwrap().stdout).unwrap().into(); assert!(valid_outputs .iter() @@ -131,21 +131,6 @@ fn main_output_long_paths() -> Vec { vec![mac_and_some_linux, ubuntu] } -#[cfg_attr(target_os = "windows", ignore)] -#[test] -pub fn test_apparent_size() { - let command_args = vec!["-c", "-s", "/tmp/test_dir"]; - exact_output_test(output_apparent_size(), command_args); -} - -fn output_apparent_size() -> Vec { - // The directory sizes vary a lot based on what the underlying filesystem is - // so different distros give different results. Really we should be checking that - // the standard '4.0K' isn't there - let apparent_size = "6B ├── hello_file│".into(); - vec![apparent_size] -} - // Check against directories and files whos names are substrings of each other #[cfg_attr(target_os = "windows", ignore)] #[test] diff --git a/tests/test_flags.rs b/tests/test_flags.rs index f4a938b6..6e9db388 100644 --- a/tests/test_flags.rs +++ b/tests/test_flags.rs @@ -1,17 +1,25 @@ use assert_cmd::Command; +use std::ffi::OsStr; use std::str; + /** * This file contains tests that test a substring of the output using '.contains' * * These tests should be the same cross platform */ +fn build_command>(command_args: Vec) -> String { + let mut a = &mut Command::cargo_bin("dust").unwrap(); + for p in command_args { + a = a.arg(p); + } + str::from_utf8(&a.unwrap().stdout).unwrap().into() +} + // We can at least test the file names are there #[test] pub fn test_basic_output() { - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd.arg("tests/test_dir/").unwrap().stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["tests/test_dir/"]); assert!(output.contains(" ┌─┴ ")); assert!(output.contains("test_dir ")); @@ -25,9 +33,7 @@ pub fn test_basic_output() { #[test] pub fn test_output_no_bars_means_no_excess_spaces() { - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd.arg("-b").arg("tests/test_dir/").unwrap().stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-b", "tests/test_dir/"]); // If bars are not being shown we don't need to pad the output with spaces assert!(output.contains("many")); assert!(!output.contains("many ")); @@ -35,15 +41,7 @@ pub fn test_output_no_bars_means_no_excess_spaces() { #[test] pub fn test_reverse_flag() { - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-c") - .arg("-r") - .arg("tests/test_dir/") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); - + let output = build_command(vec!["-r", "-c", "tests/test_dir/"]); assert!(output.contains(" └─┬ test_dir ")); assert!(output.contains(" └─┬ many ")); assert!(output.contains(" ├── hello_file")); @@ -53,15 +51,7 @@ pub fn test_reverse_flag() { #[test] pub fn test_d_flag_works() { // We should see the top level directory but not the sub dirs / files: - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-d") - .arg("1") - .arg("-s") - .arg("tests/test_dir/") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-d", "1", "tests/test_dir/"]); assert!(!output.contains("hello_file")); } @@ -69,31 +59,14 @@ pub fn test_d_flag_works() { pub fn test_d_flag_works_and_still_recurses_down() { // We had a bug where running with '-d 1' would stop at the first directory and the code // would fail to recurse down - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-d") - .arg("1") - .arg("-f") - .arg("-c") - .arg("tests/test_dir2/") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-d", "1", "-f", "-c", "tests/test_dir2/"]); assert!(output.contains("7 ┌─┴ test_dir2")); } // Check against directories and files whos names are substrings of each other #[test] pub fn test_ignore_dir() { - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-c") - .arg("-X") - .arg("dir_substring") - .arg("tests/test_dir2") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]); assert!(!output.contains("dir_substring")); } @@ -108,25 +81,12 @@ pub fn test_with_bad_param() { #[test] pub fn test_hidden_flag() { // Check we can see the hidden file normally - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-c") - .arg("tests/test_dir_hidden_entries") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-c", "tests/test_dir_hidden_entries/"]); assert!(output.contains(".hidden_file")); assert!(output.contains("┌─┴ test_dir_hidden_entries")); // Check that adding the '-h' flag causes us to not see hidden files - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-c") - .arg("-i") - .arg("tests/test_dir_hidden_entries") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-c", "-i", "tests/test_dir_hidden_entries/"]); assert!(!output.contains(".hidden_file")); assert!(output.contains("┌── test_dir_hidden_entries")); } @@ -134,16 +94,25 @@ pub fn test_hidden_flag() { #[test] pub fn test_number_of_files() { // Check we can see the hidden file normally - let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd - .arg("-c") - .arg("-f") - .arg("tests/test_dir") - .unwrap() - .stdout; - let output = str::from_utf8(&output).unwrap(); + let output = build_command(vec!["-c", "-f", "tests/test_dir"]); assert!(output.contains("1 ┌── a_file ")); assert!(output.contains("1 ├── hello_file")); assert!(output.contains("3 ┌─┴ many")); assert!(output.contains("4 ┌─┴ test_dir")); } + +#[cfg_attr(target_os = "windows", ignore)] +#[test] +pub fn test_apparent_size() { + // Check the '-s' Flag gives us byte sizes and that it doesn't round up to a block + let command_args = vec!["-c", "-s", "/tmp/test_dir"]; + let output = build_command(command_args); + + let apparent_size1 = "6B ├── hello_file│"; + let apparent_size2 = "0B ┌── a_file"; + assert!(output.contains(apparent_size1)); + assert!(output.contains(apparent_size2)); + + let incorrect_apparent_size = "4.0K ├── hello_file"; + assert!(!output.contains(incorrect_apparent_size)); +}