Skip to content

Commit

Permalink
tests: Cleanup test_exact_ouput
Browse files Browse the repository at this point in the history
  • Loading branch information
bootandy committed Jun 24, 2024
1 parent d65f410 commit b7176cf
Showing 1 changed file with 45 additions and 65 deletions.
110 changes: 45 additions & 65 deletions tests/test_exact_output.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use assert_cmd::Command;
use std::ffi::OsStr;
use std::process::Output;
use std::str;
use std::sync::Once;

Expand Down Expand Up @@ -38,56 +39,52 @@ 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
#[cfg_attr(target_os = "windows", ignore)]
#[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)]
Expand All @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand All @@ -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> {
Expand All @@ -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);
}

0 comments on commit b7176cf

Please sign in to comment.