blob: 2c63fefdbceea823283c6bb2a60c947eb88d1617 [file] [log] [blame]
Mårten Kongstad83a87602023-06-02 11:20:15 +02001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#[cfg(test)]
18pub mod test_utils {
Mårten Kongstad403658f2023-06-14 09:51:56 +020019 use crate::commands::Input;
20 use crate::protos::ProtoParsedFlags;
Mårten Kongstadb0255072023-06-08 10:15:43 +020021 use itertools;
Mårten Kongstad83a87602023-06-02 11:20:15 +020022
Mårten Kongstad403658f2023-06-14 09:51:56 +020023 pub const TEST_PACKAGE: &str = "com.android.aconfig.test";
24
Mårten Kongstad3228b292023-06-26 10:17:42 +020025 pub const TEST_FLAGS_TEXTPROTO: &str = r#"
26parsed_flag {
27 package: "com.android.aconfig.test"
28 name: "disabled_ro"
29 namespace: "aconfig_test"
30 description: "This flag is DISABLED + READ_ONLY"
31 bug: "123"
32 state: DISABLED
33 permission: READ_ONLY
34 trace {
35 source: "tests/test.aconfig"
36 state: DISABLED
37 permission: READ_WRITE
38 }
39 trace {
40 source: "tests/first.values"
41 state: DISABLED
42 permission: READ_ONLY
43 }
Zhi Dou71f1b352023-08-21 22:49:46 +000044 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +000045 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000046 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -050047 metadata {
48 purpose: PURPOSE_UNSPECIFIED
49 }
Mårten Kongstad3228b292023-06-26 10:17:42 +020050}
51parsed_flag {
52 package: "com.android.aconfig.test"
53 name: "disabled_rw"
54 namespace: "aconfig_test"
55 description: "This flag is DISABLED + READ_WRITE"
56 bug: "456"
57 state: DISABLED
58 permission: READ_WRITE
59 trace {
60 source: "tests/test.aconfig"
61 state: DISABLED
62 permission: READ_WRITE
63 }
Zhi Dou71f1b352023-08-21 22:49:46 +000064 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +000065 is_exported: true
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000066 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -050067 metadata {
68 purpose: PURPOSE_UNSPECIFIED
69 }
Zhi Dou71f1b352023-08-21 22:49:46 +000070}
71parsed_flag {
72 package: "com.android.aconfig.test"
Ted Bauer4a6af782023-11-29 15:44:24 +000073 name: "disabled_rw_exported"
74 namespace: "aconfig_test"
75 description: "This flag is exported"
76 bug: "111"
77 state: DISABLED
78 permission: READ_WRITE
79 trace {
80 source: "tests/test.aconfig"
81 state: DISABLED
82 permission: READ_WRITE
83 }
84 trace {
85 source: "tests/first.values"
86 state: DISABLED
87 permission: READ_WRITE
88 }
89 is_fixed_read_only: false
90 is_exported: true
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000091 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -050092 metadata {
93 purpose: PURPOSE_UNSPECIFIED
94 }
Ted Bauer4a6af782023-11-29 15:44:24 +000095}
96parsed_flag {
97 package: "com.android.aconfig.test"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010098 name: "disabled_rw_in_other_namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000099 namespace: "other_namespace"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +0100100 description: "This flag is DISABLED + READ_WRITE, and is defined in another namespace"
Ted Bauerc3073782023-11-15 18:04:54 +0000101 bug: "999"
102 state: DISABLED
103 permission: READ_WRITE
104 trace {
105 source: "tests/test.aconfig"
106 state: DISABLED
107 permission: READ_WRITE
108 }
109 trace {
110 source: "tests/first.values"
111 state: DISABLED
112 permission: READ_WRITE
113 }
114 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000115 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000116 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -0500117 metadata {
118 purpose: PURPOSE_UNSPECIFIED
119 }
Ted Bauerc3073782023-11-15 18:04:54 +0000120}
121parsed_flag {
122 package: "com.android.aconfig.test"
Zhi Dou71f1b352023-08-21 22:49:46 +0000123 name: "enabled_fixed_ro"
124 namespace: "aconfig_test"
125 description: "This flag is fixed READ_ONLY + ENABLED"
126 bug: ""
127 state: ENABLED
128 permission: READ_ONLY
129 trace {
130 source: "tests/test.aconfig"
131 state: DISABLED
132 permission: READ_ONLY
133 }
134 trace {
135 source: "tests/first.values"
136 state: ENABLED
137 permission: READ_ONLY
138 }
139 is_fixed_read_only: true
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000140 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000141 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -0500142 metadata {
143 purpose: PURPOSE_UNSPECIFIED
144 }
Mårten Kongstad3228b292023-06-26 10:17:42 +0200145}
146parsed_flag {
147 package: "com.android.aconfig.test"
148 name: "enabled_ro"
149 namespace: "aconfig_test"
150 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200151 bug: "abc"
152 state: ENABLED
153 permission: READ_ONLY
154 trace {
155 source: "tests/test.aconfig"
156 state: DISABLED
157 permission: READ_WRITE
158 }
159 trace {
160 source: "tests/first.values"
161 state: DISABLED
162 permission: READ_WRITE
163 }
164 trace {
165 source: "tests/second.values"
166 state: ENABLED
167 permission: READ_ONLY
168 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000169 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000170 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000171 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -0500172 metadata {
173 purpose: PURPOSE_BUGFIX
174 }
Mårten Kongstad3228b292023-06-26 10:17:42 +0200175}
176parsed_flag {
177 package: "com.android.aconfig.test"
178 name: "enabled_rw"
179 namespace: "aconfig_test"
180 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +0200181 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +0200182 state: ENABLED
183 permission: READ_WRITE
184 trace {
185 source: "tests/test.aconfig"
186 state: DISABLED
187 permission: READ_WRITE
188 }
189 trace {
190 source: "tests/first.values"
191 state: ENABLED
192 permission: READ_WRITE
193 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000194 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000195 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000196 container: "system"
Micha Schwab1a1a08a2023-11-28 16:14:30 -0500197 metadata {
198 purpose: PURPOSE_UNSPECIFIED
199 }
Mårten Kongstad3228b292023-06-26 10:17:42 +0200200}
201"#;
202
Mårten Kongstad403658f2023-06-14 09:51:56 +0200203 pub fn parse_test_flags() -> ProtoParsedFlags {
204 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200205 "com.android.aconfig.test",
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000206 Some("system"),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200207 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200208 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200209 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200210 }],
211 vec![
212 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200213 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200214 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200215 },
216 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200217 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200218 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200219 },
220 ],
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000221 crate::commands::DEFAULT_FLAG_PERMISSION,
Mårten Kongstad83a87602023-06-02 11:20:15 +0200222 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200223 .unwrap();
224 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200225 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200226
227 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
228 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
229 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
230 match itertools::diff_with(a, b, |left, right| left == right) {
231 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
232 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
233 }
234 Some(itertools::Diff::Shorter(_, mut left)) => {
235 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
236 }
237 Some(itertools::Diff::Longer(_, mut right)) => {
238 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
239 }
240 None => None,
241 }
242 }
243
244 #[test]
245 fn test_first_significant_code_diff() {
246 assert!(first_significant_code_diff("", "").is_none());
247 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
248 let a = r#"
249 public class A {
250 private static final String FOO = "FOO";
251 public static void main(String[] args) {
252 System.out.println("FOO=" + FOO);
253 }
254 }
255 "#;
256 let b = r#"
257 public class A {
258 private static final String FOO = "BAR";
259 public static void main(String[] args) {
260 System.out.println("foo=" + FOO);
261 }
262 }
263 "#;
264 assert_eq!(Some(r#"'private static final String FOO = "FOO";' vs 'private static final String FOO = "BAR";'"#.to_string()), first_significant_code_diff(a, b));
265 assert_eq!(
266 Some("LHS trailing data: 'b'".to_string()),
267 first_significant_code_diff("a\nb", "a")
268 );
269 assert_eq!(
270 Some("RHS trailing data: 'b'".to_string()),
271 first_significant_code_diff("a", "a\nb")
272 );
273 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200274}
275
276#[cfg(test)]
277pub use test_utils::*;