@@ -3,6 +3,7 @@ package config
33import (
44 "fmt"
55 "os"
6+ "strings"
67 "testing"
78
89 "github.com/stretchr/testify/assert"
@@ -25,6 +26,14 @@ func testInvalidInterpolatedLine(t *testing.T, line string) {
2526 assert .Equal (t , false , success )
2627}
2728
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+
2837func TestParseLine (t * testing.T ) {
2938 variables := map [string ]string {
3039 "A" : "ABC" ,
@@ -38,11 +47,17 @@ func TestParseLine(t *testing.T) {
3847 "defTest" : "WORKED" ,
3948 }
4049
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+
4155 testInterpolatedLine (t , "WORKED" , "$lower" , variables )
4256 testInterpolatedLine (t , "WORKED" , "${MiXeD}" , variables )
4357 testInterpolatedLine (t , "WORKED" , "${split_VaLue}" , variables )
4458 // make sure variable name is parsed correctly with default value
4559 testInterpolatedLine (t , "WORKED" , "${defTest:-sometest}" , variables )
60+ testInterpolatedLine (t , "WORKED" , "${defTest-sometest}" , variables )
4661 // Starting with a number isn't valid
4762 testInterpolatedLine (t , "" , "$9aNumber" , variables )
4863 testInterpolatedLine (t , "WORKED" , "$a9Number" , variables )
@@ -70,6 +85,7 @@ func TestParseLine(t *testing.T) {
7085 testInterpolatedLine (t , "" , "$E" , variables )
7186 testInterpolatedLine (t , "" , "${E}" , variables )
7287
88+ testInvalidInterpolatedLine (t , "${df:val}" )
7389 testInvalidInterpolatedLine (t , "${" )
7490 testInvalidInterpolatedLine (t , "$}" )
7591 testInvalidInterpolatedLine (t , "${}" )
@@ -213,7 +229,7 @@ func TestInterpolate(t *testing.T) {
213229
214230 # dictionary item value
215231 labels:
216- mylabel: "myvalue "
232+ mylabel: "my-val:ue "
217233
218234 # unset value
219235 hostname: "host-"
@@ -231,7 +247,7 @@ func TestInterpolate(t *testing.T) {
231247
232248 # dictionary item value
233249 labels:
234- mylabel: "${LABEL_VALUE:-myvalue }"
250+ mylabel: "${LABEL_VALUE-my-val:ue }"
235251
236252 # unset value
237253 hostname: "host-${UNSET_VALUE}"
0 commit comments