github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/common/variables_test.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestVariableString(t *testing.T) {
     9  	v := BuildVariable{"key", "value", false, false, false}
    10  	assert.Equal(t, "key=value", v.String())
    11  }
    12  
    13  func TestPublicAndInternalVariables(t *testing.T) {
    14  	v1 := BuildVariable{"key", "value", false, false, false}
    15  	v2 := BuildVariable{"public", "value", true, false, false}
    16  	v3 := BuildVariable{"private", "value", false, true, false}
    17  	all := BuildVariables{v1, v2, v3}
    18  	public := all.PublicOrInternal()
    19  	assert.NotContains(t, public, v1)
    20  	assert.Contains(t, public, v2)
    21  	assert.Contains(t, public, v3)
    22  }
    23  
    24  func TestListVariables(t *testing.T) {
    25  	v := BuildVariables{{"key", "value", false, false, false}}
    26  	assert.Equal(t, []string{"key=value"}, v.StringList())
    27  }
    28  
    29  func TestGetVariable(t *testing.T) {
    30  	v1 := BuildVariable{"key", "key_value", false, false, false}
    31  	v2 := BuildVariable{"public", "public_value", true, false, false}
    32  	v3 := BuildVariable{"private", "private_value", false, false, false}
    33  	all := BuildVariables{v1, v2, v3}
    34  
    35  	assert.Equal(t, "public_value", all.Get("public"))
    36  	assert.Empty(t, all.Get("other"))
    37  }
    38  
    39  func TestParseVariable(t *testing.T) {
    40  	v, err := ParseVariable("key=value=value2")
    41  	assert.NoError(t, err)
    42  	assert.Equal(t, BuildVariable{"key", "value=value2", false, false, false}, v)
    43  }
    44  
    45  func TestInvalidParseVariable(t *testing.T) {
    46  	_, err := ParseVariable("some_other_key")
    47  	assert.Error(t, err)
    48  }
    49  
    50  func TestVariablesExpansion(t *testing.T) {
    51  	all := BuildVariables{
    52  		{"key", "value_of_$public", false, false, false},
    53  		{"public", "some_value", true, false, false},
    54  		{"private", "value_of_${public}", false, false, false},
    55  		{"public", "value_of_$undefined", true, false, false},
    56  	}
    57  
    58  	expanded := all.Expand()
    59  	assert.Len(t, expanded, 4)
    60  	assert.Equal(t, expanded.Get("key"), "value_of_value_of_$undefined")
    61  	assert.Equal(t, expanded.Get("public"), "value_of_")
    62  	assert.Equal(t, expanded.Get("private"), "value_of_value_of_$undefined")
    63  	assert.Equal(t, expanded.ExpandValue("${public} ${private}"), "value_of_ value_of_value_of_$undefined")
    64  }
    65  
    66  func TestSpecialVariablesExpansion(t *testing.T) {
    67  	all := BuildVariables{
    68  		{"key", "$$", false, false, false},
    69  		{"key2", "$/dsa", true, false, false},
    70  		{"key3", "aa$@bb", false, false, false},
    71  		{"key4", "aa${@}bb", false, false, false},
    72  	}
    73  
    74  	expanded := all.Expand()
    75  	assert.Len(t, expanded, 4)
    76  	assert.Equal(t, expanded.Get("key"), "$")
    77  	assert.Equal(t, expanded.Get("key2"), "/dsa")
    78  	assert.Equal(t, expanded.Get("key3"), "aabb")
    79  	assert.Equal(t, expanded.Get("key4"), "aabb")
    80  }