test: fix Command::current_dir API

Every single call site wants to pass a path relative to the directory
the command was created for. So just make it do that automatically,
similar to `Dir::create` and friends.
This commit is contained in:
Andrew Gallant
2025-10-10 21:10:49 -04:00
parent 293ef80eaf
commit 924ba101ee
4 changed files with 24 additions and 25 deletions

View File

@@ -154,7 +154,7 @@ test
// parent ignore files and manually specified ignore files. // parent ignore files and manually specified ignore files.
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.args(&["--ignore-file", "../.not-an-ignore", "-l", "test"]); cmd.args(&["--ignore-file", "../.not-an-ignore", "-l", "test"]);
cmd.current_dir(dir.path().join("baz")); cmd.current_dir("baz");
let expected = " let expected = "
baz/bar/test baz/bar/test
test test

View File

@@ -627,7 +627,7 @@ rgtest!(ignore_git_parent, |dir: Dir, mut cmd: TestCommand| {
// Even though we search in foo/, which has no .gitignore, ripgrep will // Even though we search in foo/, which has no .gitignore, ripgrep will
// traverse parent directories and respect the gitignore files found. // traverse parent directories and respect the gitignore files found.
cmd.current_dir(dir.path().join("foo")); cmd.current_dir("foo");
cmd.assert_err(); cmd.assert_err();
}); });
@@ -651,7 +651,7 @@ rgtest!(ignore_git_parent_stop, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir("foo/bar"); dir.create_dir("foo/bar");
dir.create("foo/bar/sherlock", SHERLOCK); dir.create("foo/bar/sherlock", SHERLOCK);
cmd.arg("Sherlock"); cmd.arg("Sherlock");
cmd.current_dir(dir.path().join("foo").join("bar")); cmd.current_dir("foo/bar");
let expected = "\ let expected = "\
sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock
@@ -682,7 +682,7 @@ rgtest!(ignore_git_parent_stop_file, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir("foo/bar"); dir.create_dir("foo/bar");
dir.create("foo/bar/sherlock", SHERLOCK); dir.create("foo/bar/sherlock", SHERLOCK);
cmd.arg("Sherlock"); cmd.arg("Sherlock");
cmd.current_dir(dir.path().join("foo").join("bar")); cmd.current_dir("foo/bar");
let expected = "\ let expected = "\
sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock
@@ -700,7 +700,7 @@ rgtest!(ignore_ripgrep_parent_no_stop, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir("foo/bar"); dir.create_dir("foo/bar");
dir.create("foo/bar/sherlock", SHERLOCK); dir.create("foo/bar/sherlock", SHERLOCK);
cmd.arg("Sherlock"); cmd.arg("Sherlock");
cmd.current_dir(dir.path().join("foo").join("bar")); cmd.current_dir("foo/bar");
// The top-level .rgignore applies. // The top-level .rgignore applies.
cmd.assert_err(); cmd.assert_err();
@@ -733,7 +733,7 @@ rgtest!(no_parent_ignore_git, |dir: Dir, mut cmd: TestCommand| {
dir.create("foo/sherlock", SHERLOCK); dir.create("foo/sherlock", SHERLOCK);
dir.create("foo/watson", SHERLOCK); dir.create("foo/watson", SHERLOCK);
cmd.arg("--no-ignore-parent").arg("Sherlock"); cmd.arg("--no-ignore-parent").arg("Sherlock");
cmd.current_dir(dir.path().join("foo")); cmd.current_dir("foo");
let expected = "\ let expected = "\
sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock
@@ -749,7 +749,7 @@ rgtest!(symlink_nofollow, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir("foo/baz"); dir.create_dir("foo/baz");
dir.create("foo/baz/sherlock", SHERLOCK); dir.create("foo/baz/sherlock", SHERLOCK);
cmd.arg("Sherlock"); cmd.arg("Sherlock");
cmd.current_dir(dir.path().join("foo/bar")); cmd.current_dir("foo/bar");
cmd.assert_err(); cmd.assert_err();
}); });
@@ -762,7 +762,7 @@ rgtest!(symlink_follow, |dir: Dir, mut cmd: TestCommand| {
dir.create("foo/baz/sherlock", SHERLOCK); dir.create("foo/baz/sherlock", SHERLOCK);
dir.link_dir("foo/baz", "foo/bar/baz"); dir.link_dir("foo/baz", "foo/bar/baz");
cmd.arg("-L").arg("Sherlock"); cmd.arg("-L").arg("Sherlock");
cmd.current_dir(dir.path().join("foo/bar")); cmd.current_dir("foo/bar");
let expected = "\ let expected = "\
baz/sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock baz/sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock

View File

@@ -23,7 +23,7 @@ rgtest!(r25, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("test"); cmd.arg("test");
eqnice!("src/llvm/foo:test\n", cmd.stdout()); eqnice!("src/llvm/foo:test\n", cmd.stdout());
cmd.current_dir(dir.path().join("src")); cmd.current_dir("src");
eqnice!("llvm/foo:test\n", cmd.stdout()); eqnice!("llvm/foo:test\n", cmd.stdout());
}); });
@@ -244,7 +244,7 @@ rgtest!(r184, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("test"); cmd.arg("test");
eqnice!("foo/bar/baz:test\n", cmd.stdout()); eqnice!("foo/bar/baz:test\n", cmd.stdout());
cmd.current_dir(dir.path().join("./foo/bar")); cmd.current_dir("./foo/bar");
eqnice!("baz:test\n", cmd.stdout()); eqnice!("baz:test\n", cmd.stdout());
}); });
@@ -612,17 +612,17 @@ rgtest!(r2711, |dir: Dir, _cmd: TestCommand| {
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("a")); cmd.current_dir("a");
eqnice!(".ignore\n", cmd.arg("--hidden").arg("--files").stdout()); eqnice!(".ignore\n", cmd.arg("--hidden").arg("--files").stdout());
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("a").join("b")); cmd.current_dir("a/b");
cmd.arg("--hidden").arg("--files").assert_err(); cmd.arg("--hidden").arg("--files").assert_err();
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("./a")); cmd.current_dir("./a");
eqnice!(".ignore\n", cmd.arg("--hidden").arg("--files").stdout()); eqnice!(".ignore\n", cmd.arg("--hidden").arg("--files").stdout());
} }
}); });
@@ -643,7 +643,7 @@ rgtest!(r829_original, |dir: Dir, _cmd: TestCommand| {
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("a")); cmd.current_dir("a");
cmd.args(&["Sample"]).assert_err(); cmd.args(&["Sample"]).assert_err();
} }
}); });
@@ -706,7 +706,7 @@ rgtest!(r829_2747, |dir: Dir, _cmd: TestCommand| {
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("a/src")); cmd.current_dir("a/src");
eqnice!("f/b/foo\n", cmd.arg("--files").stdout()); eqnice!("f/b/foo\n", cmd.arg("--files").stdout());
} }
}); });
@@ -727,7 +727,7 @@ rgtest!(r829_2778, |dir: Dir, _cmd: TestCommand| {
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("parent")); cmd.current_dir("parent");
eqnice!("subdir/dont-ignore-me.txt\n", cmd.arg("--files").stdout()); eqnice!("subdir/dont-ignore-me.txt\n", cmd.arg("--files").stdout());
} }
}); });
@@ -744,7 +744,7 @@ rgtest!(r829_2836, |dir: Dir, _cmd: TestCommand| {
} }
{ {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("testdir")); cmd.current_dir("testdir");
cmd.arg("--files").assert_err(); cmd.arg("--files").assert_err();
} }
}); });
@@ -756,7 +756,7 @@ rgtest!(r829_2933, |dir: Dir, mut cmd: TestCommand| {
dir.create("testdir/sub/sub2/testfile", "needle"); dir.create("testdir/sub/sub2/testfile", "needle");
let args = &["--files-with-matches", "needle"]; let args = &["--files-with-matches", "needle"];
cmd.current_dir(dir.path().join("testdir")); cmd.current_dir("testdir");
cmd.args(args).assert_err(); cmd.args(args).assert_err();
}); });
@@ -1463,11 +1463,7 @@ rgtest!(r2770_gitignore_error, |dir: Dir, _cmd: TestCommand| {
dir.create("foo/bar/baz", "quux"); dir.create("foo/bar/baz", "quux");
dir.command().arg("-l").arg("quux").assert_err(); dir.command().arg("-l").arg("quux").assert_err();
dir.command() dir.command().current_dir("foo").arg("-l").arg("quux").assert_err();
.current_dir(dir.path().join("foo"))
.arg("-l")
.arg("quux")
.assert_err();
}); });
// See: https://github.com/BurntSushi/ripgrep/pull/2944 // See: https://github.com/BurntSushi/ripgrep/pull/2944
@@ -1627,7 +1623,7 @@ rgtest!(r3173_hidden_whitelist_only_dot, |dir: Dir, _: TestCommand| {
let cmd = || { let cmd = || {
let mut cmd = dir.command(); let mut cmd = dir.command();
cmd.current_dir(dir.path().join("subdir")); cmd.current_dir("subdir");
cmd cmd
}; };
eqnice!(cmd().args(&["--files"]).stdout(), ".foo.txt\n"); eqnice!(cmd().args(&["--files"]).stdout(), ".foo.txt\n");

View File

@@ -273,11 +273,14 @@ impl TestCommand {
/// Set the working directory for this command. /// Set the working directory for this command.
/// ///
/// The path given is interpreted relative to the directory that this
/// command was created for.
///
/// Note that this does not need to be called normally, since the creation /// Note that this does not need to be called normally, since the creation
/// of this TestCommand causes its working directory to be set to the /// of this TestCommand causes its working directory to be set to the
/// test's directory automatically. /// test's directory automatically.
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut TestCommand { pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut TestCommand {
self.cmd.current_dir(dir); self.cmd.current_dir(self.dir.path().join(dir));
self self
} }