blob: 8729381b5d2cdd491f4411264b0109c795691b8d [file] [log] [blame]
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +00001// 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
15package bazel
16
17import (
18 "reflect"
Liz Kammer57e2e7a2021-09-20 12:55:02 -040019 "strings"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000020 "testing"
Liz Kammer57e2e7a2021-09-20 12:55:02 -040021
22 "github.com/google/blueprint/proptools"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000023)
24
25func TestUniqueBazelLabels(t *testing.T) {
26 testCases := []struct {
27 originalLabels []Label
28 expectedUniqueLabels []Label
29 }{
30 {
31 originalLabels: []Label{
32 {Label: "a"},
33 {Label: "b"},
34 {Label: "a"},
35 {Label: "c"},
36 },
37 expectedUniqueLabels: []Label{
38 {Label: "a"},
39 {Label: "b"},
40 {Label: "c"},
41 },
42 },
43 }
44 for _, tc := range testCases {
Jingwen Chened9c17d2021-04-13 07:14:55 +000045 actualUniqueLabels := UniqueSortedBazelLabels(tc.originalLabels)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000046 if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) {
47 t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels)
48 }
49 }
50}
51
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000052func TestSubtractStrings(t *testing.T) {
53 testCases := []struct {
54 haystack []string
55 needle []string
56 expectedResult []string
57 }{
58 {
59 haystack: []string{
60 "a",
61 "b",
62 "c",
63 },
64 needle: []string{
65 "a",
66 },
67 expectedResult: []string{
68 "b", "c",
69 },
70 },
71 }
72 for _, tc := range testCases {
73 actualResult := SubtractStrings(tc.haystack, tc.needle)
74 if !reflect.DeepEqual(tc.expectedResult, actualResult) {
75 t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult)
76 }
77 }
78}
79
80func TestSubtractBazelLabelList(t *testing.T) {
81 testCases := []struct {
82 haystack LabelList
83 needle LabelList
84 expectedResult LabelList
85 }{
86 {
87 haystack: LabelList{
88 Includes: []Label{
89 {Label: "a"},
90 {Label: "b"},
91 {Label: "c"},
92 },
93 Excludes: []Label{
94 {Label: "x"},
95 {Label: "y"},
96 {Label: "z"},
97 },
98 },
99 needle: LabelList{
100 Includes: []Label{
101 {Label: "a"},
102 },
103 Excludes: []Label{
104 {Label: "z"},
105 },
106 },
107 // NOTE: Excludes are intentionally not subtracted
108 expectedResult: LabelList{
109 Includes: []Label{
110 {Label: "b"},
111 {Label: "c"},
112 },
113 Excludes: []Label{
114 {Label: "x"},
115 {Label: "y"},
116 {Label: "z"},
117 },
118 },
119 },
120 }
121 for _, tc := range testCases {
122 actualResult := SubtractBazelLabelList(tc.haystack, tc.needle)
123 if !reflect.DeepEqual(tc.expectedResult, actualResult) {
124 t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult)
125 }
126 }
127}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400128func TestFirstUniqueBazelLabelList(t *testing.T) {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000129 testCases := []struct {
130 originalLabelList LabelList
131 expectedUniqueLabelList LabelList
132 }{
133 {
134 originalLabelList: LabelList{
135 Includes: []Label{
136 {Label: "a"},
137 {Label: "b"},
138 {Label: "a"},
139 {Label: "c"},
140 },
141 Excludes: []Label{
142 {Label: "x"},
143 {Label: "x"},
144 {Label: "y"},
145 {Label: "z"},
146 },
147 },
148 expectedUniqueLabelList: LabelList{
149 Includes: []Label{
150 {Label: "a"},
151 {Label: "b"},
152 {Label: "c"},
153 },
154 Excludes: []Label{
155 {Label: "x"},
156 {Label: "y"},
157 {Label: "z"},
158 },
159 },
160 },
161 }
162 for _, tc := range testCases {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400163 actualUniqueLabelList := FirstUniqueBazelLabelList(tc.originalLabelList)
164 if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) {
165 t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList)
166 }
167 }
168}
169
170func TestUniqueSortedBazelLabelList(t *testing.T) {
171 testCases := []struct {
172 originalLabelList LabelList
173 expectedUniqueLabelList LabelList
174 }{
175 {
176 originalLabelList: LabelList{
177 Includes: []Label{
178 {Label: "c"},
179 {Label: "a"},
180 {Label: "a"},
181 {Label: "b"},
182 },
183 Excludes: []Label{
184 {Label: "y"},
185 {Label: "z"},
186 {Label: "x"},
187 {Label: "x"},
188 },
189 },
190 expectedUniqueLabelList: LabelList{
191 Includes: []Label{
192 {Label: "a"},
193 {Label: "b"},
194 {Label: "c"},
195 },
196 Excludes: []Label{
197 {Label: "x"},
198 {Label: "y"},
199 {Label: "z"},
200 },
201 },
202 },
203 }
204 for _, tc := range testCases {
205 actualUniqueLabelList := UniqueSortedBazelLabelList(tc.originalLabelList)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000206 if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) {
207 t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList)
208 }
209 }
210}
Liz Kammer74deed42021-06-02 13:02:03 -0400211
212func makeLabels(labels ...string) []Label {
213 var ret []Label
214 for _, l := range labels {
215 ret = append(ret, Label{Label: l})
216 }
217 return ret
218}
219
220func makeLabelList(includes, excludes []string) LabelList {
221 return LabelList{
222 Includes: makeLabels(includes...),
223 Excludes: makeLabels(excludes...),
224 }
225}
226
227func TestResolveExcludes(t *testing.T) {
228 attr := LabelListAttribute{
229 Value: makeLabelList(
230 []string{
231 "all_include",
232 "arm_exclude",
233 "android_exclude",
234 },
235 []string{"all_exclude"},
236 ),
237 ConfigurableValues: configurableLabelLists{
238 ArchConfigurationAxis: labelListSelectValues{
Jingwen Chen9af49a42021-11-02 10:27:17 +0000239 "arm": makeLabelList([]string{}, []string{"arm_exclude"}),
240 "x86": makeLabelList([]string{"x86_include"}, []string{}),
241 ConditionsDefaultConfigKey: makeLabelList([]string{"default_include"}, []string{}),
Liz Kammer74deed42021-06-02 13:02:03 -0400242 },
243 OsConfigurationAxis: labelListSelectValues{
244 "android": makeLabelList([]string{}, []string{"android_exclude"}),
245 "linux": makeLabelList([]string{"linux_include"}, []string{}),
246 },
247 OsArchConfigurationAxis: labelListSelectValues{
248 "linux_x86": makeLabelList([]string{"linux_x86_include"}, []string{}),
249 },
Alixbbfd5382022-06-09 18:52:05 +0000250 ProductVariableConfigurationAxis("product_with_defaults", NoConfigAxis): labelListSelectValues{
Jingwen Chen9af49a42021-11-02 10:27:17 +0000251 "a": makeLabelList([]string{}, []string{"not_in_value"}),
252 "b": makeLabelList([]string{"b_val"}, []string{}),
253 "c": makeLabelList([]string{"c_val"}, []string{}),
254 ConditionsDefaultConfigKey: makeLabelList([]string{"c_val", "default", "default2"}, []string{}),
255 },
Alixbbfd5382022-06-09 18:52:05 +0000256 ProductVariableConfigurationAxis("product_only_with_excludes", NoConfigAxis): labelListSelectValues{
Liz Kammer74deed42021-06-02 13:02:03 -0400257 "a": makeLabelList([]string{}, []string{"not_in_value"}),
258 },
259 },
260 }
261
262 attr.ResolveExcludes()
263
Jingwen Chen9af49a42021-11-02 10:27:17 +0000264 expectedBaseIncludes := []Label{{Label: "all_include"}}
Liz Kammer74deed42021-06-02 13:02:03 -0400265 if !reflect.DeepEqual(expectedBaseIncludes, attr.Value.Includes) {
266 t.Errorf("Expected Value includes %q, got %q", attr.Value.Includes, expectedBaseIncludes)
267 }
268 var nilLabels []Label
269 expectedConfiguredIncludes := map[ConfigurationAxis]map[string][]Label{
Jingwen Chen9af49a42021-11-02 10:27:17 +0000270 ArchConfigurationAxis: {
271 "arm": nilLabels,
272 "x86": makeLabels("arm_exclude", "x86_include"),
273 ConditionsDefaultConfigKey: makeLabels("arm_exclude", "default_include"),
Liz Kammer74deed42021-06-02 13:02:03 -0400274 },
Jingwen Chen9af49a42021-11-02 10:27:17 +0000275 OsConfigurationAxis: {
276 "android": nilLabels,
277 "linux": makeLabels("android_exclude", "linux_include"),
278 ConditionsDefaultConfigKey: makeLabels("android_exclude"),
Liz Kammer74deed42021-06-02 13:02:03 -0400279 },
Jingwen Chen9af49a42021-11-02 10:27:17 +0000280 OsArchConfigurationAxis: {
281 "linux_x86": makeLabels("linux_x86_include"),
282 ConditionsDefaultConfigKey: nilLabels,
283 },
Alixbbfd5382022-06-09 18:52:05 +0000284 ProductVariableConfigurationAxis("product_with_defaults", NoConfigAxis): {
Jingwen Chen9af49a42021-11-02 10:27:17 +0000285 "a": nilLabels,
286 "b": makeLabels("b_val"),
287 "c": makeLabels("c_val"),
288 ConditionsDefaultConfigKey: makeLabels("c_val", "default", "default2"),
Liz Kammer74deed42021-06-02 13:02:03 -0400289 },
290 }
291 for _, axis := range attr.SortedConfigurationAxes() {
292 if _, ok := expectedConfiguredIncludes[axis]; !ok {
293 t.Errorf("Found unexpected axis %s", axis)
294 continue
295 }
296 expectedForAxis := expectedConfiguredIncludes[axis]
297 gotForAxis := attr.ConfigurableValues[axis]
298 if len(expectedForAxis) != len(gotForAxis) {
299 t.Errorf("Expected %d configs for %s, got %d: %s", len(expectedForAxis), axis, len(gotForAxis), gotForAxis)
300 }
301 for config, value := range gotForAxis {
302 if expected, ok := expectedForAxis[config]; ok {
303 if !reflect.DeepEqual(expected, value.Includes) {
Jingwen Chen9af49a42021-11-02 10:27:17 +0000304 t.Errorf("For %s,\nexpected: %#v\ngot %#v", axis, expected, value.Includes)
Liz Kammer74deed42021-06-02 13:02:03 -0400305 }
306 } else {
307 t.Errorf("Got unexpected config %q for %s", config, axis)
308 }
309 }
310 }
311}
Liz Kammer5fad5012021-09-09 14:08:21 -0400312
Sam Delmericoc1130dc2022-08-25 14:43:54 -0400313func TestLabelListAttributePartition(t *testing.T) {
314 testCases := []struct {
315 name string
316 input LabelListAttribute
317 predicated LabelListAttribute
318 unpredicated LabelListAttribute
319 predicate func(label Label) bool
320 }{
321 {
322 name: "move all to predicated partition",
323 input: MakeLabelListAttribute(makeLabelList(
324 []string{"keep1", "throw1", "keep2", "throw2"},
325 []string{"keep1", "throw1", "keep2", "throw2"},
326 )),
327 predicated: MakeLabelListAttribute(makeLabelList(
328 []string{"keep1", "throw1", "keep2", "throw2"},
329 []string{"keep1", "throw1", "keep2", "throw2"},
330 )),
331 unpredicated: LabelListAttribute{},
332 predicate: func(label Label) bool {
333 return true
334 },
335 },
336 {
337 name: "move all to unpredicated partition",
338 input: MakeLabelListAttribute(makeLabelList(
339 []string{"keep1", "throw1", "keep2", "throw2"},
340 []string{"keep1", "throw1", "keep2", "throw2"},
341 )),
342 predicated: LabelListAttribute{},
343 unpredicated: MakeLabelListAttribute(makeLabelList(
344 []string{"keep1", "throw1", "keep2", "throw2"},
345 []string{"keep1", "throw1", "keep2", "throw2"},
346 )),
347 predicate: func(label Label) bool {
348 return false
349 },
350 },
351 {
352 name: "partition includes and excludes",
353 input: MakeLabelListAttribute(makeLabelList(
354 []string{"keep1", "throw1", "keep2", "throw2"},
355 []string{"keep1", "throw1", "keep2", "throw2"},
356 )),
357 predicated: MakeLabelListAttribute(makeLabelList(
358 []string{"keep1", "keep2"},
359 []string{"keep1", "keep2"},
360 )),
361 unpredicated: MakeLabelListAttribute(makeLabelList(
362 []string{"throw1", "throw2"},
363 []string{"throw1", "throw2"},
364 )),
365 predicate: func(label Label) bool {
366 return strings.HasPrefix(label.Label, "keep")
367 },
368 },
369 {
370 name: "partition excludes only",
371 input: MakeLabelListAttribute(makeLabelList(
372 []string{},
373 []string{"keep1", "throw1", "keep2", "throw2"},
374 )),
375 predicated: MakeLabelListAttribute(makeLabelList(
376 []string{},
377 []string{"keep1", "keep2"},
378 )),
379 unpredicated: MakeLabelListAttribute(makeLabelList(
380 []string{},
381 []string{"throw1", "throw2"},
382 )),
383 predicate: func(label Label) bool {
384 return strings.HasPrefix(label.Label, "keep")
385 },
386 },
387 {
388 name: "partition includes only",
389 input: MakeLabelListAttribute(makeLabelList(
390 []string{"keep1", "throw1", "keep2", "throw2"},
391 []string{},
392 )),
393 predicated: MakeLabelListAttribute(makeLabelList(
394 []string{"keep1", "keep2"},
395 []string{},
396 )),
397 unpredicated: MakeLabelListAttribute(makeLabelList(
398 []string{"throw1", "throw2"},
399 []string{},
400 )),
401 predicate: func(label Label) bool {
402 return strings.HasPrefix(label.Label, "keep")
403 },
404 },
405 {
406 name: "empty partition",
407 input: MakeLabelListAttribute(makeLabelList([]string{}, []string{})),
408 predicated: LabelListAttribute{},
409 unpredicated: LabelListAttribute{},
410 predicate: func(label Label) bool {
411 return true
412 },
413 },
414 }
415
416 for _, tc := range testCases {
417 t.Run(tc.name, func(t *testing.T) {
418 predicated, unpredicated := tc.input.Partition(tc.predicate)
419 if !predicated.Value.Equals(tc.predicated.Value) {
420 t.Errorf("expected predicated labels to be %v; got %v", tc.predicated, predicated)
421 }
422 for axis, configs := range predicated.ConfigurableValues {
423 tcConfigs, ok := tc.predicated.ConfigurableValues[axis]
424 if !ok || !reflect.DeepEqual(configs, tcConfigs) {
425 t.Errorf("expected predicated labels to be %v; got %v", tc.predicated, predicated)
426 }
427 }
428 if !unpredicated.Value.Equals(tc.unpredicated.Value) {
429 t.Errorf("expected unpredicated labels to be %v; got %v", tc.unpredicated, unpredicated)
430 }
431 for axis, configs := range unpredicated.ConfigurableValues {
432 tcConfigs, ok := tc.unpredicated.ConfigurableValues[axis]
433 if !ok || !reflect.DeepEqual(configs, tcConfigs) {
434 t.Errorf("expected unpredicated labels to be %v; got %v", tc.unpredicated, unpredicated)
435 }
436 }
437 })
438 }
439}
440
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400441// labelAddSuffixForTypeMapper returns a LabelMapper that adds suffix to label name for modules of
442// typ
443func labelAddSuffixForTypeMapper(suffix, typ string) LabelMapper {
Liz Kammer12615db2021-09-28 09:19:17 -0400444 return func(omc OtherModuleContext, label Label) (string, bool) {
445 m, ok := omc.ModuleFromName(label.Label)
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400446 if !ok {
Liz Kammer12615db2021-09-28 09:19:17 -0400447 return label.Label, false
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400448 }
449 mTyp := omc.OtherModuleType(m)
450 if typ == mTyp {
Liz Kammer12615db2021-09-28 09:19:17 -0400451 return label.Label + suffix, true
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400452 }
Liz Kammer12615db2021-09-28 09:19:17 -0400453 return label.Label, false
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400454 }
455}
456
457func TestPartitionLabelListAttribute(t *testing.T) {
458 testCases := []struct {
459 name string
Sam Delmericocc518432022-03-30 15:50:34 +0000460 ctx *OtherModuleTestContext
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400461 labelList LabelListAttribute
462 filters LabelPartitions
463 expected PartitionToLabelListAttribute
464 expectedErrMsg *string
465 }{
466 {
467 name: "no configurable values",
Sam Delmericocc518432022-03-30 15:50:34 +0000468 ctx: &OtherModuleTestContext{},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400469 labelList: LabelListAttribute{
470 Value: makeLabelList([]string{"a.a", "b.b", "c.c", "d.d", "e.e"}, []string{}),
471 },
472 filters: LabelPartitions{
473 "A": LabelPartition{Extensions: []string{".a"}},
474 "B": LabelPartition{Extensions: []string{".b"}},
475 "C": LabelPartition{Extensions: []string{".c"}},
476 },
477 expected: PartitionToLabelListAttribute{
478 "A": LabelListAttribute{Value: makeLabelList([]string{"a.a"}, []string{})},
479 "B": LabelListAttribute{Value: makeLabelList([]string{"b.b"}, []string{})},
480 "C": LabelListAttribute{Value: makeLabelList([]string{"c.c"}, []string{})},
481 },
482 },
483 {
484 name: "no configurable values, remainder partition",
Sam Delmericocc518432022-03-30 15:50:34 +0000485 ctx: &OtherModuleTestContext{},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400486 labelList: LabelListAttribute{
487 Value: makeLabelList([]string{"a.a", "b.b", "c.c", "d.d", "e.e"}, []string{}),
488 },
489 filters: LabelPartitions{
490 "A": LabelPartition{Extensions: []string{".a"}, Keep_remainder: true},
491 "B": LabelPartition{Extensions: []string{".b"}},
492 "C": LabelPartition{Extensions: []string{".c"}},
493 },
494 expected: PartitionToLabelListAttribute{
495 "A": LabelListAttribute{Value: makeLabelList([]string{"a.a", "d.d", "e.e"}, []string{})},
496 "B": LabelListAttribute{Value: makeLabelList([]string{"b.b"}, []string{})},
497 "C": LabelListAttribute{Value: makeLabelList([]string{"c.c"}, []string{})},
498 },
499 },
500 {
501 name: "no configurable values, empty partition",
Sam Delmericocc518432022-03-30 15:50:34 +0000502 ctx: &OtherModuleTestContext{},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400503 labelList: LabelListAttribute{
504 Value: makeLabelList([]string{"a.a", "c.c"}, []string{}),
505 },
506 filters: LabelPartitions{
507 "A": LabelPartition{Extensions: []string{".a"}},
508 "B": LabelPartition{Extensions: []string{".b"}},
509 "C": LabelPartition{Extensions: []string{".c"}},
510 },
511 expected: PartitionToLabelListAttribute{
512 "A": LabelListAttribute{Value: makeLabelList([]string{"a.a"}, []string{})},
513 "C": LabelListAttribute{Value: makeLabelList([]string{"c.c"}, []string{})},
514 },
515 },
516 {
517 name: "no configurable values, has map",
Sam Delmericocc518432022-03-30 15:50:34 +0000518 ctx: &OtherModuleTestContext{
519 Modules: []TestModuleInfo{{ModuleName: "srcs", Typ: "fg", Dir: "dir"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400520 },
521 labelList: LabelListAttribute{
522 Value: makeLabelList([]string{"a.a", "srcs", "b.b", "c.c"}, []string{}),
523 },
524 filters: LabelPartitions{
525 "A": LabelPartition{Extensions: []string{".a"}, LabelMapper: labelAddSuffixForTypeMapper("_a", "fg")},
526 "B": LabelPartition{Extensions: []string{".b"}},
527 "C": LabelPartition{Extensions: []string{".c"}},
528 },
529 expected: PartitionToLabelListAttribute{
530 "A": LabelListAttribute{Value: makeLabelList([]string{"a.a", "srcs_a"}, []string{})},
531 "B": LabelListAttribute{Value: makeLabelList([]string{"b.b"}, []string{})},
532 "C": LabelListAttribute{Value: makeLabelList([]string{"c.c"}, []string{})},
533 },
534 },
535 {
536 name: "configurable values, keeps empty if excludes",
Sam Delmericocc518432022-03-30 15:50:34 +0000537 ctx: &OtherModuleTestContext{},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400538 labelList: LabelListAttribute{
539 ConfigurableValues: configurableLabelLists{
540 ArchConfigurationAxis: labelListSelectValues{
541 "x86": makeLabelList([]string{"a.a", "c.c"}, []string{}),
542 "arm": makeLabelList([]string{"b.b"}, []string{}),
543 "x86_64": makeLabelList([]string{"b.b"}, []string{"d.d"}),
544 },
545 },
546 },
547 filters: LabelPartitions{
548 "A": LabelPartition{Extensions: []string{".a"}},
549 "B": LabelPartition{Extensions: []string{".b"}},
550 "C": LabelPartition{Extensions: []string{".c"}},
551 },
552 expected: PartitionToLabelListAttribute{
553 "A": LabelListAttribute{
554 ConfigurableValues: configurableLabelLists{
555 ArchConfigurationAxis: labelListSelectValues{
556 "x86": makeLabelList([]string{"a.a"}, []string{}),
557 "x86_64": makeLabelList([]string{}, []string{"c.c"}),
558 },
559 },
560 },
561 "B": LabelListAttribute{
562 ConfigurableValues: configurableLabelLists{
563 ArchConfigurationAxis: labelListSelectValues{
564 "arm": makeLabelList([]string{"b.b"}, []string{}),
565 "x86_64": makeLabelList([]string{"b.b"}, []string{"c.c"}),
566 },
567 },
568 },
569 "C": LabelListAttribute{
570 ConfigurableValues: configurableLabelLists{
571 ArchConfigurationAxis: labelListSelectValues{
572 "x86": makeLabelList([]string{"c.c"}, []string{}),
573 "x86_64": makeLabelList([]string{}, []string{"c.c"}),
574 },
575 },
576 },
577 },
578 },
579 {
580 name: "error for multiple partitions same value",
Sam Delmericocc518432022-03-30 15:50:34 +0000581 ctx: &OtherModuleTestContext{},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400582 labelList: LabelListAttribute{
583 Value: makeLabelList([]string{"a.a", "b.b", "c.c", "d.d", "e.e"}, []string{}),
584 },
585 filters: LabelPartitions{
586 "A": LabelPartition{Extensions: []string{".a"}},
587 "other A": LabelPartition{Extensions: []string{".a"}},
588 },
589 expected: PartitionToLabelListAttribute{},
590 expectedErrMsg: proptools.StringPtr(`"a.a" was found in multiple partitions:`),
591 },
592 }
593
594 for _, tc := range testCases {
595 t.Run(tc.name, func(t *testing.T) {
596 got := PartitionLabelListAttribute(tc.ctx, &tc.labelList, tc.filters)
597
598 if hasErrors, expectsErr := len(tc.ctx.errors) > 0, tc.expectedErrMsg != nil; hasErrors != expectsErr {
599 t.Errorf("Unexpected error(s): %q, expected: %q", tc.ctx.errors, *tc.expectedErrMsg)
600 } else if tc.expectedErrMsg != nil {
601 found := false
602 for _, err := range tc.ctx.errors {
603 if strings.Contains(err, *tc.expectedErrMsg) {
604 found = true
605 break
606 }
607 }
608
609 if !found {
610 t.Errorf("Expected error message: %q, got %q", *tc.expectedErrMsg, tc.ctx.errors)
611 }
612 return
613 }
614
615 if len(tc.expected) != len(got) {
616 t.Errorf("Expected %d partitions, got %d partitions", len(tc.expected), len(got))
617 }
618 for partition, expectedLla := range tc.expected {
619 gotLla, ok := got[partition]
620 if !ok {
621 t.Errorf("Expected partition %q, but it was not found %v", partition, got)
622 continue
623 }
624 expectedLabelList := expectedLla.Value
625 gotLabelList := gotLla.Value
626 if !reflect.DeepEqual(expectedLabelList.Includes, gotLabelList.Includes) {
627 t.Errorf("Expected no config includes %v, got %v", expectedLabelList.Includes, gotLabelList.Includes)
628 }
629 expectedAxes := expectedLla.SortedConfigurationAxes()
630 gotAxes := gotLla.SortedConfigurationAxes()
631 if !reflect.DeepEqual(expectedAxes, gotAxes) {
632 t.Errorf("Expected axes %v, got %v (%#v)", expectedAxes, gotAxes, gotLla)
633 }
634 for _, axis := range expectedLla.SortedConfigurationAxes() {
635 if _, exists := gotLla.ConfigurableValues[axis]; !exists {
636 t.Errorf("Expected %s to be a supported axis, but it was not found", axis)
637 }
638 if expected, got := expectedLla.ConfigurableValues[axis], gotLla.ConfigurableValues[axis]; len(expected) != len(got) {
639 t.Errorf("For axis %q: expected configs %v, got %v", axis, expected, got)
640 }
641 for config, expectedLabelList := range expectedLla.ConfigurableValues[axis] {
642 gotLabelList, exists := gotLla.ConfigurableValues[axis][config]
643 if !exists {
644 t.Errorf("Expected %s to be a supported config, but config was not found", config)
645 continue
646 }
647 if !reflect.DeepEqual(expectedLabelList.Includes, gotLabelList.Includes) {
648 t.Errorf("Expected %s %s includes %v, got %v", axis, config, expectedLabelList.Includes, gotLabelList.Includes)
649 }
650 }
651 }
652 }
653 })
654 }
655}
656
Liz Kammer5fad5012021-09-09 14:08:21 -0400657func TestDeduplicateAxesFromBase(t *testing.T) {
658 attr := StringListAttribute{
659 Value: []string{
660 "all_include",
661 "arm_include",
662 "android_include",
663 "linux_x86_include",
664 },
665 ConfigurableValues: configurableStringLists{
666 ArchConfigurationAxis: stringListSelectValues{
667 "arm": []string{"arm_include"},
668 "x86": []string{"x86_include"},
669 },
670 OsConfigurationAxis: stringListSelectValues{
671 "android": []string{"android_include"},
672 "linux": []string{"linux_include"},
673 },
674 OsArchConfigurationAxis: stringListSelectValues{
675 "linux_x86": {"linux_x86_include"},
676 },
Alixbbfd5382022-06-09 18:52:05 +0000677 ProductVariableConfigurationAxis("a", NoConfigAxis): stringListSelectValues{
Liz Kammer5fad5012021-09-09 14:08:21 -0400678 "a": []string{"not_in_value"},
679 },
680 },
681 }
682
683 attr.DeduplicateAxesFromBase()
684
685 expectedBaseIncludes := []string{
686 "all_include",
687 "arm_include",
688 "android_include",
689 "linux_x86_include",
690 }
691 if !reflect.DeepEqual(expectedBaseIncludes, attr.Value) {
692 t.Errorf("Expected Value includes %q, got %q", attr.Value, expectedBaseIncludes)
693 }
694 expectedConfiguredIncludes := configurableStringLists{
695 ArchConfigurationAxis: stringListSelectValues{
696 "x86": []string{"x86_include"},
697 },
698 OsConfigurationAxis: stringListSelectValues{
699 "linux": []string{"linux_include"},
700 },
701 OsArchConfigurationAxis: stringListSelectValues{},
Alixbbfd5382022-06-09 18:52:05 +0000702 ProductVariableConfigurationAxis("a", NoConfigAxis): stringListSelectValues{
Liz Kammer5fad5012021-09-09 14:08:21 -0400703 "a": []string{"not_in_value"},
704 },
705 }
706 for _, axis := range attr.SortedConfigurationAxes() {
707 if _, ok := expectedConfiguredIncludes[axis]; !ok {
708 t.Errorf("Found unexpected axis %s", axis)
709 continue
710 }
711 expectedForAxis := expectedConfiguredIncludes[axis]
712 gotForAxis := attr.ConfigurableValues[axis]
713 if len(expectedForAxis) != len(gotForAxis) {
714 t.Errorf("Expected %d configs for %s, got %d: %s", len(expectedForAxis), axis, len(gotForAxis), gotForAxis)
715 }
716 for config, value := range gotForAxis {
717 if expected, ok := expectedForAxis[config]; ok {
718 if !reflect.DeepEqual(expected, value) {
719 t.Errorf("For %s, expected: %#v, got %#v", axis, expected, value)
720 }
721 } else {
722 t.Errorf("Got unexpected config %q for %s", config, axis)
723 }
724 }
725 }
726}