cargo/tests/bench.rs

1070 lines
30 KiB
Rust
Raw Normal View History

extern crate cargotest;
extern crate cargo;
extern crate hamcrest;
use std::str;
use cargo::util::process;
use cargotest::is_nightly;
use cargotest::support::paths::CargoPathExt;
use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest};
use hamcrest::{assert_that, existing_file};
#[test]
fn cargo_bench_simple() {
if !is_nightly() { return }
let p = project("foo")
2015-03-26 18:17:44 +00:00
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/foo.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
fn hello() -> &'static str {
"hello"
}
pub fn main() {
println!("{}", hello())
}
#[bench]
fn bench_hello(_b: &mut test::Bencher) {
assert_eq!(hello(), "hello")
}"#);
assert_that(p.cargo_process("build"), execs());
assert_that(&p.bin("foo"), existing_file());
2015-10-28 09:20:00 +00:00
assert_that(process(&p.bin("foo")),
execs().with_stdout("hello\n"));
assert_that(p.cargo("bench"),
2016-05-15 21:17:56 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.5.0 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]", p.url()))
.with_stdout_contains("test bench_hello ... bench: [..]"));
}
#[test]
fn bench_bench_implicit() {
if !is_nightly() { return }
let prj = project("foo")
.file("Cargo.toml" , r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run1(_ben: &mut test::Bencher) { }
fn main() { println!("Hello main!"); }"#)
.file("tests/other.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run3(_ben: &mut test::Bencher) { }"#)
.file("benches/mybench.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run2(_ben: &mut test::Bencher) { }"#);
assert_that(prj.cargo_process("bench").arg("--benches"),
execs().with_status(0)
.with_stderr(format!("\
[COMPILING] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]mybench-[..][EXE]
", dir = prj.url()))
.with_stdout_contains("test run2 ... bench: [..]"));
}
#[test]
fn bench_bin_implicit() {
if !is_nightly() { return }
let prj = project("foo")
.file("Cargo.toml" , r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run1(_ben: &mut test::Bencher) { }
fn main() { println!("Hello main!"); }"#)
.file("tests/other.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run3(_ben: &mut test::Bencher) { }"#)
.file("benches/mybench.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run2(_ben: &mut test::Bencher) { }"#);
assert_that(prj.cargo_process("bench").arg("--bins"),
execs().with_status(0)
.with_stderr(format!("\
[COMPILING] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
", dir = prj.url()))
.with_stdout_contains("test run1 ... bench: [..]"));
}
#[test]
fn bench_tarname() {
if !is_nightly() { return }
let prj = project("foo")
.file("Cargo.toml" , r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
Overhaul how cargo treats profiles This commit is a complete overhaul of how Cargo handles compilation profiles internally. The external interface of Cargo is not affected by this change. Previously each Target had a Profile embedded within it. Conceptually a Target is an entry in the manifest (a binary, benchmark, etc) and a Profile controlled various flags (e.g. --test, -C opt-level, etc). Each Package then contained many profiles for each possible compilation mode. For example a Package would have one target for testing a library, benchmarking a library, documenting a library, etc. When it came to building these targets, Cargo would filter out the targets listed to determine what needed to be built. This filtering was largely done based off an "environment" represented as a string. Each mode of compilation got a separate environment string like `"test"` or `"bench"`. Altogether, however, this approach had a number of drawbacks: * Examples in release mode do not currently work. This is due to how examples are classified and how release mode is handled (e.g. the "release" environment where examples are meant to be built in the "test" environment). * It is not trivial to implement `cargo test --release` today. * It is not trivial to implement `cargo build --bin foo` where *only* the binary `foo` is built. The same is true for examples. * It is not trivial to add selective building of a number of binaries/examples/etc. * Filtering the list of targets to build has some pretty hokey logic that involves pseudo-environments like "doc-all" vs "doc". This logic is duplicated in a few places and in general is quite brittle. * The TOML parser greatly affects compilation due to the time at which profiles are generated, which seems somewhat backwards. * Profiles must be overridden, but only partially, at compile time becuase only the top-level package's profile is applied. In general, this system just needed an overhaul. This commit made a single change of separating `Profile` from `Target` and then basically hit `make` until all the tests passed again. The other large architectural changes are: * Environment strings are now entirely gone. * Filters are implemented in a much more robust fashion. * Release mode is now handled much more gracefully. * The unit of compilation in the backend is no longer (package, target) but rather (package, target, profile). This change had to be propagated many location in the `cargo_rustc` module. * The backend does not filter targets to build *at all*. All filtering now happens entirely in the frontend. I'll test issues after this change lands, but the motivation behind this is to open the door to quickly fixing a number of outstanding issues against Cargo. This change itself is not intended to close many bugs.
2015-02-19 19:30:35 +00:00
.file("benches/bin1.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench] fn run1(_ben: &mut test::Bencher) { }"#)
Overhaul how cargo treats profiles This commit is a complete overhaul of how Cargo handles compilation profiles internally. The external interface of Cargo is not affected by this change. Previously each Target had a Profile embedded within it. Conceptually a Target is an entry in the manifest (a binary, benchmark, etc) and a Profile controlled various flags (e.g. --test, -C opt-level, etc). Each Package then contained many profiles for each possible compilation mode. For example a Package would have one target for testing a library, benchmarking a library, documenting a library, etc. When it came to building these targets, Cargo would filter out the targets listed to determine what needed to be built. This filtering was largely done based off an "environment" represented as a string. Each mode of compilation got a separate environment string like `"test"` or `"bench"`. Altogether, however, this approach had a number of drawbacks: * Examples in release mode do not currently work. This is due to how examples are classified and how release mode is handled (e.g. the "release" environment where examples are meant to be built in the "test" environment). * It is not trivial to implement `cargo test --release` today. * It is not trivial to implement `cargo build --bin foo` where *only* the binary `foo` is built. The same is true for examples. * It is not trivial to add selective building of a number of binaries/examples/etc. * Filtering the list of targets to build has some pretty hokey logic that involves pseudo-environments like "doc-all" vs "doc". This logic is duplicated in a few places and in general is quite brittle. * The TOML parser greatly affects compilation due to the time at which profiles are generated, which seems somewhat backwards. * Profiles must be overridden, but only partially, at compile time becuase only the top-level package's profile is applied. In general, this system just needed an overhaul. This commit made a single change of separating `Profile` from `Target` and then basically hit `make` until all the tests passed again. The other large architectural changes are: * Environment strings are now entirely gone. * Filters are implemented in a much more robust fashion. * Release mode is now handled much more gracefully. * The unit of compilation in the backend is no longer (package, target) but rather (package, target, profile). This change had to be propagated many location in the `cargo_rustc` module. * The backend does not filter targets to build *at all*. All filtering now happens entirely in the frontend. I'll test issues after this change lands, but the motivation behind this is to open the door to quickly fixing a number of outstanding issues against Cargo. This change itself is not intended to close many bugs.
2015-02-19 19:30:35 +00:00
.file("benches/bin2.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench] fn run2(_ben: &mut test::Bencher) { }"#);
2016-05-20 12:02:09 +00:00
assert_that(prj.cargo_process("bench").arg("--bench").arg("bin2"),
execs().with_status(0)
.with_stderr(format!("\
[COMPILING] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]bin2-[..][EXE]
2016-05-20 12:02:09 +00:00
", dir = prj.url()))
.with_stdout_contains("test run2 ... bench: [..]"));
}
#[test]
fn cargo_bench_verbose() {
if !is_nightly() { return }
let p = project("foo")
2015-03-26 18:17:44 +00:00
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/foo.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
fn main() {}
#[bench] fn bench_hello(_b: &mut test::Bencher) {}
"#);
assert_that(p.cargo_process("bench").arg("-v").arg("hello"),
2016-05-15 21:48:11 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.5.0 ({url})
2016-11-29 23:11:58 +00:00
[RUNNING] `rustc [..] src[/]foo.rs [..]`
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] `[..]target[/]release[/]deps[/]foo-[..][EXE] hello --bench`", url = p.url()))
.with_stdout_contains("test bench_hello ... bench: [..]"));
}
#[test]
fn many_similar_names() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
pub fn foo() {}
#[bench] fn lib_bench(_b: &mut test::Bencher) {}
")
.file("src/main.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
fn main() {}
#[bench] fn bin_bench(_b: &mut test::Bencher) { foo::foo() }
")
.file("benches/foo.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
#[bench] fn bench_bench(_b: &mut test::Bencher) { foo::foo() }
"#);
let output = p.cargo_process("bench").exec_with_output().unwrap();
let output = str::from_utf8(&output.stdout).unwrap();
assert!(output.contains("test bin_bench"), "bin_bench missing\n{}", output);
assert!(output.contains("test lib_bench"), "lib_bench missing\n{}", output);
assert!(output.contains("test bench_bench"), "bench_bench missing\n{}", output);
}
#[test]
fn cargo_bench_failing_test() {
if !is_nightly() { return }
let p = project("foo")
2015-03-26 18:17:44 +00:00
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/foo.rs", r#"
2015-01-30 21:22:02 +00:00
#![feature(test)]
extern crate test;
fn hello() -> &'static str {
"hello"
}
pub fn main() {
println!("{}", hello())
}
#[bench]
fn bench_hello(_b: &mut test::Bencher) {
assert_eq!(hello(), "nope")
}"#);
assert_that(p.cargo_process("build"), execs());
assert_that(&p.bin("foo"), existing_file());
2015-10-28 09:20:00 +00:00
assert_that(process(&p.bin("foo")),
execs().with_stdout("hello\n"));
assert_that(p.cargo("bench"),
execs().with_stdout_contains("test bench_hello ... ")
2016-05-18 23:41:30 +00:00
.with_stderr_contains(format!("\
[COMPILING] foo v0.5.0 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
thread '[..]' panicked at 'assertion failed: \
`(left == right)` (left: \
`\"hello\"`, right: `\"nope\"`)', src[/]foo.rs:14
[..]
2016-05-18 23:41:30 +00:00
", p.url()))
.with_status(101));
}
#[test]
fn bench_with_lib_dep() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
[[bin]]
name = "baz"
path = "src/main.rs"
"#)
.file("src/lib.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
///
/// ```rust
/// extern crate foo;
/// fn main() {
/// println!("{}", foo::foo());
/// }
/// ```
///
pub fn foo(){}
#[bench] fn lib_bench(_b: &mut test::Bencher) {}
"#)
.file("src/main.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
fn main() {}
#[bench]
fn bin_bench(_b: &mut test::Bencher) {}
");
assert_that(p.cargo_process("bench"),
2016-05-15 21:48:11 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
[RUNNING] target[/]release[/]deps[/]baz-[..][EXE]", p.url()))
.with_stdout_contains("test lib_bench ... bench: [..]")
.with_stdout_contains("test bin_bench ... bench: [..]"));
}
#[test]
fn bench_with_deep_lib_dep() {
if !is_nightly() { return }
let p = project("bar")
.file("Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[dependencies.foo]
path = "../foo"
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
#[bench]
fn bar_bench(_b: &mut test::Bencher) {
foo::foo();
}
");
let p2 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
pub fn foo() {}
#[bench]
fn foo_bench(_b: &mut test::Bencher) {}
");
p2.build();
assert_that(p.cargo_process("bench"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] foo v0.0.1 ([..])
[COMPILING] bar v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]bar-[..][EXE]", dir = p.url()))
.with_stdout_contains("test bar_bench ... bench: [..]"));
}
#[test]
fn external_bench_explicit() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
[[bench]]
name = "bench"
path = "src/bench.rs"
"#)
.file("src/lib.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
pub fn get_hello() -> &'static str { "Hello" }
#[bench]
fn internal_bench(_b: &mut test::Bencher) {}
"#)
.file("src/bench.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
#[bench]
fn external_bench(_b: &mut test::Bencher) {}
"#);
assert_that(p.cargo_process("bench"),
2016-05-15 21:48:11 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
[RUNNING] target[/]release[/]deps[/]bench-[..][EXE]", p.url()))
.with_stdout_contains("test internal_bench ... bench: [..]")
.with_stdout_contains("test external_bench ... bench: [..]"));
}
#[test]
fn external_bench_implicit() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
pub fn get_hello() -> &'static str { "Hello" }
#[bench]
fn internal_bench(_b: &mut test::Bencher) {}
"#)
.file("benches/external.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
#[bench]
fn external_bench(_b: &mut test::Bencher) {}
"#);
assert_that(p.cargo_process("bench"),
2016-05-15 21:48:11 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
[RUNNING] target[/]release[/]deps[/]external-[..][EXE]", p.url()))
.with_stdout_contains("test internal_bench ... bench: [..]")
.with_stdout_contains("test external_bench ... bench: [..]"));
}
#[test]
fn dont_run_examples() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", r#"
"#)
.file("examples/dont-run-me-i-will-fail.rs", r#"
2014-10-30 01:59:06 +00:00
fn main() { panic!("Examples should not be run by 'cargo test'"); }
"#);
assert_that(p.cargo_process("bench"),
execs().with_status(0));
}
#[test]
fn pass_through_command_line() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench] fn foo(_b: &mut test::Bencher) {}
#[bench] fn bar(_b: &mut test::Bencher) {}
");
assert_that(p.cargo_process("bench").arg("bar"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]", dir = p.url()))
.with_stdout_contains("test bar ... bench: [..]"));
assert_that(p.cargo("bench").arg("foo"),
execs().with_status(0)
.with_stderr("[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
.with_stdout_contains("test foo ... bench: [..]"));
}
// Regression test for running cargo-bench twice with
// tests in an rlib
#[test]
fn cargo_bench_twice() {
if !is_nightly() { return }
let p = project("test_twice")
2015-03-26 18:17:44 +00:00
.file("Cargo.toml", &basic_lib_manifest("test_twice"))
.file("src/test_twice.rs", r#"
#![crate_type = "rlib"]
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench]
fn dummy_bench(b: &mut test::Bencher) { }
"#);
p.cargo_process("build");
2015-03-22 23:58:11 +00:00
for _ in 0..2 {
assert_that(p.cargo("bench"),
execs().with_status(0));
}
}
#[test]
fn lib_bin_same_name() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
[lib]
name = "foo"
[[bin]]
name = "foo"
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench] fn lib_bench(_b: &mut test::Bencher) {}
")
.file("src/main.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate foo;
extern crate test;
#[bench]
fn bin_bench(_b: &mut test::Bencher) {}
");
assert_that(p.cargo_process("bench"),
2016-05-15 21:48:11 +00:00
execs().with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]", p.url()))
.with_stdout_contains_n("test [..] ... bench: [..]", 2));
}
#[test]
fn lib_with_standard_name() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "syntax"
version = "0.0.1"
authors = []
"#)
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
/// ```
/// syntax::foo();
/// ```
pub fn foo() {}
#[bench]
fn foo_bench(_b: &mut test::Bencher) {}
")
.file("benches/bench.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate syntax;
extern crate test;
#[bench]
fn bench(_b: &mut test::Bencher) { syntax::foo() }
");
assert_that(p.cargo_process("bench"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] syntax v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]syntax-[..][EXE]
[RUNNING] target[/]release[/]deps[/]bench-[..][EXE]", dir = p.url()))
.with_stdout_contains("test foo_bench ... bench: [..]")
.with_stdout_contains("test bench ... bench: [..]"));
}
#[test]
fn lib_with_standard_name2() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "syntax"
version = "0.0.1"
authors = []
[lib]
name = "syntax"
bench = false
doctest = false
"#)
.file("src/lib.rs", "
pub fn foo() {}
")
.file("src/main.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate syntax;
extern crate test;
fn main() {}
#[bench]
fn bench(_b: &mut test::Bencher) { syntax::foo() }
");
assert_that(p.cargo_process("bench"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] syntax v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]syntax-[..][EXE]", dir = p.url()))
.with_stdout_contains("test bench ... bench: [..]"));
}
#[test]
fn bench_dylib() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[lib]
name = "foo"
crate_type = ["dylib"]
[dependencies.bar]
path = "bar"
"#)
2014-08-18 01:34:50 +00:00
.file("src/lib.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
2015-03-30 19:30:50 +00:00
extern crate bar as the_bar;
extern crate test;
2014-08-18 01:34:50 +00:00
pub fn bar() { the_bar::baz(); }
#[bench]
fn foo(_b: &mut test::Bencher) {}
2014-08-18 01:34:50 +00:00
"#)
.file("benches/bench.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
2015-03-30 19:30:50 +00:00
extern crate foo as the_foo;
extern crate test;
#[bench]
2014-08-18 01:34:50 +00:00
fn foo(_b: &mut test::Bencher) { the_foo::bar(); }
"#)
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[lib]
name = "bar"
crate_type = ["dylib"]
"#)
.file("bar/src/lib.rs", "
pub fn baz() {}
");
2014-08-27 01:58:58 +00:00
assert_that(p.cargo_process("bench").arg("-v"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] bar v0.0.1 ({dir}/bar)
[RUNNING] [..] -C opt-level=3 [..]
[COMPILING] foo v0.0.1 ({dir})
[RUNNING] [..] -C opt-level=3 [..]
[RUNNING] [..] -C opt-level=3 [..]
[RUNNING] [..] -C opt-level=3 [..]
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] `[..]target[/]release[/]deps[/]foo-[..][EXE] --bench`
[RUNNING] `[..]target[/]release[/]deps[/]bench-[..][EXE] --bench`", dir = p.url()))
.with_stdout_contains_n("test foo ... bench: [..]", 2));
p.root().move_into_the_past();
assert_that(p.cargo("bench").arg("-v"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[FRESH] bar v0.0.1 ({dir}/bar)
[FRESH] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] `[..]target[/]release[/]deps[/]foo-[..][EXE] --bench`
[RUNNING] `[..]target[/]release[/]deps[/]bench-[..][EXE] --bench`", dir = p.url()))
.with_stdout_contains_n("test foo ... bench: [..]", 2));
}
#[test]
fn bench_twice_with_build_cmd() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
"#)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
#[bench]
fn foo(_b: &mut test::Bencher) {}
");
assert_that(p.cargo_process("bench"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] foo v0.0.1 ({dir})
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]", dir = p.url()))
.with_stdout_contains("test foo ... bench: [..]"));
assert_that(p.cargo("bench"),
execs().with_status(0)
.with_stderr("[FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
.with_stdout_contains("test foo ... bench: [..]"));
}
#[test]
fn bench_with_examples() {
if !is_nightly() { return }
let p = project("testbench")
.file("Cargo.toml", r#"
[package]
name = "testbench"
version = "6.6.6"
authors = []
[[example]]
name = "teste1"
[[bench]]
name = "testb1"
"#)
.file("src/lib.rs", r#"
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate test;
2016-05-20 12:02:09 +00:00
#[cfg(test)]
use test::Bencher;
pub fn f1() {
println!("f1");
}
pub fn f2() {}
#[bench]
fn bench_bench1(_b: &mut Bencher) {
f2();
}
"#)
.file("benches/testb1.rs", "
2015-03-26 18:17:44 +00:00
#![feature(test)]
extern crate testbench;
extern crate test;
use test::Bencher;
#[bench]
fn bench_bench2(_b: &mut Bencher) {
testbench::f2();
}
")
.file("examples/teste1.rs", r#"
extern crate testbench;
fn main() {
println!("example1");
testbench::f1();
}
"#);
assert_that(p.cargo_process("bench").arg("-v"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr(&format!("\
[COMPILING] testbench v6.6.6 ({url})
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] release [optimized] target(s) in [..]
[RUNNING] `{dir}[/]target[/]release[/]deps[/]testbench-[..][EXE] --bench`
[RUNNING] `{dir}[/]target[/]release[/]deps[/]testb1-[..][EXE] --bench`",
2016-05-15 21:48:11 +00:00
dir = p.root().display(), url = p.url()))
.with_stdout_contains("test bench_bench1 ... bench: [..]")
.with_stdout_contains("test bench_bench2 ... bench: [..]"));
}
#[test]
fn test_a_bench() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
authors = []
version = "0.1.0"
[lib]
name = "foo"
test = false
doctest = false
[[bench]]
name = "b"
test = true
"#)
.file("src/lib.rs", "")
.file("benches/b.rs", r#"
#[test]
fn foo() {}
"#);
assert_that(p.cargo_process("test"),
execs().with_status(0)
2016-05-15 21:48:11 +00:00
.with_stderr("\
[COMPILING] foo v0.1.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] target[/]debug[/]deps[/]b-[..][EXE]")
.with_stdout_contains("test foo ... ok"));
}
#[test]
fn test_bench_no_run() {
if !is_nightly() { return }
2016-02-02 10:30:28 +00:00
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
authors = []
version = "0.1.0"
"#)
.file("src/lib.rs", "")
.file("benches/bbaz.rs", r#"
#![feature(test)]
2016-02-02 10:30:28 +00:00
extern crate test;
use test::Bencher;
#[bench]
fn bench_baz(_: &mut Bencher) {}
"#);
assert_that(p.cargo_process("bench").arg("--no-run"),
execs().with_status(0)
2016-05-14 21:15:22 +00:00
.with_stderr("\
[COMPILING] foo v0.1.0 ([..])
[FINISHED] release [optimized] target(s) in [..]
2016-05-12 17:06:36 +00:00
"));
}
2016-02-02 10:30:28 +00:00
#[test]
fn test_bench_multiple_packages() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
authors = []
version = "0.1.0"
[dependencies.bar]
path = "../bar"
[dependencies.baz]
path = "../baz"
"#)
.file("src/lib.rs", "");
let bar = project("bar")
.file("Cargo.toml", r#"
[project]
name = "bar"
authors = []
version = "0.1.0"
[[bench]]
name = "bbar"
test = true
"#)
.file("src/lib.rs", "")
.file("benches/bbar.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_bar(_b: &mut Bencher) {}
"#);
bar.build();
let baz = project("baz")
.file("Cargo.toml", r#"
[project]
name = "baz"
authors = []
version = "0.1.0"
[[bench]]
name = "bbaz"
test = true
"#)
.file("src/lib.rs", "")
.file("benches/bbaz.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_baz(_b: &mut Bencher) {}
"#);
baz.build();
assert_that(p.cargo_process("bench").arg("-p").arg("bar").arg("-p").arg("baz"),
execs().with_status(0)
2016-05-19 00:02:41 +00:00
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]bbaz-[..][EXE]")
.with_stdout_contains("test bench_baz ... bench: [..]")
2016-05-19 00:02:41 +00:00
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]bbar-[..][EXE]")
.with_stdout_contains("test bench_bar ... bench: [..]"));
}
#[test]
fn bench_all_workspace() {
if !is_nightly() { return }
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.1.0"
[dependencies]
bar = { path = "bar" }
[workspace]
"#)
.file("src/main.rs", r#"
fn main() {}
"#)
.file("benches/foo.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_foo(_: &mut Bencher) -> () { () }
"#)
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.1.0"
"#)
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#)
.file("bar/benches/bar.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_bar(_: &mut Bencher) -> () { () }
"#);
assert_that(p.cargo_process("bench")
.arg("--all"),
execs().with_status(0)
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]bar-[..][EXE]")
.with_stdout_contains("test bench_bar ... bench: [..]")
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
.with_stdout_contains("test bench_foo ... bench: [..]"));
}
#[test]
fn bench_all_virtual_manifest() {
if !is_nightly() { return }
let p = project("workspace")
.file("Cargo.toml", r#"
[workspace]
members = ["foo", "bar"]
"#)
.file("foo/Cargo.toml", r#"
[project]
name = "foo"
version = "0.1.0"
"#)
.file("foo/src/lib.rs", r#"
pub fn foo() {}
"#)
.file("foo/benches/foo.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_foo(_: &mut Bencher) -> () { () }
"#)
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.1.0"
"#)
.file("bar/src/lib.rs", r#"
pub fn bar() {}
"#)
.file("bar/benches/bar.rs", r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_bar(_: &mut Bencher) -> () { () }
"#);
// The order in which foo and bar are built is not guaranteed
assert_that(p.cargo_process("bench")
.arg("--all"),
execs().with_status(0)
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]bar-[..][EXE]")
.with_stdout_contains("test bench_bar ... bench: [..]")
.with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")
.with_stdout_contains("test bench_foo ... bench: [..]"));
}