blob: 7bfc800e6fb0d4fb77b197eb9fc6951e30877e20 [file] [log] [blame]
Jeff Gastonf5f6e492017-11-20 14:31:11 -08001// Copyright 2017 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 android
16
17import (
18 "fmt"
19 "reflect"
20 "strings"
21 "testing"
22)
23
24func validateConfigAnnotations(configurable jsonConfigurable) (err error) {
25 reflectType := reflect.TypeOf(configurable)
26 reflectType = reflectType.Elem()
27 for i := 0; i < reflectType.NumField(); i++ {
28 field := reflectType.Field(i)
29 jsonTag := field.Tag.Get("json")
30 // Check for mistakes in the json tag
31 if jsonTag != "" && !strings.HasPrefix(jsonTag, ",") {
32 if !strings.Contains(jsonTag, ",") {
33 // Probably an accidental rename, most likely "omitempty" instead of ",omitempty"
34 return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
35 "Did you mean to use an annotation of %q?\n"+
36 "(Alternatively, to change the json name of the field, rename the field in source instead.)",
37 reflectType.Name(), field.Name, field.Tag, jsonTag, ","+jsonTag)
38 } else {
39 // Although this rename was probably intentional,
40 // a json annotation is still more confusing than renaming the source variable
41 requestedName := strings.Split(jsonTag, ",")[0]
42 return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
43 "To change the json name of the field, rename the field in source instead.",
44 reflectType.Name(), field.Name, field.Tag, requestedName)
45
46 }
47 }
48 }
49 return nil
50}
51
52type configType struct {
Dan Willemsen59339a22018-07-22 21:18:45 -070053 PopulateMe *bool `json:"omitempty"`
Jeff Gastonf5f6e492017-11-20 14:31:11 -080054}
55
56func (c *configType) SetDefaultConfig() {
57}
58
59// tests that ValidateConfigAnnotation works
60func TestValidateConfigAnnotations(t *testing.T) {
61 config := configType{}
62 err := validateConfigAnnotations(&config)
Dan Willemsen59339a22018-07-22 21:18:45 -070063 expectedError := `Field configType.PopulateMe has tag json:"omitempty" which specifies to change its json field name to "omitempty".
Jeff Gastonf5f6e492017-11-20 14:31:11 -080064Did you mean to use an annotation of ",omitempty"?
65(Alternatively, to change the json name of the field, rename the field in source instead.)`
66 if err.Error() != expectedError {
67 t.Errorf("Incorrect error; expected:\n"+
68 "%s\n"+
69 "got:\n"+
70 "%s",
71 expectedError, err.Error())
72 }
73}
74
75// run validateConfigAnnotations against each type that might have json annotations
76func TestProductConfigAnnotations(t *testing.T) {
77 err := validateConfigAnnotations(&productVariables{})
78 if err != nil {
79 t.Errorf(err.Error())
80 }
Jeff Gastonf5f6e492017-11-20 14:31:11 -080081}
Dan Willemsen0fe78662018-03-26 12:41:18 -070082
83func TestMissingVendorConfig(t *testing.T) {
84 c := &config{}
85 if c.VendorConfig("test").Bool("not_set") {
86 t.Errorf("Expected false")
87 }
88}
Paul Duffin7d584e92020-10-23 18:26:03 +010089
90func assertStringEquals(t *testing.T, expected, actual string) {
91 if actual != expected {
92 t.Errorf("expected %q found %q", expected, actual)
93 }
94}
95
96func TestConfiguredJarList(t *testing.T) {
97 list1 := CreateTestConfiguredJarList([]string{"apex1:jarA"})
98
99 t.Run("create", func(t *testing.T) {
100 assertStringEquals(t, "apex1:jarA", list1.String())
101 })
102
103 list2 := list1.Append("apex2", "jarB")
104 t.Run("append", func(t *testing.T) {
105 assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String())
106 })
107
108 t.Run("append does not modify", func(t *testing.T) {
109 assertStringEquals(t, "apex1:jarA", list1.String())
110 })
111
112 // Make sure that two lists created by appending to the same list do not share storage.
113 list3 := list1.Append("apex3", "jarC")
114 t.Run("append does not share", func(t *testing.T) {
115 assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String())
116 assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String())
117 })
118
119 list4 := list3.RemoveList(list1)
120 t.Run("remove", func(t *testing.T) {
121 assertStringEquals(t, "apex3:jarC", list4.String())
122 })
123
124 t.Run("remove does not modify", func(t *testing.T) {
125 assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String())
126 })
127
128 // Make sure that two lists created by removing from the same list do not share storage.
129 list5 := list3.RemoveList(CreateTestConfiguredJarList([]string{"apex3:jarC"}))
130 t.Run("remove", func(t *testing.T) {
131 assertStringEquals(t, "apex3:jarC", list4.String())
132 assertStringEquals(t, "apex1:jarA", list5.String())
133 })
134}