@@ -3,6 +3,7 @@ package config
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "strings"
6
7
"testing"
7
8
8
9
"github.com/stretchr/testify/assert"
@@ -25,6 +26,14 @@ func testInvalidInterpolatedLine(t *testing.T, line string) {
25
26
assert .Equal (t , false , success )
26
27
}
27
28
29
+ func testInterpolatedDefault (t * testing.T , line string , delim string , expectedVar string , expectedVal string ) {
30
+ envVar , _ := parseLine (line , func (env string ) string { return env })
31
+ pos := strings .Index (line , delim )
32
+ envDefault , _ , _ := parseDefaultValue (line , pos )
33
+ assert .Equal (t , expectedVal , envDefault )
34
+ assert .Equal (t , expectedVar , envVar )
35
+ }
36
+
28
37
func TestParseLine (t * testing.T ) {
29
38
variables := map [string ]string {
30
39
"A" : "ABC" ,
@@ -35,11 +44,20 @@ func TestParseLine(t *testing.T) {
35
44
"split_VaLue" : "WORKED" ,
36
45
"9aNumber" : "WORKED" ,
37
46
"a9Number" : "WORKED" ,
47
+ "defTest" : "WORKED" ,
38
48
}
39
49
50
+ testInterpolatedDefault (t , "${defVar:-defVal}" , ":-" , "defVar" , "defVal" )
51
+ testInterpolatedDefault (t , "${defVar2-defVal2}" , "-" , "defVar2" , "defVal2" )
52
+ testInterpolatedDefault (t , "${defVar:-def:Val}" , ":-" , "defVar" , "def:Val" )
53
+ testInterpolatedDefault (t , "${defVar:-def-Val}" , ":-" , "defVar" , "def-Val" )
54
+
40
55
testInterpolatedLine (t , "WORKED" , "$lower" , variables )
41
56
testInterpolatedLine (t , "WORKED" , "${MiXeD}" , variables )
42
57
testInterpolatedLine (t , "WORKED" , "${split_VaLue}" , variables )
58
+ // make sure variable name is parsed correctly with default value
59
+ testInterpolatedLine (t , "WORKED" , "${defTest:-sometest}" , variables )
60
+ testInterpolatedLine (t , "WORKED" , "${defTest-sometest}" , variables )
43
61
// Starting with a number isn't valid
44
62
testInterpolatedLine (t , "" , "$9aNumber" , variables )
45
63
testInterpolatedLine (t , "WORKED" , "$a9Number" , variables )
@@ -67,6 +85,7 @@ func TestParseLine(t *testing.T) {
67
85
testInterpolatedLine (t , "" , "$E" , variables )
68
86
testInterpolatedLine (t , "" , "${E}" , variables )
69
87
88
+ testInvalidInterpolatedLine (t , "${df:val}" )
70
89
testInvalidInterpolatedLine (t , "${" )
71
90
testInvalidInterpolatedLine (t , "$}" )
72
91
testInvalidInterpolatedLine (t , "${}" )
@@ -198,6 +217,43 @@ func TestInterpolate(t *testing.T) {
198
217
"HOST_PORT" : "=" ,
199
218
"LABEL_VALUE" : "myvalue==" ,
200
219
})
220
+ // same as above but with default values
221
+ testInterpolatedConfig (t ,
222
+ `web:
223
+ # unbracketed name
224
+ image: busybox
225
+
226
+ # array element
227
+ ports:
228
+ - "80:8000"
229
+
230
+ # dictionary item value
231
+ labels:
232
+ mylabel: "my-val:ue"
233
+
234
+ # unset value
235
+ hostname: "host-"
236
+
237
+ # escaped interpolation
238
+ command: "${ESCAPED}"` ,
239
+
240
+ `web:
241
+ # unbracketed name
242
+ image: ${IMAGE:-busybox}
243
+
244
+ # array element
245
+ ports:
246
+ - "${HOST_PORT:-80}:8000"
247
+
248
+ # dictionary item value
249
+ labels:
250
+ mylabel: "${LABEL_VALUE-my-val:ue}"
251
+
252
+ # unset value
253
+ hostname: "host-${UNSET_VALUE}"
254
+
255
+ # escaped interpolation
256
+ command: "$${ESCAPED}"` , map [string ]string {})
201
257
202
258
testInvalidInterpolatedConfig (t ,
203
259
`web:
0 commit comments