blob: 883597ad32dbea0f663f2a08a3c0aff962a9dec3 [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"
19)
20
21func TestExpandVars(t *testing.T) {
22 testCases := []struct {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000023 description string
24 stringScope exportedStringVariables
25 stringListScope exportedStringListVariables
26 toExpand string
27 expectedValues []string
Jingwen Chenbf61afb2021-05-06 13:31:18 +000028 }{
29 {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000030 description: "no expansion for non-interpolated value",
31 toExpand: "foo",
32 expectedValues: []string{"foo"},
33 },
34 {
35 description: "single level expansion for string var",
36 stringScope: exportedStringVariables{
37 "foo": "bar",
Jingwen Chenbf61afb2021-05-06 13:31:18 +000038 },
39 toExpand: "${foo}",
40 expectedValues: []string{"bar"},
41 },
42 {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000043 description: "single level expansion string list var",
44 stringListScope: exportedStringListVariables{
45 "foo": []string{"bar"},
46 },
47 toExpand: "${foo}",
48 expectedValues: []string{"bar"},
49 },
50 {
51 description: "mixed level expansion for string list var",
52 stringScope: exportedStringVariables{
53 "foo": "${bar}",
54 "qux": "hello",
55 },
56 stringListScope: exportedStringListVariables{
57 "bar": []string{"baz", "${qux}"},
58 },
59 toExpand: "${foo}",
60 expectedValues: []string{"baz", "hello"},
61 },
62 {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000063 description: "double level expansion",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000064 stringListScope: exportedStringListVariables{
65 "foo": []string{"${bar}"},
66 "bar": []string{"baz"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000067 },
68 toExpand: "${foo}",
69 expectedValues: []string{"baz"},
70 },
71 {
72 description: "double level expansion with a literal",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000073 stringListScope: exportedStringListVariables{
74 "a": []string{"${b}", "c"},
75 "b": []string{"d"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000076 },
77 toExpand: "${a}",
78 expectedValues: []string{"d", "c"},
79 },
80 {
81 description: "double level expansion, with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000082 stringListScope: exportedStringListVariables{
83 "a": []string{"${b} ${c}"},
84 "b": []string{"d"},
85 "c": []string{"e"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000086 },
87 toExpand: "${a}",
88 expectedValues: []string{"d", "e"},
89 },
90 {
91 description: "triple level expansion with two variables in a string",
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000092 stringListScope: exportedStringListVariables{
93 "a": []string{"${b} ${c}"},
94 "b": []string{"${c}", "${d}"},
95 "c": []string{"${d}"},
96 "d": []string{"foo"},
Jingwen Chenbf61afb2021-05-06 13:31:18 +000097 },
98 toExpand: "${a}",
99 expectedValues: []string{"foo", "foo", "foo"},
100 },
101 }
102
103 for _, testCase := range testCases {
104 t.Run(testCase.description, func(t *testing.T) {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000105 output := expandVar(testCase.toExpand, testCase.stringScope, testCase.stringListScope)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000106 if len(output) != len(testCase.expectedValues) {
107 t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output))
108 }
109 for i, actual := range output {
110 expectedValue := testCase.expectedValues[i]
111 if actual != expectedValue {
112 t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue)
113 }
114 }
115 })
116 }
117}
Liz Kammer82ad8cc2021-08-02 10:41:48 -0400118
119func TestBazelToolchainVars(t *testing.T) {
120 testCases := []struct {
121 name string
122 vars []bazelVarExporter
123 expectedOut string
124 }{
125 {
126 name: "exports strings",
127 vars: []bazelVarExporter{
128 exportedStringVariables{
129 "a": "b",
130 "c": "d",
131 },
132 },
133 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
134
135_a = "b"
136
137_c = "d"
138
139constants = struct(
140 a = _a,
141 c = _c,
142)`,
143 },
144 {
145 name: "exports string lists",
146 vars: []bazelVarExporter{
147 exportedStringListVariables{
148 "a": []string{"b1", "b2"},
149 "c": []string{"d1", "d2"},
150 },
151 },
152 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
153
154_a = [
155 "b1",
156 "b2",
157]
158
159_c = [
160 "d1",
161 "d2",
162]
163
164constants = struct(
165 a = _a,
166 c = _c,
167)`,
168 },
169 {
170 name: "exports string lists dicts",
171 vars: []bazelVarExporter{
172 exportedStringListDictVariables{
173 "a": map[string][]string{"b1": []string{"b2"}},
174 "c": map[string][]string{"d1": []string{"d2"}},
175 },
176 },
177 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
178
179_a = {
180 "b1": [
181 "b2",
182 ],
183}
184
185_c = {
186 "d1": [
187 "d2",
188 ],
189}
190
191constants = struct(
192 a = _a,
193 c = _c,
194)`,
195 },
196 {
197 name: "sorts across types",
198 vars: []bazelVarExporter{
199 exportedStringVariables{
200 "b": "b-val",
201 "d": "d-val",
202 },
203 exportedStringListVariables{
204 "c": []string{"c-val"},
205 "e": []string{"e-val"},
206 },
207 exportedStringListDictVariables{
208 "a": map[string][]string{"a1": []string{"a2"}},
209 "f": map[string][]string{"f1": []string{"f2"}},
210 },
211 },
212 expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
213
214_a = {
215 "a1": [
216 "a2",
217 ],
218}
219
220_b = "b-val"
221
222_c = [
223 "c-val",
224]
225
226_d = "d-val"
227
228_e = [
229 "e-val",
230]
231
232_f = {
233 "f1": [
234 "f2",
235 ],
236}
237
238constants = struct(
239 a = _a,
240 b = _b,
241 c = _c,
242 d = _d,
243 e = _e,
244 f = _f,
245)`,
246 },
247 }
248
249 for _, tc := range testCases {
250 t.Run(tc.name, func(t *testing.T) {
251 out := bazelToolchainVars(tc.vars...)
252 if out != tc.expectedOut {
253 t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
254 }
255 })
256 }
257}