From a6589681351dc4a87b6fc268d47b7d608004b455 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Aug 2014 05:54:47 -0700 Subject: [PATCH 1/2] Add a `harness` manifest option 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 #431 --- src/cargo/core/manifest.rs | 14 +++++++++++++- src/cargo/ops/cargo_rustc/mod.rs | 2 +- src/cargo/util/toml.rs | 8 ++++++-- tests/test_cargo_test.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index fe590f119eb..174e751aa8b 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -114,6 +114,7 @@ pub struct Profile { doc: bool, dest: Option, plugin: bool, + harness: bool, // whether to use the test harness (--test) } impl Profile { @@ -127,6 +128,7 @@ impl Profile { dest: None, plugin: false, doctest: false, + harness: true, } } @@ -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 } @@ -242,6 +248,11 @@ impl Profile { self.plugin = plugin; self } + + pub fn harness(mut self, harness: bool) -> Profile { + self.harness = harness; + self + } } impl hash::Hash for Profile { @@ -253,6 +264,7 @@ impl hash::Hash 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 @@ -262,7 +274,7 @@ impl hash::Hash for Profile { test: _, doctest: _, } = *self; - (opt_level, debug, plugin, dest).hash(into) + (opt_level, debug, plugin, dest, harness).hash(into) } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index a4644e1747b..6b725426738 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -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"); } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 03445dccd8b..d205af3af9e 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -500,6 +500,7 @@ struct TomlTarget { bench: Option, doc: Option, plugin: Option, + harness: Option, } #[deriving(Decodable,Encodable,PartialEq,Clone)] @@ -519,6 +520,7 @@ impl TomlTarget { bench: None, doc: None, plugin: None, + harness: None, } } } @@ -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, @@ -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, diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index cd172fe7da9..f6f49238973 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -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())); +}) From 1322a392eb477fb289cd72ade3546fc4f4125d3e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 26 Aug 2014 18:58:58 -0700 Subject: [PATCH 2/2] Help debug a flaky windows test --- tests/test_cargo_bench.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_cargo_bench.rs b/tests/test_cargo_bench.rs index 5ce05b4e4f3..010617152f2 100644 --- a/tests/test_cargo_bench.rs +++ b/tests/test_cargo_bench.rs @@ -703,19 +703,23 @@ 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) @@ -723,6 +727,7 @@ test foo ... bench: 0 ns/iter (+/- 0) test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured {doctest} foo +{running} [..] running 0 tests