Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cargo check also check unit tests #4039

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct Profiles {
pub doc: Profile,
pub custom_build: Profile,
pub check: Profile,
pub check_test: Profile,
pub doctest: Profile,
}

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 16 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,25 @@ fn generate_targets<'a>(pkg: &'a Package,
-> CargoResult<Vec<(&'a Target, &'a Profile)>> {
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,
};
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,8 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> 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
Expand Down
82 changes: 82 additions & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}