blob: bb3d1f090285f39bb82968988f80d11ae4f345da [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
Mårten Kongstad3228b292023-06-26 10:17:42 +020045}
46parsed_flag {
47 package: "com.android.aconfig.test"
48 name: "disabled_rw"
49 namespace: "aconfig_test"
50 description: "This flag is DISABLED + READ_WRITE"
51 bug: "456"
52 state: DISABLED
53 permission: READ_WRITE
54 trace {
55 source: "tests/test.aconfig"
56 state: DISABLED
57 permission: READ_WRITE
58 }
Zhi Dou71f1b352023-08-21 22:49:46 +000059 is_fixed_read_only: false
60}
61parsed_flag {
62 package: "com.android.aconfig.test"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010063 name: "disabled_rw_in_other_namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000064 namespace: "other_namespace"
Mårten Kongstadbc76a3d2023-11-17 09:37:06 +010065 description: "This flag is DISABLED + READ_WRITE, and is defined in another namespace"
Ted Bauerc3073782023-11-15 18:04:54 +000066 bug: "999"
67 state: DISABLED
68 permission: READ_WRITE
69 trace {
70 source: "tests/test.aconfig"
71 state: DISABLED
72 permission: READ_WRITE
73 }
74 trace {
75 source: "tests/first.values"
76 state: DISABLED
77 permission: READ_WRITE
78 }
79 is_fixed_read_only: false
80}
81parsed_flag {
82 package: "com.android.aconfig.test"
Zhi Dou71f1b352023-08-21 22:49:46 +000083 name: "enabled_fixed_ro"
84 namespace: "aconfig_test"
85 description: "This flag is fixed READ_ONLY + ENABLED"
86 bug: ""
87 state: ENABLED
88 permission: READ_ONLY
89 trace {
90 source: "tests/test.aconfig"
91 state: DISABLED
92 permission: READ_ONLY
93 }
94 trace {
95 source: "tests/first.values"
96 state: ENABLED
97 permission: READ_ONLY
98 }
99 is_fixed_read_only: true
Mårten Kongstad3228b292023-06-26 10:17:42 +0200100}
101parsed_flag {
102 package: "com.android.aconfig.test"
103 name: "enabled_ro"
104 namespace: "aconfig_test"
105 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +0200106 bug: "abc"
107 state: ENABLED
108 permission: READ_ONLY
109 trace {
110 source: "tests/test.aconfig"
111 state: DISABLED
112 permission: READ_WRITE
113 }
114 trace {
115 source: "tests/first.values"
116 state: DISABLED
117 permission: READ_WRITE
118 }
119 trace {
120 source: "tests/second.values"
121 state: ENABLED
122 permission: READ_ONLY
123 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000124 is_fixed_read_only: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200125}
126parsed_flag {
127 package: "com.android.aconfig.test"
128 name: "enabled_rw"
129 namespace: "aconfig_test"
130 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +0200131 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +0200132 state: ENABLED
133 permission: READ_WRITE
134 trace {
135 source: "tests/test.aconfig"
136 state: DISABLED
137 permission: READ_WRITE
138 }
139 trace {
140 source: "tests/first.values"
141 state: ENABLED
142 permission: READ_WRITE
143 }
Zhi Dou71f1b352023-08-21 22:49:46 +0000144 is_fixed_read_only: false
Mårten Kongstad3228b292023-06-26 10:17:42 +0200145}
146"#;
147
Mårten Kongstad403658f2023-06-14 09:51:56 +0200148 pub fn parse_test_flags() -> ProtoParsedFlags {
149 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200150 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +0200151 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200152 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200153 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200154 }],
155 vec![
156 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200157 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200158 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200159 },
160 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200161 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200162 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200163 },
164 ],
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000165 crate::commands::DEFAULT_FLAG_PERMISSION,
Mårten Kongstad83a87602023-06-02 11:20:15 +0200166 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200167 .unwrap();
168 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200169 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200170
171 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
172 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
173 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
174 match itertools::diff_with(a, b, |left, right| left == right) {
175 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
176 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
177 }
178 Some(itertools::Diff::Shorter(_, mut left)) => {
179 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
180 }
181 Some(itertools::Diff::Longer(_, mut right)) => {
182 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
183 }
184 None => None,
185 }
186 }
187
188 #[test]
189 fn test_first_significant_code_diff() {
190 assert!(first_significant_code_diff("", "").is_none());
191 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
192 let a = r#"
193 public class A {
194 private static final String FOO = "FOO";
195 public static void main(String[] args) {
196 System.out.println("FOO=" + FOO);
197 }
198 }
199 "#;
200 let b = r#"
201 public class A {
202 private static final String FOO = "BAR";
203 public static void main(String[] args) {
204 System.out.println("foo=" + FOO);
205 }
206 }
207 "#;
208 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));
209 assert_eq!(
210 Some("LHS trailing data: 'b'".to_string()),
211 first_significant_code_diff("a\nb", "a")
212 );
213 assert_eq!(
214 Some("RHS trailing data: 'b'".to_string()),
215 first_significant_code_diff("a", "a\nb")
216 );
217 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200218}
219
220#[cfg(test)]
221pub use test_utils::*;