github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/include_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 TestInclude(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Include 28 }{ 29 { 30 yaml: `".gitlab-ci-production.yml"`, 31 want: Include{ 32 Local: ".gitlab-ci-production.yml", 33 }, 34 }, 35 { 36 yaml: `{ "project": "my-group/my-project", "file": "/templates/.gitlab-ci-template.yml" }`, 37 want: Include{ 38 Project: "my-group/my-project", 39 File: []string{"/templates/.gitlab-ci-template.yml"}, 40 }, 41 }, 42 { 43 yaml: `{ "project": "my-group/my-project", "file": [ "/templates/.builds.yml", "/templates/.tests.yml" ] }`, 44 want: Include{ 45 Project: "my-group/my-project", 46 File: []string{"/templates/.builds.yml", "/templates/.tests.yml"}, 47 }, 48 }, 49 { 50 yaml: `{ "remote": "https://gitlab.com/example-project/-/raw/main/.gitlab-ci.yml" }`, 51 want: Include{ 52 Remote: "https://gitlab.com/example-project/-/raw/main/.gitlab-ci.yml", 53 }, 54 }, 55 { 56 yaml: `{ "template": "Auto-DevOps.gitlab-ci.yml" }`, 57 want: Include{ 58 Template: "Auto-DevOps.gitlab-ci.yml", 59 }, 60 }, 61 { 62 yaml: `{ "project": "my-group/my-project", "ref": "main", "file": "/templates/.gitlab-ci-template.yml" }`, 63 want: Include{ 64 Project: "my-group/my-project", 65 Ref: "main", 66 File: []string{"/templates/.gitlab-ci-template.yml"}, 67 }, 68 }, 69 { 70 yaml: `{ "project": "my-group/my-project", "ref": "v1.0.0", "file": "/templates/.gitlab-ci-template.yml" }`, 71 want: Include{ 72 Project: "my-group/my-project", 73 Ref: "v1.0.0", 74 File: []string{"/templates/.gitlab-ci-template.yml"}, 75 }, 76 }, 77 { 78 yaml: `{ "project": "my-group/my-project", "ref": "787123b47f14b552955ca2786bc9542ae66fee5b", "file": "/templates/.gitlab-ci-template.yml" }`, 79 want: Include{ 80 Project: "my-group/my-project", 81 Ref: "787123b47f14b552955ca2786bc9542ae66fee5b", 82 File: []string{"/templates/.gitlab-ci-template.yml"}, 83 }, 84 }, 85 } 86 87 for i, test := range tests { 88 got := new(Include) 89 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 90 t.Error(err) 91 return 92 } 93 if diff := cmp.Diff(got, &test.want); diff != "" { 94 t.Errorf("Unexpected parsing results for test %v", i) 95 t.Log(diff) 96 } 97 } 98 } 99 100 func TestInclude_Error(t *testing.T) { 101 err := yaml.Unmarshal([]byte("[]"), new(Include)) 102 if err == nil || err.Error() != "failed to unmarshal include" { 103 t.Errorf("Expect error, got %s", err) 104 } 105 }