2017-10-04 23:19:30 +02:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2020-04-03 21:24:11 +02:00
|
|
|
use std::io::{self, Write};
|
2017-10-04 23:19:30 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use std::os::windows;
|
2020-04-03 21:24:11 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::process;
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2022-11-30 07:57:48 +01:00
|
|
|
use tempfile::TempDir;
|
2017-10-04 23:19:30 +02:00
|
|
|
|
|
|
|
/// Environment for the integration tests.
|
|
|
|
pub struct TestEnv {
|
|
|
|
/// Temporary working directory.
|
|
|
|
temp_dir: TempDir,
|
|
|
|
|
|
|
|
/// Path to the *fd* executable.
|
|
|
|
fd_exe: PathBuf,
|
2018-11-11 18:00:01 +01:00
|
|
|
|
2018-11-12 18:43:40 +01:00
|
|
|
/// Normalize each line by sorting the whitespace-separated words
|
2018-11-11 18:00:01 +01:00
|
|
|
normalize_line: bool,
|
2022-12-18 08:42:54 +01:00
|
|
|
|
|
|
|
/// Temporary directory for storing test config (global ignore file)
|
|
|
|
config_dir: Option<TempDir>,
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the working directory and the test files.
|
2017-12-06 23:52:23 +01:00
|
|
|
fn create_working_directory(
|
|
|
|
directories: &[&'static str],
|
|
|
|
files: &[&'static str],
|
|
|
|
) -> Result<TempDir, io::Error> {
|
2022-11-30 07:57:48 +01:00
|
|
|
let temp_dir = tempfile::Builder::new().prefix("fd-tests").tempdir()?;
|
2017-10-04 23:19:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
let root = temp_dir.path();
|
|
|
|
|
2018-07-30 17:19:37 +02:00
|
|
|
// Pretend that this is a Git repository in order for `.gitignore` files to be respected
|
|
|
|
fs::create_dir_all(root.join(".git"))?;
|
|
|
|
|
2017-12-06 23:52:23 +01:00
|
|
|
for directory in directories {
|
|
|
|
fs::create_dir_all(root.join(directory))?;
|
|
|
|
}
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2017-12-06 23:52:23 +01:00
|
|
|
for file in files {
|
|
|
|
fs::File::create(root.join(file))?;
|
|
|
|
}
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2018-01-01 12:16:43 +01:00
|
|
|
#[cfg(unix)]
|
|
|
|
unix::fs::symlink(root.join("one/two"), root.join("symlink"))?;
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2017-10-07 09:40:44 +02:00
|
|
|
// Note: creating symlinks on Windows requires the `SeCreateSymbolicLinkPrivilege` which
|
|
|
|
// is by default only granted for administrators.
|
2018-01-01 12:16:43 +01:00
|
|
|
#[cfg(windows)]
|
|
|
|
windows::fs::symlink_dir(root.join("one/two"), root.join("symlink"))?;
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2018-02-21 21:41:52 +01:00
|
|
|
fs::File::create(root.join(".fdignore"))?.write_all(b"fdignored.foo")?;
|
2017-11-21 22:54:00 +01:00
|
|
|
|
2018-01-01 12:16:43 +01:00
|
|
|
fs::File::create(root.join(".gitignore"))?.write_all(b"gitignored.foo")?;
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(temp_dir)
|
|
|
|
}
|
|
|
|
|
2022-12-18 08:42:54 +01:00
|
|
|
fn create_config_directory_with_global_ignore(ignore_file_content: &str) -> io::Result<TempDir> {
|
|
|
|
let config_dir = tempfile::Builder::new().prefix("fd-config").tempdir()?;
|
|
|
|
let fd_dir = config_dir.path().join("fd");
|
|
|
|
fs::create_dir(&fd_dir)?;
|
|
|
|
let mut ignore_file = fs::File::create(fd_dir.join("ignore"))?;
|
|
|
|
ignore_file.write_all(ignore_file_content.as_bytes())?;
|
|
|
|
|
|
|
|
Ok(config_dir)
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:19:30 +02:00
|
|
|
/// Find the *fd* executable.
|
|
|
|
fn find_fd_exe() -> PathBuf {
|
|
|
|
// Tests exe is in target/debug/deps, the *fd* exe is in target/debug
|
2017-10-12 08:01:51 +02:00
|
|
|
let root = env::current_exe()
|
|
|
|
.expect("tests executable")
|
|
|
|
.parent()
|
|
|
|
.expect("tests executable directory")
|
|
|
|
.parent()
|
|
|
|
.expect("fd executable directory")
|
2017-10-04 23:19:30 +02:00
|
|
|
.to_path_buf();
|
|
|
|
|
|
|
|
let exe_name = if cfg!(windows) { "fd.exe" } else { "fd" };
|
|
|
|
|
|
|
|
root.join(exe_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format an error message for when *fd* did not exit successfully.
|
|
|
|
fn format_exit_error(args: &[&str], output: &process::Output) -> String {
|
|
|
|
format!(
|
|
|
|
"`fd {}` did not exit successfully.\nstdout:\n---\n{}---\nstderr:\n---\n{}---",
|
|
|
|
args.join(" "),
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
2017-10-12 08:01:51 +02:00
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
)
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format an error message for when the output of *fd* did not match the expected output.
|
|
|
|
fn format_output_error(args: &[&str], expected: &str, actual: &str) -> String {
|
|
|
|
// Generate diff text.
|
2017-10-12 08:01:51 +02:00
|
|
|
let diff_text = diff::lines(expected, actual)
|
|
|
|
.into_iter()
|
|
|
|
.map(|diff| match diff {
|
2024-10-06 18:33:58 +02:00
|
|
|
diff::Result::Left(l) => format!("-{l}"),
|
|
|
|
diff::Result::Both(l, _) => format!(" {l}"),
|
|
|
|
diff::Result::Right(r) => format!("+{r}"),
|
2018-09-27 23:01:38 +02:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2017-10-12 08:01:51 +02:00
|
|
|
.join("\n");
|
2017-10-04 23:19:30 +02:00
|
|
|
|
|
|
|
format!(
|
|
|
|
concat!(
|
|
|
|
"`fd {}` did not produce the expected output.\n",
|
2017-10-12 08:01:51 +02:00
|
|
|
"Showing diff between expected and actual:\n{}\n"
|
|
|
|
),
|
2017-10-04 23:19:30 +02:00
|
|
|
args.join(" "),
|
2017-10-12 08:01:51 +02:00
|
|
|
diff_text
|
|
|
|
)
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalize the output for comparison.
|
2019-03-02 01:19:47 +01:00
|
|
|
fn normalize_output(s: &str, trim_start: bool, normalize_line: bool) -> String {
|
2017-10-04 23:19:30 +02:00
|
|
|
// Split into lines and normalize separators.
|
2018-05-14 18:39:47 +02:00
|
|
|
let mut lines = s
|
|
|
|
.replace('\0', "NULL\n")
|
2017-10-04 23:19:30 +02:00
|
|
|
.lines()
|
|
|
|
.map(|line| {
|
2019-03-02 01:19:47 +01:00
|
|
|
let line = if trim_start { line.trim_start() } else { line };
|
2024-04-15 04:36:10 +02:00
|
|
|
let line = line.replace('/', std::path::MAIN_SEPARATOR_STR);
|
2018-11-11 18:00:01 +01:00
|
|
|
if normalize_line {
|
2018-11-12 18:43:40 +01:00
|
|
|
let mut words: Vec<_> = line.split_whitespace().collect();
|
2021-07-27 08:38:09 +02:00
|
|
|
words.sort_unstable();
|
2018-11-12 18:43:40 +01:00
|
|
|
return words.join(" ");
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
|
|
|
line
|
2018-09-27 23:01:38 +02:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2017-10-04 23:19:30 +02:00
|
|
|
|
2018-11-11 18:00:01 +01:00
|
|
|
lines.sort();
|
2017-10-04 23:19:30 +02:00
|
|
|
lines.join("\n")
|
|
|
|
}
|
|
|
|
|
2022-01-04 08:35:49 +01:00
|
|
|
/// Trim whitespace from the beginning of each line.
|
|
|
|
fn trim_lines(s: &str) -> String {
|
|
|
|
s.lines()
|
|
|
|
.map(|line| line.trim_start())
|
|
|
|
.fold(String::new(), |mut str, line| {
|
|
|
|
str.push_str(line);
|
|
|
|
str.push('\n');
|
|
|
|
str
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:19:30 +02:00
|
|
|
impl TestEnv {
|
2017-12-06 23:52:23 +01:00
|
|
|
pub fn new(directories: &[&'static str], files: &[&'static str]) -> TestEnv {
|
|
|
|
let temp_dir = create_working_directory(directories, files).expect("working directory");
|
2017-10-04 23:19:30 +02:00
|
|
|
let fd_exe = find_fd_exe();
|
|
|
|
|
|
|
|
TestEnv {
|
2018-11-11 18:00:01 +01:00
|
|
|
temp_dir,
|
|
|
|
fd_exe,
|
|
|
|
normalize_line: false,
|
2022-12-18 08:42:54 +01:00
|
|
|
config_dir: None,
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn normalize_line(self, normalize: bool) -> TestEnv {
|
|
|
|
TestEnv {
|
|
|
|
temp_dir: self.temp_dir,
|
|
|
|
fd_exe: self.fd_exe,
|
|
|
|
normalize_line: normalize,
|
2022-12-18 08:42:54 +01:00
|
|
|
config_dir: self.config_dir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn global_ignore_file(self, content: &str) -> TestEnv {
|
|
|
|
let config_dir =
|
|
|
|
create_config_directory_with_global_ignore(content).expect("config directory");
|
|
|
|
TestEnv {
|
|
|
|
config_dir: Some(config_dir),
|
|
|
|
..self
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:29:27 +02:00
|
|
|
/// Create a broken symlink at the given path in the temp_dir.
|
|
|
|
pub fn create_broken_symlink<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
|
|
|
link_path: P,
|
|
|
|
) -> Result<PathBuf, io::Error> {
|
|
|
|
let root = self.test_root();
|
|
|
|
let broken_symlink_link = root.join(link_path);
|
|
|
|
{
|
2022-11-30 07:57:48 +01:00
|
|
|
let temp_target_dir = tempfile::Builder::new()
|
|
|
|
.prefix("fd-tests-broken-symlink")
|
|
|
|
.tempdir()?;
|
2019-10-08 12:29:27 +02:00
|
|
|
let broken_symlink_target = temp_target_dir.path().join("broken_symlink_target");
|
|
|
|
fs::File::create(&broken_symlink_target)?;
|
|
|
|
#[cfg(unix)]
|
|
|
|
unix::fs::symlink(&broken_symlink_target, &broken_symlink_link)?;
|
|
|
|
#[cfg(windows)]
|
|
|
|
windows::fs::symlink_file(&broken_symlink_target, &broken_symlink_link)?;
|
|
|
|
}
|
|
|
|
Ok(broken_symlink_link)
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:19:30 +02:00
|
|
|
/// Get the root directory for the tests.
|
2017-10-18 20:04:34 +02:00
|
|
|
pub fn test_root(&self) -> PathBuf {
|
2017-10-04 23:19:30 +02:00
|
|
|
self.temp_dir.path().to_path_buf()
|
|
|
|
}
|
|
|
|
|
2022-08-08 17:07:57 +02:00
|
|
|
/// Get the path of the fd executable.
|
2022-11-11 08:34:30 +01:00
|
|
|
#[cfg_attr(windows, allow(unused))]
|
2022-08-08 17:07:57 +02:00
|
|
|
pub fn test_exe(&self) -> &PathBuf {
|
|
|
|
&self.fd_exe
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:04:34 +02:00
|
|
|
/// Get the root directory of the file system.
|
|
|
|
pub fn system_root(&self) -> PathBuf {
|
|
|
|
let mut components = self.temp_dir.path().components();
|
|
|
|
PathBuf::from(components.next().expect("root directory").as_os_str())
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:19:30 +02:00
|
|
|
/// Assert that calling *fd* in the specified path under the root working directory,
|
|
|
|
/// and with the specified arguments produces the expected output.
|
2020-04-02 17:52:44 +02:00
|
|
|
pub fn assert_success_and_get_output<P: AsRef<Path>>(
|
2017-10-12 08:01:51 +02:00
|
|
|
&self,
|
|
|
|
path: P,
|
|
|
|
args: &[&str],
|
2020-04-02 17:52:44 +02:00
|
|
|
) -> process::Output {
|
2017-10-04 23:19:30 +02:00
|
|
|
// Run *fd*.
|
2022-12-18 08:42:54 +01:00
|
|
|
let output = self.run_command(path.as_ref(), args);
|
2017-10-04 23:19:30 +02:00
|
|
|
|
|
|
|
// Check for exit status.
|
|
|
|
if !output.status.success() {
|
2021-07-27 08:38:09 +02:00
|
|
|
panic!("{}", format_exit_error(args, &output));
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:52:44 +02:00
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2022-01-07 11:08:22 +01:00
|
|
|
pub fn assert_success_and_get_normalized_output<P: AsRef<Path>>(
|
|
|
|
&self,
|
|
|
|
path: P,
|
|
|
|
args: &[&str],
|
|
|
|
) -> String {
|
|
|
|
let output = self.assert_success_and_get_output(path, args);
|
|
|
|
normalize_output(
|
|
|
|
&String::from_utf8_lossy(&output.stdout),
|
|
|
|
false,
|
|
|
|
self.normalize_line,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:52:44 +02:00
|
|
|
/// Assert that calling *fd* with the specified arguments produces the expected output.
|
|
|
|
pub fn assert_output(&self, args: &[&str], expected: &str) {
|
|
|
|
self.assert_output_subdirectory(".", args, expected)
|
|
|
|
}
|
|
|
|
|
2020-04-04 11:54:06 +02:00
|
|
|
/// Similar to assert_output, but able to handle non-utf8 output
|
2020-04-04 13:39:45 +02:00
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
2020-04-04 11:54:06 +02:00
|
|
|
pub fn assert_output_raw(&self, args: &[&str], expected: &[u8]) {
|
2020-04-04 13:39:45 +02:00
|
|
|
let output = self.assert_success_and_get_output(".", args);
|
|
|
|
|
|
|
|
assert_eq!(expected, &output.stdout[..]);
|
2020-04-04 11:54:06 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:52:44 +02:00
|
|
|
/// 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>>(
|
|
|
|
&self,
|
|
|
|
path: P,
|
|
|
|
args: &[&str],
|
|
|
|
expected: &str,
|
|
|
|
) {
|
2017-10-04 23:19:30 +02:00
|
|
|
// Normalize both expected and actual output.
|
2018-11-11 18:00:01 +01:00
|
|
|
let expected = normalize_output(expected, true, self.normalize_line);
|
2022-01-07 11:08:22 +01:00
|
|
|
let actual = self.assert_success_and_get_normalized_output(path, args);
|
2017-10-04 23:19:30 +02:00
|
|
|
|
|
|
|
// Compare actual output to expected output.
|
|
|
|
if expected != actual {
|
2021-07-27 08:38:09 +02:00
|
|
|
panic!("{}", format_output_error(args, &expected, &actual));
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-11 18:00:01 +01:00
|
|
|
|
2020-05-19 14:27:20 +02:00
|
|
|
/// Assert that calling *fd* with the specified arguments produces the expected error,
|
|
|
|
/// and does not succeed.
|
|
|
|
pub fn assert_failure_with_error(&self, args: &[&str], expected: &str) {
|
2020-10-25 21:18:53 +01:00
|
|
|
let status = self.assert_error_subdirectory(".", args, Some(expected));
|
2020-05-19 14:27:20 +02:00
|
|
|
if status.success() {
|
2024-10-06 18:33:58 +02:00
|
|
|
panic!("error '{expected}' did not occur.");
|
2020-05-19 14:27:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:18:53 +01:00
|
|
|
/// Assert that calling *fd* with the specified arguments does not succeed.
|
|
|
|
pub fn assert_failure(&self, args: &[&str]) {
|
|
|
|
let status = self.assert_error_subdirectory(".", args, None);
|
|
|
|
if status.success() {
|
|
|
|
panic!("Failure did not occur as expected.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:00:01 +01:00
|
|
|
/// Assert that calling *fd* with the specified arguments produces the expected error.
|
2020-05-19 14:27:20 +02:00
|
|
|
pub fn assert_error(&self, args: &[&str], expected: &str) -> process::ExitStatus {
|
2020-10-25 21:18:53 +01:00
|
|
|
self.assert_error_subdirectory(".", args, Some(expected))
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
|
|
|
|
2022-12-18 08:42:54 +01:00
|
|
|
fn run_command(&self, path: &Path, args: &[&str]) -> process::Output {
|
|
|
|
// Setup *fd* command.
|
|
|
|
let mut cmd = process::Command::new(&self.fd_exe);
|
|
|
|
cmd.current_dir(self.temp_dir.path().join(path));
|
|
|
|
if let Some(config_dir) = &self.config_dir {
|
|
|
|
cmd.env("XDG_CONFIG_HOME", config_dir.path());
|
|
|
|
} else {
|
|
|
|
cmd.arg("--no-global-ignore-file");
|
|
|
|
}
|
2024-06-08 23:36:02 +02:00
|
|
|
// Make sure LS_COLORS is unset to ensure consistent
|
|
|
|
// color output
|
|
|
|
cmd.env("LS_COLORS", "");
|
2022-12-18 08:42:54 +01:00
|
|
|
cmd.args(args);
|
|
|
|
|
|
|
|
// Run *fd*.
|
|
|
|
cmd.output().expect("fd output")
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:00:01 +01:00
|
|
|
/// Assert that calling *fd* in the specified path under the root working directory,
|
|
|
|
/// and with the specified arguments produces an error with the expected message.
|
2020-05-19 15:37:38 +02:00
|
|
|
fn assert_error_subdirectory<P: AsRef<Path>>(
|
|
|
|
&self,
|
|
|
|
path: P,
|
|
|
|
args: &[&str],
|
2020-10-25 21:18:53 +01:00
|
|
|
expected: Option<&str>,
|
2020-05-19 15:37:38 +02:00
|
|
|
) -> process::ExitStatus {
|
2022-12-18 08:42:54 +01:00
|
|
|
let output = self.run_command(path.as_ref(), args);
|
2018-11-11 18:00:01 +01:00
|
|
|
|
2020-10-25 21:18:53 +01:00
|
|
|
if let Some(expected) = expected {
|
|
|
|
// Normalize both expected and actual output.
|
2022-01-04 08:35:49 +01:00
|
|
|
let expected_error = trim_lines(expected);
|
|
|
|
let actual_err = trim_lines(&String::from_utf8_lossy(&output.stderr));
|
2020-10-25 21:18:53 +01:00
|
|
|
|
|
|
|
// Compare actual output to expected output.
|
|
|
|
if !actual_err.trim_start().starts_with(&expected_error) {
|
2021-07-27 08:38:09 +02:00
|
|
|
panic!(
|
|
|
|
"{}",
|
|
|
|
format_output_error(args, &expected_error, &actual_err)
|
|
|
|
);
|
2020-10-25 21:18:53 +01:00
|
|
|
}
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
2020-05-19 14:27:20 +02:00
|
|
|
|
2021-07-27 08:38:09 +02:00
|
|
|
output.status
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
2017-10-04 23:19:30 +02:00
|
|
|
}
|