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 | } |