blob: 3118df1f8475c31a85acdc9363c808700f29da9f [file] [log] [blame]
Jingwen Chenbf61afb2021-05-06 13:31:18 +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 config
16
17import (
18 "testing"
Chris Parsons3b1f83d2021-10-14 14:08:38 -040019
20 "android/soong/android"
Jingwen Chenbf61afb2021-05-06 13:31:18 +000021)
22
23func TestExpandVars(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -040024 android_arm64_config := android.TestConfig("out", nil, "", nil)
25 android_arm64_config.BuildOS = android.Android
26 android_arm64_config.BuildArch = android.Arm64
27
Jingwen Chenbf61afb2021-05-06 13:31:18 +000028 testCases := []struct {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000029 description string
Chris Parsons3b1f83d2021-10-14 14:08:38 -040030 config android.Config
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000031 stringScope exportedStringVariables
32 stringListScope exportedStringListVariables
Chris Parsons3b1f83d2021-10-14 14:08:38 -040033 configVars exportedConfigDependingVariables
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000034 toExpand string
35 expectedValues []string
Jingwen Chenbf61afb2021-05-06 13:31:18 +000036 }{
37 {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000038 description: "no expansion for non-interpolated value",
39 toExpand: "foo",
40 expectedValues: []string{"foo"},
41 },
42 {
43 description: "single level expansion for string var",
44 stringScope: exportedStringVariables{
45 "foo": "bar",
Jingwen Chenbf61afb2021-05-06 13:31:18 +000046 },
47 toExpand: "${foo}",
48 expectedValues: []string{"bar"},
49 },
50 {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000051 description: "single level expansion string list var",
52 stringListScope: exportedStringListVariables{
53 "foo": []string{"bar"},
54 },
55 toExpand: "${foo}",
56 expectedValues: []string{"bar"},
57 },
58 {
59 description: "mixed level expansion for string list var",
60 stringScope: exportedStringVariables{
61 "foo": "${bar}",
62 "qux": "hello",
63 },
64 stringListScope: exportedStringListVariables{
65 "bar": []string{"baz", "${qux}"},
66 },
67 toExpand: "${foo}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -040068 expectedValues: []string{"baz hello"},
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000069 },
70 {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000071 description: "double level expansion",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000072 stringListScope: exportedStringListVariables{
73 "foo": []string{"${bar}"},
74 "bar": []string{"baz"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000075 },
76 toExpand: "${foo}",
77 expectedValues: []string{"baz"},
78 },
79 {
80 description: "double level expansion with a literal",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000081 stringListScope: exportedStringListVariables{
82 "a": []string{"${b}", "c"},
83 "b": []string{"d"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000084 },
85 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -040086 expectedValues: []string{"d c"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000087 },
88 {
89 description: "double level expansion, with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000090 stringListScope: exportedStringListVariables{
91 "a": []string{"${b} ${c}"},
92 "b": []string{"d"},
93 "c": []string{"e"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000094 },
95 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -040096 expectedValues: []string{"d e"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000097 },
98 {
99 description: "triple level expansion with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000100 stringListScope: exportedStringListVariables{
101 "a": []string{"${b} ${c}"},
102 "b": []string{"${c}", "${d}"},
103 "c": []string{"${d}"},
104 "d": []string{"foo"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000105 },
106 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400107 expectedValues: []string{"foo foo foo"},
108 },
109 {
110 description: "expansion with config depending vars",
111 configVars: exportedConfigDependingVariables{
112 "a": func(c android.Config) string { return c.BuildOS.String() },
113 "b": func(c android.Config) string { return c.BuildArch.String() },
114 },
115 config: android_arm64_config,
116 toExpand: "${a}-${b}",
117 expectedValues: []string{"android-arm64"},
118 },
119 {
120 description: "double level multi type expansion",
121 stringListScope: exportedStringListVariables{
122 "platform": []string{"${os}-${arch}"},
123 "const": []string{"const"},
124 },
125 configVars: exportedConfigDependingVariables{
126 "os": func(c android.Config) string { return c.BuildOS.String() },
127 "arch": func(c android.Config) string { return c.BuildArch.String() },
128 "foo": func(c android.Config) string { return "foo" },
129 },
130 config: android_arm64_config,
131 toExpand: "${const}/${platform}/${foo}",
132 expectedValues: []string{"const/android-arm64/foo"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000133 },
134 }
135
136 for _, testCase := range testCases {
137 t.Run(testCase.description, func(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400138 output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000139 if len(output) != len(testCase.expectedValues) {
140 t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output))
141 }
142 for i, actual := range output {
143 expectedValue := testCase.expectedValues[i]
144 if actual != expectedValue {
145 t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue)
146 }
147 }
148 })
149 }
150}
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400151
152func TestBazelToolchainVars(t *testing.T) {
153 testCases := []struct {
154 name string
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400155 config android.Config
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400156 vars []bazelVarExporter
157 expectedOut string
158 }{
159 {
160 name: "exports strings",
161 vars: []bazelVarExporter{
162 exportedStringVariables{
163 "a": "b",
164 "c": "d",
165 },
166 },
167 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
168
169_a = "b"
170
171_c = "d"
172
173constants = struct(
174 a = _a,
175 c = _c,
176)`,
177 },
178 {
179 name: "exports string lists",
180 vars: []bazelVarExporter{
181 exportedStringListVariables{
182 "a": []string{"b1", "b2"},
183 "c": []string{"d1", "d2"},
184 },
185 },
186 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
187
188_a = [
189 "b1",
190 "b2",
191]
192
193_c = [
194 "d1",
195 "d2",
196]
197
198constants = struct(
199 a = _a,
200 c = _c,
201)`,
202 },
203 {
204 name: "exports string lists dicts",
205 vars: []bazelVarExporter{
206 exportedStringListDictVariables{
207 "a": map[string][]string{"b1": []string{"b2"}},
208 "c": map[string][]string{"d1": []string{"d2"}},
209 },
210 },
211 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
212
213_a = {
214 "b1": [
215 "b2",
216 ],
217}
218
219_c = {
220 "d1": [
221 "d2",
222 ],
223}
224
225constants = struct(
226 a = _a,
227 c = _c,
228)`,
229 },
230 {
231 name: "sorts across types",
232 vars: []bazelVarExporter{
233 exportedStringVariables{
234 "b": "b-val",
235 "d": "d-val",
236 },
237 exportedStringListVariables{
238 "c": []string{"c-val"},
239 "e": []string{"e-val"},
240 },
241 exportedStringListDictVariables{
242 "a": map[string][]string{"a1": []string{"a2"}},
243 "f": map[string][]string{"f1": []string{"f2"}},
244 },
245 },
246 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
247
248_a = {
249 "a1": [
250 "a2",
251 ],
252}
253
254_b = "b-val"
255
256_c = [
257 "c-val",
258]
259
260_d = "d-val"
261
262_e = [
263 "e-val",
264]
265
266_f = {
267 "f1": [
268 "f2",
269 ],
270}
271
272constants = struct(
273 a = _a,
274 b = _b,
275 c = _c,
276 d = _d,
277 e = _e,
278 f = _f,
279)`,
280 },
281 }
282
283 for _, tc := range testCases {
284 t.Run(tc.name, func(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400285 out := bazelToolchainVars(tc.config, tc.vars...)
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400286 if out != tc.expectedOut {
287 t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
288 }
289 })
290 }
291}