github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/on_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 TestOnConditions(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want On 28 }{ 29 // 30 // test string 31 // 32 { 33 yaml: `push`, 34 want: On{ 35 Push: &Push{}, 36 }, 37 }, 38 // 39 // test slice 40 // 41 { 42 yaml: `[push, fork]`, 43 want: On{ 44 Push: &Push{}, 45 Fork: &struct{}{}, 46 }, 47 }, 48 // 49 // test object 50 // 51 { 52 yaml: `{ push: { branches: [ main, 'releases/**' ] } }`, 53 want: On{ 54 Push: &Push{ 55 Branches: []string{"main", "releases/**"}, 56 }, 57 }, 58 }, 59 // 60 // test individual keywords to ensure non-nil structs 61 // and default event types 62 // 63 { 64 yaml: `branch_protection_rule`, 65 want: On{BranchProtectionRule: &Event{}}, 66 }, 67 { 68 yaml: `check_run`, 69 want: On{CheckRun: &Event{}}, 70 }, 71 { 72 yaml: `check_suite`, 73 want: On{CheckSuite: &Event{}}, 74 }, 75 { 76 yaml: `create`, 77 want: On{Create: &struct{}{}}, 78 }, 79 { 80 yaml: `delete`, 81 want: On{Delete: &struct{}{}}, 82 }, 83 { 84 yaml: `deployment`, 85 want: On{Deployment: &struct{}{}}, 86 }, 87 { 88 yaml: `deployment_status`, 89 want: On{DeploymentStatus: &struct{}{}}, 90 }, 91 { 92 yaml: `discussion`, 93 want: On{Discussion: &Event{}}, 94 }, 95 { 96 yaml: `discussion_comment`, 97 want: On{DiscussionComment: &Event{}}, 98 }, 99 { 100 yaml: `fork`, 101 want: On{Fork: &struct{}{}}, 102 }, 103 { 104 yaml: `gollum`, 105 want: On{Gollum: &struct{}{}}, 106 }, 107 { 108 yaml: `issue_comment`, 109 want: On{IssueComment: &Event{}}, 110 }, 111 { 112 yaml: `issues`, 113 want: On{Issues: &Event{}}, 114 }, 115 { 116 yaml: `label`, 117 want: On{Label: &Event{}}, 118 }, 119 { 120 yaml: `member`, 121 want: On{Member: &Event{}}, 122 }, 123 { 124 yaml: `merge_group`, 125 want: On{MergeGroup: &Event{}}, 126 }, 127 { 128 yaml: `milestone`, 129 want: On{Milestone: &Event{}}, 130 }, 131 { 132 yaml: `page_build`, 133 want: On{PageBuild: &struct{}{}}, 134 }, 135 { 136 yaml: `project`, 137 want: On{Project: &Event{}}, 138 }, 139 { 140 yaml: `project_card`, 141 want: On{ProjectCard: &Event{}}, 142 }, 143 { 144 yaml: `project_column`, 145 want: On{ProjectColumn: &Event{}}, 146 }, 147 { 148 yaml: `public`, 149 want: On{Public: &struct{}{}}, 150 }, 151 { 152 yaml: `pull_request`, 153 want: On{PullRequest: &PullRequest{}}, 154 }, 155 { 156 yaml: `pull_request_review`, 157 want: On{PullRequestReview: &Event{}}, 158 }, 159 { 160 yaml: `pull_request_review_comment`, 161 want: On{PullRequestReviewComment: &Event{}}, 162 }, 163 { 164 yaml: `pull_request_target`, 165 want: On{PullRequestTarget: &PullRequestTarget{}}, 166 }, 167 { 168 yaml: `push`, 169 want: On{Push: &Push{}}, 170 }, 171 { 172 yaml: `registry_package`, 173 want: On{RegistryPackage: &Event{}}, 174 }, 175 { 176 yaml: `repository_dispatch`, 177 want: On{RepositoryDispatch: &Event{}}, 178 }, 179 { 180 yaml: `release`, 181 want: On{Release: &Event{}}, 182 }, 183 { 184 yaml: `schedule`, 185 want: On{Schedule: &Schedule{}}, 186 }, 187 { 188 yaml: `status`, 189 want: On{Status: &struct{}{}}, 190 }, 191 { 192 yaml: `watch`, 193 want: On{Watch: &Event{}}, 194 }, 195 { 196 yaml: `workflow_call`, 197 want: On{WorkflowCall: &WorkflowCall{}}, 198 }, 199 { 200 yaml: `workflow_dispatch`, 201 want: On{WorkflowDispatch: &WorkflowDispatch{}}, 202 }, 203 { 204 yaml: `workflow_run`, 205 want: On{WorkflowRun: &WorkflowRun{}}, 206 }, 207 } 208 209 for i, test := range tests { 210 got := new(On) 211 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 212 t.Log(test.yaml) 213 t.Error(err) 214 return 215 } 216 if diff := cmp.Diff(got, &test.want); diff != "" { 217 t.Log(test.yaml) 218 t.Errorf("Unexpected parsing results for test %v", i) 219 t.Log(diff) 220 } 221 } 222 } 223 224 func TestOn_Error(t *testing.T) { 225 err := yaml.Unmarshal([]byte("[[]]"), new(On)) 226 if err == nil || err.Error() != "failed to unmarshal on" { 227 t.Errorf("Expect error, got %s", err) 228 } 229 }