From 0277236ce570f804bd73fa61f7a8d23fd6d145ad Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Tue, 1 Aug 2023 17:41:03 +0100 Subject: [PATCH] Add tests for crate_to_bp_modules. Bug: 293289578 Test: atest cargo_embargo.test Change-Id: I9063a2dffea3a716224a1bd6a722e408b5eb0494 --- tools/cargo_embargo/Android.bp | 14 ++++++-- tools/cargo_embargo/src/bp.rs | 10 +++--- tools/cargo_embargo/src/config.rs | 2 +- tools/cargo_embargo/src/main.rs | 55 +++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/tools/cargo_embargo/Android.bp b/tools/cargo_embargo/Android.bp index 07abe450d..ca2c8f7ef 100644 --- a/tools/cargo_embargo/Android.bp +++ b/tools/cargo_embargo/Android.bp @@ -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"], +} diff --git a/tools/cargo_embargo/src/bp.rs b/tools/cargo_embargo/src/bp.rs index 9364c5bb5..7489a9f93 100644 --- a/tools/cargo_embargo/src/bp.rs +++ b/tools/cargo_embargo/src/bp.rs @@ -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, + pub map: BTreeMap, /// 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, } -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum BpValue { Object(BpProperties), Bool(bool), diff --git a/tools/cargo_embargo/src/config.rs b/tools/cargo_embargo/src/config.rs index 6c76376d6..327624635 100644 --- a/tools/cargo_embargo/src/config.rs +++ b/tools/cargo_embargo/src/config.rs @@ -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. diff --git a/tools/cargo_embargo/src/main.rs b/tools/cargo_embargo/src/main.rs index e47f22ccb..c1982e9ce 100644 --- a/tools/cargo_embargo/src/main.rs +++ b/tools/cargo_embargo/src/main.rs @@ -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 + } + }] + ); + } +}