Merge pull request #1212 from sharkdp/global-ignore-test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add tests for global ignore file
authored by Thayne McCombs · committed by GitHub · parents ab7d5ef, b6c7ebc · Browse files · 2 files·+82 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+82 added−13 removed
- tests:+82 added−13 removed
Provenance
- Commit
503ede7- Tree
ad4bcb3- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
ab7d5ef- Parent 2
b6c7ebc
2 files changed+82 added−13 removed
modified:tests/testenv/mod.rs+41 added-13 removed
18 unmodified lines19 fd_exe: PathBuf,20 21 /// Normalize each line by sorting the whitespace-separated words22 normalize_line: bool,23+24+ /// Temporary directory for storing test config (global ignore file)25+ config_dir: Option<TempDir>,26 }27 28 /// Create the working directory and the test files.29 fn create_working_directory(31 unmodified lines61 62 Ok(temp_dir)63 }64 65+fn create_config_directory_with_global_ignore(ignore_file_content: &str) -> io::Result<TempDir> {66+ let config_dir = tempfile::Builder::new().prefix("fd-config").tempdir()?;67+ let fd_dir = config_dir.path().join("fd");68+ fs::create_dir(&fd_dir)?;69+ let mut ignore_file = fs::File::create(fd_dir.join("ignore"))?;70+ ignore_file.write_all(ignore_file_content.as_bytes())?;71+72+ Ok(config_dir)73+}74+75 /// Find the *fd* executable.76 fn find_fd_exe() -> PathBuf {77 // Tests exe is in target/debug/deps, the *fd* exe is in target/debug78 let root = env::current_exe()83 unmodified lines162 TestEnv {163 temp_dir,164 fd_exe,165 normalize_line: false,166+ config_dir: None,167 }168 }169 170 pub fn normalize_line(self, normalize: bool) -> TestEnv {171 TestEnv {172 temp_dir: self.temp_dir,173 fd_exe: self.fd_exe,174 normalize_line: normalize,175+ config_dir: self.config_dir,176 }177 }178 179+ pub fn global_ignore_file(self, content: &str) -> TestEnv {180+ let config_dir =181+ create_config_directory_with_global_ignore(content).expect("config directory");182+ TestEnv {183+ config_dir: Some(config_dir),184+ ..self185+ }186+ }187+188 /// Create a broken symlink at the given path in the temp_dir.189 pub fn create_broken_symlink<P: AsRef<Path>>(190 &mut self,191 link_path: P,37 unmodified lines229 &self,230 path: P,231 args: &[&str],232 ) -> process::Output {209- // Setup *fd* command.210- let mut cmd = process::Command::new(&self.fd_exe);211- cmd.current_dir(self.temp_dir.path().join(path));212- cmd.arg("--no-global-ignore-file").args(args);213-233 // Run *fd*.215- let output = cmd.output().expect("fd output");234+ let output = self.run_command(path.as_ref(), args);235 236 // Check for exit status.237 if !output.status.success() {238 panic!("{}", format_exit_error(args, &output));67 unmodified lines306 pub fn assert_error(&self, args: &[&str], expected: &str) -> process::ExitStatus {307 self.assert_error_subdirectory(".", args, Some(expected))308 }309 310+ fn run_command(&self, path: &Path, args: &[&str]) -> process::Output {311+ // Setup *fd* command.312+ let mut cmd = process::Command::new(&self.fd_exe);313+ cmd.current_dir(self.temp_dir.path().join(path));314+ if let Some(config_dir) = &self.config_dir {315+ cmd.env("XDG_CONFIG_HOME", config_dir.path());316+ } else {317+ cmd.arg("--no-global-ignore-file");318+ }319+ cmd.args(args);320+321+ // Run *fd*.322+ cmd.output().expect("fd output")323+ }324+325 /// Assert that calling *fd* in the specified path under the root working directory,326 /// and with the specified arguments produces an error with the expected message.327 fn assert_error_subdirectory<P: AsRef<Path>>(328 &self,329 path: P,330 args: &[&str],331 expected: Option<&str>,332 ) -> process::ExitStatus {299- // Setup *fd* command.300- let mut cmd = process::Command::new(&self.fd_exe);301- cmd.current_dir(self.temp_dir.path().join(path));302- cmd.arg("--no-global-ignore-file").args(args);333+ let output = self.run_command(path.as_ref(), args);334 304- // Run *fd*.305- let output = cmd.output().expect("fd output");306-335 if let Some(expected) = expected {336 // Normalize both expected and actual output.337 let expected_error = trim_lines(expected);338 let actual_err = trim_lines(&String::from_utf8_lossy(&output.stderr));13 unmodified linesmodified:tests/tests.rs+41 added
877 unmodified lines878 one/two/three/directory_foo/",879 );880 }881 882+#[cfg(not(windows))]883+#[test]884+fn test_global_ignore() {885+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");886+ te.assert_output(887+ &[],888+ "a.foo889+ e1 e2890+ symlink",891+ );892+}893+894+#[cfg(not(windows))]895+#[test_case("--unrestricted", ".hidden.foo896+a.foo897+fdignored.foo898+gitignored.foo899+one/b.foo900+one/two/c.foo901+one/two/C.Foo2902+one/two/three/d.foo903+one/two/three/directory_foo/"; "unrestricted")]904+#[test_case("--no-ignore", "a.foo905+fdignored.foo906+gitignored.foo907+one/b.foo908+one/two/c.foo909+one/two/C.Foo2910+one/two/three/d.foo911+one/two/three/directory_foo/"; "no-ignore")]912+#[test_case("--no-global-ignore-file", "a.foo913+one/b.foo914+one/two/c.foo915+one/two/C.Foo2916+one/two/three/d.foo917+one/two/three/directory_foo/"; "no-global-ignore-file")]918+fn test_no_global_ignore(flag: &str, expected_output: &str) {919+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");920+ te.assert_output(&[flag, "foo"], expected_output);921+}922+923 /// Symlinks (--follow)924 #[test]925 fn test_follow() {926 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1560 unmodified lines