diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 17500ad1..401c13a9 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,38 @@ pub fn initialize() { }); } +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(); + + assert!(valid_outputs + .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 #[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); + 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 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 command_args = vec![ + "-c", + "/tmp/test_dir/many/", + "/tmp/test_dir", + "/tmp/test_dir", + ]; + exact_output_test(main_output(), command_args); } fn main_output() -> Vec { @@ -108,21 +107,8 @@ fn main_output() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[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 command_args = vec!["-c", "-p", "/tmp/test_dir/"]; + exact_output_test(main_output_long_paths(), command_args); } fn main_output_long_paths() -> Vec { @@ -145,41 +131,12 @@ fn main_output_long_paths() -> Vec { vec![mac_and_some_linux, ubuntu] } -#[cfg_attr(target_os = "windows", ignore)] -#[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); -} - -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] 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 command_args = vec!["-c", "/tmp/test_dir2"]; + exact_output_test(no_substring_of_names_output(), command_args); } fn no_substring_of_names_output() -> Vec { @@ -212,15 +169,8 @@ fn no_substring_of_names_output() -> Vec { #[cfg_attr(target_os = "windows", ignore)] #[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 command_args = vec!["-c", "/tmp/test_dir_unicode"]; + exact_output_test(unicode_dir(), command_args); } fn unicode_dir() -> Vec { 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)); +}