github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/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 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/gotidy/ptr" 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: `"dist/**"`, 32 want: Artifacts{ 33 Paths: []string{"dist/**"}, 34 }, 35 }, 36 { 37 yaml: `[ "dist/**" ]`, 38 want: Artifacts{ 39 Paths: []string{"dist/**"}, 40 }, 41 }, 42 { 43 yaml: `{ paths: [ "dist/**" ], download: false }`, 44 want: Artifacts{ 45 Download: ptr.Bool(false), 46 Paths: []string{"dist/**"}, 47 }, 48 }, 49 { 50 yaml: `{ paths: [ "dist/**" ], download: true }`, 51 want: Artifacts{ 52 Download: ptr.Bool(true), 53 Paths: []string{"dist/**"}, 54 }, 55 }, 56 } 57 58 for i, test := range tests { 59 got := new(Artifacts) 60 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 61 t.Log(test.yaml) 62 t.Error(err) 63 return 64 } 65 if diff := cmp.Diff(got, &test.want); diff != "" { 66 t.Log(test.yaml) 67 t.Errorf("Unexpected parsing results for test %v", i) 68 t.Log(diff) 69 } 70 } 71 } 72 73 func TestArtifacts_Error(t *testing.T) { 74 err := yaml.Unmarshal([]byte("[[]]"), new(Artifacts)) 75 if err == nil || err.Error() != "failed to unmarshal artifacts" { 76 t.Errorf("Expect error, got %s", err) 77 } 78 } 79 80 func TestArtifacts_Marshal(t *testing.T) { 81 tests := []struct { 82 before Artifacts 83 after string 84 }{ 85 { 86 before: Artifacts{ 87 Paths: []string{"dist/**"}, 88 }, 89 after: "- dist/**\n", 90 }, 91 { 92 before: Artifacts{ 93 Download: ptr.Bool(true), 94 Paths: []string{"dist/**"}, 95 }, 96 after: "download: true\npaths:\n - dist/**\n", 97 }, 98 { 99 before: Artifacts{ 100 Download: ptr.Bool(false), 101 Paths: []string{"dist/**"}, 102 }, 103 after: "download: false\npaths:\n - dist/**\n", 104 }, 105 { 106 before: Artifacts{ 107 Download: nil, 108 Paths: nil, 109 }, 110 after: "null\n", 111 }, 112 } 113 114 for _, test := range tests { 115 after, err := yaml.Marshal(&test.before) 116 if err != nil { 117 t.Error(err) 118 return 119 } 120 if got, want := string(after), test.after; got != want { 121 t.Errorf("want yaml %q, got %q", want, got) 122 } 123 } 124 }