Add tests for global ignore file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by Thayne McCombs · parent 7c86c7d · Browse files · 2 files·+80 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+80 added−13 removed
- tests:+80 added−13 removed
Provenance
- Commit
fd707b4- Tree
be2a4be- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
Thayne McCombs <astrothayne@gmail.com>- Parent
7c86c7d
2 files changed+80 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+39 added
877 unmodified lines878 one/two/three/directory_foo/",879 );880 }881 882+#[test]883+fn test_global_ignore() {884+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");885+ te.assert_output(886+ &[],887+ "a.foo888+ e1 e2889+ symlink",890+ );891+}892+893+#[test_case("--unrestricted", ".hidden.foo894+a.foo895+fdignored.foo896+gitignored.foo897+one/b.foo898+one/two/c.foo899+one/two/C.Foo2900+one/two/three/d.foo901+one/two/three/directory_foo/"; "unrestricted")]902+#[test_case("--no-ignore", "a.foo903+fdignored.foo904+gitignored.foo905+one/b.foo906+one/two/c.foo907+one/two/C.Foo2908+one/two/three/d.foo909+one/two/three/directory_foo/"; "no-ignore")]910+#[test_case("--no-global-ignore-file", "a.foo911+one/b.foo912+one/two/c.foo913+one/two/C.Foo2914+one/two/three/d.foo915+one/two/three/directory_foo/"; "no-global-ignore-file")]916+fn test_no_global_ignore(flag: &str, expected_output: &str) {917+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");918+ te.assert_output(&[flag, "foo"], expected_output);919+}920+921 /// Symlinks (--follow)922 #[test]923 fn test_follow() {924 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1553 unmodified lines