Skip to content

Commit

Permalink
Wrap ProcessBuilder in Execs & make .cargo return that
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Aug 28, 2018
1 parent 76e840b commit b5ee363
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 31 deletions.
1 change: 0 additions & 1 deletion tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
11 changes: 5 additions & 6 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/build_plan.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use support::ChannelChanger;
use support::{execs, project, publish};
use support::hamcrest::assert_that;

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/edition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use support::{basic_lib_manifest, is_nightly, execs, project};
use support::ChannelChanger;
use support::hamcrest::assert_that;

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/metabuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/out_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/profile_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use support::{basic_lib_manifest, execs, paths, project};
use support::ChannelChanger;
use support::hamcrest::assert_that;

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/profile_overrides.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use support::{basic_manifest, basic_lib_manifest, execs, project};
use support::ChannelChanger;
use support::hamcrest::assert_that;

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,6 @@ fn update_lockfile() {

#[test]
fn update_offline() {
use support::ChannelChanger;
let p = project()
.file(
"Cargo.toml",
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/rename_deps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use support::ChannelChanger;
use support::git;
use support::paths;
use support::registry::Package;
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/run.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
113 changes: 109 additions & 4 deletions tests/testsuite/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -526,6 +526,8 @@ pub fn cargo_exe() -> PathBuf {
#[must_use]
#[derive(Clone)]
pub struct Execs {
ran: bool,
process_builder: Option<ProcessBuilder>,
expect_stdout: Option<String>,
expect_stdin: Option<String>,
expect_stderr: Option<String>,
Expand All @@ -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<S: ToString>(&mut self, expected: S) -> &mut Self {
Expand Down Expand Up @@ -674,6 +681,59 @@ impl Execs {
self
}

pub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut Self {
if let Some(ref mut p) = self.process_builder {
p.arg(arg);
}
self
}

pub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut Self {
if let Some(ref mut p) = self.process_builder {
p.cwd(path);
}
self
}

pub fn env<T: AsRef<OsStr>>(&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<Output> {
// 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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1187,6 +1256,40 @@ impl fmt::Debug for Execs {
}
}

// TODO: Remove this temporary matcher while transitioning off of assert_that
impl ham::Matcher<Execs> 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<Execs> 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<ProcessBuilder> for Execs {
fn matches(&self, process: ProcessBuilder) -> ham::MatchResult {
self.match_process(&process)
Expand Down Expand Up @@ -1225,6 +1328,8 @@ impl<'t> ham::Matcher<Output> for &'t mut Execs {

pub fn execs() -> Execs {
Execs {
ran: false,
process_builder: None,
expect_stdout: None,
expect_stderr: None,
expect_stdin: None,
Expand Down

0 comments on commit b5ee363

Please sign in to comment.