diff --git a/tests/testenv/mod.rs b/tests/testenv/mod.rs index d4bfad2..188559d 100644 --- a/tests/testenv/mod.rs +++ b/tests/testenv/mod.rs @@ -20,6 +20,9 @@ pub struct TestEnv { /// Normalize each line by sorting the whitespace-separated words normalize_line: bool, + + /// Temporary directory for storing test config (global ignore file) + config_dir: Option, } /// Create the working directory and the test files. @@ -59,6 +62,16 @@ fn create_working_directory( Ok(temp_dir) } +fn create_config_directory_with_global_ignore(ignore_file_content: &str) -> io::Result { + 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) +} + /// Find the *fd* executable. fn find_fd_exe() -> PathBuf { // Tests exe is in target/debug/deps, the *fd* exe is in target/debug @@ -150,6 +163,7 @@ impl TestEnv { temp_dir, fd_exe, normalize_line: false, + config_dir: None, } } @@ -158,6 +172,16 @@ impl TestEnv { temp_dir: self.temp_dir, fd_exe: self.fd_exe, normalize_line: normalize, + 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 } } @@ -206,13 +230,8 @@ impl TestEnv { path: P, 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)); - cmd.arg("--no-global-ignore-file").args(args); - // Run *fd*. - let output = cmd.output().expect("fd output"); + let output = self.run_command(path.as_ref(), args); // Check for exit status. if !output.status.success() { @@ -288,6 +307,21 @@ impl TestEnv { self.assert_error_subdirectory(".", args, Some(expected)) } + 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"); + } + cmd.args(args); + + // Run *fd*. + cmd.output().expect("fd output") + } + /// 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. fn assert_error_subdirectory>( @@ -296,13 +330,7 @@ impl TestEnv { args: &[&str], expected: Option<&str>, ) -> process::ExitStatus { - // Setup *fd* command. - let mut cmd = process::Command::new(&self.fd_exe); - cmd.current_dir(self.temp_dir.path().join(path)); - cmd.arg("--no-global-ignore-file").args(args); - - // Run *fd*. - let output = cmd.output().expect("fd output"); + let output = self.run_command(path.as_ref(), args); if let Some(expected) = expected { // Normalize both expected and actual output. diff --git a/tests/tests.rs b/tests/tests.rs index 5036afd..98fe9c8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -879,6 +879,47 @@ fn test_no_ignore_aliases() { ); } +#[cfg(not(windows))] +#[test] +fn test_global_ignore() { + let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one"); + te.assert_output( + &[], + "a.foo + e1 e2 + symlink", + ); +} + +#[cfg(not(windows))] +#[test_case("--unrestricted", ".hidden.foo +a.foo +fdignored.foo +gitignored.foo +one/b.foo +one/two/c.foo +one/two/C.Foo2 +one/two/three/d.foo +one/two/three/directory_foo/"; "unrestricted")] +#[test_case("--no-ignore", "a.foo +fdignored.foo +gitignored.foo +one/b.foo +one/two/c.foo +one/two/C.Foo2 +one/two/three/d.foo +one/two/three/directory_foo/"; "no-ignore")] +#[test_case("--no-global-ignore-file", "a.foo +one/b.foo +one/two/c.foo +one/two/C.Foo2 +one/two/three/d.foo +one/two/three/directory_foo/"; "no-global-ignore-file")] +fn test_no_global_ignore(flag: &str, expected_output: &str) { + let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one"); + te.assert_output(&[flag, "foo"], expected_output); +} + /// Symlinks (--follow) #[test] fn test_follow() {