This hasn't been updated in awhile and in general we've been barely using it.
This drops the outdated dependency and vendors a small amount of the
functionality that it provided. I think eventually we'll want to transition away
from this method of assertions but I wanted to get this piece in to avoid too
much churn in one commit.
[dev-dependencies]
bufstream = "0.1"
filetime = "0.1"
-hamcrest = "=0.1.1"
[[bin]]
name = "cargo"
-extern crate hamcrest;
-
use cargotest::support::{basic_bin_manifest, execs, project, Project};
use hamcrest::{assert_that};
}
}
-impl fmt::Display for InstalledExe {
+impl fmt::Debug for InstalledExe {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "installed exe `{}`", self.0)
}
fn match_status(&self, actual: &Output) -> ham::MatchResult {
match self.expect_exit_code {
- None => ham::success(),
- Some(code) => {
- ham::expect(
- actual.status.code() == Some(code),
- format!("exited with {}\n--- stdout\n{}\n--- stderr\n{}",
+ None => Ok(()),
+ Some(code) if actual.status.code() == Some(code) => Ok(()),
+ Some(_) => {
+ Err(format!("exited with {}\n--- stdout\n{}\n--- stderr\n{}",
actual.status,
String::from_utf8_lossy(&actual.stdout),
String::from_utf8_lossy(&actual.stderr)))
kind: MatchKind) -> ham::MatchResult {
let out = match expected {
Some(out) => out,
- None => return ham::success(),
+ None => return Ok(()),
};
let actual = match str::from_utf8(actual) {
Err(..) => return Err(format!("{} was not utf8 encoded",
let e = out.lines();
let diffs = self.diff_lines(a, e, false);
- ham::expect(diffs.is_empty(),
- format!("differences:\n\
- {}\n\n\
- other output:\n\
- `{}`", diffs.join("\n"),
- String::from_utf8_lossy(extra)))
+ if diffs.is_empty() {
+ Ok(())
+ } else {
+ Err(format!("differences:\n\
+ {}\n\n\
+ other output:\n\
+ `{}`", diffs.join("\n"),
+ String::from_utf8_lossy(extra)))
+ }
}
MatchKind::Partial => {
let mut a = actual.lines();
diffs = a;
}
}
- ham::expect(diffs.is_empty(),
- format!("expected to find:\n\
- {}\n\n\
- did not find in output:\n\
- {}", out,
- actual))
+ if diffs.is_empty() {
+ Ok(())
+ } else {
+ Err(format!("expected to find:\n\
+ {}\n\n\
+ did not find in output:\n\
+ {}", out,
+ actual))
+ }
}
MatchKind::PartialN(number) => {
let mut a = actual.lines();
a.next()
} {}
- ham::expect(matches == number,
- format!("expected to find {} occurrences:\n\
- {}\n\n\
- did not find in output:\n\
- {}", number, out,
- actual))
+ if matches == number {
+ Ok(())
+ } else {
+ Err(format!("expected to find {} occurrences:\n\
+ {}\n\n\
+ did not find in output:\n\
+ {}", number, out,
+ actual))
+ }
}
MatchKind::NotPresent => {
- ham::expect(!actual.contains(out),
- format!("expected not to find:\n\
- {}\n\n\
- but found in output:\n\
- {}", out,
- actual))
+ if !actual.contains(out) {
+ Ok(())
+ } else {
+ Err(format!("expected not to find:\n\
+ {}\n\n\
+ but found in output:\n\
+ {}", out,
+ actual))
+ }
}
}
}
}
}
-impl fmt::Display for Execs {
+impl fmt::Debug for Execs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "execs")
}
+use cargotest::install::exe;
use cargotest::is_nightly;
-use cargotest::support::{execs, project};
-use cargotest::support::registry::Package;
-use hamcrest::prelude::*;
use cargotest::support::paths::CargoPathExt;
-use cargotest::install::exe;
+use cargotest::support::registry::Package;
+use cargotest::support::{execs, project};
use glob::glob;
+use hamcrest::{assert_that, existing_file, is_not};
const SIMPLE_MANIFEST: &str = r#"
[package]
is_not(existing_file()));
assert_that(&p.root().join("target/debug").join(exe("foo")),
is_not(existing_file()));
- assert_that(glob(&p.root().join("target/debug/t1-*").to_str().unwrap())
- .unwrap().count(),
- is(equal_to(0)));
+ assert_eq!(glob(&p.root().join("target/debug/t1-*").to_str().unwrap())
+ .unwrap().count(), 0);
p.root().join("target").rm_rf();
assert_that(p.cargo("check").arg("--example").arg("ex1"),
is_not(existing_file()));
assert_that(&p.root().join("target/debug").join(exe("foo")),
is_not(existing_file()));
- assert_that(glob(&p.root().join("target/debug/b1-*").to_str().unwrap())
- .unwrap().count(),
- is(equal_to(0)));
+ assert_eq!(glob(&p.root().join("target/debug/b1-*").to_str().unwrap())
+ .unwrap().count(), 0);
}
--- /dev/null
+use std::fmt;
+use std::marker;
+use std::path::Path;
+
+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 existing_file() -> ExistingFile {
+ ExistingFile
+}
+
+#[derive(Debug)]
+pub struct ExistingFile;
+
+impl<P> Matcher<P> for ExistingFile where P: AsRef<Path> {
+ fn matches(&self, actual: P) -> Result<(), String> {
+ if actual.as_ref().is_file() {
+ Ok(())
+ } else {
+ Err(format!("{} was not a file", actual.as_ref().display()))
+ }
+ }
+}
+
+pub fn existing_dir() -> ExistingDir {
+ ExistingDir
+}
+
+#[derive(Debug)]
+pub struct ExistingDir;
+
+impl<P> Matcher<P> for ExistingDir where P: AsRef<Path> {
+ fn matches(&self, actual: P) -> Result<(), String> {
+ if actual.as_ref().is_dir() {
+ Ok(())
+ } else {
+ Err(format!("{} was not a dir", actual.as_ref().display()))
+ }
+ }
+}
+
+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(()),
+ }
+ }
+}
+
+pub fn contains<T>(item: Vec<T>) -> Contains<T> {
+ Contains(item)
+}
+
+#[derive(Debug)]
+pub struct Contains<T>(Vec<T>);
+
+impl<'a, T> Matcher<&'a Vec<T>> for Contains<T> where T: fmt::Debug + PartialEq {
+ fn matches(&self, actual: &'a Vec<T>) -> Result<(), String> {
+ for item in self.0.iter() {
+ if !actual.contains(item) {
+ return Err(format!("failed to find {:?}", item))
+ }
+ }
+ Ok(())
+ }
+}
extern crate flate2;
extern crate git2;
extern crate glob;
-extern crate hamcrest;
extern crate hex;
extern crate libc;
#[macro_use]
#[macro_use]
mod cargotest;
+mod hamcrest;
mod alt_registry;
mod bad_config;
use cargotest::support::{project, execs, paths, git, path2url, cargo_exe, registry};
use cargotest::support::registry::Package;
use flate2::read::GzDecoder;
-use hamcrest::{assert_that, existing_file, contains, equal_to};
+use hamcrest::{assert_that, existing_file, contains};
use tar::Archive;
#[test]
entry.read_to_string(&mut contents).unwrap();
// BTreeMap makes the order of dependencies in the generated file deterministic
// by sorting alphabetically
- assert_that(&contents[..], equal_to(
+ assert_eq!(&contents[..],
&*format!(
r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
[dependencies.ghi]
version = "1.0"
"#,
- registry::alt_registry())));
+ registry::alt_registry()));
}
#[test]
.unwrap();
let mut contents = String::new();
entry.read_to_string(&mut contents).unwrap();
- assert_that(&contents[..], equal_to(
+ assert_eq!(&contents[..],
r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
name = "bar"
version = "0.1.0"
authors = []
-"#));
+"#);
}
#[test]
-extern crate hamcrest;
-
use cargotest::support::{project, execs};
use cargotest::support::registry::Package;
use cargotest::ChannelChanger;
use cargotest::is_nightly;
use cargotest::install::{cargo_home, has_installed_exe};
use cargotest::support::{project, execs};
-use hamcrest::{assert_that, existing_file, not};
+use hamcrest::{assert_that, existing_file, is_not};
#[test]
fn build_bin_default_features() {
assert_that(p.cargo("build"),
execs().with_status(0));
- assert_that(&p.bin("foo_1"), not(existing_file()));
+ assert_that(&p.bin("foo_1"), is_not(existing_file()));
assert_that(&p.bin("foo_2"), existing_file());
assert_that(p.cargo("build").arg("--features").arg("c"),
assert_that(p.cargo("build").arg("--example=foo_2"),
execs().with_status(0));
- assert_that(&p.bin("examples/foo_1"), not(existing_file()));
+ assert_that(&p.bin("examples/foo_1"), is_not(existing_file()));
assert_that(&p.bin("examples/foo_2"), existing_file());
assert_that(p.cargo("build").arg("--example=foo_1")
[FINISHED] release [optimized] target(s) in [..]
[ERROR] no binaries are available for install using the selected features
")));
- assert_that(cargo_home(), not(has_installed_exe("foo")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_that(p.cargo("install").arg("--bin=foo"),
execs().with_status(0));
target `foo` requires the features: `a`
Consider enabling them by passing e.g. `--features=\"a\"`
")));
- assert_that(cargo_home(), not(has_installed_exe("foo")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_that(p.cargo("install").arg("--example=foo"),
execs().with_status(0));
target `foo` requires the features: `a`
Consider enabling them by passing e.g. `--features=\"a\"`
")));
- assert_that(cargo_home(), not(has_installed_exe("foo")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo")));
}
#[test]
assert_that(p.cargo("install"),
execs().with_status(0));
- assert_that(cargo_home(), not(has_installed_exe("foo_1")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo_1")));
assert_that(cargo_home(), has_installed_exe("foo_2"));
assert_that(p.cargo("uninstall").arg("foo"),
execs().with_status(0));
[FINISHED] release [optimized] target(s) in [..]
[ERROR] no binaries are available for install using the selected features
"));
- assert_that(cargo_home(), not(has_installed_exe("foo_1")));
- assert_that(cargo_home(), not(has_installed_exe("foo_2")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo_1")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo_2")));
}
#[test]
[FINISHED] release [optimized] target(s) in [..]
[ERROR] no binaries are available for install using the selected features
")));
- assert_that(cargo_home(), not(has_installed_exe("foo")));
+ assert_that(cargo_home(), is_not(has_installed_exe("foo")));
assert_that(p.cargo("install").arg("--features").arg("bar/a"),
execs().with_status(0));
use std::collections::BTreeMap;
-use hamcrest::{assert_that, equal_to, contains, not};
+use hamcrest::{assert_that, contains, is_not};
use cargo::core::source::{SourceId, GitReference};
use cargo::core::dependency::Kind::{self, Development};
let res = resolve(&pkg_id("root"), Vec::new(),
®istry(vec![])).unwrap();
- assert_that(&res, equal_to(&names(&["root"])));
+ assert_eq!(res, names(&["root"]));
+}
+
+fn assert_same(a: &[PackageId], b: &[PackageId]) {
+ assert_eq!(a.len(), b.len());
+ for item in a {
+ assert!(b.contains(item));
+ }
}
#[test]
fn test_resolving_only_package() {
let reg = registry(vec![pkg("foo")]);
- let res = resolve(&pkg_id("root"), vec![dep("foo")], ®);
-
- assert_that(&res.unwrap(), contains(names(&["root", "foo"])).exactly());
+ let res = resolve(&pkg_id("root"), vec![dep("foo")], ®).unwrap();
+ assert_same(&res, &names(&["root", "foo"]));
}
#[test]
fn test_resolving_one_dep() {
let reg = registry(vec![pkg("foo"), pkg("bar")]);
- let res = resolve(&pkg_id("root"), vec![dep("foo")], ®);
-
- assert_that(&res.unwrap(), contains(names(&["root", "foo"])).exactly());
+ let res = resolve(&pkg_id("root"), vec![dep("foo")], ®).unwrap();
+ assert_same(&res, &names(&["root", "foo"]));
}
#[test]
let reg = registry(vec![pkg!("foo"), pkg!("bar"), pkg!("baz")]);
let res = resolve(&pkg_id("root"), vec![dep("foo"), dep("baz")],
®).unwrap();
-
- assert_that(&res, contains(names(&["root", "foo", "baz"])).exactly());
+ assert_same(&res, &names(&["root", "foo", "baz"]));
}
#[test]
let res = resolve(&pkg_id("root"),
vec![dep_loc("foo", "http://first.example.com"),
dep_loc("bar", "http://second.example.com")],
- ®);
+ ®).unwrap();
let mut names = loc_names(&[("foo", "http://first.example.com"),
("bar", "http://second.example.com")]);
names.push(pkg_id("root"));
-
- assert_that(&res.unwrap(), contains(names).exactly());
+ assert_same(&res, &names);
}
#[test]
("foo", "1.0.0"),
("bar", "1.0.0"),
("util", "1.2.2")])));
- assert_that(&res, not(contains(names(&[("util", "1.0.1")]))));
- assert_that(&res, not(contains(names(&[("util", "1.1.1")]))));
+ assert_that(&res, is_not(contains(names(&[("util", "1.0.1")]))));
+ assert_that(&res, is_not(contains(names(&[("util", "1.1.1")]))));
}
#[test]