Add tests originally written by @alexmaco in #309

This commit is contained in:
sharkdp 2020-04-04 11:54:06 +02:00 committed by David Peter
parent 7e3c69c096
commit ea21df3f76
2 changed files with 69 additions and 0 deletions

View File

@ -208,6 +208,12 @@ impl TestEnv {
self.assert_output_subdirectory(".", args, expected)
}
/// Similar to assert_output, but able to handle non-utf8 output
pub fn assert_output_raw(&self, args: &[&str], expected: &[u8]) {
let actual = self.raw_output_subdirectory(".", args);
assert_eq!(expected, actual.as_ref());
}
/// Assert that calling *fd* in the specified path under the root working directory,
/// and with the specified arguments produces the expected output.
pub fn assert_output_subdirectory<P: AsRef<Path>>(
@ -232,6 +238,23 @@ impl TestEnv {
}
}
fn raw_output_subdirectory<P: AsRef<Path>>(&self, path: P, args: &[&str]) -> Box<[u8]> {
// Setup *fd* command.
let mut cmd = process::Command::new(&self.fd_exe);
cmd.current_dir(self.temp_dir.path().join(path));
cmd.args(args);
// Run *fd*.
let output = cmd.output().expect("fd output");
// Check for exit status.
if !output.status.success() {
panic!(format_exit_error(args, &output));
}
output.stdout.into_boxed_slice()
}
/// Assert that calling *fd* with the specified arguments produces the expected error.
pub fn assert_error(&self, args: &[&str], expected: &str) {
self.assert_error_subdirectory(".", args, expected)

View File

@ -1464,3 +1464,49 @@ fn test_max_results() {
let stdout = stdout.replace(&std::path::MAIN_SEPARATOR.to_string(), "/");
assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo");
}
/// Filenames with non-utf8 paths are passed to the executed program unchanged
///
/// Note:
/// - the test is disabled on Darwin/OSX, since it coerces file names to UTF-8,
/// even when the requested file name is not valid UTF-8.
/// - the test is currently disabled on Windows because I'm not sure how to create
/// invalid UTF-8 files on Windows
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_exec_invalid_utf8() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let dirs = &["test1"];
let files = &[];
let te = TestEnv::new(dirs, files);
fs::File::create(
te.test_root()
.join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),
)
.unwrap();
te.assert_output_raw(
&["", "test1/", "--exec", "echo", "{}"],
b"test1/test_\xFEinvalid.txt\n",
);
te.assert_output_raw(
&["", "test1/", "--exec", "echo", "{/}"],
b"test_\xFEinvalid.txt\n",
);
te.assert_output_raw(&["", "test1/", "--exec", "echo", "{//}"], b"test1\n");
te.assert_output_raw(
&["", "test1/", "--exec", "echo", "{.}"],
b"test1/test_\xFEinvalid\n",
);
te.assert_output_raw(
&["", "test1/", "--exec", "echo", "{/.}"],
b"test_\xFEinvalid\n",
);
}