Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func TestExpandVars(t *testing.T) { |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 24 | android_arm64_config := android.TestConfig("out", nil, "", nil) |
| 25 | android_arm64_config.BuildOS = android.Android |
| 26 | android_arm64_config.BuildArch = android.Arm64 |
| 27 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 28 | testCases := []struct { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 29 | description string |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 30 | config android.Config |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 31 | stringScope exportedStringVariables |
| 32 | stringListScope exportedStringListVariables |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 33 | configVars exportedConfigDependingVariables |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 34 | toExpand string |
| 35 | expectedValues []string |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 36 | }{ |
| 37 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 38 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 46 | }, |
| 47 | toExpand: "${foo}", |
| 48 | expectedValues: []string{"bar"}, |
| 49 | }, |
| 50 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame^] | 51 | 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 Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 59 | 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 Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 76 | expectedValues: []string{"baz hello"}, |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 77 | }, |
| 78 | { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 79 | description: "double level expansion", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 80 | stringListScope: exportedStringListVariables{ |
| 81 | "foo": []string{"${bar}"}, |
| 82 | "bar": []string{"baz"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 83 | }, |
| 84 | toExpand: "${foo}", |
| 85 | expectedValues: []string{"baz"}, |
| 86 | }, |
| 87 | { |
| 88 | description: "double level expansion with a literal", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 89 | stringListScope: exportedStringListVariables{ |
| 90 | "a": []string{"${b}", "c"}, |
| 91 | "b": []string{"d"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 92 | }, |
| 93 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 94 | expectedValues: []string{"d c"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 95 | }, |
| 96 | { |
| 97 | description: "double level expansion, with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 98 | stringListScope: exportedStringListVariables{ |
| 99 | "a": []string{"${b} ${c}"}, |
| 100 | "b": []string{"d"}, |
| 101 | "c": []string{"e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 102 | }, |
| 103 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 104 | expectedValues: []string{"d e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 105 | }, |
| 106 | { |
| 107 | description: "triple level expansion with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 108 | stringListScope: exportedStringListVariables{ |
| 109 | "a": []string{"${b} ${c}"}, |
| 110 | "b": []string{"${c}", "${d}"}, |
| 111 | "c": []string{"${d}"}, |
| 112 | "d": []string{"foo"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 113 | }, |
| 114 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 115 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 141 | }, |
| 142 | } |
| 143 | |
| 144 | for _, testCase := range testCases { |
| 145 | t.Run(testCase.description, func(t *testing.T) { |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 146 | output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 147 | 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 Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 159 | |
| 160 | func TestBazelToolchainVars(t *testing.T) { |
| 161 | testCases := []struct { |
| 162 | name string |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 163 | config android.Config |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 164 | 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 | |
| 181 | constants = 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 | |
| 206 | constants = 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 Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 222 | "b1": ["b2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | _c = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 226 | "d1": ["d2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | constants = struct( |
| 230 | a = _a, |
| 231 | c = _c, |
| 232 | )`, |
| 233 | }, |
| 234 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame^] | 235 | 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 | |
| 252 | constants = struct( |
| 253 | a = _a, |
| 254 | c = _c, |
| 255 | )`, |
| 256 | }, |
| 257 | { |
| 258 | name: "sorts across types with variable references last", |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 259 | 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 Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame^] | 272 | exportedVariableReferenceDictVariables{ |
| 273 | "aa": map[string]string{"b1": "${b}"}, |
| 274 | "cc": map[string]string{"d1": "${config.d}"}, |
| 275 | }, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 276 | }, |
| 277 | expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT. |
| 278 | |
| 279 | _a = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 280 | "a1": ["a2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | _b = "b-val" |
| 284 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 285 | _c = ["c-val"] |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 286 | |
| 287 | _d = "d-val" |
| 288 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 289 | _e = ["e-val"] |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 290 | |
| 291 | _f = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 292 | "f1": ["f2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame^] | 295 | _aa = { |
| 296 | "b1": _b, |
| 297 | } |
| 298 | |
| 299 | _cc = { |
| 300 | "d1": _d, |
| 301 | } |
| 302 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 303 | constants = struct( |
| 304 | a = _a, |
| 305 | b = _b, |
| 306 | c = _c, |
| 307 | d = _d, |
| 308 | e = _e, |
| 309 | f = _f, |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame^] | 310 | aa = _aa, |
| 311 | cc = _cc, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 312 | )`, |
| 313 | }, |
| 314 | } |
| 315 | |
| 316 | for _, tc := range testCases { |
| 317 | t.Run(tc.name, func(t *testing.T) { |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 318 | out := bazelToolchainVars(tc.config, tc.vars...) |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 319 | if out != tc.expectedOut { |
| 320 | t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out) |
| 321 | } |
| 322 | }) |
| 323 | } |
| 324 | } |