From 4d8ebd605cb666814809479f90d6f84e56f45b6a Mon Sep 17 00:00:00 2001 From: "Tristan Bourvon (BurningMind)" Date: Sat, 13 May 2017 22:50:55 +0200 Subject: [PATCH 1/4] Add the check_test profile to enable cargo ckecking of unit tests This allows users to check their unit test code with cargo check. This functionality is associated with --tests, and is enabled by default when no arguments are passed. --- src/cargo/core/manifest.rs | 9 +++++++++ src/cargo/core/workspace.rs | 1 + src/cargo/ops/cargo_clean.rs | 4 ++-- src/cargo/ops/cargo_compile.rs | 11 ++++++++++- src/cargo/util/toml.rs | 2 ++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index e0b8d36016d..993be73d0bd 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -170,6 +170,7 @@ pub struct Profiles { pub doc: Profile, pub custom_build: Profile, pub check: Profile, + pub check_test: Profile, pub doctest: Profile, } @@ -598,6 +599,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 5015448a569..ac083919bde 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -505,6 +505,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 1256787f4f8..887f39d84a8 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -557,11 +557,20 @@ 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 { .. } => true, + CompileFilter::Only { tests, .. } => match tests { + FilterRule::All => true, + 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, }; diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 771f48ddfda..67563147f87 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1449,6 +1449,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 From eb4f6381f5e3eb8e58b91798c9144aa5edc39dee Mon Sep 17 00:00:00 2001 From: "Tristan Bourvon (BurningMind)" Date: Sat, 13 May 2017 22:51:30 +0200 Subject: [PATCH 2/4] Add unit tests for cargo checking unit tests --- tests/check.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/check.rs b/tests/check.rs index 09ebfb0ac44..d8702073a85 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -393,3 +393,84 @@ 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(101)); +} + +#[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("--lib").arg("--tests"), + execs().with_status(101)); +} + +#[test] +fn check_no_unit_test() { + 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("--lib"), + execs().with_status(0)); +} From 36cd8224d7a85aedb078f9bc8663ae74354544dc Mon Sep 17 00:00:00 2001 From: Tristan 'BurningMind' Bourvon Date: Tue, 16 May 2017 23:25:18 +0200 Subject: [PATCH 3/4] Fix behaviour to not check unit tests by default (with no arguments) --- src/cargo/ops/cargo_compile.rs | 4 ++-- tests/check.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 887f39d84a8..c02e3cb8844 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -559,7 +559,7 @@ fn generate_targets<'a>(pkg: &'a Package, let test = if release {&profiles.bench} else {&profiles.test}; let is_check_test = match *filter { - CompileFilter::Everything { .. } => true, + CompileFilter::Everything { .. } => false, CompileFilter::Only { tests, .. } => match tests { FilterRule::All => true, FilterRule::Just(_) => false, @@ -587,7 +587,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/tests/check.rs b/tests/check.rs index d8702073a85..d55183f5bbc 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -418,7 +418,7 @@ fn check_unit_test_implicit() { "#); assert_that(foo.cargo_process("check"), - execs().with_status(101)); + execs().with_status(0)); } #[test] @@ -444,12 +444,12 @@ fn check_unit_test_explicit() { } "#); - assert_that(foo.cargo_process("check").arg("--lib").arg("--tests"), + assert_that(foo.cargo_process("check").arg("--tests"), execs().with_status(101)); } #[test] -fn check_no_unit_test() { +fn check_unit_test_specific() { let foo = project("foo") .file("Cargo.toml", r#" [package] @@ -469,8 +469,9 @@ fn check_no_unit_test() { test_fn(1); } } - "#); + "#) + .file("tests/a.rs", ""); - assert_that(foo.cargo_process("check").arg("--lib"), + assert_that(foo.cargo_process("check").arg("--test").arg("a"), execs().with_status(0)); -} +} \ No newline at end of file From a4e362a2df3708fb7e1842e16a5eea69e5f91655 Mon Sep 17 00:00:00 2001 From: Tristan 'BurningMind' Bourvon Date: Mon, 22 May 2017 21:53:28 +0200 Subject: [PATCH 4/4] Fix a small bug introduced in the last commit that would sometimes select the check_test profile even when not in Check mode --- src/cargo/ops/cargo_compile.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 6d596b40e0b..d4b6923f9d3 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -561,7 +561,12 @@ fn generate_targets<'a>(pkg: &'a Package, let is_check_test = match *filter { CompileFilter::Everything { .. } => false, CompileFilter::Only { tests, .. } => match tests { - FilterRule::All => true, + FilterRule::All => { + match mode { + CompileMode::Check => true, + _ => false, + } + }, FilterRule::Just(_) => false, }, };