summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandy.boot <bootandy@gmail.com>2024-06-19 22:30:35 +0100
committerandy.boot <bootandy@gmail.com>2024-06-19 22:39:20 +0100
commit7f93c90f5e35f76daa8d8399d0a880a8545c503f (patch)
treed587101f2c34eac07859f8d3c11661f873490033
parent08e4240b419d6c314d75414341f5851ec0942e1b (diff)
tests: Cleanup test_exact_ouputclean_tests
-rw-r--r--tests/test_exact_output.rs110
1 files changed, 45 insertions, 65 deletions
diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs
index cb58b5a..fb4a2bf 100644
--- a/tests/test_exact_output.rs
+++ b/tests/test_exact_output.rs
@@ -1,5 +1,6 @@
use assert_cmd::Command;
use std::ffi::OsStr;
+use std::process::Output;
use std::str;
use std::sync::Once;
@@ -38,48 +39,44 @@ fn initialize() {
copy_test_data("tests/test_dir");
copy_test_data("tests/test_dir2");
copy_test_data("tests/test_dir_unicode");
+
+ Command::new("sh")
+ .arg("-c")
+ .arg("mkdir -p /tmp/unreadable_folder && chmod 000 /tmp/unreadable_folder")
+ .output()
+ .unwrap();
});
}
-fn exact_output_test<T: AsRef<OsStr>>(
- command_args: Vec<T>,
- valid_outputs: Vec<String>,
- error_outputs: Vec<String>,
-) {
+fn run_cmd<T: AsRef<OsStr>>(command_args: Vec<T>) -> Output {
initialize();
-
- let mut a = &mut Command::cargo_bin("dust").unwrap();
-
+ let mut to_run = &mut Command::cargo_bin("dust").unwrap();
for p in command_args {
- a = a.arg(p);
+ to_run = to_run.arg(p);
}
+ to_run.unwrap()
+}
- if !valid_outputs.is_empty() {
- let stdout_output = str::from_utf8(&a.unwrap().stdout).unwrap().to_owned();
- let will_fail = valid_outputs.iter().any(|i| stdout_output.contains(i));
- if !will_fail {
- eprintln!(
- "output(stdout):\n{}\ndoes not contain any of:\n{}",
- stdout_output,
- valid_outputs.join("\n\n")
- );
- }
- assert!(will_fail);
- }
+fn exact_stdout_test<T: AsRef<OsStr>>(command_args: Vec<T>, valid_stdout: Vec<String>) {
+ let to_run = run_cmd(command_args);
- // check stderr
- if !error_outputs.is_empty() {
- let stderr_output = str::from_utf8(&a.unwrap().stderr).unwrap().to_owned();
- let will_fail = error_outputs.iter().any(|i| stderr_output.contains(i));
- if !will_fail {
- eprintln!(
- "output(stderr):\n{}\ndoes not contain any of:\n{}",
- stderr_output,
- valid_outputs.join("\n\n")
- );
- }
- assert!(will_fail);
+ let stdout_output = str::from_utf8(&to_run.stdout).unwrap().to_owned();
+ let will_fail = valid_stdout.iter().any(|i| stdout_output.contains(i));
+ if !will_fail {
+ eprintln!(
+ "output(stdout):\n{}\ndoes not contain any of:\n{}",
+ stdout_output,
+ valid_stdout.join("\n\n")
+ );
}
+ assert!(will_fail);
+}
+
+fn exact_stderr_test<T: AsRef<OsStr>>(command_args: Vec<T>, valid_stderr: String) {
+ let to_run = run_cmd(command_args);
+
+ let stderr_output = str::from_utf8(&to_run.stderr).unwrap().trim();
+ assert_eq!(stderr_output, valid_stderr);
}
// "windows" result data can vary by host (size seems to be variable by one byte); fix code vs test and re-enable
@@ -87,7 +84,7 @@ fn exact_output_test<T: AsRef<OsStr>>(
#[test]
pub fn test_main_basic() {
// -c is no color mode - This makes testing much simpler
- exact_output_test(vec!["-c", "-B", "/tmp/test_dir/"], main_output(), vec![]);
+ exact_stdout_test(vec!["-c", "-B", "/tmp/test_dir/"], main_output());
}
#[cfg_attr(target_os = "windows", ignore)]
@@ -100,7 +97,7 @@ pub fn test_main_multi_arg() {
"/tmp/test_dir",
"/tmp/test_dir",
];
- exact_output_test(command_args, main_output(), vec![]);
+ exact_stdout_test(command_args, main_output());
}
fn main_output() -> Vec<String> {
@@ -131,7 +128,7 @@ fn main_output() -> Vec<String> {
#[test]
pub fn test_main_long_paths() {
let command_args = vec!["-c", "-p", "-B", "/tmp/test_dir/"];
- exact_output_test(command_args, main_output_long_paths(), vec![]);
+ exact_stdout_test(command_args, main_output_long_paths());
}
fn main_output_long_paths() -> Vec<String> {
@@ -159,7 +156,7 @@ fn main_output_long_paths() -> Vec<String> {
#[test]
pub fn test_substring_of_names_and_long_names() {
let command_args = vec!["-c", "-B", "/tmp/test_dir2"];
- exact_output_test(command_args, no_substring_of_names_output(), vec![]);
+ exact_stdout_test(command_args, no_substring_of_names_output());
}
fn no_substring_of_names_output() -> Vec<String> {
@@ -193,7 +190,7 @@ fn no_substring_of_names_output() -> Vec<String> {
#[test]
pub fn test_unicode_directories() {
let command_args = vec!["-c", "-B", "/tmp/test_dir_unicode"];
- exact_output_test(command_args, unicode_dir(), vec![]);
+ exact_stdout_test(command_args, unicode_dir());
}
fn unicode_dir() -> Vec<String> {
@@ -220,7 +217,7 @@ fn unicode_dir() -> Vec<String> {
#[test]
pub fn test_apparent_size() {
let command_args = vec!["-c", "-s", "-b", "/tmp/test_dir"];
- exact_output_test(command_args, apparent_size_output(), vec![]);
+ exact_stdout_test(command_args, apparent_size_output());
}
fn apparent_size_output() -> Vec<String> {
@@ -245,38 +242,21 @@ fn apparent_size_output() -> Vec<String> {
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_permission_normal() {
- Command::new("sh")
- .arg("-c")
- .arg("mkdir -p /tmp/unreadable_folder && chmod 000 /tmp/unreadable_folder")
- .output()
- .unwrap();
let command_args = vec!["/tmp/unreadable_folder"];
- let permission_msg = r#"Did not have permissions for all"#.trim().to_string();
- exact_output_test(command_args, vec![], vec![permission_msg]);
-
- Command::new("sh")
- .arg("-c")
- .arg("chmod 555 /tmp/unreadable_folder")
- .output()
- .unwrap();
+ let permission_msg =
+ r#"Did not have permissions for all directories (add --print-errors to see errors)"#
+ .trim()
+ .to_string();
+ exact_stderr_test(command_args, permission_msg);
}
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_permission_flag() {
- Command::new("sh")
- .arg("-c")
- .arg("mkdir -p /tmp/unreadable_folder && chmod 000 /tmp/unreadable_folder")
- .output()
- .unwrap();
// add the flag to CLI
let command_args = vec!["--print-errors", "/tmp/unreadable_folder"];
- let permission_msg = r#"Did not have permissions for directories"#.trim().to_string();
- exact_output_test(command_args, vec![], vec![permission_msg]);
-
- Command::new("sh")
- .arg("-c")
- .arg("chmod 555 /tmp/unreadable_folder")
- .output()
- .unwrap();
+ let permission_msg = r#"Did not have permissions for directories: /tmp/unreadable_folder"#
+ .trim()
+ .to_string();
+ exact_stderr_test(command_args, permission_msg);
}