gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/helpers/converter_test.go (about) 1 package helpers 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "github.com/stretchr/testify/require" 6 "gopkg.in/yaml.v2" 7 "reflect" 8 "testing" 9 ) 10 11 type TestObj struct { 12 Text string `json:"TextJson" yaml:"TextYaml"` 13 Number int 14 } 15 16 func TestSimpleYamlMarshalling(t *testing.T) { 17 18 ymlString := ToYAML(TestObj{ 19 Text: "example", 20 Number: 25, 21 }) 22 expectedYml := "TextYaml: example\nnumber: 25\n" 23 24 if ymlString != expectedYml { 25 t.Error("Expected ", expectedYml, ", got ", ymlString) 26 } 27 } 28 29 func TestSimpleTomlMarshalling(t *testing.T) { 30 31 tomlString := ToTOML(TestObj{ 32 Text: "example", 33 Number: 25, 34 }) 35 expectedToml := "Text = \"example\"\nNumber = 25\n" 36 37 if tomlString != expectedToml { 38 t.Error("Expected ", expectedToml, ", got ", tomlString) 39 } 40 } 41 42 func TestToConfigMap(t *testing.T) { 43 data := ` 44 build: 45 script: 46 - echo "1" >> foo 47 - cat foo 48 49 cache: 50 untracked: true 51 paths: 52 - vendor/ 53 - foo 54 55 test: 56 script: 57 - make test 58 ` 59 60 config := make(map[string]interface{}) 61 err := yaml.Unmarshal([]byte(data), config) 62 if err != nil { 63 t.Error("Error parsing test YAML data") 64 } 65 66 expectedCacheConfig := map[string]interface{}{ 67 "untracked": true, 68 "paths": []interface{}{"vendor/", "foo"}, 69 } 70 cacheConfig, ok := ToConfigMap(config["cache"]) 71 72 if !ok { 73 t.Error("Conversion failed") 74 } 75 76 if !reflect.DeepEqual(cacheConfig, expectedCacheConfig) { 77 t.Error("Result ", cacheConfig, " was not equal to ", expectedCacheConfig) 78 } 79 } 80 81 func TestGetMapKey(t *testing.T) { 82 data := ` 83 test: 84 script: 85 - make test 86 cache: 87 untracked: true 88 paths: 89 - vendor/ 90 - foo 91 ` 92 93 config1 := make(map[string]interface{}) 94 require.NoError(t, yaml.Unmarshal([]byte(data), config1)) 95 96 value, ok := GetMapKey(config1, "test", "cache", "untracked") 97 assert.True(t, ok) 98 assert.Equal(t, true, value) 99 100 _, ok = GetMapKey(config1, "test", "undefined", "untracked") 101 assert.False(t, ok) 102 }