2016-09-09 22:58:30 -04:00
|
|
|
use std::env;
|
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::process;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
static TEST_DIR: &'static str = "ripgrep-tests";
|
|
|
|
static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
2017-12-30 22:06:16 +01:00
|
|
|
/// `WorkDir` represents a directory in which tests are run.
|
2016-09-09 22:58:30 -04:00
|
|
|
///
|
|
|
|
/// Directories are created from a global atomic counter to avoid duplicates.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WorkDir {
|
|
|
|
/// 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
|
|
|
|
/// files, they should go in here.
|
|
|
|
dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkDir {
|
|
|
|
/// 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.
|
|
|
|
pub fn new(name: &str) -> WorkDir {
|
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
let root = env::current_exe().unwrap()
|
|
|
|
.parent().expect("executable's directory").to_path_buf();
|
|
|
|
let dir = root.join(TEST_DIR).join(name).join(&format!("{}", id));
|
|
|
|
nice_err(&dir, repeat(|| fs::create_dir_all(&dir)));
|
|
|
|
WorkDir {
|
|
|
|
root: root,
|
|
|
|
dir: dir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09 22:58:30 -04: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.
|
2017-10-16 11:45:20 +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 18:38:06 +13: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]) {
|
2016-09-09 22:58:30 -04:00
|
|
|
let path = self.dir.join(name);
|
2017-10-16 11:45:20 +02:00
|
|
|
nice_err(&path, self.try_create_bytes(&path, contents));
|
|
|
|
}
|
|
|
|
|
2017-10-16 14:27:15 +02:00
|
|
|
/// Try to create a new file with the given name and contents in this
|
|
|
|
/// directory.
|
2017-10-16 11:45:20 +02:00
|
|
|
fn try_create_bytes<P: AsRef<Path>>(&self, path: P, contents: &[u8]) -> io::Result<()> {
|
|
|
|
let mut file = File::create(&path)?;
|
|
|
|
file.write_all(contents)?;
|
|
|
|
file.flush()
|
2016-09-09 22:58:30 -04: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.
|
|
|
|
pub fn command(&self) -> process::Command {
|
|
|
|
let mut cmd = process::Command::new(&self.bin());
|
2018-02-03 20:33:52 -05:00
|
|
|
cmd.env_remove("RIPGREP_CONFIG_PATH");
|
2016-09-09 22:58:30 -04:00
|
|
|
cmd.current_dir(&self.dir);
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-12 21:48:11 -05:00
|
|
|
#[cfg(not(windows))]
|
2016-09-09 22:58:30 -04:00
|
|
|
pub fn bin(&self) -> PathBuf {
|
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-12 21:48:11 -05:00
|
|
|
let path = self.root.join("rg");
|
|
|
|
if !path.is_file() {
|
|
|
|
// Looks like a recent version of Cargo changed the cwd or the
|
|
|
|
// location of the test executable.
|
|
|
|
self.root.join("../rg")
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the ripgrep executable.
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn bin(&self) -> PathBuf {
|
|
|
|
let path = self.root.join("rg.exe");
|
|
|
|
if !path.is_file() {
|
|
|
|
// Looks like a recent version of Cargo changed the cwd or the
|
|
|
|
// location of the test executable.
|
|
|
|
self.root.join("../rg.exe")
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
}
|
2016-09-09 22:58:30 -04: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-10 19:34:57 -04:00
|
|
|
pub fn link_dir<S: AsRef<Path>, T: AsRef<Path>>(&self, src: S, target: T) {
|
2016-09-09 22:58:30 -04: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-10 19:34:57 -04:00
|
|
|
/// Creates a directory symlink to the src with the given target name
|
|
|
|
/// in this directory.
|
2016-09-09 22:58:30 -04:00
|
|
|
#[cfg(windows)]
|
2016-10-10 19:34:57 -04:00
|
|
|
pub fn link_dir<S: AsRef<Path>, T: AsRef<Path>>(&self, src: S, target: T) {
|
2016-09-09 22:58:30 -04: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-10 19:34:57 -04: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)]
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:58:30 -04:00
|
|
|
/// Runs and captures the stdout of the given command.
|
|
|
|
///
|
|
|
|
/// If the return type could not be created from a string, then this
|
|
|
|
/// panics.
|
|
|
|
pub fn stdout<E: fmt::Debug, T: FromStr<Err=E>>(
|
|
|
|
&self,
|
|
|
|
cmd: &mut process::Command,
|
|
|
|
) -> T {
|
|
|
|
let o = self.output(cmd);
|
|
|
|
let stdout = String::from_utf8_lossy(&o.stdout);
|
|
|
|
match stdout.parse() {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(err) => {
|
|
|
|
panic!("could not convert from string: {:?}\n\n{}", err, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the output of a command. If the command failed, then this panics.
|
|
|
|
pub fn output(&self, cmd: &mut process::Command) -> process::Output {
|
2016-11-09 06:07:53 -05:00
|
|
|
let output = cmd.output().unwrap();
|
|
|
|
self.expect_success(cmd, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pipe `input` to a command, and collect the output.
|
|
|
|
pub fn pipe(
|
|
|
|
&self,
|
|
|
|
cmd: &mut process::Command,
|
|
|
|
input: &str
|
|
|
|
) -> process::Output {
|
|
|
|
cmd.stdin(process::Stdio::piped());
|
|
|
|
cmd.stdout(process::Stdio::piped());
|
|
|
|
cmd.stderr(process::Stdio::piped());
|
|
|
|
|
|
|
|
let mut child = cmd.spawn().unwrap();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
let worker = thread::spawn(move || {
|
|
|
|
write!(stdin, "{}", input)
|
|
|
|
});
|
|
|
|
|
|
|
|
let output = self.expect_success(cmd, child.wait_with_output().unwrap());
|
|
|
|
worker.join().unwrap().unwrap();
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If `o` is not the output of a successful process run
|
|
|
|
fn expect_success(
|
|
|
|
&self,
|
|
|
|
cmd: &process::Command,
|
|
|
|
o: process::Output
|
|
|
|
) -> process::Output {
|
2016-09-09 22:58:30 -04:00
|
|
|
if !o.status.success() {
|
|
|
|
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\
|
|
|
|
command failed but expected success!\
|
|
|
|
{}\
|
|
|
|
\n\ncommand: {:?}\
|
|
|
|
\ncwd: {}\
|
|
|
|
\n\nstatus: {}\
|
|
|
|
\n\nstdout: {}\
|
|
|
|
\n\nstderr: {}\
|
|
|
|
\n\n==========\n",
|
|
|
|
suggest, cmd, self.dir.display(), o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr));
|
|
|
|
}
|
|
|
|
o
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the given command and asserts that it resulted in an error exit
|
|
|
|
/// code.
|
|
|
|
pub fn assert_err(&self, cmd: &mut process::Command) {
|
|
|
|
let o = cmd.output().unwrap();
|
|
|
|
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",
|
|
|
|
cmd,
|
|
|
|
self.dir.display(),
|
|
|
|
o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr)
|
|
|
|
);
|
2016-09-09 22:58:30 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 17:21:07 -04:00
|
|
|
|
2018-06-19 13:41:44 +02:00
|
|
|
/// Runs the given command and asserts that its exit code matches expected exit code.
|
|
|
|
pub fn assert_exit_code(&self, expected_code: i32, cmd: &mut process::Command) {
|
|
|
|
let code = cmd.status().unwrap().code().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
expected_code, code,
|
|
|
|
"\n\n===== {:?} =====\n\
|
|
|
|
expected exit code did not match\
|
|
|
|
\n\nexpected: {}\
|
|
|
|
\n\nfound: {}\
|
|
|
|
\n\n=====\n",
|
|
|
|
cmd, expected_code, code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:21:07 -04:00
|
|
|
/// Runs the given command and asserts that something was printed to
|
|
|
|
/// stderr.
|
|
|
|
pub fn assert_non_empty_stderr(&self, cmd: &mut process::Command) {
|
|
|
|
let o = cmd.output().unwrap();
|
|
|
|
if o.status.success() || o.stderr.is_empty() {
|
|
|
|
panic!("\n\n===== {:?} =====\n\
|
|
|
|
command succeeded but expected failure!\
|
|
|
|
\n\ncwd: {}\
|
|
|
|
\n\nstatus: {}\
|
|
|
|
\n\nstdout: {}\n\nstderr: {}\
|
|
|
|
\n\n=====\n",
|
|
|
|
cmd, self.dir.display(), o.status,
|
|
|
|
String::from_utf8_lossy(&o.stdout),
|
|
|
|
String::from_utf8_lossy(&o.stderr));
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 22:58:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn nice_err<P: AsRef<Path>, T, E: error::Error>(
|
|
|
|
path: P,
|
|
|
|
res: Result<T, E>,
|
|
|
|
) -> T {
|
|
|
|
match res {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(err) => {
|
|
|
|
panic!("{}: {:?}", path.as_ref().display(), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|