blob: 1da0b49adbb9a13d0c4c3b1ba18e8ee85bb56cce [file] [log] [blame]
Colin Cross9d34f352019-11-22 16:03:51 -08001// Copyright 2020 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 soongconfig
16
17import (
18 "reflect"
19 "testing"
Liz Kammerfe8853d2020-12-16 09:34:33 -080020
21 "github.com/google/blueprint/proptools"
Colin Cross9d34f352019-11-22 16:03:51 -080022)
23
24func Test_CanonicalizeToProperty(t *testing.T) {
25 tests := []struct {
26 name string
27 arg string
28 want string
29 }{
30 {
31 name: "lowercase",
32 arg: "board",
33 want: "board",
34 },
35 {
36 name: "uppercase",
37 arg: "BOARD",
38 want: "BOARD",
39 },
40 {
41 name: "numbers",
42 arg: "BOARD123",
43 want: "BOARD123",
44 },
45 {
46 name: "underscore",
47 arg: "TARGET_BOARD",
48 want: "TARGET_BOARD",
49 },
50 {
51 name: "dash",
52 arg: "TARGET-BOARD",
53 want: "TARGET_BOARD",
54 },
55 {
56 name: "unicode",
57 arg: "boardĪ»",
58 want: "board_",
59 },
60 }
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 if got := CanonicalizeToProperty(tt.arg); got != tt.want {
64 t.Errorf("canonicalizeToProperty() = %v, want %v", got, tt.want)
65 }
66 })
67 }
68}
69
70func Test_typeForPropertyFromPropertyStruct(t *testing.T) {
71 tests := []struct {
72 name string
73 ps interface{}
74 property string
75 want string
76 }{
77 {
78 name: "string",
79 ps: struct {
80 A string
81 }{},
82 property: "a",
83 want: "string",
84 },
85 {
86 name: "list",
87 ps: struct {
88 A []string
89 }{},
90 property: "a",
91 want: "[]string",
92 },
93 {
94 name: "missing",
95 ps: struct {
96 A []string
97 }{},
98 property: "b",
99 want: "",
100 },
101 {
102 name: "nested",
103 ps: struct {
104 A struct {
105 B string
106 }
107 }{},
108 property: "a.b",
109 want: "string",
110 },
111 {
112 name: "missing nested",
113 ps: struct {
114 A struct {
115 B string
116 }
117 }{},
118 property: "a.c",
119 want: "",
120 },
121 {
122 name: "not a struct",
123 ps: struct {
124 A string
125 }{},
126 property: "a.b",
127 want: "",
128 },
129 {
130 name: "nested pointer",
131 ps: struct {
132 A *struct {
133 B string
134 }
135 }{},
136 property: "a.b",
137 want: "string",
138 },
139 {
140 name: "nested interface",
141 ps: struct {
142 A interface{}
143 }{
144 A: struct {
145 B string
146 }{},
147 },
148 property: "a.b",
149 want: "string",
150 },
151 {
152 name: "nested interface pointer",
153 ps: struct {
154 A interface{}
155 }{
156 A: &struct {
157 B string
158 }{},
159 },
160 property: "a.b",
161 want: "string",
162 },
163 {
164 name: "nested interface nil pointer",
165 ps: struct {
166 A interface{}
167 }{
168 A: (*struct {
169 B string
170 })(nil),
171 },
172 property: "a.b",
173 want: "string",
174 },
175 }
176 for _, tt := range tests {
177 t.Run(tt.name, func(t *testing.T) {
178 typ := typeForPropertyFromPropertyStruct(tt.ps, tt.property)
179 got := ""
180 if typ != nil {
181 got = typ.String()
182 }
183 if got != tt.want {
184 t.Errorf("typeForPropertyFromPropertyStruct() = %v, want %v", got, tt.want)
185 }
186 })
187 }
188}
189
190func Test_createAffectablePropertiesType(t *testing.T) {
Colin Cross9d34f352019-11-22 16:03:51 -0800191 tests := []struct {
192 name string
193 affectableProperties []string
194 factoryProps interface{}
195 want string
196 }{
197 {
198 name: "string",
199 affectableProperties: []string{"cflags"},
200 factoryProps: struct {
201 Cflags string
202 }{},
203 want: "*struct { Cflags string }",
204 },
205 {
206 name: "list",
207 affectableProperties: []string{"cflags"},
208 factoryProps: struct {
209 Cflags []string
210 }{},
211 want: "*struct { Cflags []string }",
212 },
213 {
214 name: "string pointer",
215 affectableProperties: []string{"cflags"},
216 factoryProps: struct {
217 Cflags *string
218 }{},
219 want: "*struct { Cflags *string }",
220 },
221 {
222 name: "subset",
223 affectableProperties: []string{"cflags"},
224 factoryProps: struct {
225 Cflags string
226 Ldflags string
227 }{},
228 want: "*struct { Cflags string }",
229 },
230 {
231 name: "none",
232 affectableProperties: []string{"cflags"},
233 factoryProps: struct {
234 Ldflags string
235 }{},
236 want: "",
237 },
Colin Cross997f27a2021-03-05 17:25:41 -0800238 {
239 name: "nested",
240 affectableProperties: []string{"multilib.lib32.cflags"},
241 factoryProps: struct {
242 Multilib struct {
243 Lib32 struct {
244 Cflags string
245 }
246 }
247 }{},
248 want: "*struct { Multilib struct { Lib32 struct { Cflags string } } }",
249 },
250 {
251 name: "complex",
252 affectableProperties: []string{
253 "cflags",
254 "multilib.lib32.cflags",
255 "multilib.lib32.ldflags",
256 "multilib.lib64.cflags",
257 "multilib.lib64.ldflags",
258 "zflags",
259 },
260 factoryProps: struct {
261 Cflags string
262 Multilib struct {
263 Lib32 struct {
264 Cflags string
265 Ldflags string
266 }
267 Lib64 struct {
268 Cflags string
269 Ldflags string
270 }
271 }
272 Zflags string
273 }{},
274 want: "*struct { Cflags string; Multilib struct { Lib32 struct { Cflags string; Ldflags string }; Lib64 struct { Cflags string; Ldflags string } }; Zflags string }",
275 },
Colin Cross9d34f352019-11-22 16:03:51 -0800276 }
277 for _, tt := range tests {
278 t.Run(tt.name, func(t *testing.T) {
279 typ := createAffectablePropertiesType(tt.affectableProperties, []interface{}{tt.factoryProps})
280 got := ""
281 if typ != nil {
282 got = typ.String()
283 }
284 if !reflect.DeepEqual(got, tt.want) {
285 t.Errorf("createAffectablePropertiesType() = %v, want %v", got, tt.want)
286 }
287 })
288 }
289}
Liz Kammerfe8853d2020-12-16 09:34:33 -0800290
291type properties struct {
292 A *string
293 B bool
294}
Liz Kammer432bd592020-12-16 12:42:02 -0800295
296type boolVarProps struct {
297 A *string
298 B bool
299 Conditions_default *properties
Liz Kammerfe8853d2020-12-16 09:34:33 -0800300}
301
Cole Fausta03ac3a2024-01-12 12:12:26 -0800302type boolSoongConfigVars struct {
Liz Kammer432bd592020-12-16 12:42:02 -0800303 Bool_var interface{}
Liz Kammerfe8853d2020-12-16 09:34:33 -0800304}
305
Cole Faustb0a91332022-11-01 17:10:23 +0000306type stringSoongConfigVars struct {
307 String_var interface{}
308}
309
Cole Fausta03ac3a2024-01-12 12:12:26 -0800310type valueSoongConfigVars struct {
311 My_value_var interface{}
312}
313
314func Test_PropertiesToApply_Bool(t *testing.T) {
Liz Kammer432bd592020-12-16 12:42:02 -0800315 mt, _ := newModuleType(&ModuleTypeProperties{
316 Module_type: "foo",
317 Config_namespace: "bar",
318 Bool_variables: []string{"bool_var"},
319 Properties: []string{"a", "b"},
320 })
321 boolVarPositive := &properties{
322 A: proptools.StringPtr("A"),
323 B: true,
Liz Kammerfe8853d2020-12-16 09:34:33 -0800324 }
Liz Kammer432bd592020-12-16 12:42:02 -0800325 conditionsDefault := &properties{
326 A: proptools.StringPtr("default"),
327 B: false,
328 }
329 actualProps := &struct {
Cole Fausta03ac3a2024-01-12 12:12:26 -0800330 Soong_config_variables boolSoongConfigVars
Liz Kammer432bd592020-12-16 12:42:02 -0800331 }{
Cole Fausta03ac3a2024-01-12 12:12:26 -0800332 Soong_config_variables: boolSoongConfigVars{
Liz Kammer432bd592020-12-16 12:42:02 -0800333 Bool_var: &boolVarProps{
334 A: boolVarPositive.A,
335 B: boolVarPositive.B,
336 Conditions_default: conditionsDefault,
Liz Kammerfe8853d2020-12-16 09:34:33 -0800337 },
338 },
339 }
Liz Kammer432bd592020-12-16 12:42:02 -0800340 props := reflect.ValueOf(actualProps)
Liz Kammerfe8853d2020-12-16 09:34:33 -0800341
342 testCases := []struct {
Liz Kammer432bd592020-12-16 12:42:02 -0800343 name string
Liz Kammerfe8853d2020-12-16 09:34:33 -0800344 config SoongConfig
345 wantProps []interface{}
346 }{
347 {
Liz Kammer432bd592020-12-16 12:42:02 -0800348 name: "no_vendor_config",
349 config: Config(map[string]string{}),
350 wantProps: []interface{}{conditionsDefault},
Liz Kammerfe8853d2020-12-16 09:34:33 -0800351 },
352 {
Liz Kammer432bd592020-12-16 12:42:02 -0800353 name: "vendor_config_false",
354 config: Config(map[string]string{"bool_var": "n"}),
355 wantProps: []interface{}{conditionsDefault},
356 },
357 {
358 name: "bool_var_true",
Liz Kammerfe8853d2020-12-16 09:34:33 -0800359 config: Config(map[string]string{"bool_var": "y"}),
Liz Kammer432bd592020-12-16 12:42:02 -0800360 wantProps: []interface{}{boolVarPositive},
Liz Kammerfe8853d2020-12-16 09:34:33 -0800361 },
362 }
363
364 for _, tc := range testCases {
Liz Kammer432bd592020-12-16 12:42:02 -0800365 gotProps, err := PropertiesToApply(mt, props, tc.config)
Liz Kammerfe8853d2020-12-16 09:34:33 -0800366 if err != nil {
Liz Kammer432bd592020-12-16 12:42:02 -0800367 t.Errorf("%s: Unexpected error in PropertiesToApply: %s", tc.name, err)
Liz Kammerfe8853d2020-12-16 09:34:33 -0800368 }
369
370 if !reflect.DeepEqual(gotProps, tc.wantProps) {
Liz Kammer432bd592020-12-16 12:42:02 -0800371 t.Errorf("%s: Expected %s, got %s", tc.name, tc.wantProps, gotProps)
Liz Kammerfe8853d2020-12-16 09:34:33 -0800372 }
373 }
374}
Jingwen Chen01812022021-11-19 14:29:43 +0000375
Cole Fausta03ac3a2024-01-12 12:12:26 -0800376func Test_PropertiesToApply_Value(t *testing.T) {
377 mt, _ := newModuleType(&ModuleTypeProperties{
378 Module_type: "foo",
379 Config_namespace: "bar",
380 Value_variables: []string{"my_value_var"},
381 Properties: []string{"a", "b"},
382 })
383 conditionsDefault := &properties{
384 A: proptools.StringPtr("default"),
385 B: false,
386 }
387 actualProps := &struct {
388 Soong_config_variables valueSoongConfigVars
389 }{
390 Soong_config_variables: valueSoongConfigVars{
391 My_value_var: &boolVarProps{
392 A: proptools.StringPtr("A=%s"),
393 B: true,
394 Conditions_default: conditionsDefault,
395 },
396 },
397 }
398 props := reflect.ValueOf(actualProps)
399
400 testCases := []struct {
401 name string
402 config SoongConfig
403 wantProps []interface{}
404 }{
405 {
406 name: "no_vendor_config",
407 config: Config(map[string]string{}),
408 wantProps: []interface{}{conditionsDefault},
409 },
410 {
411 name: "value_var_set",
412 config: Config(map[string]string{"my_value_var": "Hello"}),
413 wantProps: []interface{}{&properties{
414 A: proptools.StringPtr("A=Hello"),
415 B: true,
416 }},
417 },
418 }
419
420 for _, tc := range testCases {
421 gotProps, err := PropertiesToApply(mt, props, tc.config)
422 if err != nil {
423 t.Errorf("%s: Unexpected error in PropertiesToApply: %s", tc.name, err)
424 }
425
426 if !reflect.DeepEqual(gotProps, tc.wantProps) {
427 t.Errorf("%s: Expected %s, got %s", tc.name, tc.wantProps, gotProps)
428 }
429 }
430}
431
Cole Faust1da0b202024-02-21 10:50:33 -0800432func Test_PropertiesToApply_Value_Nested(t *testing.T) {
433 mt, _ := newModuleType(&ModuleTypeProperties{
434 Module_type: "foo",
435 Config_namespace: "bar",
436 Value_variables: []string{"my_value_var"},
437 Properties: []string{"a.b"},
438 })
439 type properties struct {
440 A struct {
441 B string
442 }
443 }
444 conditionsDefault := &properties{
445 A: struct{ B string }{
446 B: "default",
447 },
448 }
449 type valueVarProps struct {
450 A struct {
451 B string
452 }
453 Conditions_default *properties
454 }
455 actualProps := &struct {
456 Soong_config_variables valueSoongConfigVars
457 }{
458 Soong_config_variables: valueSoongConfigVars{
459 My_value_var: &valueVarProps{
460 A: struct{ B string }{
461 B: "A.B=%s",
462 },
463 Conditions_default: conditionsDefault,
464 },
465 },
466 }
467 props := reflect.ValueOf(actualProps)
468
469 testCases := []struct {
470 name string
471 config SoongConfig
472 wantProps []interface{}
473 }{
474 {
475 name: "no_vendor_config",
476 config: Config(map[string]string{}),
477 wantProps: []interface{}{conditionsDefault},
478 },
479 {
480 name: "value_var_set",
481 config: Config(map[string]string{"my_value_var": "Hello"}),
482 wantProps: []interface{}{&properties{
483 A: struct{ B string }{
484 B: "A.B=Hello",
485 },
486 }},
487 },
488 }
489
490 for _, tc := range testCases {
491 gotProps, err := PropertiesToApply(mt, props, tc.config)
492 if err != nil {
493 t.Errorf("%s: Unexpected error in PropertiesToApply: %s", tc.name, err)
494 }
495
496 if !reflect.DeepEqual(gotProps, tc.wantProps) {
497 t.Errorf("%s: Expected %s, got %s", tc.name, tc.wantProps, gotProps)
498 }
499 }
500}
501
Cole Faustb0a91332022-11-01 17:10:23 +0000502func Test_PropertiesToApply_String_Error(t *testing.T) {
503 mt, _ := newModuleType(&ModuleTypeProperties{
504 Module_type: "foo",
505 Config_namespace: "bar",
506 Variables: []string{"string_var"},
507 Properties: []string{"a", "b"},
508 })
509 mt.Variables = append(mt.Variables, &stringVariable{
510 baseVariable: baseVariable{
511 variable: "string_var",
512 },
513 values: []string{"a", "b", "c"},
514 })
515 stringVarPositive := &properties{
516 A: proptools.StringPtr("A"),
517 B: true,
518 }
519 conditionsDefault := &properties{
520 A: proptools.StringPtr("default"),
521 B: false,
522 }
523 actualProps := &struct {
524 Soong_config_variables stringSoongConfigVars
525 }{
526 Soong_config_variables: stringSoongConfigVars{
527 String_var: &boolVarProps{
528 A: stringVarPositive.A,
529 B: stringVarPositive.B,
530 Conditions_default: conditionsDefault,
531 },
532 },
533 }
534 props := reflect.ValueOf(actualProps)
535
536 _, err := PropertiesToApply(mt, props, Config(map[string]string{
537 "string_var": "x",
538 }))
539 expected := `Soong config property "string_var" must be one of [a b c], found "x"`
540 if err == nil {
541 t.Fatalf("Expected an error, got nil")
542 } else if err.Error() != expected {
543 t.Fatalf("Error message was not correct, expected %q, got %q", expected, err.Error())
544 }
545}