blob: 9a8178af6b7dd76aa8c84e88de3a4a4bff4b4313 [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 {
Liz Kammere8303bd2022-02-16 09:02:48 -050051 description: "single level expansion with short-name for string var",
52 stringScope: exportedStringVariables{
53 "foo": "bar",
54 },
55 toExpand: "${config.foo}",
56 expectedValues: []string{"bar"},
57 },
58 {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000059 description: "single level expansion string list var",
60 stringListScope: exportedStringListVariables{
61 "foo": []string{"bar"},
62 },
63 toExpand: "${foo}",
64 expectedValues: []string{"bar"},
65 },
66 {
67 description: "mixed level expansion for string list var",
68 stringScope: exportedStringVariables{
69 "foo": "${bar}",
70 "qux": "hello",
71 },
72 stringListScope: exportedStringListVariables{
73 "bar": []string{"baz", "${qux}"},
74 },
75 toExpand: "${foo}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -040076 expectedValues: []string{"baz hello"},
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000077 },
78 {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000079 description: "double level expansion",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000080 stringListScope: exportedStringListVariables{
81 "foo": []string{"${bar}"},
82 "bar": []string{"baz"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000083 },
84 toExpand: "${foo}",
85 expectedValues: []string{"baz"},
86 },
87 {
88 description: "double level expansion with a literal",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000089 stringListScope: exportedStringListVariables{
90 "a": []string{"${b}", "c"},
91 "b": []string{"d"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000092 },
93 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -040094 expectedValues: []string{"d c"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000095 },
96 {
97 description: "double level expansion, with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000098 stringListScope: exportedStringListVariables{
99 "a": []string{"${b} ${c}"},
100 "b": []string{"d"},
101 "c": []string{"e"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000102 },
103 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400104 expectedValues: []string{"d e"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000105 },
106 {
107 description: "triple level expansion with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000108 stringListScope: exportedStringListVariables{
109 "a": []string{"${b} ${c}"},
110 "b": []string{"${c}", "${d}"},
111 "c": []string{"${d}"},
112 "d": []string{"foo"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000113 },
114 toExpand: "${a}",
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400115 expectedValues: []string{"foo foo foo"},
116 },
117 {
118 description: "expansion with config depending vars",
119 configVars: exportedConfigDependingVariables{
120 "a": func(c android.Config) string { return c.BuildOS.String() },
121 "b": func(c android.Config) string { return c.BuildArch.String() },
122 },
123 config: android_arm64_config,
124 toExpand: "${a}-${b}",
125 expectedValues: []string{"android-arm64"},
126 },
127 {
128 description: "double level multi type expansion",
129 stringListScope: exportedStringListVariables{
130 "platform": []string{"${os}-${arch}"},
131 "const": []string{"const"},
132 },
133 configVars: exportedConfigDependingVariables{
134 "os": func(c android.Config) string { return c.BuildOS.String() },
135 "arch": func(c android.Config) string { return c.BuildArch.String() },
136 "foo": func(c android.Config) string { return "foo" },
137 },
138 config: android_arm64_config,
139 toExpand: "${const}/${platform}/${foo}",
140 expectedValues: []string{"const/android-arm64/foo"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000141 },
142 }
143
144 for _, testCase := range testCases {
145 t.Run(testCase.description, func(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400146 output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000147 if len(output) != len(testCase.expectedValues) {
148 t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output))
149 }
150 for i, actual := range output {
151 expectedValue := testCase.expectedValues[i]
152 if actual != expectedValue {
153 t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue)
154 }
155 }
156 })
157 }
158}
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400159
160func TestBazelToolchainVars(t *testing.T) {
161 testCases := []struct {
162 name string
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400163 config android.Config
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400164 vars []bazelVarExporter
165 expectedOut string
166 }{
167 {
168 name: "exports strings",
169 vars: []bazelVarExporter{
170 exportedStringVariables{
171 "a": "b",
172 "c": "d",
173 },
174 },
175 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
176
177_a = "b"
178
179_c = "d"
180
181constants = struct(
182 a = _a,
183 c = _c,
184)`,
185 },
186 {
187 name: "exports string lists",
188 vars: []bazelVarExporter{
189 exportedStringListVariables{
190 "a": []string{"b1", "b2"},
191 "c": []string{"d1", "d2"},
192 },
193 },
194 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
195
196_a = [
197 "b1",
198 "b2",
199]
200
201_c = [
202 "d1",
203 "d2",
204]
205
206constants = struct(
207 a = _a,
208 c = _c,
209)`,
210 },
211 {
212 name: "exports string lists dicts",
213 vars: []bazelVarExporter{
214 exportedStringListDictVariables{
215 "a": map[string][]string{"b1": []string{"b2"}},
216 "c": map[string][]string{"d1": []string{"d2"}},
217 },
218 },
219 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
220
221_a = {
Liz Kammer72beb342022-02-03 08:42:10 -0500222 "b1": ["b2"],
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400223}
224
225_c = {
Liz Kammer72beb342022-02-03 08:42:10 -0500226 "d1": ["d2"],
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400227}
228
229constants = struct(
230 a = _a,
231 c = _c,
232)`,
233 },
234 {
Liz Kammere8303bd2022-02-16 09:02:48 -0500235 name: "exports dict with var refs",
236 vars: []bazelVarExporter{
237 exportedVariableReferenceDictVariables{
238 "a": map[string]string{"b1": "${b2}"},
239 "c": map[string]string{"d1": "${config.d2}"},
240 },
241 },
242 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
243
244_a = {
245 "b1": _b2,
246}
247
248_c = {
249 "d1": _d2,
250}
251
252constants = struct(
253 a = _a,
254 c = _c,
255)`,
256 },
257 {
258 name: "sorts across types with variable references last",
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400259 vars: []bazelVarExporter{
260 exportedStringVariables{
261 "b": "b-val",
262 "d": "d-val",
263 },
264 exportedStringListVariables{
265 "c": []string{"c-val"},
266 "e": []string{"e-val"},
267 },
268 exportedStringListDictVariables{
269 "a": map[string][]string{"a1": []string{"a2"}},
270 "f": map[string][]string{"f1": []string{"f2"}},
271 },
Liz Kammere8303bd2022-02-16 09:02:48 -0500272 exportedVariableReferenceDictVariables{
273 "aa": map[string]string{"b1": "${b}"},
274 "cc": map[string]string{"d1": "${config.d}"},
275 },
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400276 },
277 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
278
279_a = {
Liz Kammer72beb342022-02-03 08:42:10 -0500280 "a1": ["a2"],
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400281}
282
283_b = "b-val"
284
Liz Kammer72beb342022-02-03 08:42:10 -0500285_c = ["c-val"]
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400286
287_d = "d-val"
288
Liz Kammer72beb342022-02-03 08:42:10 -0500289_e = ["e-val"]
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400290
291_f = {
Liz Kammer72beb342022-02-03 08:42:10 -0500292 "f1": ["f2"],
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400293}
294
Liz Kammere8303bd2022-02-16 09:02:48 -0500295_aa = {
296 "b1": _b,
297}
298
299_cc = {
300 "d1": _d,
301}
302
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400303constants = struct(
304 a = _a,
305 b = _b,
306 c = _c,
307 d = _d,
308 e = _e,
309 f = _f,
Liz Kammere8303bd2022-02-16 09:02:48 -0500310 aa = _aa,
311 cc = _cc,
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400312)`,
313 },
314 }
315
316 for _, tc := range testCases {
317 t.Run(tc.name, func(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -0400318 out := bazelToolchainVars(tc.config, tc.vars...)
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400319 if out != tc.expectedOut {
320 t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
321 }
322 })
323 }
324}