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" |
| 19 | ) |
| 20 | |
| 21 | func TestExpandVars(t *testing.T) { |
| 22 | testCases := []struct { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 23 | description string |
| 24 | stringScope exportedStringVariables |
| 25 | stringListScope exportedStringListVariables |
| 26 | toExpand string |
| 27 | expectedValues []string |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 28 | }{ |
| 29 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 30 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 38 | }, |
| 39 | toExpand: "${foo}", |
| 40 | expectedValues: []string{"bar"}, |
| 41 | }, |
| 42 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 43 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 63 | description: "double level expansion", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 64 | stringListScope: exportedStringListVariables{ |
| 65 | "foo": []string{"${bar}"}, |
| 66 | "bar": []string{"baz"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 67 | }, |
| 68 | toExpand: "${foo}", |
| 69 | expectedValues: []string{"baz"}, |
| 70 | }, |
| 71 | { |
| 72 | description: "double level expansion with a literal", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 73 | stringListScope: exportedStringListVariables{ |
| 74 | "a": []string{"${b}", "c"}, |
| 75 | "b": []string{"d"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 76 | }, |
| 77 | toExpand: "${a}", |
| 78 | expectedValues: []string{"d", "c"}, |
| 79 | }, |
| 80 | { |
| 81 | description: "double level expansion, with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 82 | stringListScope: exportedStringListVariables{ |
| 83 | "a": []string{"${b} ${c}"}, |
| 84 | "b": []string{"d"}, |
| 85 | "c": []string{"e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 86 | }, |
| 87 | toExpand: "${a}", |
| 88 | expectedValues: []string{"d", "e"}, |
| 89 | }, |
| 90 | { |
| 91 | description: "triple level expansion with two variables in a string", |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 92 | stringListScope: exportedStringListVariables{ |
| 93 | "a": []string{"${b} ${c}"}, |
| 94 | "b": []string{"${c}", "${d}"}, |
| 95 | "c": []string{"${d}"}, |
| 96 | "d": []string{"foo"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 97 | }, |
| 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 Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 105 | output := expandVar(testCase.toExpand, testCase.stringScope, testCase.stringListScope) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 106 | 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 Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 118 | |
| 119 | func 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 | |
| 139 | constants = 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 | |
| 164 | constants = 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 | |
| 191 | constants = 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 | |
| 238 | constants = 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 | } |