|
17 | 17 | package interpolation |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "testing" |
21 | | - |
22 | 20 | "strconv" |
| 21 | + "testing" |
23 | 22 |
|
24 | 23 | "gotest.tools/v3/assert" |
25 | 24 | is "gotest.tools/v3/assert/cmp" |
@@ -95,6 +94,75 @@ func TestInterpolateWithDefaults(t *testing.T) { |
95 | 94 | assert.Check(t, is.DeepEqual(expected, result)) |
96 | 95 | } |
97 | 96 |
|
| 97 | +func TestValidUnexistentInterpolation(t *testing.T) { |
| 98 | + var testcases = []struct { |
| 99 | + test string |
| 100 | + expected string |
| 101 | + }{ |
| 102 | + {test: "{{{ ${FOO:-foo_} }}}", expected: "{{{ foo_ }}}"}, |
| 103 | + {test: "{{{ ${FOO:-foo-bar-value} }}}", expected: "{{{ foo-bar-value }}}"}, |
| 104 | + {test: "{{{ ${FOO:-foo} ${BAR:-DEFAULT_VALUE} }}}", expected: "{{{ foo DEFAULT_VALUE }}}"}, |
| 105 | + {test: "{{{ ${BAR} }}}", expected: "{{{ }}}"}, |
| 106 | + {test: "${FOO:-baz} }}}", expected: "baz }}}"}, |
| 107 | + {test: "${FOO-baz} }}}", expected: "baz }}}"}, |
| 108 | + } |
| 109 | + |
| 110 | + getServiceConfig := func(val string) map[string]interface{} { |
| 111 | + return map[string]interface{}{ |
| 112 | + "myservice": map[string]interface{}{ |
| 113 | + "environment": map[string]interface{}{ |
| 114 | + "TESTVAR": val, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + for _, testcase := range testcases { |
| 121 | + result, err := Interpolate(getServiceConfig(testcase.test), Options{}) |
| 122 | + assert.NilError(t, err) |
| 123 | + assert.Check(t, is.DeepEqual(getServiceConfig(testcase.expected), result)) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestValidExistentInterpolation(t *testing.T) { |
| 128 | + var testcases = []struct { |
| 129 | + test string |
| 130 | + expected string |
| 131 | + }{ |
| 132 | + // Only FOO is set |
| 133 | + {test: "{{{ ${FOO:-foo_} }}}", expected: "{{{ bar }}}"}, |
| 134 | + {test: "{{{ ${FOO:-foo-bar-value} }}}", expected: "{{{ bar }}}"}, |
| 135 | + {test: "{{{ ${FOO:-foo} ${BAR:-DEFAULT_VALUE} }}}", expected: "{{{ bar DEFAULT_VALUE }}}"}, |
| 136 | + {test: "{{{ ${BAR} }}}", expected: "{{{ }}}"}, |
| 137 | + {test: "${FOO:-baz} }}}", expected: "bar }}}"}, |
| 138 | + {test: "${FOO-baz} }}}", expected: "bar }}}"}, |
| 139 | + |
| 140 | + // Both FOO and USER are set |
| 141 | + {test: "{{{ ${FOO:-foo_} }}}", expected: "{{{ bar }}}"}, |
| 142 | + {test: "{{{ ${FOO:-foo-bar-value} }}}", expected: "{{{ bar }}}"}, |
| 143 | + {test: "{{{ ${FOO:-foo} ${USER:-bar} }}}", expected: "{{{ bar jenny }}}"}, |
| 144 | + {test: "{{{ ${USER} }}}", expected: "{{{ jenny }}}"}, |
| 145 | + {test: "${FOO:-baz} }}}", expected: "bar }}}"}, |
| 146 | + {test: "${FOO-baz} }}}", expected: "bar }}}"}, |
| 147 | + } |
| 148 | + |
| 149 | + getServiceConfig := func(val string) map[string]interface{} { |
| 150 | + return map[string]interface{}{ |
| 151 | + "myservice": map[string]interface{}{ |
| 152 | + "environment": map[string]interface{}{ |
| 153 | + "TESTVAR": val, |
| 154 | + }, |
| 155 | + }, |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + for _, testcase := range testcases { |
| 160 | + result, err := Interpolate(getServiceConfig(testcase.test), Options{LookupValue: defaultMapping}) |
| 161 | + assert.NilError(t, err) |
| 162 | + assert.Check(t, is.DeepEqual(getServiceConfig(testcase.expected), result)) |
| 163 | + } |
| 164 | +} |
| 165 | + |
98 | 166 | func TestInterpolateWithCast(t *testing.T) { |
99 | 167 | config := map[string]interface{}{ |
100 | 168 | "foo": map[string]interface{}{ |
|
0 commit comments