diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 65393527aa1..d103dfd5fb4 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -246,19 +246,23 @@ fn clean_tests(toml_tests: Option<&Vec>, package_root: &Path, errors: &mut Vec) -> CargoResult> { - let inferred = infer_from_directory(&package_root.join("tests")); + let tests_path = package_root.join("tests"); + let mut result = Vec::new(); + + let inferred = infer_from_directory(&tests_path.join("bin")); + if !inferred.is_empty() { + let targets = clean_targets("test_bin", "test_bin", + toml_tests, &inferred, + package_root, errors)?; + result.extend(configure_targets(targets, &Target::bin_target)); + } + let inferred = infer_from_directory(&tests_path); let targets = clean_targets("test", "test", toml_tests, &inferred, package_root, errors)?; - let mut result = Vec::new(); - for (path, toml) in targets { - let mut target = Target::test_target(&toml.name(), path, - toml.required_features.clone()); - configure(&toml, &mut target); - result.push(target); - } + result.extend(configure_targets(targets, &Target::test_target)); Ok(result) } @@ -287,15 +291,7 @@ fn clean_benches(toml_benches: Option<&Vec>, errors, &mut legacy_bench_path)?; - let mut result = Vec::new(); - for (path, toml) in targets { - let mut target = Target::bench_target(&toml.name(), path, - toml.required_features.clone()); - configure(&toml, &mut target); - result.push(target); - } - - Ok(result) + Ok(configure_targets(targets, &Target::bench_target)) } fn clean_targets(target_kind_human: &str, target_kind: &str, @@ -462,6 +458,17 @@ fn configure(toml: &TomlTarget, target: &mut Target) { }); } +fn configure_targets(targets: Vec<(PathBuf, TomlTarget)>, + f: &Fn(&str, PathBuf, Option>) -> Target) -> Vec { + let mut result = Vec::new(); + for (path, toml) in targets { + let mut target = f(&toml.name(), path, toml.required_features.clone()); + configure(&toml, &mut target); + result.push(target); + } + result +} + fn target_path(target: &TomlTarget, inferred: &[(String, PathBuf)], target_kind: &str, diff --git a/tests/test.rs b/tests/test.rs index 2a454f043c4..8a7f6a59846 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -2939,3 +2939,24 @@ fn test_hint_not_masked_by_doctest() { .with_stderr_contains("[ERROR] test failed, to rerun pass \ '--test integ'")); } + +#[test] +fn cargo_test_bin_helper() { + let p = project("foo") + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", "pub fn main() {}") + .file("tests/bin/bin_helper.rs", "pub fn main() {}") + .file("tests/bin.rs", r#" + #[test] + fn test_helper() { + use std::process::Command; + + let status = Command::new("target/debug/bin_helper") + .status() + .expect("Failed to start binary helper"); + assert!(status.success()); + }"#) + .build(); + + assert_that(p.cargo("test"), execs().with_status(0)); +}