diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index 23c003ced..861cf9a82 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -1,4 +1,3 @@ -use support::ChannelChanger; use support::registry::{self, alt_api_path, Package}; use support::{basic_manifest, execs, paths, project}; use support::hamcrest::assert_that; diff --git a/tests/testsuite/bench.rs b/tests/testsuite/bench.rs index 603b1be3a..7f4a2d710 100644 --- a/tests/testsuite/bench.rs +++ b/tests/testsuite/bench.rs @@ -1,7 +1,7 @@ use std::str; use cargo::util::process; -use support::{is_nightly, ChannelChanger}; +use support::is_nightly; use support::paths::CargoPathExt; use support::{basic_manifest, basic_bin_manifest, basic_lib_manifest, execs, project}; use support::hamcrest::{assert_that, existing_file}; diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 642c8acd5..5429f3af6 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -3,13 +3,12 @@ use std::fs::{self, File}; use std::io::prelude::*; use cargo::util::paths::dylib_path_envvar; -use cargo::util::{process, ProcessBuilder}; +use cargo::util::process; use support::{basic_manifest, basic_bin_manifest, basic_lib_manifest, is_nightly, rustc_host, sleep_ms}; use support::paths::{root, CargoPathExt}; use support::ProjectBuilder; -use support::{execs, main_file, project}; +use support::{Execs, execs, main_file, project}; use support::registry::Package; -use support::ChannelChanger; use support::hamcrest::{assert_that, existing_dir, existing_file, is_not}; #[test] @@ -1553,15 +1552,15 @@ fn crate_authors_env_vars() { } // The tester may already have LD_LIBRARY_PATH=::/foo/bar which leads to a false positive error -fn setenv_for_removing_empty_component(mut p: ProcessBuilder) -> ProcessBuilder { +fn setenv_for_removing_empty_component(mut execs: Execs) -> Execs { let v = dylib_path_envvar(); if let Ok(search_path) = env::var(v) { let new_search_path = env::join_paths( env::split_paths(&search_path).filter(|e| !e.as_os_str().is_empty()), ).expect("join_paths"); - p.env(v, new_search_path); // build_command() will override LD_LIBRARY_PATH accordingly + execs.env(v, new_search_path); // build_command() will override LD_LIBRARY_PATH accordingly } - p + execs } // Regression test for #4277 diff --git a/tests/testsuite/build_plan.rs b/tests/testsuite/build_plan.rs index d2eeae26c..488d36831 100644 --- a/tests/testsuite/build_plan.rs +++ b/tests/testsuite/build_plan.rs @@ -1,4 +1,3 @@ -use support::ChannelChanger; use support::{basic_manifest, basic_bin_manifest, execs, main_file, project}; use support::hamcrest::{assert_that, existing_file, is_not}; diff --git a/tests/testsuite/cargo_features.rs b/tests/testsuite/cargo_features.rs index 73bb32ed7..1d622f1e3 100644 --- a/tests/testsuite/cargo_features.rs +++ b/tests/testsuite/cargo_features.rs @@ -1,4 +1,3 @@ -use support::ChannelChanger; use support::{execs, project, publish}; use support::hamcrest::assert_that; diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 5004c81de..901045507 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -3,7 +3,7 @@ use std::str; use std::fs::{self, File}; use std::io::Read; -use support::{is_nightly, rustc_host, ChannelChanger}; +use support::{is_nightly, rustc_host}; use support::{basic_manifest, basic_lib_manifest, execs, git, project, path2url}; use support::paths::CargoPathExt; use support::registry::Package; diff --git a/tests/testsuite/edition.rs b/tests/testsuite/edition.rs index c3926091c..59adeaa78 100644 --- a/tests/testsuite/edition.rs +++ b/tests/testsuite/edition.rs @@ -1,5 +1,4 @@ use support::{basic_lib_manifest, is_nightly, execs, project}; -use support::ChannelChanger; use support::hamcrest::assert_that; #[test] diff --git a/tests/testsuite/features.rs b/tests/testsuite/features.rs index 17524b96c..6bb9f1a95 100644 --- a/tests/testsuite/features.rs +++ b/tests/testsuite/features.rs @@ -3,7 +3,6 @@ use std::io::prelude::*; use support::paths::CargoPathExt; use support::{basic_manifest, execs, project}; -use support::ChannelChanger; use support::hamcrest::assert_that; use support::registry::Package; diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 0ad9d5502..b03fb0c11 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -4,7 +4,7 @@ use git2; use support::git; use support::{basic_manifest, execs, project}; -use support::{is_nightly, ChannelChanger}; +use support::is_nightly; use support::hamcrest::assert_that; #[test] diff --git a/tests/testsuite/generate_lockfile.rs b/tests/testsuite/generate_lockfile.rs index f25cb2338..ce02ac453 100644 --- a/tests/testsuite/generate_lockfile.rs +++ b/tests/testsuite/generate_lockfile.rs @@ -3,7 +3,6 @@ use std::io::prelude::*; use support::registry::Package; use support::{basic_manifest, execs, paths, project, ProjectBuilder}; -use support::ChannelChanger; use support::hamcrest::{assert_that, existing_file, is_not}; #[test] diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index d4614eddf..62cb6ffe7 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -11,7 +11,6 @@ use cargo::util::process; use support::sleep_ms; use support::paths::{self, CargoPathExt}; use support::{basic_manifest, basic_lib_manifest, execs, git, main_file, project, path2url}; -use support::ChannelChanger; use support::hamcrest::{assert_that, existing_file}; #[test] diff --git a/tests/testsuite/metabuild.rs b/tests/testsuite/metabuild.rs index 460068775..1dcfca856 100644 --- a/tests/testsuite/metabuild.rs +++ b/tests/testsuite/metabuild.rs @@ -3,7 +3,7 @@ use serde_json; use std::str; use support::{ basic_lib_manifest, basic_manifest, execs, hamcrest::assert_that, project, registry::Package, - rustc_host, ChannelChanger, Project, + rustc_host, Project, }; #[test] diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 2ad48f7db..f2e3ea656 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -1,5 +1,5 @@ use support::registry::Package; -use support::{basic_bin_manifest, basic_lib_manifest, execs, main_file, project, ChannelChanger}; +use support::{basic_bin_manifest, basic_lib_manifest, execs, main_file, project}; use support::hamcrest::assert_that; #[test] diff --git a/tests/testsuite/out_dir.rs b/tests/testsuite/out_dir.rs index 47a14be3b..6428b1c76 100644 --- a/tests/testsuite/out_dir.rs +++ b/tests/testsuite/out_dir.rs @@ -4,7 +4,7 @@ use std::env; use support::hamcrest::assert_that; -use support::{process, sleep_ms, ChannelChanger}; +use support::{process, sleep_ms}; use support::{basic_manifest, execs, project}; #[test] diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 7e431422a..ff3de7f17 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -4,7 +4,7 @@ use std::io::prelude::*; use std::path::{Path, PathBuf}; use git2; -use support::{cargo_process, sleep_ms, ChannelChanger}; +use support::{cargo_process, sleep_ms}; use support::{basic_manifest, execs, git, is_nightly, paths, project, registry, path2url}; use support::registry::Package; use flate2::read::GzDecoder; diff --git a/tests/testsuite/profile_config.rs b/tests/testsuite/profile_config.rs index 393693fcf..791cdc27b 100644 --- a/tests/testsuite/profile_config.rs +++ b/tests/testsuite/profile_config.rs @@ -1,5 +1,4 @@ use support::{basic_lib_manifest, execs, paths, project}; -use support::ChannelChanger; use support::hamcrest::assert_that; #[test] diff --git a/tests/testsuite/profile_overrides.rs b/tests/testsuite/profile_overrides.rs index eb8ebb74c..7e678f17a 100644 --- a/tests/testsuite/profile_overrides.rs +++ b/tests/testsuite/profile_overrides.rs @@ -1,5 +1,4 @@ use support::{basic_manifest, basic_lib_manifest, execs, project}; -use support::ChannelChanger; use support::hamcrest::assert_that; #[test] diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index bcc1d61c8..9bce776ff 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -2,7 +2,6 @@ use std::io::prelude::*; use std::fs::{self, File}; use std::io::SeekFrom; -use support::ChannelChanger; use support::git::repo; use support::paths; use support::{basic_manifest, execs, project, publish}; diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 64dad2e07..96adc786e 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -750,7 +750,6 @@ fn update_lockfile() { #[test] fn update_offline() { - use support::ChannelChanger; let p = project() .file( "Cargo.toml", diff --git a/tests/testsuite/rename_deps.rs b/tests/testsuite/rename_deps.rs index a725e71b0..2816b315c 100644 --- a/tests/testsuite/rename_deps.rs +++ b/tests/testsuite/rename_deps.rs @@ -1,4 +1,3 @@ -use support::ChannelChanger; use support::git; use support::paths; use support::registry::Package; diff --git a/tests/testsuite/resolve.rs b/tests/testsuite/resolve.rs index 2acaadab0..8e35ef921 100644 --- a/tests/testsuite/resolve.rs +++ b/tests/testsuite/resolve.rs @@ -8,7 +8,6 @@ use cargo::core::{Dependency, PackageId, Registry, Summary, enable_nightly_featu use cargo::util::{CargoResult, Config, ToUrl}; use cargo::core::resolver::{self, Method}; -use support::ChannelChanger; use support::{execs, project}; use support::registry::Package; diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index aaf50f96a..812d88136 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -1,5 +1,5 @@ use cargo::util::paths::dylib_path_envvar; -use support::{self, ChannelChanger}; +use support; use support::{basic_bin_manifest, basic_lib_manifest, execs, project, Project, path2url}; use support::hamcrest::{assert_that, existing_file}; diff --git a/tests/testsuite/support/mod.rs b/tests/testsuite/support/mod.rs index 47f8aaa38..7a07221b1 100644 --- a/tests/testsuite/support/mod.rs +++ b/tests/testsuite/support/mod.rs @@ -118,12 +118,12 @@ use std::fs; use std::io::prelude::*; use std::os; use std::path::{Path, PathBuf}; -use std::process::Output; +use std::process::{Command, Output}; use std::str; use std::time::Duration; use std::usize; -use cargo::util::{ProcessBuilder, ProcessError, Rustc}; +use cargo::util::{CargoResult, ProcessBuilder, ProcessError, Rustc}; use cargo; use serde_json::{self, Value}; use url::Url; @@ -384,10 +384,10 @@ impl Project { /// Arguments can be separated by spaces. /// Example: /// assert_that(p.cargo("build --bin foo"), execs()); - pub fn cargo(&self, cmd: &str) -> ProcessBuilder { + pub fn cargo(&self, cmd: &str) -> Execs { let mut p = self.process(&cargo_exe()); split_and_add_args(&mut p, cmd); - p + execs().with_process_builder(p) } /// Returns the contents of `Cargo.lock`. @@ -526,6 +526,8 @@ pub fn cargo_exe() -> PathBuf { #[must_use] #[derive(Clone)] pub struct Execs { + ran: bool, + process_builder: Option, expect_stdout: Option, expect_stdin: Option, expect_stderr: Option, @@ -543,6 +545,11 @@ pub struct Execs { } impl Execs { + pub fn with_process_builder(mut self, p: ProcessBuilder) -> Execs { + self.process_builder = Some(p); + self + } + /// Verify that stdout is equal to the given lines. /// See `lines_match` for supported patterns. pub fn with_stdout(&mut self, expected: S) -> &mut Self { @@ -674,6 +681,59 @@ impl Execs { self } + pub fn arg>(&mut self, arg: T) -> &mut Self { + if let Some(ref mut p) = self.process_builder { + p.arg(arg); + } + self + } + + pub fn cwd>(&mut self, path: T) -> &mut Self { + if let Some(ref mut p) = self.process_builder { + p.cwd(path); + } + self + } + + pub fn env>(&mut self, key: &str, val: T) -> &mut Self { + if let Some(ref mut p) = self.process_builder { + p.env(key, val); + } + self + } + + pub fn env_remove(&mut self, key: &str) -> &mut Self { + if let Some(ref mut p) = self.process_builder { + p.env_remove(key); + } + self + } + + pub fn exec_with_output(&self) -> CargoResult { + // TODO avoid unwrap + let p = (&self.process_builder).clone().unwrap(); + p.exec_with_output() + } + + pub fn build_command(&self) -> Command { + // TODO avoid unwrap + let p = (&self.process_builder).clone().unwrap(); + p.build_command() + } + + pub fn masquerade_as_nightly_cargo(&mut self) -> &mut Self { + if let Some(ref mut p) = self.process_builder { + p.masquerade_as_nightly_cargo(); + } + self + } + + #[allow(dead_code)] + pub fn run(&mut self) { + self.ran = true; + let p = (&self.process_builder).clone().unwrap(); + ham::assert_that(p, self) + } fn match_process(&self, process: &ProcessBuilder) -> ham::MatchResult { println!("running {}", process); @@ -1052,6 +1112,15 @@ impl Execs { } } +impl Drop for Execs { + fn drop(&mut self) { + if !self.ran { + // TODO: Re-enable when everything goes through Execs#run + // panic!("forgot to run this command"); + } + } +} + #[derive(Debug, PartialEq, Eq, Clone, Copy)] enum MatchKind { Exact, @@ -1187,6 +1256,40 @@ impl fmt::Debug for Execs { } } +// TODO: Remove this temporary matcher while transitioning off of assert_that +impl ham::Matcher for Execs { + fn matches(&self, mut execs: Execs) -> ham::MatchResult { + self.matches(&mut execs) + } +} + +// TODO: Remove this temporary matcher while transitioning off of assert_that +impl<'a> ham::Matcher<&'a mut Execs> for Execs { + fn matches(&self, execs: &'a mut Execs) -> ham::MatchResult { + // TODO: avoid unwrap + let mut p = (&execs.process_builder).clone().unwrap(); + self.matches(&mut p) + } +} + +// TODO: Remove this temporary matcher while transitioning off of assert_that +impl<'t> ham::Matcher for &'t mut Execs { + fn matches(&self, execs: Execs) -> ham::MatchResult { + // TODO: avoid unwrap + let mut p = (&execs.process_builder).clone().unwrap(); + self.matches(&mut p) + } +} + +// TODO: Remove this temporary matcher while transitioning off of assert_that +impl<'a, 't> ham::Matcher<&'a mut Execs> for &'t mut Execs { + fn matches(&self, execs: &'a mut Execs) -> ham::MatchResult { + // TODO: avoid unwrap + let mut p = (&execs.process_builder).clone().unwrap(); + self.matches(&mut p) + } +} + impl ham::Matcher for Execs { fn matches(&self, process: ProcessBuilder) -> ham::MatchResult { self.match_process(&process) @@ -1225,6 +1328,8 @@ impl<'t> ham::Matcher for &'t mut Execs { pub fn execs() -> Execs { Execs { + ran: false, + process_builder: None, expect_stdout: None, expect_stderr: None, expect_stdin: None,