Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 1 | /* |
| 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)] |
| 18 | pub mod test_utils { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 19 | use crate::commands::Input; |
| 20 | use crate::protos::ProtoParsedFlags; |
Mårten Kongstad | b025507 | 2023-06-08 10:15:43 +0200 | [diff] [blame] | 21 | use itertools; |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 22 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 23 | pub const TEST_PACKAGE: &str = "com.android.aconfig.test"; |
| 24 | |
Mårten Kongstad | 3228b29 | 2023-06-26 10:17:42 +0200 | [diff] [blame^] | 25 | pub const TEST_FLAGS_TEXTPROTO: &str = r#" |
| 26 | parsed_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 | } |
| 45 | parsed_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 | } |
| 59 | parsed_flag { |
| 60 | package: "com.android.aconfig.test" |
| 61 | name: "enabled_ro" |
| 62 | namespace: "aconfig_test" |
| 63 | description: "This flag is ENABLED + READ_ONLY" |
| 64 | bug: "789" |
| 65 | bug: "abc" |
| 66 | state: ENABLED |
| 67 | permission: READ_ONLY |
| 68 | trace { |
| 69 | source: "tests/test.aconfig" |
| 70 | state: DISABLED |
| 71 | permission: READ_WRITE |
| 72 | } |
| 73 | trace { |
| 74 | source: "tests/first.values" |
| 75 | state: DISABLED |
| 76 | permission: READ_WRITE |
| 77 | } |
| 78 | trace { |
| 79 | source: "tests/second.values" |
| 80 | state: ENABLED |
| 81 | permission: READ_ONLY |
| 82 | } |
| 83 | } |
| 84 | parsed_flag { |
| 85 | package: "com.android.aconfig.test" |
| 86 | name: "enabled_rw" |
| 87 | namespace: "aconfig_test" |
| 88 | description: "This flag is ENABLED + READ_WRITE" |
| 89 | 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 Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 104 | pub fn parse_test_flags() -> ProtoParsedFlags { |
| 105 | let bytes = crate::commands::parse_flags( |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 106 | "com.android.aconfig.test", |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 107 | vec![Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 108 | source: "tests/test.aconfig".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 109 | reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 110 | }], |
| 111 | vec![ |
| 112 | Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 113 | source: "tests/first.values".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 114 | reader: Box::new(include_bytes!("../tests/first.values").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 115 | }, |
| 116 | Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 117 | source: "tests/second.values".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 118 | reader: Box::new(include_bytes!("../tests/second.values").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 119 | }, |
| 120 | ], |
| 121 | ) |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 122 | .unwrap(); |
| 123 | crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 124 | } |
Mårten Kongstad | b025507 | 2023-06-08 10:15:43 +0200 | [diff] [blame] | 125 | |
| 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 Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #[cfg(test)] |
| 176 | pub use test_utils::*; |