Add tests for crate_to_bp_modules.

Bug: 293289578
Test: atest cargo_embargo.test
Change-Id: I9063a2dffea3a716224a1bd6a722e408b5eb0494
This commit is contained in:
Andrew Walbran
2023-08-01 17:41:03 +01:00
parent 4c2345a3c3
commit 0277236ce5
4 changed files with 73 additions and 8 deletions

View File

@@ -16,8 +16,8 @@ package {
default_applicable_licenses: ["Android-Apache-2.0"],
}
rust_binary_host {
name: "cargo_embargo",
rust_defaults {
name: "cargo_embargo.defaults",
srcs: ["src/main.rs"],
// Disable LTO for faster builds. Don't need the performance here.
flags: ["-C lto=off"],
@@ -31,3 +31,13 @@ rust_binary_host {
"libserde_json",
],
}
rust_binary_host {
name: "cargo_embargo",
defaults: ["cargo_embargo.defaults"],
}
rust_test_host {
name: "cargo_embargo.test",
defaults: ["cargo_embargo.defaults"],
}

View File

@@ -16,16 +16,16 @@ use anyhow::Result;
use std::collections::BTreeMap;
/// Build module.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct BpModule {
module_type: String,
pub module_type: String,
pub props: BpProperties,
}
/// Properties of a build module, or of a nested object value.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct BpProperties {
map: BTreeMap<String, BpValue>,
pub map: BTreeMap<String, BpValue>,
/// A raw block of text to append after the last key-value pair, but before the closing brace.
/// For example, if you have the properties
///
@@ -44,7 +44,7 @@ pub struct BpProperties {
pub raw_block: Option<String>,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum BpValue {
Object(BpProperties),
Bool(bool),

View File

@@ -29,7 +29,7 @@ fn default_true() -> bool {
}
/// Options that apply to everything.
#[derive(Deserialize)]
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Whether to output "rust_test" modules.

View File

@@ -535,3 +535,58 @@ fn crate_to_bp_modules(
}
Ok(modules)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crate_to_bp_empty() {
let c = Crate {
name: "name".to_string(),
package_name: "package_name".to_string(),
edition: "2021".to_string(),
types: vec![],
..Default::default()
};
let cfg = Config { ..Default::default() };
let package_cfg = PackageConfig { ..Default::default() };
let modules = crate_to_bp_modules(&c, &cfg, &package_cfg, &[]).unwrap();
assert_eq!(modules, vec![]);
}
#[test]
fn crate_to_bp_minimal() {
let c = Crate {
name: "name".to_string(),
package_name: "package_name".to_string(),
edition: "2021".to_string(),
types: vec![CrateType::Lib],
..Default::default()
};
let cfg = Config { ..Default::default() };
let package_cfg = PackageConfig { ..Default::default() };
let modules = crate_to_bp_modules(&c, &cfg, &package_cfg, &[]).unwrap();
assert_eq!(
modules,
vec![BpModule {
module_type: "rust_library".to_string(),
props: BpProperties {
map: [
("cargo_env_compat".to_string(), BpValue::Bool(true)),
("crate_name".to_string(), BpValue::String("name".to_string())),
("edition".to_string(), BpValue::String("2021".to_string())),
("host_supported".to_string(), BpValue::Bool(true)),
("name".to_string(), BpValue::String("libname".to_string())),
("srcs".to_string(), BpValue::List(vec![BpValue::String("".to_string())])),
]
.into_iter()
.collect(),
raw_block: None
}
}]
);
}
}