blob: 9f598d00211e191cd50576d177937f2f8b75bab4 [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
Mårten Kongstad3228b292023-06-26 10:17:42 +020046}
47parsed_flag {
48 package: "com.android.aconfig.test"
49 name: "disabled_rw"
50 namespace: "aconfig_test"
51 description: "This flag is DISABLED + READ_WRITE"
52 bug: "456"
53 state: DISABLED
54 permission: READ_WRITE
55 trace {
56 source: "tests/test.aconfig"
57 state: DISABLED
58 permission: READ_WRITE
59 }
Zhi Dou71f1b352023-08-21 22:49:46 +000060 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +000061 is_exported: true
Zhi Dou71f1b352023-08-21 22:49:46 +000062}
63parsed_flag {
64 package: "com.android.aconfig.test"
Ted Bauer4a6af782023-11-29 15:44:24 +000065 name: "disabled_rw_exported"
66 namespace: "aconfig_test"
67 description: "This flag is exported"
68 bug: "111"
69 state: DISABLED
70 permission: READ_WRITE
71 trace {
72 source: "tests/test.aconfig"
73 state: DISABLED
74 permission: READ_WRITE
75 }
76 trace {
77 source: "tests/first.values"
78 state: DISABLED
79 permission: READ_WRITE
80 }
81 is_fixed_read_only: false
82 is_exported: true
83}
84parsed_flag {
85 package: "com.android.aconfig.test"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010086 name: "disabled_rw_in_other_namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000087 namespace: "other_namespace"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010088 description: "This flag is DISABLED + READ_WRITE, and is defined in another namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000089 bug: "999"
90 state: DISABLED
91 permission: READ_WRITE
92 trace {
93 source: "tests/test.aconfig"
94 state: DISABLED
95 permission: READ_WRITE
96 }
97 trace {
98 source: "tests/first.values"
99 state: DISABLED
100 permission: READ_WRITE
101 }
102 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000103 is_exported: false
Ted Bauerc3073782023-11-15 18:04:54 +0000104}
105parsed_flag {
106 package: "com.android.aconfig.test"
Zhi Dou71f1b352023-08-21 22:49:46 +0000107 name: "enabled_fixed_ro"
108 namespace: "aconfig_test"
109 description: "This flag is fixed READ_ONLY + ENABLED"
110 bug: ""
111 state: ENABLED
112 permission: READ_ONLY
113 trace {
114 source: "tests/test.aconfig"
115 state: DISABLED
116 permission: READ_ONLY
117 }
118 trace {
119 source: "tests/first.values"
120 state: ENABLED
121 permission: READ_ONLY
122 }
123 is_fixed_read_only: true
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000124 is_exported: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200125}
126parsed_flag {
127 package: "com.android.aconfig.test"
128 name: "enabled_ro"
129 namespace: "aconfig_test"
130 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200131 bug: "abc"
132 state: ENABLED
133 permission: READ_ONLY
134 trace {
135 source: "tests/test.aconfig"
136 state: DISABLED
137 permission: READ_WRITE
138 }
139 trace {
140 source: "tests/first.values"
141 state: DISABLED
142 permission: READ_WRITE
143 }
144 trace {
145 source: "tests/second.values"
146 state: ENABLED
147 permission: READ_ONLY
148 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000149 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000150 is_exported: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200151}
152parsed_flag {
153 package: "com.android.aconfig.test"
154 name: "enabled_rw"
155 namespace: "aconfig_test"
156 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +0200157 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +0200158 state: ENABLED
159 permission: READ_WRITE
160 trace {
161 source: "tests/test.aconfig"
162 state: DISABLED
163 permission: READ_WRITE
164 }
165 trace {
166 source: "tests/first.values"
167 state: ENABLED
168 permission: READ_WRITE
169 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000170 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000171 is_exported: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200172}
173"#;
174
Mårten Kongstad403658f2023-06-14 09:51:56 +0200175 pub fn parse_test_flags() -> ProtoParsedFlags {
176 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200177 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +0200178 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200179 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200180 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200181 }],
182 vec![
183 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200184 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200185 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200186 },
187 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200188 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200189 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200190 },
191 ],
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000192 crate::commands::DEFAULT_FLAG_PERMISSION,
Mårten Kongstad83a87602023-06-02 11:20:15 +0200193 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200194 .unwrap();
195 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200196 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200197
198 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
199 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
200 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
201 match itertools::diff_with(a, b, |left, right| left == right) {
202 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
203 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
204 }
205 Some(itertools::Diff::Shorter(_, mut left)) => {
206 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
207 }
208 Some(itertools::Diff::Longer(_, mut right)) => {
209 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
210 }
211 None => None,
212 }
213 }
214
215 #[test]
216 fn test_first_significant_code_diff() {
217 assert!(first_significant_code_diff("", "").is_none());
218 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
219 let a = r#"
220 public class A {
221 private static final String FOO = "FOO";
222 public static void main(String[] args) {
223 System.out.println("FOO=" + FOO);
224 }
225 }
226 "#;
227 let b = r#"
228 public class A {
229 private static final String FOO = "BAR";
230 public static void main(String[] args) {
231 System.out.println("foo=" + FOO);
232 }
233 }
234 "#;
235 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));
236 assert_eq!(
237 Some("LHS trailing data: 'b'".to_string()),
238 first_significant_code_diff("a\nb", "a")
239 );
240 assert_eq!(
241 Some("RHS trailing data: 'b'".to_string()),
242 first_significant_code_diff("a", "a\nb")
243 );
244 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200245}
246
247#[cfg(test)]
248pub use test_utils::*;