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 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 51 | 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 Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 68 | expectedValues: []string{"baz hello"}, |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 69 | }, |
| 70 | { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 71 | description: "double level expansion", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 72 | stringListScope: exportedStringListVariables{ |
| 73 | "foo": []string{"${bar}"}, |
| 74 | "bar": []string{"baz"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 75 | }, |
| 76 | toExpand: "${foo}", |
| 77 | expectedValues: []string{"baz"}, |
| 78 | }, |
| 79 | { |
| 80 | description: "double level expansion with a literal", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 81 | stringListScope: exportedStringListVariables{ |
| 82 | "a": []string{"${b}", "c"}, |
| 83 | "b": []string{"d"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 84 | }, |
| 85 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 86 | expectedValues: []string{"d c"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 87 | }, |
| 88 | { |
| 89 | description: "double level expansion, with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 90 | stringListScope: exportedStringListVariables{ |
| 91 | "a": []string{"${b} ${c}"}, |
| 92 | "b": []string{"d"}, |
| 93 | "c": []string{"e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 94 | }, |
| 95 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 96 | expectedValues: []string{"d e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 97 | }, |
| 98 | { |
| 99 | description: "triple level expansion with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 100 | stringListScope: exportedStringListVariables{ |
| 101 | "a": []string{"${b} ${c}"}, |
| 102 | "b": []string{"${c}", "${d}"}, |
| 103 | "c": []string{"${d}"}, |
| 104 | "d": []string{"foo"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 105 | }, |
| 106 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 107 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 133 | }, |
| 134 | } |
| 135 | |
| 136 | for _, testCase := range testCases { |
| 137 | t.Run(testCase.description, func(t *testing.T) { |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 138 | output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 139 | 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 Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 151 | |
| 152 | func TestBazelToolchainVars(t *testing.T) { |
| 153 | testCases := []struct { |
| 154 | name string |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 155 | config android.Config |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 156 | 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 | |
| 173 | constants = 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 | |
| 198 | constants = 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 | |
| 225 | constants = 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 | |
| 272 | constants = 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 Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame^] | 285 | out := bazelToolchainVars(tc.config, tc.vars...) |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 286 | if out != tc.expectedOut { |
| 287 | t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out) |
| 288 | } |
| 289 | }) |
| 290 | } |
| 291 | } |