blob: 14beb9328e72925823373275af9bfa050abc344c [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 }
44}
45parsed_flag {
46 package: "com.android.aconfig.test"
47 name: "disabled_rw"
48 namespace: "aconfig_test"
49 description: "This flag is DISABLED + READ_WRITE"
50 bug: "456"
51 state: DISABLED
52 permission: READ_WRITE
53 trace {
54 source: "tests/test.aconfig"
55 state: DISABLED
56 permission: READ_WRITE
57 }
58}
59parsed_flag {
60 package: "com.android.aconfig.test"
61 name: "enabled_ro"
62 namespace: "aconfig_test"
63 description: "This flag is ENABLED + READ_ONLY"
Mårten Kongstad3228b292023-06-26 10:17:42 +020064 bug: "abc"
65 state: ENABLED
66 permission: READ_ONLY
67 trace {
68 source: "tests/test.aconfig"
69 state: DISABLED
70 permission: READ_WRITE
71 }
72 trace {
73 source: "tests/first.values"
74 state: DISABLED
75 permission: READ_WRITE
76 }
77 trace {
78 source: "tests/second.values"
79 state: ENABLED
80 permission: READ_ONLY
81 }
82}
83parsed_flag {
84 package: "com.android.aconfig.test"
85 name: "enabled_rw"
86 namespace: "aconfig_test"
87 description: "This flag is ENABLED + READ_WRITE"
Mårten Kongstad6353c6c2023-07-26 13:18:50 +020088 bug: ""
Mårten Kongstad3228b292023-06-26 10:17:42 +020089 state: ENABLED
90 permission: READ_WRITE
91 trace {
92 source: "tests/test.aconfig"
93 state: DISABLED
94 permission: READ_WRITE
95 }
96 trace {
97 source: "tests/first.values"
98 state: ENABLED
99 permission: READ_WRITE
100 }
101}
102"#;
103
Mårten Kongstad403658f2023-06-14 09:51:56 +0200104 pub fn parse_test_flags() -> ProtoParsedFlags {
105 let bytes = crate::commands::parse_flags(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200106 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +0200107 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200108 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200109 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200110 }],
111 vec![
112 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200113 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200114 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200115 },
116 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200117 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +0200118 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +0200119 },
120 ],
121 )
Mårten Kongstad403658f2023-06-14 09:51:56 +0200122 .unwrap();
123 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +0200124 }
Mårten Kongstadb0255072023-06-08 10:15:43 +0200125
126 pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
127 let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
128 let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
129 match itertools::diff_with(a, b, |left, right| left == right) {
130 Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
131 Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
132 }
133 Some(itertools::Diff::Shorter(_, mut left)) => {
134 Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
135 }
136 Some(itertools::Diff::Longer(_, mut right)) => {
137 Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
138 }
139 None => None,
140 }
141 }
142
143 #[test]
144 fn test_first_significant_code_diff() {
145 assert!(first_significant_code_diff("", "").is_none());
146 assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
147 let a = r#"
148 public class A {
149 private static final String FOO = "FOO";
150 public static void main(String[] args) {
151 System.out.println("FOO=" + FOO);
152 }
153 }
154 "#;
155 let b = r#"
156 public class A {
157 private static final String FOO = "BAR";
158 public static void main(String[] args) {
159 System.out.println("foo=" + FOO);
160 }
161 }
162 "#;
163 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));
164 assert_eq!(
165 Some("LHS trailing data: 'b'".to_string()),
166 first_significant_code_diff("a\nb", "a")
167 );
168 assert_eq!(
169 Some("RHS trailing data: 'b'".to_string()),
170 first_significant_code_diff("a", "a\nb")
171 );
172 }
Mårten Kongstad83a87602023-06-02 11:20:15 +0200173}
174
175#[cfg(test)]
176pub use test_utils::*;