From af677038b3d4703fde854b9c0723b63b1e2e0015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Wed, 17 May 2023 16:18:25 +0200 Subject: [PATCH] aconfig: dump: support multiple caches Teach `aconfig dump` to read multiple caches at the same time. A cache represents a single namespace, and the Android build creates multiple caches for different namespaces. By passing in all those cache files to `aconfig dump`, aconfig will create a complete overview of all flags in the Android tree. Caches are traversed in order of namespace to produce the same output regardless of the order the cache files are given on the command line. If two caches use the same namespace, their order with respect to each other is undefined. Bug: 279485059 Test: atest aconfig.test Change-Id: I54c3950bbb7b2be7d96c8928e78ae83aa626c2e2 --- tools/aconfig/src/commands.rs | 106 ++++++++++++++++++++++++---------- tools/aconfig/src/main.rs | 13 +++-- 2 files changed, 84 insertions(+), 35 deletions(-) diff --git a/tools/aconfig/src/commands.rs b/tools/aconfig/src/commands.rs index 0bdb0b5f57..5a56af829f 100644 --- a/tools/aconfig/src/commands.rs +++ b/tools/aconfig/src/commands.rs @@ -107,29 +107,35 @@ pub enum DumpFormat { Protobuf, } -pub fn dump_cache(cache: Cache, format: DumpFormat) -> Result> { - match format { - DumpFormat::Text => { - let mut lines = vec![]; - for item in cache.iter() { - lines.push(format!("{}: {:?}\n", item.name, item.state)); +pub fn dump_cache(mut caches: Vec, format: DumpFormat) -> Result> { + let mut output = Vec::new(); + caches.sort_by_cached_key(|cache| cache.namespace().to_string()); + for cache in caches.into_iter() { + match format { + DumpFormat::Text => { + let mut lines = vec![]; + for item in cache.iter() { + lines.push(format!( + "{}/{}: {:?} {:?}\n", + item.namespace, item.name, item.state, item.permission + )); + } + output.append(&mut lines.concat().into()); } - Ok(lines.concat().into()) - } - DumpFormat::Debug => { - let mut lines = vec![]; - for item in cache.iter() { - lines.push(format!("{:?}\n", item)); + DumpFormat::Debug => { + let mut lines = vec![]; + for item in cache.iter() { + lines.push(format!("{:?}\n", item)); + } + output.append(&mut lines.concat().into()); + } + DumpFormat::Protobuf => { + let parsed_flags: ProtoParsedFlags = cache.into(); + parsed_flags.write_to_vec(&mut output)?; } - Ok(lines.concat().into()) - } - DumpFormat::Protobuf => { - let parsed_flags: ProtoParsedFlags = cache.into(); - let mut output = vec![]; - parsed_flags.write_to_vec(&mut output)?; - Ok(output) } } + Ok(output) } #[cfg(test)] @@ -137,9 +143,9 @@ mod tests { use super::*; use crate::aconfig::{FlagState, Permission}; - fn create_test_cache() -> Cache { + fn create_test_cache_ns1() -> Cache { let s = r#" - namespace: "ns" + namespace: "ns1" flag { name: "a" description: "Description of a" @@ -152,28 +158,49 @@ mod tests { let declarations = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }]; let o = r#" flag_value { - namespace: "ns" + namespace: "ns1" name: "a" state: DISABLED permission: READ_ONLY } "#; let values = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }]; - create_cache("ns", declarations, values).unwrap() + create_cache("ns1", declarations, values).unwrap() + } + + fn create_test_cache_ns2() -> Cache { + let s = r#" + namespace: "ns2" + flag { + name: "c" + description: "Description of c" + } + "#; + let declarations = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }]; + let o = r#" + flag_value { + namespace: "ns2" + name: "c" + state: DISABLED + permission: READ_ONLY + } + "#; + let values = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }]; + create_cache("ns2", declarations, values).unwrap() } #[test] fn test_create_cache() { - let cache = create_test_cache(); // calls create_cache - let item = cache.iter().find(|&item| item.name == "a").unwrap(); + let caches = create_test_cache_ns1(); // calls create_cache + let item = caches.iter().find(|&item| item.name == "a").unwrap(); assert_eq!(FlagState::Disabled, item.state); assert_eq!(Permission::ReadOnly, item.permission); } #[test] fn test_dump_text_format() { - let cache = create_test_cache(); - let bytes = dump_cache(cache, DumpFormat::Text).unwrap(); + let caches = vec![create_test_cache_ns1()]; + let bytes = dump_cache(caches, DumpFormat::Text).unwrap(); let text = std::str::from_utf8(&bytes).unwrap(); assert!(text.contains("a: Disabled")); } @@ -183,8 +210,8 @@ mod tests { use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoTracepoint}; use protobuf::Message; - let cache = create_test_cache(); - let bytes = dump_cache(cache, DumpFormat::Protobuf).unwrap(); + let caches = vec![create_test_cache_ns1()]; + let bytes = dump_cache(caches, DumpFormat::Protobuf).unwrap(); let actual = ProtoParsedFlags::parse_from_bytes(&bytes).unwrap(); assert_eq!( @@ -194,7 +221,7 @@ mod tests { let item = actual.parsed_flag.iter().find(|item| item.name == Some("b".to_string())).unwrap(); - assert_eq!(item.namespace(), "ns"); + assert_eq!(item.namespace(), "ns1"); assert_eq!(item.name(), "b"); assert_eq!(item.description(), "Description of b"); assert_eq!(item.state(), ProtoFlagState::DISABLED); @@ -205,4 +232,23 @@ mod tests { tp.set_permission(ProtoFlagPermission::READ_WRITE); assert_eq!(item.trace, vec![tp]); } + + #[test] + fn test_dump_multiple_caches() { + let caches = vec![create_test_cache_ns1(), create_test_cache_ns2()]; + let bytes = dump_cache(caches, DumpFormat::Protobuf).unwrap(); + let dump = ProtoParsedFlags::parse_from_bytes(&bytes).unwrap(); + assert_eq!( + dump.parsed_flag + .iter() + .map(|parsed_flag| format!("{}/{}", parsed_flag.namespace(), parsed_flag.name())) + .collect::>(), + vec!["ns1/a".to_string(), "ns1/b".to_string(), "ns2/c".to_string()] + ); + + let caches = vec![create_test_cache_ns2(), create_test_cache_ns1()]; + let bytes = dump_cache(caches, DumpFormat::Protobuf).unwrap(); + let dump_reversed_input = ProtoParsedFlags::parse_from_bytes(&bytes).unwrap(); + assert_eq!(dump, dump_reversed_input); + } } diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs index 6db5948013..d02307d702 100644 --- a/tools/aconfig/src/main.rs +++ b/tools/aconfig/src/main.rs @@ -56,7 +56,7 @@ fn cli() -> Command { ) .subcommand( Command::new("dump") - .arg(Arg::new("cache").long("cache").required(true)) + .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) .arg( Arg::new("format") .long("format") @@ -130,11 +130,14 @@ fn main() -> Result<()> { write_output_file_realtive_to_dir(&dir, &generated_file)?; } Some(("dump", sub_matches)) => { - let path = get_required_arg::(sub_matches, "cache")?; - let file = fs::File::open(path)?; - let cache = Cache::read_from_reader(file)?; + let mut caches = Vec::new(); + for path in sub_matches.get_many::("cache").unwrap_or_default() { + let file = fs::File::open(path)?; + let cache = Cache::read_from_reader(file)?; + caches.push(cache); + } let format = get_required_arg::(sub_matches, "format")?; - let output = commands::dump_cache(cache, *format)?; + let output = commands::dump_cache(caches, *format)?; let path = get_required_arg::(sub_matches, "out")?; let mut file: Box = if *path == "-" { Box::new(io::stdout())