diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 14087106492..c4940455d25 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -173,6 +173,7 @@ pub struct Profiles { pub doc: Profile, pub custom_build: Profile, pub check: Profile, + pub check_test: Profile, pub doctest: Profile, } @@ -604,6 +605,14 @@ impl Profile { } } + pub fn default_check_test() -> Profile { + Profile { + check: true, + test: true, + ..Profile::default_dev() + } + } + pub fn default_doctest() -> Profile { Profile { doc: true, diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 6486233a8d0..2714affbe40 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -524,6 +524,7 @@ impl<'cfg> Workspace<'cfg> { doc: Profile::default_doc(), custom_build: Profile::default_custom_build(), check: Profile::default_check(), + check_test: Profile::default_check_test(), doctest: Profile::default_doctest(), }; diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index b7c214a75d9..2456be0e7c4 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -52,10 +52,10 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> { let Profiles { ref release, ref dev, ref test, ref bench, ref doc, ref custom_build, ref test_deps, ref bench_deps, ref check, - ref doctest, + ref check_test, ref doctest, } = *profiles; let profiles = [release, dev, test, bench, doc, custom_build, - test_deps, bench_deps, check, doctest]; + test_deps, bench_deps, check, check_test, doctest]; for profile in profiles.iter() { units.push(Unit { pkg: &pkg, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 8506a4523a3..d4b6923f9d3 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -557,11 +557,25 @@ fn generate_targets<'a>(pkg: &'a Package, -> CargoResult> { let build = if release {&profiles.release} else {&profiles.dev}; let test = if release {&profiles.bench} else {&profiles.test}; + + let is_check_test = match *filter { + CompileFilter::Everything { .. } => false, + CompileFilter::Only { tests, .. } => match tests { + FilterRule::All => { + match mode { + CompileMode::Check => true, + _ => false, + } + }, + FilterRule::Just(_) => false, + }, + }; + let profile = match mode { CompileMode::Test => test, CompileMode::Bench => &profiles.bench, CompileMode::Build => build, - CompileMode::Check => &profiles.check, + CompileMode::Check => if is_check_test { &profiles.check_test } else { &profiles.check }, CompileMode::Doc { .. } => &profiles.doc, CompileMode::Doctest => &profiles.doctest, }; @@ -578,7 +592,7 @@ fn generate_targets<'a>(pkg: &'a Package, CompileFilter::Only { lib, bins, examples, tests, benches } => { let mut targets = Vec::new(); - if lib { + if lib || is_check_test { if let Some(t) = pkg.targets().iter().find(|t| t.is_lib()) { targets.push(BuildProposal { target: t, diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 0c0646fe25e..2d7c77471a0 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1570,6 +1570,8 @@ fn build_profiles(profiles: &Option) -> Profiles { custom_build: Profile::default_custom_build(), check: merge(Profile::default_check(), profiles.and_then(|p| p.dev.as_ref())), + check_test: merge(Profile::default_check_test(), + profiles.and_then(|p| p.dev.as_ref())), doctest: Profile::default_doctest(), }; // The test/bench targets cannot have panic=abort because they'll all get diff --git a/tests/check.rs b/tests/check.rs index 09ebfb0ac44..d55183f5bbc 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -393,3 +393,85 @@ fn check_all() { .with_stderr_contains("[..] --crate-name b b[/]src[/]main.rs [..]") ); } + +#[test] +fn check_unit_test_implicit() { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + #[cfg(test)] + mod tests { + fn test_fn(string: String) { + println!("{}", string); + } + + #[test] + fn it_works() { + test_fn(1); + } + } + "#); + + assert_that(foo.cargo_process("check"), + execs().with_status(0)); +} + +#[test] +fn check_unit_test_explicit() { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + #[cfg(test)] + mod tests { + fn test_fn(string: String) { + println!("{}", string); + } + + #[test] + fn it_works() { + test_fn(1); + } + } + "#); + + assert_that(foo.cargo_process("check").arg("--tests"), + execs().with_status(101)); +} + +#[test] +fn check_unit_test_specific() { + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + #[cfg(test)] + mod tests { + fn test_fn(string: String) { + println!("{}", string); + } + + #[test] + fn it_works() { + test_fn(1); + } + } + "#) + .file("tests/a.rs", ""); + + assert_that(foo.cargo_process("check").arg("--test").arg("a"), + execs().with_status(0)); +} \ No newline at end of file