Skip to content

Commit

Permalink
auto merge of rust-lang#435 : alexcrichton/cargo/issue-431, r=brson
Browse files Browse the repository at this point in the history
This option is used to disable the --test flag to rustc for a test or benchmark
target in order to signal that the binary already knows how to run the testing
infrastructure.

The test/benchmark is still compiled and run as usual, and the exit code is
expected to reflect the result of the test/benchmark.

Closes rust-lang#431
  • Loading branch information
bors committed Aug 27, 2014
2 parents 6f765b3 + 1322a39 commit caf8345
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct Profile {
doc: bool,
dest: Option<String>,
plugin: bool,
harness: bool, // whether to use the test harness (--test)
}

impl Profile {
Expand All @@ -127,6 +128,7 @@ impl Profile {
dest: None,
plugin: false,
doctest: false,
harness: true,
}
}

Expand Down Expand Up @@ -189,6 +191,10 @@ impl Profile {
self.test
}

pub fn uses_test_harness(&self) -> bool {
self.harness
}

pub fn is_doctest(&self) -> bool {
self.doctest
}
Expand Down Expand Up @@ -242,6 +248,11 @@ impl Profile {
self.plugin = plugin;
self
}

pub fn harness(mut self, harness: bool) -> Profile {
self.harness = harness;
self
}
}

impl<H: hash::Writer> hash::Hash<H> for Profile {
Expand All @@ -253,6 +264,7 @@ impl<H: hash::Writer> hash::Hash<H> for Profile {
debug,
plugin,
dest: ref dest,
harness: harness,

// test flags are separated by file, not by profile hash, and
// env/doc also don't matter for the actual contents of the output
Expand All @@ -262,7 +274,7 @@ impl<H: hash::Writer> hash::Hash<H> for Profile {
test: _,
doctest: _,
} = *self;
(opt_level, debug, plugin, dest).hash(into)
(opt_level, debug, plugin, dest, harness).hash(into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn build_base_args(mut cmd: ProcessBuilder,
cmd = cmd.args(["--cfg", "ndebug"]);
}

if profile.is_test() {
if profile.is_test() && profile.uses_test_harness() {
cmd = cmd.arg("--test");
}

Expand Down
8 changes: 6 additions & 2 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ struct TomlTarget {
bench: Option<bool>,
doc: Option<bool>,
plugin: Option<bool>,
harness: Option<bool>,
}

#[deriving(Decodable,Encodable,PartialEq,Clone)]
Expand All @@ -519,6 +520,7 @@ impl TomlTarget {
bench: None,
doc: None,
plugin: None,
harness: None,
}
}
}
Expand Down Expand Up @@ -660,12 +662,13 @@ fn normalize(libs: &[TomlLibTarget],
let path = test.path.clone().unwrap_or_else(|| {
TomlString(default(test))
});
let harness = test.harness.unwrap_or(true);

// make sure this metadata is different from any same-named libs.
let mut metadata = metadata.clone();
metadata.mix(&format!("test-{}", test.name));

let profile = &Profile::default_test();
let profile = &Profile::default_test().harness(harness);
dst.push(Target::test_target(test.name.as_slice(),
&path.to_path(),
profile,
Expand All @@ -680,12 +683,13 @@ fn normalize(libs: &[TomlLibTarget],
let path = bench.path.clone().unwrap_or_else(|| {
TomlString(default(bench))
});
let harness = bench.harness.unwrap_or(true);

// make sure this metadata is different from any same-named libs.
let mut metadata = metadata.clone();
metadata.mix(&format!("bench-{}", bench.name));

let profile = &Profile::default_bench();
let profile = &Profile::default_bench().harness(harness);
dst.push(Target::bench_target(bench.name.as_slice(),
&path.to_path(),
profile,
Expand Down
11 changes: 8 additions & 3 deletions tests/test_cargo_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,26 +703,31 @@ test!(bench_dylib {
pub fn baz() {}
");

assert_that(p.cargo_process("bench"),
assert_that(p.cargo_process("bench").arg("-v"),
execs().with_status(0)
.with_stdout(format!("\
{running} [..]
{running} [..]
{running} [..]
{running} [..]
{compiling} bar v0.0.1 ({dir})
{compiling} foo v0.0.1 ({dir})
{running} target[..]release[..]bench-[..]
{running} [..]target[..]release[..]bench-[..]
running 1 test
test foo ... bench: 0 ns/iter (+/- 0)
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
{running} target[..]release[..]foo-[..]
{running} [..]target[..]release[..]foo-[..]
running 1 test
test foo ... bench: 0 ns/iter (+/- 0)
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
{doctest} foo
{running} [..]
running 0 tests
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,33 @@ test!(test_no_run {
compiling = COMPILING,
dir = p.url()).as_slice()));
})

test!(test_no_harness {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[[bin]]
name = "foo"
test = false
[[test]]
name = "bar"
path = "foo.rs"
harness = false
"#)
.file("src/main.rs", "fn main() {}")
.file("foo.rs", "fn main() {}");

assert_that(p.cargo_process("test"),
execs().with_status(0)
.with_stdout(format!("\
{compiling} foo v0.0.1 ({dir})
{running} target[..]bar-[..]
",
compiling = COMPILING, running = RUNNING,
dir = p.url()).as_slice()));
})

0 comments on commit caf8345

Please sign in to comment.