github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_apt_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 TestApt(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Apt 28 }{ 29 // bool value 30 { 31 yaml: `true`, 32 want: Apt{ 33 Enabled: true, 34 }, 35 }, 36 // string value 37 { 38 yaml: `"cmake"`, 39 want: Apt{ 40 Enabled: true, 41 Packages: []string{"cmake"}, 42 }, 43 }, 44 // string slice value 45 { 46 yaml: `["cmake"]`, 47 want: Apt{ 48 Enabled: true, 49 Packages: []string{"cmake"}, 50 }, 51 }, 52 // struct 53 { 54 yaml: `{ packages: ["cmake"] }`, 55 want: Apt{ 56 Enabled: true, 57 Packages: []string{"cmake"}, 58 }, 59 }, 60 // source string 61 { 62 yaml: `{ sources: "deadsnakes" }`, 63 want: Apt{ 64 Enabled: true, 65 Sources: []*AptSource{ 66 {Alias: "deadsnakes"}, 67 }, 68 }, 69 }, 70 // source string slice 71 { 72 yaml: `{ sources: [ "deadsnakes", { sourceline: "ppa:ubuntu-toolchain-r/test" } ] }`, 73 want: Apt{ 74 Enabled: true, 75 Sources: []*AptSource{ 76 {Alias: "deadsnakes"}, 77 {Sourceline: "ppa:ubuntu-toolchain-r/test"}, 78 }, 79 }, 80 }, 81 // source struct 82 { 83 yaml: `{ sources: { sourceline: "ppa:ubuntu-toolchain-r/test" } }`, 84 want: Apt{ 85 Enabled: true, 86 Sources: []*AptSource{ 87 {Sourceline: "ppa:ubuntu-toolchain-r/test"}, 88 }, 89 }, 90 }, 91 // source struct w/ enabled false 92 { 93 yaml: `{ enabled: false }`, 94 want: Apt{ 95 Enabled: false, 96 }, 97 }, 98 } 99 100 for i, test := range tests { 101 got := new(Apt) 102 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 103 t.Log(test.yaml) 104 t.Error(err) 105 return 106 } 107 if diff := cmp.Diff(got, &test.want); diff != "" { 108 t.Log(test.yaml) 109 t.Errorf("Unexpected parsing results for test %v", i) 110 t.Log(diff) 111 } 112 } 113 } 114 115 func TestApt_Error(t *testing.T) { 116 err := yaml.Unmarshal([]byte("[[]]"), new(Apt)) 117 if err == nil || err.Error() != "failed to unmarshal apt" { 118 t.Errorf("Expect error, got %s", err) 119 } 120 } 121 122 func TestAptSources_Error(t *testing.T) { 123 err := yaml.Unmarshal([]byte("[[]]"), new(AptSources)) 124 if err == nil || err.Error() != "failed to unmarshal apt source list" { 125 t.Errorf("Expect error, got %s", err) 126 } 127 } 128 129 func TestAptSource_Error(t *testing.T) { 130 err := yaml.Unmarshal([]byte("[[]]"), new(AptSource)) 131 if err == nil || err.Error() != "failed to unmarshal apt source" { 132 t.Errorf("Expect error, got %s", err) 133 } 134 }