123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- // Copyright 2020 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package soongconfig
- import (
- "reflect"
- "testing"
- "github.com/google/blueprint/proptools"
- )
- func Test_CanonicalizeToProperty(t *testing.T) {
- tests := []struct {
- name string
- arg string
- want string
- }{
- {
- name: "lowercase",
- arg: "board",
- want: "board",
- },
- {
- name: "uppercase",
- arg: "BOARD",
- want: "BOARD",
- },
- {
- name: "numbers",
- arg: "BOARD123",
- want: "BOARD123",
- },
- {
- name: "underscore",
- arg: "TARGET_BOARD",
- want: "TARGET_BOARD",
- },
- {
- name: "dash",
- arg: "TARGET-BOARD",
- want: "TARGET_BOARD",
- },
- {
- name: "unicode",
- arg: "boardλ",
- want: "board_",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := CanonicalizeToProperty(tt.arg); got != tt.want {
- t.Errorf("canonicalizeToProperty() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_typeForPropertyFromPropertyStruct(t *testing.T) {
- tests := []struct {
- name string
- ps interface{}
- property string
- want string
- }{
- {
- name: "string",
- ps: struct {
- A string
- }{},
- property: "a",
- want: "string",
- },
- {
- name: "list",
- ps: struct {
- A []string
- }{},
- property: "a",
- want: "[]string",
- },
- {
- name: "missing",
- ps: struct {
- A []string
- }{},
- property: "b",
- want: "",
- },
- {
- name: "nested",
- ps: struct {
- A struct {
- B string
- }
- }{},
- property: "a.b",
- want: "string",
- },
- {
- name: "missing nested",
- ps: struct {
- A struct {
- B string
- }
- }{},
- property: "a.c",
- want: "",
- },
- {
- name: "not a struct",
- ps: struct {
- A string
- }{},
- property: "a.b",
- want: "",
- },
- {
- name: "nested pointer",
- ps: struct {
- A *struct {
- B string
- }
- }{},
- property: "a.b",
- want: "string",
- },
- {
- name: "nested interface",
- ps: struct {
- A interface{}
- }{
- A: struct {
- B string
- }{},
- },
- property: "a.b",
- want: "string",
- },
- {
- name: "nested interface pointer",
- ps: struct {
- A interface{}
- }{
- A: &struct {
- B string
- }{},
- },
- property: "a.b",
- want: "string",
- },
- {
- name: "nested interface nil pointer",
- ps: struct {
- A interface{}
- }{
- A: (*struct {
- B string
- })(nil),
- },
- property: "a.b",
- want: "string",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- typ := typeForPropertyFromPropertyStruct(tt.ps, tt.property)
- got := ""
- if typ != nil {
- got = typ.String()
- }
- if got != tt.want {
- t.Errorf("typeForPropertyFromPropertyStruct() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_createAffectablePropertiesType(t *testing.T) {
- tests := []struct {
- name string
- affectableProperties []string
- factoryProps interface{}
- want string
- }{
- {
- name: "string",
- affectableProperties: []string{"cflags"},
- factoryProps: struct {
- Cflags string
- }{},
- want: "*struct { Cflags string }",
- },
- {
- name: "list",
- affectableProperties: []string{"cflags"},
- factoryProps: struct {
- Cflags []string
- }{},
- want: "*struct { Cflags []string }",
- },
- {
- name: "string pointer",
- affectableProperties: []string{"cflags"},
- factoryProps: struct {
- Cflags *string
- }{},
- want: "*struct { Cflags *string }",
- },
- {
- name: "subset",
- affectableProperties: []string{"cflags"},
- factoryProps: struct {
- Cflags string
- Ldflags string
- }{},
- want: "*struct { Cflags string }",
- },
- {
- name: "none",
- affectableProperties: []string{"cflags"},
- factoryProps: struct {
- Ldflags string
- }{},
- want: "",
- },
- {
- name: "nested",
- affectableProperties: []string{"multilib.lib32.cflags"},
- factoryProps: struct {
- Multilib struct {
- Lib32 struct {
- Cflags string
- }
- }
- }{},
- want: "*struct { Multilib struct { Lib32 struct { Cflags string } } }",
- },
- {
- name: "complex",
- affectableProperties: []string{
- "cflags",
- "multilib.lib32.cflags",
- "multilib.lib32.ldflags",
- "multilib.lib64.cflags",
- "multilib.lib64.ldflags",
- "zflags",
- },
- factoryProps: struct {
- Cflags string
- Multilib struct {
- Lib32 struct {
- Cflags string
- Ldflags string
- }
- Lib64 struct {
- Cflags string
- Ldflags string
- }
- }
- Zflags string
- }{},
- want: "*struct { Cflags string; Multilib struct { Lib32 struct { Cflags string; Ldflags string }; Lib64 struct { Cflags string; Ldflags string } }; Zflags string }",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- typ := createAffectablePropertiesType(tt.affectableProperties, []interface{}{tt.factoryProps})
- got := ""
- if typ != nil {
- got = typ.String()
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("createAffectablePropertiesType() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- type properties struct {
- A *string
- B bool
- }
- type boolVarProps struct {
- A *string
- B bool
- Conditions_default *properties
- }
- type soongConfigVars struct {
- Bool_var interface{}
- }
- type stringSoongConfigVars struct {
- String_var interface{}
- }
- func Test_PropertiesToApply(t *testing.T) {
- mt, _ := newModuleType(&ModuleTypeProperties{
- Module_type: "foo",
- Config_namespace: "bar",
- Bool_variables: []string{"bool_var"},
- Properties: []string{"a", "b"},
- })
- boolVarPositive := &properties{
- A: proptools.StringPtr("A"),
- B: true,
- }
- conditionsDefault := &properties{
- A: proptools.StringPtr("default"),
- B: false,
- }
- actualProps := &struct {
- Soong_config_variables soongConfigVars
- }{
- Soong_config_variables: soongConfigVars{
- Bool_var: &boolVarProps{
- A: boolVarPositive.A,
- B: boolVarPositive.B,
- Conditions_default: conditionsDefault,
- },
- },
- }
- props := reflect.ValueOf(actualProps)
- testCases := []struct {
- name string
- config SoongConfig
- wantProps []interface{}
- }{
- {
- name: "no_vendor_config",
- config: Config(map[string]string{}),
- wantProps: []interface{}{conditionsDefault},
- },
- {
- name: "vendor_config_false",
- config: Config(map[string]string{"bool_var": "n"}),
- wantProps: []interface{}{conditionsDefault},
- },
- {
- name: "bool_var_true",
- config: Config(map[string]string{"bool_var": "y"}),
- wantProps: []interface{}{boolVarPositive},
- },
- }
- for _, tc := range testCases {
- gotProps, err := PropertiesToApply(mt, props, tc.config)
- if err != nil {
- t.Errorf("%s: Unexpected error in PropertiesToApply: %s", tc.name, err)
- }
- if !reflect.DeepEqual(gotProps, tc.wantProps) {
- t.Errorf("%s: Expected %s, got %s", tc.name, tc.wantProps, gotProps)
- }
- }
- }
- func Test_PropertiesToApply_String_Error(t *testing.T) {
- mt, _ := newModuleType(&ModuleTypeProperties{
- Module_type: "foo",
- Config_namespace: "bar",
- Variables: []string{"string_var"},
- Properties: []string{"a", "b"},
- })
- mt.Variables = append(mt.Variables, &stringVariable{
- baseVariable: baseVariable{
- variable: "string_var",
- },
- values: []string{"a", "b", "c"},
- })
- stringVarPositive := &properties{
- A: proptools.StringPtr("A"),
- B: true,
- }
- conditionsDefault := &properties{
- A: proptools.StringPtr("default"),
- B: false,
- }
- actualProps := &struct {
- Soong_config_variables stringSoongConfigVars
- }{
- Soong_config_variables: stringSoongConfigVars{
- String_var: &boolVarProps{
- A: stringVarPositive.A,
- B: stringVarPositive.B,
- Conditions_default: conditionsDefault,
- },
- },
- }
- props := reflect.ValueOf(actualProps)
- _, err := PropertiesToApply(mt, props, Config(map[string]string{
- "string_var": "x",
- }))
- expected := `Soong config property "string_var" must be one of [a b c], found "x"`
- if err == nil {
- t.Fatalf("Expected an error, got nil")
- } else if err.Error() != expected {
- t.Fatalf("Error message was not correct, expected %q, got %q", expected, err.Error())
- }
- }
- func Test_Bp2BuildSoongConfigDefinitionsAddVars(t *testing.T) {
- testCases := []struct {
- desc string
- defs []*SoongConfigDefinition
- expected Bp2BuildSoongConfigDefinitions
- }{
- {
- desc: "non-overlapping",
- defs: []*SoongConfigDefinition{
- &SoongConfigDefinition{
- ModuleTypes: map[string]*ModuleType{
- "a": &ModuleType{
- ConfigNamespace: "foo",
- Variables: []soongConfigVariable{
- &stringVariable{
- baseVariable: baseVariable{"string_var"},
- values: []string{"a", "b", "c"},
- },
- },
- },
- },
- },
- &SoongConfigDefinition{
- ModuleTypes: map[string]*ModuleType{
- "b": &ModuleType{
- ConfigNamespace: "foo",
- Variables: []soongConfigVariable{
- &stringVariable{
- baseVariable: baseVariable{"string_var"},
- values: []string{"a", "b", "c"},
- },
- &boolVariable{baseVariable: baseVariable{"bool_var"}},
- &valueVariable{baseVariable: baseVariable{"variable_var"}},
- },
- },
- },
- },
- },
- expected: Bp2BuildSoongConfigDefinitions{
- StringVars: map[string]map[string]bool{
- "foo__string_var": map[string]bool{"a": true, "b": true, "c": true},
- },
- BoolVars: map[string]bool{"foo__bool_var": true},
- ValueVars: map[string]bool{"foo__variable_var": true},
- },
- },
- {
- desc: "overlapping",
- defs: []*SoongConfigDefinition{
- &SoongConfigDefinition{
- ModuleTypes: map[string]*ModuleType{
- "a": &ModuleType{
- ConfigNamespace: "foo",
- Variables: []soongConfigVariable{
- &stringVariable{
- baseVariable: baseVariable{"string_var"},
- values: []string{"a", "b", "c"},
- },
- },
- },
- },
- },
- &SoongConfigDefinition{
- ModuleTypes: map[string]*ModuleType{
- "b": &ModuleType{
- ConfigNamespace: "foo",
- Variables: []soongConfigVariable{
- &stringVariable{
- baseVariable: baseVariable{"string_var"},
- values: []string{"b", "c", "d"},
- },
- &boolVariable{baseVariable: baseVariable{"bool_var"}},
- &valueVariable{baseVariable: baseVariable{"variable_var"}},
- },
- },
- },
- },
- },
- expected: Bp2BuildSoongConfigDefinitions{
- StringVars: map[string]map[string]bool{
- "foo__string_var": map[string]bool{"a": true, "b": true, "c": true, "d": true},
- },
- BoolVars: map[string]bool{"foo__bool_var": true},
- ValueVars: map[string]bool{"foo__variable_var": true},
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, func(t *testing.T) {
- actual := &Bp2BuildSoongConfigDefinitions{}
- for _, d := range tc.defs {
- func(def *SoongConfigDefinition) {
- actual.AddVars(def)
- }(d)
- }
- if !reflect.DeepEqual(*actual, tc.expected) {
- t.Errorf("Expected %#v, got %#v", tc.expected, *actual)
- }
- })
- }
- }
- func Test_Bp2BuildSoongConfigDefinitions(t *testing.T) {
- testCases := []struct {
- desc string
- defs Bp2BuildSoongConfigDefinitions
- expected string
- }{
- {
- desc: "all empty",
- defs: Bp2BuildSoongConfigDefinitions{},
- expected: `soong_config_bool_variables = {}
- soong_config_value_variables = {}
- soong_config_string_variables = {}`}, {
- desc: "only bool",
- defs: Bp2BuildSoongConfigDefinitions{
- BoolVars: map[string]bool{
- "bool_var": true,
- },
- },
- expected: `soong_config_bool_variables = {
- "bool_var": True,
- }
- soong_config_value_variables = {}
- soong_config_string_variables = {}`}, {
- desc: "only value vars",
- defs: Bp2BuildSoongConfigDefinitions{
- ValueVars: map[string]bool{
- "value_var": true,
- },
- },
- expected: `soong_config_bool_variables = {}
- soong_config_value_variables = {
- "value_var": True,
- }
- soong_config_string_variables = {}`}, {
- desc: "only string vars",
- defs: Bp2BuildSoongConfigDefinitions{
- StringVars: map[string]map[string]bool{
- "string_var": map[string]bool{
- "choice1": true,
- "choice2": true,
- "choice3": true,
- },
- },
- },
- expected: `soong_config_bool_variables = {}
- soong_config_value_variables = {}
- soong_config_string_variables = {
- "string_var": [
- "choice1",
- "choice2",
- "choice3",
- ],
- }`}, {
- desc: "all vars",
- defs: Bp2BuildSoongConfigDefinitions{
- BoolVars: map[string]bool{
- "bool_var_one": true,
- },
- ValueVars: map[string]bool{
- "value_var_one": true,
- "value_var_two": true,
- },
- StringVars: map[string]map[string]bool{
- "string_var_one": map[string]bool{
- "choice1": true,
- "choice2": true,
- "choice3": true,
- },
- "string_var_two": map[string]bool{
- "foo": true,
- "bar": true,
- },
- },
- },
- expected: `soong_config_bool_variables = {
- "bool_var_one": True,
- }
- soong_config_value_variables = {
- "value_var_one": True,
- "value_var_two": True,
- }
- soong_config_string_variables = {
- "string_var_one": [
- "choice1",
- "choice2",
- "choice3",
- ],
- "string_var_two": [
- "bar",
- "foo",
- ],
- }`},
- }
- for _, test := range testCases {
- t.Run(test.desc, func(t *testing.T) {
- actual := test.defs.String()
- if actual != test.expected {
- t.Errorf("Expected:\n%s\nbut got:\n%s", test.expected, actual)
- }
- })
- }
- }
|