From fd707b42c2eb9ebec1b0a9f3f6346e29cd3d2d26 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sun, 18 Dec 2022 00:42:54 -0700 Subject: [PATCH 1/2] Add tests for global ignore file --- tests/testenv/mod.rs | 54 +++++++++++++++++++++++++++++++++----------- tests/tests.rs | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 13 deletions(-) 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 b77eb65..e4c348b 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -879,6 +879,45 @@ fn test_no_ignore_aliases() { ); } +#[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", + ); +} + +#[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() { From b6c7ebc4f18755daeea41357ac933eac9be7d135 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sun, 18 Dec 2022 23:33:20 -0700 Subject: [PATCH 2/2] Don't test global ignore file on windows Becase XDG_CONFIG_HOME doesn't work there. --- tests/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/tests.rs b/tests/tests.rs index e4c348b..bb8d9aa 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -879,6 +879,7 @@ 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"); @@ -890,6 +891,7 @@ fn test_global_ignore() { ); } +#[cfg(not(windows))] #[test_case("--unrestricted", ".hidden.foo a.foo fdignored.foo