Skip to content
Myr

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

2 files+82 added−13 removed

modified:tests/testenv/mod.rs+41 added-13 removed
18 unmodified lines
19 fd_exe: PathBuf,2021 /// Normalize each line by sorting the whitespace-separated words22 normalize_line: bool,2324 /// Temporary directory for storing test config (global ignore file)25 config_dir: Option<TempDir>,26}2728/// Create the working directory and the test files.29fn create_working_directory(
31 unmodified lines
6162 Ok(temp_dir)63}6465fn 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())?;7172 Ok(config_dir)73}7475/// Find the *fd* executable.76fn 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 lines
162 TestEnv {163 temp_dir,164 fd_exe,165 normalize_line: false,166 config_dir: None,167 }168 }169170 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 }178179 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 }187188 /// 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 lines
229 &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);213233 // Run *fd*.215 let output = cmd.output().expect("fd output");234 let output = self.run_command(path.as_ref(), args);235236 // Check for exit status.237 if !output.status.success() {238 panic!("{}", format_exit_error(args, &output));
67 unmodified lines
306 pub fn assert_error(&self, args: &[&str], expected: &str) -> process::ExitStatus {307 self.assert_error_subdirectory(".", args, Some(expected))308 }309310 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);320321 // Run *fd*.322 cmd.output().expect("fd output")323 }324325 /// 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);334304 // Run *fd*.305 let output = cmd.output().expect("fd output");306335 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 lines
modified:tests/tests.rs+41 added
877 unmodified lines
878 one/two/three/directory_foo/",879 );880}881882#[cfg(not(windows))]883#[test]884fn 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}893894#[cfg(not(windows))]895#[test_case("--unrestricted", ".hidden.foo896a.foo897fdignored.foo898gitignored.foo899one/b.foo900one/two/c.foo901one/two/C.Foo2902one/two/three/d.foo903one/two/three/directory_foo/"; "unrestricted")]904#[test_case("--no-ignore", "a.foo905fdignored.foo906gitignored.foo907one/b.foo908one/two/c.foo909one/two/C.Foo2910one/two/three/d.foo911one/two/three/directory_foo/"; "no-ignore")]912#[test_case("--no-global-ignore-file", "a.foo913one/b.foo914one/two/c.foo915one/two/C.Foo2916one/two/three/d.foo917one/two/three/directory_foo/"; "no-global-ignore-file")]918fn 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}922923/// Symlinks (--follow)924#[test]925fn test_follow() {926 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
1560 unmodified lines