blob: 31c67b3ce4fb71c52f6558c251507a3be29f7e89 [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"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010065 name: "disabled_rw_in_other_namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000066 namespace: "other_namespace"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010067 description: "This flag is DISABLED + READ_WRITE, and is defined in another namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000068 bug: "999"
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
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +000082 is_exported: false
Ted Bauerc3073782023-11-15 18:04:54 +000083}
84parsed_flag {
85 package: "com.android.aconfig.test"
Zhi Dou71f1b352023-08-21 22:49:46 +000086 name: "enabled_fixed_ro"
87 namespace: "aconfig_test"
88 description: "This flag is fixed READ_ONLY + ENABLED"
89 bug: ""
90 state: ENABLED
91 permission: READ_ONLY
92 trace {
93 source: "tests/test.aconfig"
94 state: DISABLED
95 permission: READ_ONLY
96 }
97 trace {
98 source: "tests/first.values"
99 state: ENABLED
100 permission: READ_ONLY
101 }
102 is_fixed_read_only: true
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000103 is_exported: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200104}
105parsed_flag {
106 package: "com.android.aconfig.test"
107 name: "enabled_ro"
108 namespace: "aconfig_test"
109 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200110 bug: "abc"
111 state: ENABLED
112 permission: READ_ONLY
113 trace {
114 source: "tests/test.aconfig"
115 state: DISABLED
116 permission: READ_WRITE
117 }
118 trace {
119 source: "tests/first.values"
120 state: DISABLED
121 permission: READ_WRITE
122 }
123 trace {
124 source: "tests/second.values"
125 state: ENABLED
126 permission: READ_ONLY
127 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000128 is_fixed_read_only: false
Oriol Prieto Gasco0b9d2892023-11-20 16:23:51 +0000129 is_exported: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200130}
131parsed_flag {
132 package: "com.android.aconfig.test"
133 name: "enabled_rw"
134 namespace: "aconfig_test"
135 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +0200136 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +0200137 state: ENABLED
138 permission: READ_WRITE
139 trace {
140 source: "tests/test.aconfig"
141 state: DISABLED
142 permission: READ_WRITE
143 }
144 trace {
145 source: "tests/first.values"
146 state: ENABLED
147 permission: READ_WRITE
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}
152"#;
153
Mårten Kongstad403658f2023-06-14 09:51:56 +0200154 pub fn parse_test_flags() -> ProtoParsedFlags {
155 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200156 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +0200157 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200158 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200159 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200160 }],
161 vec![
162 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200163 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200164 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200165 },
166 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200167 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200168 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200169 },
170 ],
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000171 crate::commands::DEFAULT_FLAG_PERMISSION,
Mårten Kongstad83a87602023-06-02 11:20:15 +0200172 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200173 .unwrap();
174 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200175 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200176
177 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
178 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
179 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
180 match itertools::diff_with(a, b, |left, right| left == right) {
181 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
182 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
183 }
184 Some(itertools::Diff::Shorter(_, mut left)) => {
185 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
186 }
187 Some(itertools::Diff::Longer(_, mut right)) => {
188 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
189 }
190 None => None,
191 }
192 }
193
194 #[test]
195 fn test_first_significant_code_diff() {
196 assert!(first_significant_code_diff("", "").is_none());
197 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
198 let a = r#"
199 public class A {
200 private static final String FOO = "FOO";
201 public static void main(String[] args) {
202 System.out.println("FOO=" + FOO);
203 }
204 }
205 "#;
206 let b = r#"
207 public class A {
208 private static final String FOO = "BAR";
209 public static void main(String[] args) {
210 System.out.println("foo=" + FOO);
211 }
212 }
213 "#;
214 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));
215 assert_eq!(
216 Some("LHS trailing data: 'b'".to_string()),
217 first_significant_code_diff("a\nb", "a")
218 );
219 assert_eq!(
220 Some("RHS trailing data: 'b'".to_string()),
221 first_significant_code_diff("a", "a\nb")
222 );
223 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200224}
225
226#[cfg(test)]
227pub use test_utils::*;