github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/env_test.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "gopkg.in/yaml.v3" 22 ) 23 24 func TestEnv(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Env 28 }{ 29 // test string in key=value format 30 { 31 yaml: `"FOO=foo"`, 32 want: Env{ 33 Global: []map[string]string{ 34 {"FOO": "foo"}, 35 }, 36 }, 37 }, 38 // test map 39 { 40 yaml: `{ FOO: foo }`, 41 want: Env{ 42 Global: []map[string]string{ 43 {"FOO": "foo"}, 44 }, 45 }, 46 }, 47 // test map array 48 { 49 yaml: `[ { FOO: foo } ]`, 50 want: Env{ 51 Global: []map[string]string{ 52 {"FOO": "foo"}, 53 }, 54 }, 55 }, 56 // test slice array of key=value items 57 { 58 yaml: `[ FOO=foo, BAR=bar ]`, 59 want: Env{ 60 Global: []map[string]string{ 61 {"FOO": "foo", "BAR": "bar"}, 62 }, 63 }, 64 }, 65 // test env array with different value types 66 // first value type is a string in key=value format 67 // second value type is slice of map values 68 { 69 yaml: `[ "FOO=foo", { BAR: bar } ]`, 70 want: Env{ 71 Global: []map[string]string{ 72 {"FOO": "foo"}, 73 {"BAR": "bar"}, 74 }, 75 }, 76 }, 77 // test env struct 78 { 79 yaml: `{ global: [ FOO: foo ], jobs: [ BAR: bar ] }`, 80 want: Env{ 81 Global: []map[string]string{ 82 {"FOO": "foo"}, 83 }, 84 Jobs: []map[string]string{ 85 {"BAR": "bar"}, 86 }, 87 }, 88 }, 89 } 90 91 for i, test := range tests { 92 got := new(Env) 93 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 94 t.Log(test.yaml) 95 t.Error(err) 96 return 97 } 98 if diff := cmp.Diff(got, &test.want); diff != "" { 99 t.Log(test.yaml) 100 t.Errorf("Unexpected parsing results for test %v", i) 101 t.Log(diff) 102 } 103 } 104 }