Skip to content

Allow building helper binaries for tests. #4846

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

Closed
wants to merge 1 commit 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
41 changes: 24 additions & 17 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,23 @@ fn clean_tests(toml_tests: Option<&Vec<TomlTestTarget>>,
package_root: &Path,
errors: &mut Vec<String>) -> CargoResult<Vec<Target>> {

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)
}

Expand Down Expand Up @@ -287,15 +291,7 @@ fn clean_benches(toml_benches: Option<&Vec<TomlBenchTarget>>,
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,
Expand Down Expand Up @@ -462,6 +458,17 @@ fn configure(toml: &TomlTarget, target: &mut Target) {
});
}

fn configure_targets(targets: Vec<(PathBuf, TomlTarget)>,
f: &Fn(&str, PathBuf, Option<Vec<String>>) -> Target) -> Vec<Target> {
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,
Expand Down
21 changes: 21 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}