Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package bazel |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestUniqueBazelLabels(t *testing.T) { |
| 23 | testCases := []struct { |
| 24 | originalLabels []Label |
| 25 | expectedUniqueLabels []Label |
| 26 | }{ |
| 27 | { |
| 28 | originalLabels: []Label{ |
| 29 | {Label: "a"}, |
| 30 | {Label: "b"}, |
| 31 | {Label: "a"}, |
| 32 | {Label: "c"}, |
| 33 | }, |
| 34 | expectedUniqueLabels: []Label{ |
| 35 | {Label: "a"}, |
| 36 | {Label: "b"}, |
| 37 | {Label: "c"}, |
| 38 | }, |
| 39 | }, |
| 40 | } |
| 41 | for _, tc := range testCases { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 42 | actualUniqueLabels := UniqueSortedBazelLabels(tc.originalLabels) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 43 | if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) { |
| 44 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 49 | func TestSubtractStrings(t *testing.T) { |
| 50 | testCases := []struct { |
| 51 | haystack []string |
| 52 | needle []string |
| 53 | expectedResult []string |
| 54 | }{ |
| 55 | { |
| 56 | haystack: []string{ |
| 57 | "a", |
| 58 | "b", |
| 59 | "c", |
| 60 | }, |
| 61 | needle: []string{ |
| 62 | "a", |
| 63 | }, |
| 64 | expectedResult: []string{ |
| 65 | "b", "c", |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | for _, tc := range testCases { |
| 70 | actualResult := SubtractStrings(tc.haystack, tc.needle) |
| 71 | if !reflect.DeepEqual(tc.expectedResult, actualResult) { |
| 72 | t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestSubtractBazelLabelList(t *testing.T) { |
| 78 | testCases := []struct { |
| 79 | haystack LabelList |
| 80 | needle LabelList |
| 81 | expectedResult LabelList |
| 82 | }{ |
| 83 | { |
| 84 | haystack: LabelList{ |
| 85 | Includes: []Label{ |
| 86 | {Label: "a"}, |
| 87 | {Label: "b"}, |
| 88 | {Label: "c"}, |
| 89 | }, |
| 90 | Excludes: []Label{ |
| 91 | {Label: "x"}, |
| 92 | {Label: "y"}, |
| 93 | {Label: "z"}, |
| 94 | }, |
| 95 | }, |
| 96 | needle: LabelList{ |
| 97 | Includes: []Label{ |
| 98 | {Label: "a"}, |
| 99 | }, |
| 100 | Excludes: []Label{ |
| 101 | {Label: "z"}, |
| 102 | }, |
| 103 | }, |
| 104 | // NOTE: Excludes are intentionally not subtracted |
| 105 | expectedResult: LabelList{ |
| 106 | Includes: []Label{ |
| 107 | {Label: "b"}, |
| 108 | {Label: "c"}, |
| 109 | }, |
| 110 | Excludes: []Label{ |
| 111 | {Label: "x"}, |
| 112 | {Label: "y"}, |
| 113 | {Label: "z"}, |
| 114 | }, |
| 115 | }, |
| 116 | }, |
| 117 | } |
| 118 | for _, tc := range testCases { |
| 119 | actualResult := SubtractBazelLabelList(tc.haystack, tc.needle) |
| 120 | if !reflect.DeepEqual(tc.expectedResult, actualResult) { |
| 121 | t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult) |
| 122 | } |
| 123 | } |
| 124 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 125 | func TestFirstUniqueBazelLabelList(t *testing.T) { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 126 | testCases := []struct { |
| 127 | originalLabelList LabelList |
| 128 | expectedUniqueLabelList LabelList |
| 129 | }{ |
| 130 | { |
| 131 | originalLabelList: LabelList{ |
| 132 | Includes: []Label{ |
| 133 | {Label: "a"}, |
| 134 | {Label: "b"}, |
| 135 | {Label: "a"}, |
| 136 | {Label: "c"}, |
| 137 | }, |
| 138 | Excludes: []Label{ |
| 139 | {Label: "x"}, |
| 140 | {Label: "x"}, |
| 141 | {Label: "y"}, |
| 142 | {Label: "z"}, |
| 143 | }, |
| 144 | }, |
| 145 | expectedUniqueLabelList: LabelList{ |
| 146 | Includes: []Label{ |
| 147 | {Label: "a"}, |
| 148 | {Label: "b"}, |
| 149 | {Label: "c"}, |
| 150 | }, |
| 151 | Excludes: []Label{ |
| 152 | {Label: "x"}, |
| 153 | {Label: "y"}, |
| 154 | {Label: "z"}, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | } |
| 159 | for _, tc := range testCases { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 160 | actualUniqueLabelList := FirstUniqueBazelLabelList(tc.originalLabelList) |
| 161 | if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) { |
| 162 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestUniqueSortedBazelLabelList(t *testing.T) { |
| 168 | testCases := []struct { |
| 169 | originalLabelList LabelList |
| 170 | expectedUniqueLabelList LabelList |
| 171 | }{ |
| 172 | { |
| 173 | originalLabelList: LabelList{ |
| 174 | Includes: []Label{ |
| 175 | {Label: "c"}, |
| 176 | {Label: "a"}, |
| 177 | {Label: "a"}, |
| 178 | {Label: "b"}, |
| 179 | }, |
| 180 | Excludes: []Label{ |
| 181 | {Label: "y"}, |
| 182 | {Label: "z"}, |
| 183 | {Label: "x"}, |
| 184 | {Label: "x"}, |
| 185 | }, |
| 186 | }, |
| 187 | expectedUniqueLabelList: LabelList{ |
| 188 | Includes: []Label{ |
| 189 | {Label: "a"}, |
| 190 | {Label: "b"}, |
| 191 | {Label: "c"}, |
| 192 | }, |
| 193 | Excludes: []Label{ |
| 194 | {Label: "x"}, |
| 195 | {Label: "y"}, |
| 196 | {Label: "z"}, |
| 197 | }, |
| 198 | }, |
| 199 | }, |
| 200 | } |
| 201 | for _, tc := range testCases { |
| 202 | actualUniqueLabelList := UniqueSortedBazelLabelList(tc.originalLabelList) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 203 | if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) { |
| 204 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList) |
| 205 | } |
| 206 | } |
| 207 | } |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 208 | |
| 209 | func makeLabels(labels ...string) []Label { |
| 210 | var ret []Label |
| 211 | for _, l := range labels { |
| 212 | ret = append(ret, Label{Label: l}) |
| 213 | } |
| 214 | return ret |
| 215 | } |
| 216 | |
| 217 | func makeLabelList(includes, excludes []string) LabelList { |
| 218 | return LabelList{ |
| 219 | Includes: makeLabels(includes...), |
| 220 | Excludes: makeLabels(excludes...), |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func TestResolveExcludes(t *testing.T) { |
| 225 | attr := LabelListAttribute{ |
| 226 | Value: makeLabelList( |
| 227 | []string{ |
| 228 | "all_include", |
| 229 | "arm_exclude", |
| 230 | "android_exclude", |
| 231 | }, |
| 232 | []string{"all_exclude"}, |
| 233 | ), |
| 234 | ConfigurableValues: configurableLabelLists{ |
| 235 | ArchConfigurationAxis: labelListSelectValues{ |
| 236 | "arm": makeLabelList([]string{}, []string{"arm_exclude"}), |
| 237 | "x86": makeLabelList([]string{"x86_include"}, []string{}), |
| 238 | }, |
| 239 | OsConfigurationAxis: labelListSelectValues{ |
| 240 | "android": makeLabelList([]string{}, []string{"android_exclude"}), |
| 241 | "linux": makeLabelList([]string{"linux_include"}, []string{}), |
| 242 | }, |
| 243 | OsArchConfigurationAxis: labelListSelectValues{ |
| 244 | "linux_x86": makeLabelList([]string{"linux_x86_include"}, []string{}), |
| 245 | }, |
| 246 | ProductVariableConfigurationAxis("a"): labelListSelectValues{ |
| 247 | "a": makeLabelList([]string{}, []string{"not_in_value"}), |
| 248 | }, |
| 249 | }, |
| 250 | } |
| 251 | |
| 252 | attr.ResolveExcludes() |
| 253 | |
| 254 | expectedBaseIncludes := []Label{Label{Label: "all_include"}} |
| 255 | if !reflect.DeepEqual(expectedBaseIncludes, attr.Value.Includes) { |
| 256 | t.Errorf("Expected Value includes %q, got %q", attr.Value.Includes, expectedBaseIncludes) |
| 257 | } |
| 258 | var nilLabels []Label |
| 259 | expectedConfiguredIncludes := map[ConfigurationAxis]map[string][]Label{ |
| 260 | ArchConfigurationAxis: map[string][]Label{ |
| 261 | "arm": nilLabels, |
| 262 | "x86": makeLabels("arm_exclude", "x86_include"), |
| 263 | "conditions_default": makeLabels("arm_exclude"), |
| 264 | }, |
| 265 | OsConfigurationAxis: map[string][]Label{ |
| 266 | "android": nilLabels, |
| 267 | "linux": makeLabels("android_exclude", "linux_include"), |
| 268 | "conditions_default": makeLabels("android_exclude"), |
| 269 | }, |
| 270 | OsArchConfigurationAxis: map[string][]Label{ |
| 271 | "linux_x86": makeLabels("linux_x86_include"), |
| 272 | "conditions_default": nilLabels, |
| 273 | }, |
| 274 | } |
| 275 | for _, axis := range attr.SortedConfigurationAxes() { |
| 276 | if _, ok := expectedConfiguredIncludes[axis]; !ok { |
| 277 | t.Errorf("Found unexpected axis %s", axis) |
| 278 | continue |
| 279 | } |
| 280 | expectedForAxis := expectedConfiguredIncludes[axis] |
| 281 | gotForAxis := attr.ConfigurableValues[axis] |
| 282 | if len(expectedForAxis) != len(gotForAxis) { |
| 283 | t.Errorf("Expected %d configs for %s, got %d: %s", len(expectedForAxis), axis, len(gotForAxis), gotForAxis) |
| 284 | } |
| 285 | for config, value := range gotForAxis { |
| 286 | if expected, ok := expectedForAxis[config]; ok { |
| 287 | if !reflect.DeepEqual(expected, value.Includes) { |
| 288 | t.Errorf("For %s, expected: %#v, got %#v", axis, expected, value.Includes) |
| 289 | } |
| 290 | } else { |
| 291 | t.Errorf("Got unexpected config %q for %s", config, axis) |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame^] | 296 | |
| 297 | func TestDeduplicateAxesFromBase(t *testing.T) { |
| 298 | attr := StringListAttribute{ |
| 299 | Value: []string{ |
| 300 | "all_include", |
| 301 | "arm_include", |
| 302 | "android_include", |
| 303 | "linux_x86_include", |
| 304 | }, |
| 305 | ConfigurableValues: configurableStringLists{ |
| 306 | ArchConfigurationAxis: stringListSelectValues{ |
| 307 | "arm": []string{"arm_include"}, |
| 308 | "x86": []string{"x86_include"}, |
| 309 | }, |
| 310 | OsConfigurationAxis: stringListSelectValues{ |
| 311 | "android": []string{"android_include"}, |
| 312 | "linux": []string{"linux_include"}, |
| 313 | }, |
| 314 | OsArchConfigurationAxis: stringListSelectValues{ |
| 315 | "linux_x86": {"linux_x86_include"}, |
| 316 | }, |
| 317 | ProductVariableConfigurationAxis("a"): stringListSelectValues{ |
| 318 | "a": []string{"not_in_value"}, |
| 319 | }, |
| 320 | }, |
| 321 | } |
| 322 | |
| 323 | attr.DeduplicateAxesFromBase() |
| 324 | |
| 325 | expectedBaseIncludes := []string{ |
| 326 | "all_include", |
| 327 | "arm_include", |
| 328 | "android_include", |
| 329 | "linux_x86_include", |
| 330 | } |
| 331 | if !reflect.DeepEqual(expectedBaseIncludes, attr.Value) { |
| 332 | t.Errorf("Expected Value includes %q, got %q", attr.Value, expectedBaseIncludes) |
| 333 | } |
| 334 | expectedConfiguredIncludes := configurableStringLists{ |
| 335 | ArchConfigurationAxis: stringListSelectValues{ |
| 336 | "x86": []string{"x86_include"}, |
| 337 | }, |
| 338 | OsConfigurationAxis: stringListSelectValues{ |
| 339 | "linux": []string{"linux_include"}, |
| 340 | }, |
| 341 | OsArchConfigurationAxis: stringListSelectValues{}, |
| 342 | ProductVariableConfigurationAxis("a"): stringListSelectValues{ |
| 343 | "a": []string{"not_in_value"}, |
| 344 | }, |
| 345 | } |
| 346 | for _, axis := range attr.SortedConfigurationAxes() { |
| 347 | if _, ok := expectedConfiguredIncludes[axis]; !ok { |
| 348 | t.Errorf("Found unexpected axis %s", axis) |
| 349 | continue |
| 350 | } |
| 351 | expectedForAxis := expectedConfiguredIncludes[axis] |
| 352 | gotForAxis := attr.ConfigurableValues[axis] |
| 353 | if len(expectedForAxis) != len(gotForAxis) { |
| 354 | t.Errorf("Expected %d configs for %s, got %d: %s", len(expectedForAxis), axis, len(gotForAxis), gotForAxis) |
| 355 | } |
| 356 | for config, value := range gotForAxis { |
| 357 | if expected, ok := expectedForAxis[config]; ok { |
| 358 | if !reflect.DeepEqual(expected, value) { |
| 359 | t.Errorf("For %s, expected: %#v, got %#v", axis, expected, value) |
| 360 | } |
| 361 | } else { |
| 362 | t.Errorf("Got unexpected config %q for %s", config, axis) |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |