blob: 8df4493250b793b0fa1db668d3201067a7940c07 [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"
Mårten Kongstad3228b292023-06-26 10:17:42 +020047}
48parsed_flag {
49 package: "com.android.aconfig.test"
50 name: "disabled_rw"
51 namespace: "aconfig_test"
52 description: "This flag is DISABLED + READ_WRITE"
53 bug: "456"
54 state: DISABLED
55 permission: READ_WRITE
56 trace {
57 source: "tests/test.aconfig"
58 state: DISABLED
59 permission: READ_WRITE
60 }
Zhi Dou71f1b352023-08-21 22:49:46 +000061 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +000062 is_exported: true
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000063 container: "system"
Zhi Dou71f1b352023-08-21 22:49:46 +000064}
65parsed_flag {
66 package: "com.android.aconfig.test"
Ted Bauer4a6af782023-11-29 15:44:24 +000067 name: "disabled_rw_exported"
68 namespace: "aconfig_test"
69 description: "This flag is exported"
70 bug: "111"
71 state: DISABLED
72 permission: READ_WRITE
73 trace {
74 source: "tests/test.aconfig"
75 state: DISABLED
76 permission: READ_WRITE
77 }
78 trace {
79 source: "tests/first.values"
80 state: DISABLED
81 permission: READ_WRITE
82 }
83 is_fixed_read_only: false
84 is_exported: true
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000085 container: "system"
Ted Bauer4a6af782023-11-29 15:44:24 +000086}
87parsed_flag {
88 package: "com.android.aconfig.test"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010089 name: "disabled_rw_in_other_namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000090 namespace: "other_namespace"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010091 description: "This flag is DISABLED + READ_WRITE, and is defined in another namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000092 bug: "999"
93 state: DISABLED
94 permission: READ_WRITE
95 trace {
96 source: "tests/test.aconfig"
97 state: DISABLED
98 permission: READ_WRITE
99 }
100 trace {
101 source: "tests/first.values"
102 state: DISABLED
103 permission: READ_WRITE
104 }
105 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000106 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000107 container: "system"
Ted Bauerc3073782023-11-15 18:04:54 +0000108}
109parsed_flag {
110 package: "com.android.aconfig.test"
Zhi Dou71f1b352023-08-21 22:49:46 +0000111 name: "enabled_fixed_ro"
112 namespace: "aconfig_test"
113 description: "This flag is fixed READ_ONLY + ENABLED"
114 bug: ""
115 state: ENABLED
116 permission: READ_ONLY
117 trace {
118 source: "tests/test.aconfig"
119 state: DISABLED
120 permission: READ_ONLY
121 }
122 trace {
123 source: "tests/first.values"
124 state: ENABLED
125 permission: READ_ONLY
126 }
127 is_fixed_read_only: true
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000128 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000129 container: "system"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200130}
131parsed_flag {
132 package: "com.android.aconfig.test"
133 name: "enabled_ro"
134 namespace: "aconfig_test"
135 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200136 bug: "abc"
137 state: ENABLED
138 permission: READ_ONLY
139 trace {
140 source: "tests/test.aconfig"
141 state: DISABLED
142 permission: READ_WRITE
143 }
144 trace {
145 source: "tests/first.values"
146 state: DISABLED
147 permission: READ_WRITE
148 }
149 trace {
150 source: "tests/second.values"
151 state: ENABLED
152 permission: READ_ONLY
153 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000154 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000155 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000156 container: "system"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200157}
158parsed_flag {
159 package: "com.android.aconfig.test"
160 name: "enabled_rw"
161 namespace: "aconfig_test"
162 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +0200163 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +0200164 state: ENABLED
165 permission: READ_WRITE
166 trace {
167 source: "tests/test.aconfig"
168 state: DISABLED
169 permission: READ_WRITE
170 }
171 trace {
172 source: "tests/first.values"
173 state: ENABLED
174 permission: READ_WRITE
175 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000176 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000177 is_exported: false
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000178 container: "system"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200179}
180"#;
181
Mårten Kongstad403658f2023-06-14 09:51:56 +0200182 pub fn parse_test_flags() -> ProtoParsedFlags {
183 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200184 "com.android.aconfig.test",
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000185 Some("system"),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200186 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200187 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200188 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200189 }],
190 vec![
191 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200192 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200193 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200194 },
195 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200196 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200197 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200198 },
199 ],
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000200 crate::commands::DEFAULT_FLAG_PERMISSION,
Mårten Kongstad83a87602023-06-02 11:20:15 +0200201 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200202 .unwrap();
203 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200204 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200205
206 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
207 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
208 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
209 match itertools::diff_with(a, b, |left, right| left == right) {
210 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
211 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
212 }
213 Some(itertools::Diff::Shorter(_, mut left)) => {
214 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
215 }
216 Some(itertools::Diff::Longer(_, mut right)) => {
217 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
218 }
219 None => None,
220 }
221 }
222
223 #[test]
224 fn test_first_significant_code_diff() {
225 assert!(first_significant_code_diff("", "").is_none());
226 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
227 let a = r#"
228 public class A {
229 private static final String FOO = "FOO";
230 public static void main(String[] args) {
231 System.out.println("FOO=" + FOO);
232 }
233 }
234 "#;
235 let b = r#"
236 public class A {
237 private static final String FOO = "BAR";
238 public static void main(String[] args) {
239 System.out.println("foo=" + FOO);
240 }
241 }
242 "#;
243 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));
244 assert_eq!(
245 Some("LHS trailing data: 'b'".to_string()),
246 first_significant_code_diff("a\nb", "a")
247 );
248 assert_eq!(
249 Some("RHS trailing data: 'b'".to_string()),
250 first_significant_code_diff("a", "a\nb")
251 );
252 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200253}
254
255#[cfg(test)]
256pub use test_utils::*;