Drop outdated hamcrest dependency
authorAlex Crichton <alex@alexcrichton.com>
Thu, 1 Mar 2018 18:14:17 +0000 (10:14 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 1 Mar 2018 19:03:54 +0000 (11:03 -0800)
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.

Cargo.toml
tests/testsuite/build_lib.rs
tests/testsuite/cargotest/install.rs
tests/testsuite/cargotest/support/mod.rs
tests/testsuite/check.rs
tests/testsuite/hamcrest.rs [new file with mode: 0644]
tests/testsuite/lib.rs
tests/testsuite/package.rs
tests/testsuite/rename_deps.rs
tests/testsuite/required_features.rs
tests/testsuite/resolve.rs

index 1b71cc79702c601e3f2fa3de51f4f90c90c9789d..e7d52c3e56b7d76a1ee4e609feed718cd186ec28 100644 (file)
@@ -81,7 +81,6 @@ features = [
 [dev-dependencies]
 bufstream = "0.1"
 filetime = "0.1"
-hamcrest = "=0.1.1"
 
 [[bin]]
 name = "cargo"
index 90a5a5097fca48fe30cd32032b634d60c4a77832..87243d6b34492d6a762bc646232d89f4b908e987 100644 (file)
@@ -1,5 +1,3 @@
-extern crate hamcrest;
-
 use cargotest::support::{basic_bin_manifest, execs, project, Project};
 use hamcrest::{assert_that};
 
index b9905ea2459682d41e5fb16df2c574793ecbd2bd..d9ab279903d8c76970059ff913086b94a3e323b7 100644 (file)
@@ -24,7 +24,7 @@ impl<P: AsRef<Path>> Matcher<P> for InstalledExe {
     }
 }
 
-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)
     }
index 754d3c96f18ebbf280fb3b0a535f146219418748..733d1e66f977abbf1cdeabe062605236107c6f7d 100644 (file)
@@ -416,11 +416,10 @@ impl Execs {
 
     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)))
@@ -497,7 +496,7 @@ impl Execs {
                  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",
@@ -514,12 +513,15 @@ impl Execs {
                 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();
@@ -532,12 +534,15 @@ impl Execs {
                         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();
@@ -552,20 +557,26 @@ impl Execs {
                     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))
+                }
             }
         }
     }
@@ -736,7 +747,7 @@ fn zip_all<T, I1: Iterator<Item=T>, I2: Iterator<Item=T>>(a: I1, b: I2) -> ZipAl
     }
 }
 
-impl fmt::Display for Execs {
+impl fmt::Debug for Execs {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         write!(f, "execs")
     }
index 864dc2cf7c7fd83f19e19fd746d7474bee0c870e..38b7d214ca157b005bcce5a81574d3e3a64c5105 100644 (file)
@@ -1,10 +1,10 @@
+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]
@@ -662,9 +662,8 @@ fn check_artifacts()
         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"),
@@ -685,7 +684,6 @@ fn check_artifacts()
         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);
 }
diff --git a/tests/testsuite/hamcrest.rs b/tests/testsuite/hamcrest.rs
new file mode 100644 (file)
index 0000000..4144318
--- /dev/null
@@ -0,0 +1,86 @@
+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(())
+    }
+}
index b804ab0d05f4ee0d928c90b867c5bc66899da7e6..321364b4725daa5ee8473421817e01ab8e04da4c 100644 (file)
@@ -4,7 +4,6 @@ extern crate filetime;
 extern crate flate2;
 extern crate git2;
 extern crate glob;
-extern crate hamcrest;
 extern crate hex;
 extern crate libc;
 #[macro_use]
@@ -20,6 +19,7 @@ extern crate winapi;
 
 #[macro_use]
 mod cargotest;
+mod hamcrest;
 
 mod alt_registry;
 mod bad_config;
index f22e1a3ffadb295a095e52c9e6f167989b48a495..cb30b6abe82a92bf19a99eaaa116dc2adf353fa5 100644 (file)
@@ -8,7 +8,7 @@ use cargotest::{cargo_process, process, ChannelChanger};
 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]
@@ -756,7 +756,7 @@ fn generated_manifest() {
     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
 #
@@ -795,7 +795,7 @@ registry-index = "{}"
 [dependencies.ghi]
 version = "1.0"
 "#,
-    registry::alt_registry())));
+    registry::alt_registry()));
 }
 
 #[test]
@@ -838,7 +838,7 @@ fn ignore_workspace_specifier() {
                         .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
@@ -855,7 +855,7 @@ r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
 name = "bar"
 version = "0.1.0"
 authors = []
-"#));
+"#);
 }
 
 #[test]
index cc8a28fa7b57e408a029011bad935c10b4e5917f..92f9b10b2d61cc8bed89a9e6dc48de5e17095c56 100644 (file)
@@ -1,5 +1,3 @@
-extern crate hamcrest;
-
 use cargotest::support::{project, execs};
 use cargotest::support::registry::Package;
 use cargotest::ChannelChanger;
index c99ff6a5cea641c4aa612e303f1902012dcfdffd..e92d72b313b7b46bec9e7284d2d57d1c526035c7 100644 (file)
@@ -1,7 +1,7 @@
 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() {
@@ -110,7 +110,7 @@ fn build_bin_multiple_required_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"),
@@ -213,7 +213,7 @@ Consider enabling them by passing e.g. `--features=\"b 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")
@@ -553,7 +553,7 @@ fn install_default_features() {
 [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));
@@ -571,7 +571,7 @@ Caused by:
   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));
@@ -589,7 +589,7 @@ Caused by:
   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]
@@ -649,7 +649,7 @@ fn install_multiple_required_features() {
 
     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));
@@ -667,8 +667,8 @@ fn install_multiple_required_features() {
 [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]
@@ -872,7 +872,7 @@ Consider enabling them by passing e.g. `--features=\"bar/a\"`
 [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));
index 55d683feefa17d76ad09410295d5681e11895fef..d99cf94f315a5771e1e66cadea09675d7b69d1e9 100644 (file)
@@ -2,7 +2,7 @@
 
 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};
@@ -156,23 +156,28 @@ fn test_resolving_empty_dependency_list() {
     let res = resolve(&pkg_id("root"), Vec::new(),
                       &registry(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")], &reg);
-
-    assert_that(&res.unwrap(), contains(names(&["root", "foo"])).exactly());
+    let res = resolve(&pkg_id("root"), vec![dep("foo")], &reg).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")], &reg);
-
-    assert_that(&res.unwrap(), contains(names(&["root", "foo"])).exactly());
+    let res = resolve(&pkg_id("root"), vec![dep("foo")], &reg).unwrap();
+    assert_same(&res, &names(&["root", "foo"]));
 }
 
 #[test]
@@ -180,8 +185,7 @@ fn test_resolving_multiple_deps() {
     let reg = registry(vec![pkg!("foo"), pkg!("bar"), pkg!("baz")]);
     let res = resolve(&pkg_id("root"), vec![dep("foo"), dep("baz")],
                       &reg).unwrap();
-
-    assert_that(&res, contains(names(&["root", "foo", "baz"])).exactly());
+    assert_same(&res, &names(&["root", "foo", "baz"]));
 }
 
 #[test]
@@ -210,14 +214,13 @@ fn test_resolving_with_same_name() {
     let res = resolve(&pkg_id("root"),
                       vec![dep_loc("foo", "http://first.example.com"),
                            dep_loc("bar", "http://second.example.com")],
-                      &reg);
+                      &reg).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]
@@ -280,8 +283,8 @@ fn test_resolving_maximum_version_with_transitive_deps() {
                                        ("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]