cargo_embargo: detect and ignore harness-less tests

These currently cause cargo_embargo to fail. We should support them
eventually, but for now we'll ignore them to unblock crosvm merges.

Test: external/crosvm/android-merge-2-cargo-embargo.sh
Bug: 275386231
Change-Id: I3e156afa5672ddd6257c7caabd05346e060448fb
This commit is contained in:
Frederick Mayle
2023-03-29 14:18:44 -07:00
parent 04a1650a5f
commit 5a6dbaae7a
2 changed files with 24 additions and 3 deletions

View File

@@ -35,6 +35,8 @@ pub enum CrateType {
ProcMacro,
// --test
Test,
// "--cfg test" without --test. (Assume it is a test with the harness disabled.
TestNoHarness,
}
/// Info extracted from `CargoOut` for a crate.
@@ -383,8 +385,13 @@ impl Crate {
if out.main_src.as_os_str().is_empty() {
bail!("missing main source file");
}
// Must have at least one type.
if out.types.is_empty() {
bail!("must specify one of --test or --crate-type");
if out.cfgs.contains(&"test".to_string()) {
out.types.push(CrateType::TestNoHarness);
} else {
bail!("failed to detect crate type. did not have --crate-type or --test or '--cfg test'");
}
}
if out.types.contains(&CrateType::Test) && out.types.len() != 1 {
bail!("cannot specify both --test and --crate-type");

View File

@@ -360,7 +360,14 @@ fn write_android_bp(
};
for c in crates {
modules.extend(crate_to_bp_modules(c, cfg, package_cfg, &extra_srcs)?);
modules.extend(crate_to_bp_modules(c, cfg, package_cfg, &extra_srcs).with_context(
|| {
format!(
"failed to generate bp module for crate \"{}\" with package name \"{}\"",
c.name, c.package_name
)
},
)?);
}
if modules.is_empty() {
return Ok(());
@@ -444,10 +451,17 @@ fn crate_to_bp_modules(
let stem = "lib".to_string() + &crate_.name;
("rust_proc_macro".to_string(), stem.clone(), stem)
}
CrateType::Test => {
CrateType::Test | CrateType::TestNoHarness => {
let suffix = crate_.main_src.to_string_lossy().into_owned();
let suffix = suffix.replace('/', "_").replace(".rs", "");
let stem = crate_.package_name.clone() + "_test_" + &suffix;
if crate_type == &CrateType::TestNoHarness {
eprintln!(
"WARNING: ignoring test \"{}\" with harness=false. not supported yet",
stem
);
return Ok(Vec::new());
}
("rust_test".to_string() + host, stem.clone(), stem)
}
};