blob: a4745e609488f594b3e2f47f5390eed930e09893 [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}