Skip to content

Commit

Permalink
Auto merge of #3708 - matklad:assert-tests, r=alexcrichton
Browse files Browse the repository at this point in the history
Assert that we don't build test project twice.

Discussed in #3691 (comment).

I've modify the offending tests to be more explicit about recreating projects.
  • Loading branch information
bors committed Feb 21, 2017
2 parents 833c66e + 3d15df9 commit 24256af
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 129 deletions.
6 changes: 4 additions & 2 deletions tests/build-script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,9 @@ fn code_generation() {
").unwrap();
}
"#);
assert_that(p.cargo_process("run"),
p.build();

assert_that(p.cargo("run"),
execs().with_status(0)
.with_stderr("\
[COMPILING] foo v0.5.0 (file://[..])
Expand All @@ -936,7 +938,7 @@ fn code_generation() {
Hello, World!
"));

assert_that(p.cargo_process("test"),
assert_that(p.cargo("test"),
execs().with_status(0));
}

Expand Down
73 changes: 36 additions & 37 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ fn cargo_compile_incremental() {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]));
p.build();

assert_that(
p.cargo_process("build").arg("-v").env("CARGO_INCREMENTAL", "1"),
p.cargo("build").arg("-v").env("CARGO_INCREMENTAL", "1"),
execs().with_stderr_contains(
"[RUNNING] `rustc [..] -Zincremental=[..][/]target[/]debug[/]incremental`\n")
.with_status(0));

assert_that(
p.cargo_process("test").arg("-v").env("CARGO_INCREMENTAL", "1"),
p.cargo("test").arg("-v").env("CARGO_INCREMENTAL", "1"),
execs().with_stderr_contains(
"[RUNNING] `rustc [..] -Zincremental=[..][/]target[/]debug[/]incremental`\n")
.with_status(0));
Expand Down Expand Up @@ -700,22 +701,23 @@ fn cargo_compile_with_filename() {
.file("examples/a.rs", r#"
fn main() { println!("example"); }
"#);
p.build();

assert_that(p.cargo_process("build").arg("--bin").arg("bin.rs"),
assert_that(p.cargo("build").arg("--bin").arg("bin.rs"),
execs().with_status(101).with_stderr("\
[ERROR] no bin target named `bin.rs`"));

assert_that(p.cargo_process("build").arg("--bin").arg("a.rs"),
assert_that(p.cargo("build").arg("--bin").arg("a.rs"),
execs().with_status(101).with_stderr("\
[ERROR] no bin target named `a.rs`
Did you mean `a`?"));

assert_that(p.cargo_process("build").arg("--example").arg("example.rs"),
assert_that(p.cargo("build").arg("--example").arg("example.rs"),
execs().with_status(101).with_stderr("\
[ERROR] no example target named `example.rs`"));

assert_that(p.cargo_process("build").arg("--example").arg("a.rs"),
assert_that(p.cargo("build").arg("--example").arg("a.rs"),
execs().with_status(101).with_stderr("\
[ERROR] no example target named `a.rs`
Expand Down Expand Up @@ -814,9 +816,10 @@ fn cargo_default_env_metadata_env_var() {
crate_type = ["dylib"]
"#)
.file("bar/src/lib.rs", "// hello");
p.build();

// No metadata on libbar since it's a dylib path dependency
assert_that(p.cargo_process("build").arg("-v"),
assert_that(p.cargo("build").arg("-v"),
execs().with_status(0).with_stderr(&format!("\
[COMPILING] bar v0.0.1 ({url}/bar)
[RUNNING] `rustc --crate-name bar bar[/]src[/]lib.rs --crate-type dylib \
Expand All @@ -840,10 +843,10 @@ prefix = env::consts::DLL_PREFIX,
suffix = env::consts::DLL_SUFFIX,
)));

assert_that(p.cargo_process("clean"), execs().with_status(0));
assert_that(p.cargo("clean"), execs().with_status(0));

// If you set the env-var, then we expect metadata on libbar
assert_that(p.cargo_process("build").arg("-v").env("__CARGO_DEFAULT_LIB_METADATA", "1"),
assert_that(p.cargo("build").arg("-v").env("__CARGO_DEFAULT_LIB_METADATA", "1"),
execs().with_status(0).with_stderr(&format!("\
[COMPILING] bar v0.0.1 ({url}/bar)
[RUNNING] `rustc --crate-name bar bar[/]src[/]lib.rs --crate-type dylib \
Expand Down Expand Up @@ -1447,15 +1450,16 @@ fn deletion_causes_failure() {
authors = []
"#)
.file("bar/src/lib.rs", "");
p.build();

assert_that(p.cargo_process("build"), execs().with_status(0));
let p = p.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#);
assert_that(p.cargo_process("build"), execs().with_status(101));
assert_that(p.cargo("build"), execs().with_status(0));
p.change_file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#);
assert_that(p.cargo("build"), execs().with_status(101));
}

#[test]
Expand Down Expand Up @@ -2356,7 +2360,6 @@ fn build_multiple_packages() {
doctest = false
"#)
.file("d2/src/main.rs", "fn main() { println!(\"d2\"); }");
p.build();

assert_that(p.cargo_process("build").arg("-p").arg("d1").arg("-p").arg("d2")
.arg("-p").arg("foo"),
Expand Down Expand Up @@ -2408,12 +2411,12 @@ fn invalid_spec() {
.file("d1/src/main.rs", "fn main() { println!(\"d1\"); }");
p.build();

assert_that(p.cargo_process("build").arg("-p").arg("notAValidDep"),
assert_that(p.cargo("build").arg("-p").arg("notAValidDep"),
execs().with_status(101).with_stderr("\
[ERROR] package id specification `notAValidDep` matched no packages
"));

assert_that(p.cargo_process("build").arg("-p").arg("d1").arg("-p").arg("notAValidDep"),
assert_that(p.cargo("build").arg("-p").arg("d1").arg("-p").arg("notAValidDep"),
execs().with_status(101).with_stderr("\
[ERROR] package id specification `notAValidDep` matched no packages
"));
Expand Down Expand Up @@ -2456,22 +2459,23 @@ fn panic_abort_compiles_with_panic_abort() {

#[test]
fn explicit_color_config_is_propagated_to_rustc() {
let mut p = project("foo");
p = p
.file("Cargo.toml", r#"
[package]
name = "test"
version = "0.0.0"
authors = []
"#)
.file("src/lib.rs", "");
let p = project("foo")
.file("Cargo.toml", r#"
[package]
assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("always"),
name = "test"
version = "0.0.0"
authors = []
"#)
.file("src/lib.rs", "");
p.build();
assert_that(p.cargo("build").arg("-v").arg("--color").arg("always"),
execs().with_status(0).with_stderr_contains(
"[..]rustc [..] src[/]lib.rs --color always[..]"));

assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("never"),
assert_that(p.cargo("clean"), execs().with_status(0));

assert_that(p.cargo("build").arg("-v").arg("--color").arg("never"),
execs().with_status(0).with_stderr("\
[COMPILING] test v0.0.0 ([..])
[RUNNING] `rustc [..] --color never [..]`
Expand Down Expand Up @@ -2576,7 +2580,6 @@ fn wrong_message_format_option() {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}");
p.build();

assert_that(p.cargo_process("build").arg("--message-format").arg("XML"),
execs().with_status(1)
Expand Down Expand Up @@ -2659,7 +2662,6 @@ fn cargo_build_empty_target() {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}");
p.build();

assert_that(p.cargo_process("build").arg("--target").arg(""),
execs().with_status(101)
Expand Down Expand Up @@ -2690,7 +2692,6 @@ fn build_all_workspace() {
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#);
p.build();

assert_that(p.cargo_process("build")
.arg("--all"),
Expand Down Expand Up @@ -2723,7 +2724,6 @@ fn build_all_virtual_manifest() {
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#);
p.build();

// The order in which foo and bar are built is not guaranteed
assert_that(p.cargo_process("build")
Expand Down Expand Up @@ -2754,7 +2754,6 @@ fn build_all_member_dependency_same_name() {
.file("a/src/lib.rs", r#"
pub fn a() {}
"#);
p.build();

Package::new("a", "0.1.0").publish();

Expand Down
18 changes: 16 additions & 2 deletions tests/cargotest/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::Cell;
use std::env;
use std::error::Error;
use std::ffi::OsStr;
Expand Down Expand Up @@ -95,7 +96,8 @@ pub struct ProjectBuilder {
name: String,
root: PathBuf,
files: Vec<FileBuilder>,
symlinks: Vec<SymlinkBuilder>
symlinks: Vec<SymlinkBuilder>,
is_build: Cell<bool>,
}

impl ProjectBuilder {
Expand All @@ -104,7 +106,8 @@ impl ProjectBuilder {
name: name.to_string(),
root: root,
files: vec![],
symlinks: vec![]
symlinks: vec![],
is_build: Cell::new(false),
}
}

Expand Down Expand Up @@ -159,6 +162,9 @@ impl ProjectBuilder {
}

pub fn cargo(&self, cmd: &str) -> ProcessBuilder {
assert!(self.is_build.get(),
"call `.build()` before calling `.cargo()`, \
or use `.cargo_process()`");
let mut p = self.process(&cargo_dir().join("cargo"));
p.arg(cmd);
return p;
Expand All @@ -175,6 +181,11 @@ impl ProjectBuilder {
self
}

pub fn change_file(&self, path: &str, body: &str) {
assert!(self.is_build.get());
FileBuilder::new(self.root.join(path), body).mk()
}

pub fn symlink<T: AsRef<Path>>(mut self, dst: T,
src: T) -> ProjectBuilder {
self.symlinks.push(SymlinkBuilder::new(self.root.join(dst),
Expand All @@ -184,6 +195,9 @@ impl ProjectBuilder {

// TODO: return something different than a ProjectBuilder
pub fn build(&self) -> &ProjectBuilder {
assert!(!self.is_build.get(),
"can `.build()` project only once");
self.is_build.set(true);
// First, clean the directory if it already exists
self.rm_root();

Expand Down
13 changes: 8 additions & 5 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ fn check_build() {
::bar::baz();
}
"#);
foo.build();

let bar = project("bar")
.file("Cargo.toml", r#"
[package]
Expand All @@ -175,9 +177,9 @@ fn check_build() {
"#);
bar.build();

assert_that(foo.cargo_process("check"),
assert_that(foo.cargo("check"),
execs().with_status(0));
assert_that(foo.cargo_process("build"),
assert_that(foo.cargo("build"),
execs().with_status(0));
}

Expand All @@ -203,6 +205,8 @@ fn build_check() {
::bar::baz();
}
"#);
foo.build();

let bar = project("bar")
.file("Cargo.toml", r#"
[package]
Expand All @@ -215,9 +219,9 @@ fn build_check() {
"#);
bar.build();

assert_that(foo.cargo_process("build"),
assert_that(foo.cargo("build"),
execs().with_status(0));
assert_that(foo.cargo_process("check"),
assert_that(foo.cargo("check"),
execs().with_status(0));
}

Expand All @@ -240,7 +244,6 @@ fn issue_3418() {
"#)
.file("src/lib.rs", "")
.file("src/main.rs", "fn main() {}");
foo.build();

assert_that(foo.cargo_process("check").arg("-v"),
execs().with_status(0)
Expand Down
9 changes: 4 additions & 5 deletions tests/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ fn clean_multiple_packages() {
name = "d2"
"#)
.file("d2/src/main.rs", "fn main() { println!(\"d2\"); }");
p.build();

assert_that(p.cargo_process("build").arg("-p").arg("d1").arg("-p").arg("d2")
.arg("-p").arg("foo"),
Expand Down Expand Up @@ -120,7 +119,7 @@ fn clean_release() {
.file("a/src/lib.rs", "");
p.build();

assert_that(p.cargo_process("build").arg("--release"),
assert_that(p.cargo("build").arg("--release"),
execs().with_status(0));

assert_that(p.cargo("clean").arg("-p").arg("foo"),
Expand Down Expand Up @@ -164,7 +163,7 @@ fn build_script() {
.file("a/src/lib.rs", "");
p.build();

assert_that(p.cargo_process("build").env("FIRST", "1"),
assert_that(p.cargo("build").env("FIRST", "1"),
execs().with_status(0));
assert_that(p.cargo("clean").arg("-p").arg("foo"),
execs().with_status(0));
Expand Down Expand Up @@ -203,7 +202,7 @@ fn clean_git() {
.file("src/main.rs", "fn main() {}");
p.build();

assert_that(p.cargo_process("build"),
assert_that(p.cargo("build"),
execs().with_status(0));
assert_that(p.cargo("clean").arg("-p").arg("dep"),
execs().with_status(0).with_stdout(""));
Expand All @@ -228,7 +227,7 @@ fn registry() {

Package::new("bar", "0.1.0").publish();

assert_that(p.cargo_process("build"),
assert_that(p.cargo("build"),
execs().with_status(0));
assert_that(p.cargo("clean").arg("-p").arg("bar"),
execs().with_status(0).with_stdout(""));
Expand Down
Loading

0 comments on commit 24256af

Please sign in to comment.