Tests: Refactor: Improve exact output tests

Move more shared code into single function
This commit is contained in:
andy.boot
2021-08-05 17:13:11 +01:00
parent 87b1f50b39
commit 9cf260e42b
+19 -27
View File
@@ -46,17 +46,16 @@ fn initialize() {
}); });
} }
fn run_dust_with<T: AsRef<OsStr>>(params: Vec<T>) -> String { fn exact_output_test<T: AsRef<OsStr>>(valid_outputs: Vec<String>, command_args : Vec<T>) {
let mut cmd = Command::cargo_bin("dust").unwrap(); initialize();
let mut a = &mut cmd;
for p in params { let mut a = &mut Command::cargo_bin("dust").unwrap();
for p in command_args {
a = a.arg(p); 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<String>) { assert!(valid_outputs
assert!(func()
.iter() .iter()
.fold(false, |sum, i| sum || output.contains(i))); .fold(false, |sum, i| sum || output.contains(i)));
} }
@@ -66,22 +65,19 @@ fn check_dust_output(output: String, func: fn() -> Vec<String>) {
#[test] #[test]
pub fn test_main_basic() { pub fn test_main_basic() {
// -c is no color mode - This makes testing much simpler // -c is no color mode - This makes testing much simpler
initialize(); exact_output_test(main_output(), vec!["-c", "/tmp/test_dir/"])
let output = run_dust_with(vec!["-c", "/tmp/test_dir/"]);
check_dust_output(output, main_output);
} }
#[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() {
initialize(); let command_args = vec![
let output = run_dust_with(vec![
"-c", "-c",
"/tmp/test_dir/many/", "/tmp/test_dir/many/",
"/tmp/test_dir", "/tmp/test_dir",
"/tmp/test_dir", "/tmp/test_dir",
]); ];
check_dust_output(output, main_output); exact_output_test(main_output(), command_args);
} }
fn main_output() -> Vec<String> { fn main_output() -> Vec<String> {
@@ -111,9 +107,8 @@ fn main_output() -> Vec<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() {
initialize(); let command_args = vec!["-c", "-p", "/tmp/test_dir/"];
let output = run_dust_with(vec!["-c", "-p", "/tmp/test_dir/"]); exact_output_test(main_output_long_paths(), command_args);
check_dust_output(output, main_output_long_paths);
} }
fn main_output_long_paths() -> Vec<String> { fn main_output_long_paths() -> Vec<String> {
@@ -139,9 +134,8 @@ fn main_output_long_paths() -> Vec<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() {
initialize(); let command_args = vec!["-c", "-s", "/tmp/test_dir"];
let output = run_dust_with(vec!["-c", "-s", "/tmp/test_dir"]); exact_output_test(output_apparent_size(), command_args);
check_dust_output(output, output_apparent_size);
} }
fn output_apparent_size() -> Vec<String> { fn output_apparent_size() -> Vec<String> {
@@ -156,9 +150,8 @@ fn output_apparent_size() -> Vec<String> {
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_substring_of_names_and_long_names() { pub fn test_substring_of_names_and_long_names() {
initialize(); let command_args = vec!["-c", "/tmp/test_dir2"];
let output = run_dust_with(vec!["-c", "/tmp/test_dir2"]); exact_output_test(no_substring_of_names_output(), command_args);
check_dust_output(output, no_substring_of_names_output);
} }
fn no_substring_of_names_output() -> Vec<String> { fn no_substring_of_names_output() -> Vec<String> {
@@ -191,9 +184,8 @@ fn no_substring_of_names_output() -> Vec<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() {
initialize(); let command_args = vec!["-c", "/tmp/test_dir_unicode"];
let output = run_dust_with(vec!["-c", "/tmp/test_dir_unicode"]); exact_output_test(unicode_dir(), command_args);
check_dust_output(output, unicode_dir);
} }
fn unicode_dir() -> Vec<String> { fn unicode_dir() -> Vec<String> {