github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/artifacts_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 "strings" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "gopkg.in/yaml.v3" 23 ) 24 25 func TestArtifacts(t *testing.T) { 26 tests := []struct { 27 yaml string 28 want Artifacts 29 }{ 30 { 31 yaml: ` 32 paths: 33 - binaries/ 34 - .config`, 35 want: Artifacts{ 36 Paths: Stringorslice{ 37 "binaries/", 38 ".config", 39 }, 40 }, 41 }, 42 { 43 yaml: ` 44 paths: 45 - test.txt`, 46 want: Artifacts{ 47 Paths: Stringorslice{ 48 "test.txt", 49 }, 50 }, 51 }, 52 { 53 yaml: ` 54 paths: []`, 55 want: Artifacts{}, 56 }, 57 { 58 yaml: ` 59 paths: 60 - binaries/ 61 exclude: 62 - binaries/**/*.o`, 63 want: Artifacts{ 64 Paths: Stringorslice{ 65 "binaries/", 66 }, 67 Exclude: Stringorslice{ 68 "binaries/**/*.o", 69 }, 70 }, 71 }, 72 { 73 yaml: ` 74 paths: 75 - test.txt 76 exclude: 77 - test.log`, 78 want: Artifacts{ 79 Paths: Stringorslice{ 80 "test.txt", 81 }, 82 Exclude: Stringorslice{ 83 "test.log", 84 }, 85 }, 86 }, 87 { 88 yaml: ` 89 paths: 90 - test.txt 91 exclude: []`, 92 want: Artifacts{ 93 Paths: Stringorslice{ 94 "test.txt", 95 }, 96 }, 97 }, 98 { 99 yaml: ` 100 expire_in: '42'`, 101 want: Artifacts{ 102 ExpireIn: "42", 103 }, 104 }, 105 { 106 yaml: ` 107 expire_in: '2 hrs 20 min'`, 108 want: Artifacts{ 109 ExpireIn: "2 hrs 20 min", 110 }, 111 }, 112 { 113 yaml: ` 114 expire_in: '6 mos 1 day'`, 115 want: Artifacts{ 116 ExpireIn: "6 mos 1 day", 117 }, 118 }, 119 { 120 yaml: ` 121 expire_in: 'never'`, 122 want: Artifacts{ 123 ExpireIn: "never", 124 }, 125 }, 126 { 127 yaml: ` 128 expose_as: 'artifact 1' 129 paths: ['file.txt']`, 130 want: Artifacts{ 131 ExposeAs: "artifact 1", 132 Paths: Stringorslice{"file.txt"}, 133 }, 134 }, 135 { 136 yaml: ` 137 name: "job1-artifacts-file" 138 paths: ["binaries/"]`, 139 want: Artifacts{ 140 Name: "job1-artifacts-file", 141 Paths: Stringorslice{"binaries/"}, 142 }, 143 }, 144 { 145 yaml: ` 146 public: false`, 147 want: Artifacts{ 148 Public: pointerToBool(false), 149 }, 150 }, 151 { 152 yaml: ` 153 public: true`, 154 want: Artifacts{ 155 Public: pointerToBool(true), 156 }, 157 }, 158 { 159 yaml: ` 160 reports: 161 junit: rspec.xml`, 162 want: Artifacts{ 163 Reports: map[string]interface{}{ 164 "junit": "rspec.xml", 165 }, 166 }, 167 }, 168 { 169 yaml: ` 170 reports: 171 type1: output1.xml 172 type2: output2.xml`, 173 want: Artifacts{ 174 Reports: map[string]interface{}{ 175 "type1": "output1.xml", 176 "type2": "output2.xml", 177 }, 178 }, 179 }, 180 { 181 yaml: ` 182 untracked: true`, 183 want: Artifacts{ 184 Untracked: true, 185 }, 186 }, 187 { 188 yaml: ` 189 untracked: false`, 190 want: Artifacts{ 191 Untracked: false, 192 }, 193 }, 194 { 195 yaml: ` 196 paths: 197 - binaries/`, 198 want: Artifacts{ 199 Paths: []string{"binaries/"}, 200 Untracked: false, // default value if not defined 201 }, 202 }, 203 { 204 yaml: ` 205 when: on_failure`, 206 want: Artifacts{ 207 When: "on_failure", 208 }, 209 }, 210 { 211 yaml: ` 212 when: always`, 213 want: Artifacts{ 214 When: "always", 215 }, 216 }, 217 } 218 219 for i, test := range tests { 220 got := new(Artifacts) 221 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 222 t.Error(err) 223 return 224 } 225 if diff := cmp.Diff(got, &test.want); diff != "" { 226 t.Errorf("Unexpected parsing results for test %v", i) 227 t.Log(diff) 228 } 229 } 230 } 231 232 func TestArtifacts_Error(t *testing.T) { 233 err := yaml.Unmarshal([]byte("[]"), new(Artifacts)) 234 if err == nil || !strings.Contains(err.Error(), "cannot unmarshal") { 235 t.Errorf("Expect error, got %s", err) 236 } 237 } 238 239 func pointerToBool(b bool) *bool { 240 return &b 241 }