2016-09-10 04:58:30 +02:00
|
|
|
use std::env;
|
|
|
|
use std::error;
|
2018-08-07 02:11:58 +02:00
|
|
|
use std::ffi::OsStr;
|
2016-09-10 04:58:30 +02:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
2018-08-07 02:11:58 +02:00
|
|
|
use std::process::{self, Command};
|
2019-02-09 23:27:25 +02:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2016-09-10 04:58:30 +02:00
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
static TEST_DIR: &'static str = "ripgrep-tests";
|
2019-02-09 23:27:25 +02:00
|
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
2016-09-10 04:58:30 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Setup an empty work directory and return a command pointing to the ripgrep
|
|
|
|
/// executable whose CWD is set to the work directory.
|
|
|
|
///
|
|
|
|
/// The name given will be used to create the directory. Generally, it should
|
|
|
|
/// correspond to the test name.
|
|
|
|
pub fn setup(test_name: &str) -> (Dir, TestCommand) {
|
|
|
|
let dir = Dir::new(test_name);
|
|
|
|
let cmd = dir.command();
|
|
|
|
(dir, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `setup`, but uses PCRE2 as the underlying regex engine.
|
|
|
|
pub fn setup_pcre2(test_name: &str) -> (Dir, TestCommand) {
|
|
|
|
let mut dir = Dir::new(test_name);
|
|
|
|
dir.pcre2(true);
|
|
|
|
let cmd = dir.command();
|
|
|
|
(dir, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Break the given string into lines, sort them and then join them back
|
|
|
|
/// together. This is useful for testing output from ripgrep that may not
|
|
|
|
/// always be in the same order.
|
|
|
|
pub fn sort_lines(lines: &str) -> String {
|
|
|
|
let mut lines: Vec<&str> = lines.trim().lines().collect();
|
|
|
|
lines.sort();
|
|
|
|
format!("{}\n", lines.join("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if the given program can be successfully executed
|
|
|
|
/// with a `--help` flag.
|
|
|
|
pub fn cmd_exists(program: &str) -> bool {
|
|
|
|
Command::new(program).arg("--help").output().is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dir represents a directory in which tests should be run.
|
2016-09-10 04:58:30 +02:00
|
|
|
///
|
|
|
|
/// Directories are created from a global atomic counter to avoid duplicates.
|
2018-08-07 02:11:58 +02:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Dir {
|
2016-09-10 04:58:30 +02:00
|
|
|
/// The directory in which this test executable is running.
|
|
|
|
root: PathBuf,
|
|
|
|
/// The directory in which the test should run. If a test needs to create
|
2018-07-29 16:15:20 +02:00
|
|
|
/// files, they should go in here. This directory is also used as the CWD
|
|
|
|
/// for any processes created by the test.
|
2016-09-10 04:58:30 +02:00
|
|
|
dir: PathBuf,
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Set to true when the test should use PCRE2 as the regex engine.
|
|
|
|
pcre2: bool,
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
impl Dir {
|
2016-09-10 04:58:30 +02:00
|
|
|
/// Create a new test working directory with the given name. The name
|
|
|
|
/// does not need to be distinct for each invocation, but should correspond
|
|
|
|
/// to a logical grouping of tests.
|
2018-08-07 02:11:58 +02:00
|
|
|
pub fn new(name: &str) -> Dir {
|
2016-09-10 04:58:30 +02:00
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
2018-07-29 16:15:20 +02:00
|
|
|
let root = env::current_exe()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.expect("executable's directory")
|
|
|
|
.to_path_buf();
|
2020-02-18 01:08:47 +02:00
|
|
|
let dir =
|
|
|
|
env::temp_dir().join(TEST_DIR).join(name).join(&format!("{}", id));
|
2020-02-15 16:12:21 +02:00
|
|
|
if dir.exists() {
|
|
|
|
nice_err(&dir, fs::remove_dir_all(&dir));
|
|
|
|
}
|
2016-09-10 04:58:30 +02:00
|
|
|
nice_err(&dir, repeat(|| fs::create_dir_all(&dir)));
|
2020-02-18 01:08:47 +02:00
|
|
|
Dir { root: root, dir: dir, pcre2: false }
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Use PCRE2 for this test.
|
|
|
|
pub fn pcre2(&mut self, yes: bool) {
|
|
|
|
self.pcre2 = yes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if this test is configured to use PCRE2 as
|
|
|
|
/// the regex engine.
|
|
|
|
pub fn is_pcre2(&self) -> bool {
|
|
|
|
self.pcre2
|
|
|
|
}
|
|
|
|
|
2017-10-16 14:27:15 +02:00
|
|
|
/// Create a new file with the given name and contents in this directory,
|
|
|
|
/// or panic on error.
|
2016-09-10 04:58:30 +02:00
|
|
|
pub fn create<P: AsRef<Path>>(&self, name: P, contents: &str) {
|
2016-09-23 04:59:25 +02:00
|
|
|
self.create_bytes(name, contents.as_bytes());
|
|
|
|
}
|
|
|
|
|
2017-10-16 14:27:15 +02:00
|
|
|
/// Try to create a new file with the given name and contents in this
|
|
|
|
/// directory.
|
2018-08-22 02:26:33 +02:00
|
|
|
#[allow(dead_code)] // unused on Windows
|
2018-07-29 15:40:38 +02:00
|
|
|
pub fn try_create<P: AsRef<Path>>(
|
|
|
|
&self,
|
|
|
|
name: P,
|
|
|
|
contents: &str,
|
|
|
|
) -> io::Result<()> {
|
2017-10-16 15:29:55 +02:00
|
|
|
let path = self.dir.join(name);
|
|
|
|
self.try_create_bytes(path, contents.as_bytes())
|
2017-10-16 11:45:20 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 07:38:06 +02:00
|
|
|
/// Create a new file with the given name and size.
|
|
|
|
pub fn create_size<P: AsRef<Path>>(&self, name: P, filesize: u64) {
|
|
|
|
let path = self.dir.join(name);
|
|
|
|
let file = nice_err(&path, File::create(&path));
|
|
|
|
nice_err(&path, file.set_len(filesize));
|
|
|
|
}
|
|
|
|
|
2017-10-16 14:27:15 +02:00
|
|
|
/// Create a new file with the given name and contents in this directory,
|
|
|
|
/// or panic on error.
|
2016-09-23 04:59:25 +02:00
|
|
|
pub fn create_bytes<P: AsRef<Path>>(&self, name: P, contents: &[u8]) {
|
2018-08-07 02:11:58 +02:00
|
|
|
let path = self.dir.join(&name);
|
|
|
|
nice_err(&path, self.try_create_bytes(name, contents));
|
2017-10-16 11:45:20 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 14:27:15 +02:00
|
|
|
/// Try to create a new file with the given name and contents in this
|
|
|
|
/// directory.
|
2018-08-07 02:11:58 +02:00
|
|
|
pub fn try_create_bytes<P: AsRef<Path>>(
|
2018-07-29 15:40:38 +02:00
|
|
|
&self,
|
2018-08-07 02:11:58 +02:00
|
|
|
name: P,
|
2018-07-29 15:40:38 +02:00
|
|
|
contents: &[u8],
|
|
|
|
) -> io::Result<()> {
|
2018-08-07 02:11:58 +02:00
|
|
|
let path = self.dir.join(name);
|
|
|
|
let mut file = File::create(path)?;
|
2017-10-16 11:45:20 +02:00
|
|
|
file.write_all(contents)?;
|
|
|
|
file.flush()
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a file with the given name from this directory.
|
|
|
|
pub fn remove<P: AsRef<Path>>(&self, name: P) {
|
|
|
|
let path = self.dir.join(name);
|
|
|
|
nice_err(&path, fs::remove_file(&path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new directory with the given path (and any directories above
|
|
|
|
/// it) inside this directory.
|
|
|
|
pub fn create_dir<P: AsRef<Path>>(&self, path: P) {
|
|
|
|
let path = self.dir.join(path);
|
|
|
|
nice_err(&path, repeat(|| fs::create_dir_all(&path)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new command that is set to use the ripgrep executable in
|
|
|
|
/// this working directory.
|
2018-08-07 02:11:58 +02:00
|
|
|
///
|
|
|
|
/// This also:
|
|
|
|
///
|
|
|
|
/// * Unsets the `RIPGREP_CONFIG_PATH` environment variable.
|
|
|
|
/// * Sets the `--path-separator` to `/` so that paths have the same output
|
|
|
|
/// on all systems. Tests that need to check `--path-separator` itself
|
|
|
|
/// can simply pass it again to override it.
|
|
|
|
pub fn command(&self) -> TestCommand {
|
2016-09-10 04:58:30 +02:00
|
|
|
let mut cmd = process::Command::new(&self.bin());
|
2018-02-04 03:33:52 +02:00
|
|
|
cmd.env_remove("RIPGREP_CONFIG_PATH");
|
2016-09-10 04:58:30 +02:00
|
|
|
cmd.current_dir(&self.dir);
|
2018-08-07 02:11:58 +02:00
|
|
|
cmd.arg("--path-separator").arg("/");
|
|
|
|
if self.is_pcre2() {
|
|
|
|
cmd.arg("--pcre2");
|
|
|
|
}
|
|
|
|
TestCommand { dir: self.clone(), cmd: cmd }
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the ripgrep executable.
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
pub fn bin(&self) -> PathBuf {
|
2018-07-29 16:15:20 +02:00
|
|
|
if cfg!(windows) {
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
self.root.join("../rg.exe")
|
|
|
|
} else {
|
2018-07-29 16:15:20 +02:00
|
|
|
self.root.join("../rg")
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to this directory.
|
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
&self.dir
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a directory symlink to the src with the given target name
|
|
|
|
/// in this directory.
|
|
|
|
#[cfg(not(windows))]
|
2016-10-11 01:34:57 +02:00
|
|
|
pub fn link_dir<S: AsRef<Path>, T: AsRef<Path>>(&self, src: S, target: T) {
|
2016-09-10 04:58:30 +02:00
|
|
|
use std::os::unix::fs::symlink;
|
|
|
|
let src = self.dir.join(src);
|
|
|
|
let target = self.dir.join(target);
|
|
|
|
let _ = fs::remove_file(&target);
|
|
|
|
nice_err(&target, symlink(&src, &target));
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:34:57 +02:00
|
|
|
/// Creates a directory symlink to the src with the given target name
|
|
|
|
/// in this directory.
|
2016-09-10 04:58:30 +02:00
|
|
|
#[cfg(windows)]
|
2016-10-11 01:34:57 +02:00
|
|
|
pub fn link_dir<S: AsRef<Path>, T: AsRef<Path>>(&self, src: S, target: T) {
|
2016-09-10 04:58:30 +02:00
|
|
|
use std::os::windows::fs::symlink_dir;
|
|
|
|
let src = self.dir.join(src);
|
|
|
|
let target = self.dir.join(target);
|
|
|
|
let _ = fs::remove_dir(&target);
|
|
|
|
nice_err(&target, symlink_dir(&src, &target));
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:34:57 +02:00
|
|
|
/// Creates a file symlink to the src with the given target name
|
|
|
|
/// in this directory.
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
pub fn link_file<S: AsRef<Path>, T: AsRef<Path>>(
|
|
|
|
&self,
|
|
|
|
src: S,
|
|
|
|
target: T,
|
|
|
|
) {
|
|
|
|
self.link_dir(src, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a file symlink to the src with the given target name
|
|
|
|
/// in this directory.
|
|
|
|
#[cfg(windows)]
|
2018-08-22 02:26:33 +02:00
|
|
|
#[allow(dead_code)] // unused on Windows
|
2016-10-11 01:34:57 +02:00
|
|
|
pub fn link_file<S: AsRef<Path>, T: AsRef<Path>>(
|
|
|
|
&self,
|
|
|
|
src: S,
|
|
|
|
target: T,
|
|
|
|
) {
|
|
|
|
use std::os::windows::fs::symlink_file;
|
|
|
|
let src = self.dir.join(src);
|
|
|
|
let target = self.dir.join(target);
|
|
|
|
let _ = fs::remove_file(&target);
|
|
|
|
nice_err(&target, symlink_file(&src, &target));
|
|
|
|
}
|
2018-08-07 02:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A simple wrapper around a process::Command with some conveniences.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TestCommand {
|
|
|
|
/// The dir used to launched this command.
|
|
|
|
dir: Dir,
|
|
|
|
/// The actual command we use to control the process.
|
|
|
|
cmd: Command,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCommand {
|
|
|
|
/// Returns a mutable reference to the underlying command.
|
|
|
|
pub fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
2016-10-11 01:34:57 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Add an argument to pass to the command.
|
|
|
|
pub fn arg<A: AsRef<OsStr>>(&mut self, arg: A) -> &mut TestCommand {
|
|
|
|
self.cmd.arg(arg);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add any number of arguments to the command.
|
2020-02-18 01:08:47 +02:00
|
|
|
pub fn args<I, A>(&mut self, args: I) -> &mut TestCommand
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = A>,
|
|
|
|
A: AsRef<OsStr>,
|
2018-08-07 02:11:58 +02:00
|
|
|
{
|
|
|
|
self.cmd.args(args);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the working directory for this command.
|
2016-09-10 04:58:30 +02:00
|
|
|
///
|
2018-08-07 02:11:58 +02:00
|
|
|
/// 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
|
|
|
|
/// test's directory automatically.
|
|
|
|
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut TestCommand {
|
|
|
|
self.cmd.current_dir(dir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs and captures the stdout of the given command.
|
|
|
|
pub fn stdout(&mut self) -> String {
|
|
|
|
let o = self.output();
|
2016-09-10 04:58:30 +02:00
|
|
|
let stdout = String::from_utf8_lossy(&o.stdout);
|
|
|
|
match stdout.parse() {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(err) => {
|
2018-07-29 15:40:38 +02:00
|
|
|
panic!(
|
|
|
|
"could not convert from string: {:?}\n\n{}",
|
2020-02-18 01:08:47 +02:00
|
|
|
err, stdout
|
2018-07-29 15:40:38 +02:00
|
|
|
);
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:07:53 +02:00
|
|
|
/// Pipe `input` to a command, and collect the output.
|
binary: rejigger ripgrep's handling of binary files
This commit attempts to surface binary filtering in a slightly more
user friendly way. Namely, before, ripgrep would silently stop
searching a file if it detected a NUL byte, even if it had previously
printed a match. This can lead to the user quite reasonably assuming
that there are no more matches, since a partial search is fairly
unintuitive. (ripgrep has this behavior by default because it really
wants to NOT search binary files at all, just like it doesn't search
gitignored or hidden files.)
With this commit, if a match has already been printed and ripgrep detects
a NUL byte, then it will print a warning message indicating that the search
stopped prematurely.
Moreover, this commit adds a new flag, --binary, which causes ripgrep to
stop filtering binary files, but in a way that still avoids dumping
binary data into terminals. That is, the --binary flag makes ripgrep
behave more like grep's default behavior.
For files explicitly specified in a search, e.g., `rg foo some-file`,
then no binary filtering is applied (just like no gitignore and no
hidden file filtering is applied). Instead, ripgrep behaves as if you
gave the --binary flag for all explicitly given files.
This was a fairly invasive change, and potentially increases the UX
complexity of ripgrep around binary files. (Before, there were two
binary modes, where as now there are three.) However, ripgrep is now a
bit louder with warning messages when binary file detection might
otherwise be hiding potential matches, so hopefully this is a net
improvement.
Finally, the `-uuu` convenience now maps to `--no-ignore --hidden
--binary`, since this is closer to the actualy intent of the
`--unrestricted` flag, i.e., to reduce ripgrep's smart filtering. As a
consequence, `rg -uuu foo` should now search roughly the same number of
bytes as `grep -r foo`, and `rg -uuua foo` should search roughly the
same number of bytes as `grep -ra foo`. (The "roughly" weasel word is
used because grep's and ripgrep's binary file detection might differ
somewhat---perhaps based on buffer sizes---which can impact exactly what
is and isn't searched.)
See the numerous tests in tests/binary.rs for intended behavior.
Fixes #306, Fixes #855
2019-04-09 01:28:38 +02:00
|
|
|
pub fn pipe(&mut self, input: &[u8]) -> String {
|
2018-08-07 02:11:58 +02:00
|
|
|
self.cmd.stdin(process::Stdio::piped());
|
|
|
|
self.cmd.stdout(process::Stdio::piped());
|
|
|
|
self.cmd.stderr(process::Stdio::piped());
|
2016-11-09 13:07:53 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
let mut child = self.cmd.spawn().unwrap();
|
2016-11-09 13:07:53 +02:00
|
|
|
|
|
|
|
// Pipe input to child process using a separate thread to avoid
|
|
|
|
// risk of deadlock between parent and child process.
|
|
|
|
let mut stdin = child.stdin.take().expect("expected standard input");
|
|
|
|
let input = input.to_owned();
|
2020-02-18 01:08:47 +02:00
|
|
|
let worker = thread::spawn(move || stdin.write_all(&input));
|
2016-11-09 13:07:53 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
let output = self.expect_success(child.wait_with_output().unwrap());
|
2016-11-09 13:07:53 +02:00
|
|
|
worker.join().unwrap().unwrap();
|
2016-09-10 04:58:30 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
|
|
match stdout.parse() {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(err) => {
|
|
|
|
panic!(
|
|
|
|
"could not convert from string: {:?}\n\n{}",
|
2020-02-18 01:08:47 +02:00
|
|
|
err, stdout
|
2018-08-07 02:11:58 +02:00
|
|
|
);
|
|
|
|
}
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Gets the output of a command. If the command failed, then this panics.
|
|
|
|
pub fn output(&mut self) -> process::Output {
|
|
|
|
let output = self.cmd.output().unwrap();
|
|
|
|
self.expect_success(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the command and asserts that it resulted in an error exit code.
|
|
|
|
pub fn assert_err(&mut self) {
|
|
|
|
let o = self.cmd.output().unwrap();
|
2016-09-10 04:58:30 +02:00
|
|
|
if o.status.success() {
|
2018-06-19 13:41:44 +02:00
|
|
|
panic!(
|
|
|
|
"\n\n===== {:?} =====\n\
|
|
|
|
command succeeded but expected failure!\
|
|
|
|
\n\ncwd: {}\
|
|
|
|
\n\nstatus: {}\
|
|
|
|
\n\nstdout: {}\n\nstderr: {}\
|
|
|
|
\n\n=====\n",
|
2018-08-07 02:11:58 +02:00
|
|
|
self.cmd,
|
|
|
|
self.dir.dir.display(),
|
2018-06-19 13:41:44 +02:00
|
|
|
o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr)
|
|
|
|
);
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 23:21:07 +02:00
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Runs the command and asserts that its exit code matches expected exit
|
|
|
|
/// code.
|
|
|
|
pub fn assert_exit_code(&mut self, expected_code: i32) {
|
|
|
|
let code = self.cmd.output().unwrap().status.code().unwrap();
|
2018-06-19 13:41:44 +02:00
|
|
|
assert_eq!(
|
|
|
|
expected_code, code,
|
|
|
|
"\n\n===== {:?} =====\n\
|
|
|
|
expected exit code did not match\
|
|
|
|
\n\nexpected: {}\
|
|
|
|
\n\nfound: {}\
|
|
|
|
\n\n=====\n",
|
2020-02-18 01:08:47 +02:00
|
|
|
self.cmd, expected_code, code
|
2018-06-19 13:41:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-07 02:11:58 +02:00
|
|
|
/// Runs the command and asserts that something was printed to stderr.
|
|
|
|
pub fn assert_non_empty_stderr(&mut self) {
|
|
|
|
let o = self.cmd.output().unwrap();
|
2017-04-12 23:21:07 +02:00
|
|
|
if o.status.success() || o.stderr.is_empty() {
|
2018-08-07 02:11:58 +02:00
|
|
|
panic!(
|
|
|
|
"\n\n===== {:?} =====\n\
|
|
|
|
command succeeded but expected failure!\
|
|
|
|
\n\ncwd: {}\
|
|
|
|
\n\nstatus: {}\
|
|
|
|
\n\nstdout: {}\n\nstderr: {}\
|
|
|
|
\n\n=====\n",
|
|
|
|
self.cmd,
|
|
|
|
self.dir.dir.display(),
|
|
|
|
o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expect_success(&self, o: process::Output) -> process::Output {
|
|
|
|
if !o.status.success() {
|
2020-02-18 01:08:47 +02:00
|
|
|
let suggest = if o.stderr.is_empty() {
|
|
|
|
"\n\nDid your search end up with no results?".to_string()
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
panic!(
|
|
|
|
"\n\n==========\n\
|
2018-08-07 02:11:58 +02:00
|
|
|
command failed but expected success!\
|
|
|
|
{}\
|
|
|
|
\n\ncommand: {:?}\
|
|
|
|
\ncwd: {}\
|
2017-04-12 23:21:07 +02:00
|
|
|
\n\nstatus: {}\
|
2018-08-07 02:11:58 +02:00
|
|
|
\n\nstdout: {}\
|
|
|
|
\n\nstderr: {}\
|
|
|
|
\n\n==========\n",
|
2020-02-18 01:08:47 +02:00
|
|
|
suggest,
|
|
|
|
self.cmd,
|
|
|
|
self.dir.dir.display(),
|
|
|
|
o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr)
|
|
|
|
);
|
2017-04-12 23:21:07 +02:00
|
|
|
}
|
2018-08-07 02:11:58 +02:00
|
|
|
o
|
2017-04-12 23:21:07 +02:00
|
|
|
}
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 01:08:47 +02:00
|
|
|
fn nice_err<T, E: error::Error>(path: &Path, res: Result<T, E>) -> T {
|
2016-09-10 04:58:30 +02:00
|
|
|
match res {
|
|
|
|
Ok(t) => t,
|
2018-08-07 02:11:58 +02:00
|
|
|
Err(err) => panic!("{}: {:?}", path.display(), err),
|
2016-09-10 04:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn repeat<F: FnMut() -> io::Result<()>>(mut f: F) -> io::Result<()> {
|
|
|
|
let mut last_err = None;
|
|
|
|
for _ in 0..10 {
|
|
|
|
if let Err(err) = f() {
|
|
|
|
last_err = Some(err);
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
|
|
|
} else {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(last_err.unwrap())
|
|
|
|
}
|