Remove hamcrest has_installed_exe & is_not

This commit is contained in:
Dale Wijnand 2018-08-29 08:32:28 +02:00
parent 570fe8927d
commit f009dc4e1b
No known key found for this signature in database
GPG key ID: 4F256E3D151DF5EF
5 changed files with 78 additions and 125 deletions

View file

@ -10,8 +10,7 @@ use std::{env, str};
use git2;
use support::cargo_process;
use support::git;
use support::hamcrest::assert_that;
use support::install::{cargo_home, has_installed_exe};
use support::install::{cargo_home, assert_has_installed_exe};
use support::registry::Package;
use support::{basic_manifest, execs, project};
@ -46,8 +45,8 @@ fn multiple_installs() {
execs().run_output(&a);
execs().run_output(&b);
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), has_installed_exe("bar"));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
}
#[test]
@ -75,8 +74,8 @@ fn concurrent_installs() {
execs().run_output(&a);
execs().run_output(&b);
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), has_installed_exe("bar"));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
}
#[test]
@ -115,7 +114,7 @@ fn one_install_should_be_bad() {
.with_stderr_contains("warning: be sure to add `[..]` to your PATH [..]")
.run_output(&good);
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]

View file

@ -5,8 +5,7 @@ use support;
use git2;
use support::cross_compile;
use support::git;
use support::hamcrest::{assert_that, is_not};
use support::install::{cargo_home, has_installed_exe};
use support::install::{assert_has_installed_exe, assert_has_not_installed_exe, cargo_home};
use support::paths;
use support::registry::Package;
use support::{basic_manifest, cargo_process, project};
@ -37,14 +36,14 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
cargo_process("uninstall foo")
.with_stderr(&format!(
"[REMOVING] {home}[..]bin[..]foo[..]",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
}
#[test]
@ -74,8 +73,8 @@ error: some crates failed to install
",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), has_installed_exe("bar"));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
cargo_process("uninstall foo bar")
.with_stderr(&format!(
@ -87,8 +86,8 @@ error: some crates failed to install
home = cargo_home().display()
)).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_that(cargo_home(), is_not(has_installed_exe("bar")));
assert_has_not_installed_exe(cargo_home(), "foo");
assert_has_not_installed_exe(cargo_home(), "bar");
}
#[test]
@ -112,7 +111,7 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -126,7 +125,7 @@ fn installs_beta_version_by_explicit_name_from_git() {
.arg(p.url().to_string())
.arg("foo")
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -201,29 +200,29 @@ fn install_location_precedence() {
.arg(&t1)
.env("CARGO_INSTALL_ROOT", &t2)
.run();
assert_that(&t1, has_installed_exe("foo"));
assert_that(&t2, is_not(has_installed_exe("foo")));
assert_has_installed_exe(&t1, "foo");
assert_has_not_installed_exe(&t2, "foo");
println!("install CARGO_INSTALL_ROOT");
cargo_process("install foo")
.env("CARGO_INSTALL_ROOT", &t2)
.run();
assert_that(&t2, has_installed_exe("foo"));
assert_that(&t3, is_not(has_installed_exe("foo")));
assert_has_installed_exe(&t2, "foo");
assert_has_not_installed_exe(&t3, "foo");
println!("install install.root");
cargo_process("install foo").run();
assert_that(&t3, has_installed_exe("foo"));
assert_that(&t4, is_not(has_installed_exe("foo")));
assert_has_installed_exe(&t3, "foo");
assert_has_not_installed_exe(&t4, "foo");
fs::remove_file(root.join(".cargo/config")).unwrap();
println!("install cargo home");
cargo_process("install foo").run();
assert_that(&t4, has_installed_exe("foo"));
assert_has_installed_exe(&t4, "foo");
}
#[test]
@ -231,7 +230,7 @@ fn install_path() {
let p = project().file("src/main.rs", "fn main() {}").build();
cargo_process("install --path").arg(p.root()).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
cargo_process("install --path .")
.cwd(p.root())
.with_status(101)
@ -277,14 +276,14 @@ fn multiple_crates_select() {
.arg(p.url().to_string())
.arg("foo")
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), is_not(has_installed_exe("bar")));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_not_installed_exe(cargo_home(), "bar");
cargo_process("install --git")
.arg(p.url().to_string())
.arg("bar")
.run();
assert_that(cargo_home(), has_installed_exe("bar"));
assert_has_installed_exe(cargo_home(), "bar");
}
#[test]
@ -307,7 +306,7 @@ fn multiple_crates_auto_binaries() {
.build();
cargo_process("install --path").arg(p.root()).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -340,7 +339,7 @@ fn multiple_crates_auto_examples() {
.arg(p.root())
.arg("--example=foo")
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -399,7 +398,7 @@ fn examples() {
.arg(p.root())
.arg("--example=foo")
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -580,8 +579,8 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -666,20 +665,20 @@ fn uninstall_piecemeal() {
.build();
cargo_process("install --path").arg(p.root()).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), has_installed_exe("bar"));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_installed_exe(cargo_home(), "bar");
cargo_process("uninstall foo --bin=bar")
.with_stderr("[REMOVING] [..]bar[..]")
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_that(cargo_home(), is_not(has_installed_exe("bar")));
assert_has_installed_exe(cargo_home(), "foo");
assert_has_not_installed_exe(cargo_home(), "bar");
cargo_process("uninstall foo --bin=foo")
.with_stderr("[REMOVING] [..]foo[..]")
.run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
cargo_process("uninstall foo")
.with_status(101)
@ -711,7 +710,7 @@ fn installs_from_cwd_by_default() {
use `cargo install --path .` instead. \
Use `cargo build` if you want to simply build the package.",
).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -746,7 +745,7 @@ fn installs_from_cwd_with_2018_warnings() {
use `cargo install --path .` instead. \
Use `cargo build` if you want to simply build the package.",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
}
#[test]
@ -763,7 +762,7 @@ warning: be sure to add `{home}/bin` to your PATH to be able to run the installe
home = cargo_home().display(),
url = p.url(),
)).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall")
.with_stdout("")
@ -772,7 +771,7 @@ warning: be sure to add `{home}/bin` to your PATH to be able to run the installe
[REMOVING] {home}/bin/foo[EXE]",
home = cargo_home().display()
)).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
}
#[test]
@ -826,7 +825,7 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
assert!(p.build_dir().exists());
assert!(p.release_bin("foo").exists());
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -902,7 +901,7 @@ fn readonly_dir() {
fs::set_permissions(dir, perms).unwrap();
cargo_process("install foo").cwd(dir).run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -998,7 +997,7 @@ fn install_target_native() {
cargo_process("install foo --target")
.arg(support::rustc_host())
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -1012,7 +1011,7 @@ fn install_target_foreign() {
cargo_process("install foo --target")
.arg(cross_compile::alternate())
.run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
}
#[test]
@ -1093,8 +1092,8 @@ error: some packages failed to uninstall
home = cargo_home().display()
)).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_that(cargo_home(), is_not(has_installed_exe("bar")));
assert_has_not_installed_exe(cargo_home(), "foo");
assert_has_not_installed_exe(cargo_home(), "bar");
}
#[test]

View file

@ -1,5 +1,4 @@
use support::hamcrest::{assert_that, is_not};
use support::install::{cargo_home, has_installed_exe};
use support::install::{cargo_home, assert_has_installed_exe, assert_has_not_installed_exe};
use support::is_nightly;
use support::project;
@ -608,7 +607,7 @@ fn install_default_features() {
.build();
p.cargo("install --path .").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
p.cargo("install --path . --no-default-features")
@ -620,10 +619,10 @@ fn install_default_features() {
[ERROR] no binaries are available for install using the selected features
",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
p.cargo("install --path . --bin=foo").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
p.cargo("install --path . --bin=foo --no-default-features")
@ -639,10 +638,10 @@ Caused by:
Consider enabling them by passing e.g. `--features=\"a\"`
",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
p.cargo("install --path . --example=foo").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
p.cargo("install --path . --example=foo --no-default-features")
@ -658,7 +657,7 @@ Caused by:
Consider enabling them by passing e.g. `--features=\"a\"`
",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
}
#[test]
@ -683,7 +682,7 @@ fn install_arg_features() {
.build();
p.cargo("install --features a").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
}
@ -719,13 +718,13 @@ fn install_multiple_required_features() {
.build();
p.cargo("install --path .").run();
assert_that(cargo_home(), is_not(has_installed_exe("foo_1")));
assert_that(cargo_home(), has_installed_exe("foo_2"));
assert_has_not_installed_exe(cargo_home(), "foo_1");
assert_has_installed_exe(cargo_home(), "foo_2");
p.cargo("uninstall foo").run();
p.cargo("install --path . --features c").run();
assert_that(cargo_home(), has_installed_exe("foo_1"));
assert_that(cargo_home(), has_installed_exe("foo_2"));
assert_has_installed_exe(cargo_home(), "foo_1");
assert_has_installed_exe(cargo_home(), "foo_2");
p.cargo("uninstall foo").run();
p.cargo("install --path . --no-default-features")
@ -737,8 +736,8 @@ fn install_multiple_required_features() {
[ERROR] no binaries are available for install using the selected features
",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo_1")));
assert_that(cargo_home(), is_not(has_installed_exe("foo_2")));
assert_has_not_installed_exe(cargo_home(), "foo_1");
assert_has_not_installed_exe(cargo_home(), "foo_2");
}
#[test]
@ -834,7 +833,7 @@ fn dep_feature_in_toml() {
// install
p.cargo("install").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
}
@ -967,10 +966,10 @@ Consider enabling them by passing e.g. `--features=\"bar/a\"`
[ERROR] no binaries are available for install using the selected features
",
).run();
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_has_not_installed_exe(cargo_home(), "foo");
p.cargo("install --features bar/a").run();
assert_that(cargo_home(), has_installed_exe("foo"));
assert_has_installed_exe(cargo_home(), "foo");
p.cargo("uninstall foo").run();
}

View file

@ -1,39 +1,7 @@
use std::fmt;
use std::marker;
pub type MatchResult = Result<(), String>;
pub trait Matcher<T>: fmt::Debug {
fn matches(&self, actual: T) -> Result<(), String>;
}
pub fn assert_that<T, U: Matcher<T>>(actual: T, matcher: U) {
if let Err(e) = matcher.matches(actual) {
panic!("\nExpected: {:?}\n but: {}", matcher, e)
}
}
pub fn is_not<T, M: Matcher<T>>(matcher: M) -> IsNot<T, M> {
IsNot {
matcher,
_marker: marker::PhantomData,
}
}
#[derive(Debug)]
pub struct IsNot<T, M> {
matcher: M,
_marker: marker::PhantomData<T>,
}
impl<T, M: Matcher<T>> Matcher<T> for IsNot<T, M>
where
T: fmt::Debug,
{
fn matches(&self, actual: T) -> Result<(), String> {
match self.matcher.matches(actual) {
Ok(_) => Err("matched".to_string()),
Err(_) => Ok(()),
}
}
}

View file

@ -1,22 +1,27 @@
use std::fmt;
use std::path::{Path, PathBuf};
use support::hamcrest::{MatchResult, Matcher};
use support::paths;
pub use self::InstalledExe as has_installed_exe;
/// Used by `cargo install` tests to assert an executable binary
/// has been installed. Example usage:
///
/// assert_has_installed_exe(cargo_home(), "foo");
pub fn assert_has_installed_exe<P: AsRef<Path>>(path: P, name: &'static str) {
assert!(check_has_installed_exe(path, name));
}
pub fn assert_has_not_installed_exe<P: AsRef<Path>>(path: P, name: &'static str) {
assert!(!check_has_installed_exe(path, name));
}
fn check_has_installed_exe<P: AsRef<Path>>(path: P, name: &'static str) -> bool {
path.as_ref().join("bin").join(exe(name)).is_file()
}
pub fn cargo_home() -> PathBuf {
paths::home().join(".cargo")
}
/// A `Matcher` used by `cargo install` tests to check if an executable binary
/// has been installed. Example usage:
///
/// assert_that(cargo_home(), has_installed_exe("foo"));
pub struct InstalledExe(pub &'static str);
pub fn exe(name: &str) -> String {
if cfg!(windows) {
format!("{}.exe", name)
@ -24,20 +29,3 @@ pub fn exe(name: &str) -> String {
name.to_string()
}
}
impl<P: AsRef<Path>> Matcher<P> for InstalledExe {
fn matches(&self, path: P) -> MatchResult {
let path = path.as_ref().join("bin").join(exe(self.0));
if path.is_file() {
Ok(())
} else {
Err(format!("{} was not a file", path.display()))
}
}
}
impl fmt::Debug for InstalledExe {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "installed exe `{}`", self.0)
}
}