github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/parse_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 "io/ioutil" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 ) 23 24 func TestParseFile(t *testing.T) { 25 got, err := ParseFile("testdata/hello.yaml") 26 if err != nil { 27 t.Error(err) 28 } 29 30 want := &Pipeline{ 31 Stages: []string{"greeting"}, 32 Jobs: map[string]*Job{ 33 "en": { 34 Script: Stringorslice{"echo hello world"}, 35 Stage: "greeting", 36 }, 37 "fr": { 38 Script: Stringorslice{"echo bonjour monde"}, 39 Stage: "greeting", 40 }, 41 }, 42 } 43 44 if diff := cmp.Diff(got, want); diff != "" { 45 t.Errorf("Unexpected parsing result") 46 t.Log(diff) 47 } 48 } 49 50 func TestParseFile_Error(t *testing.T) { 51 _, err := ParseFile("testdata/file-does-not-exist.yaml") 52 if err == nil { 53 t.Errorf("Expect parsing error") 54 } 55 } 56 57 func TestParseString(t *testing.T) { 58 out, _ := ioutil.ReadFile("testdata/hello.yaml") 59 got, err := ParseString(string(out)) 60 if err != nil { 61 t.Error(err) 62 } 63 64 want := &Pipeline{ 65 Stages: []string{"greeting"}, 66 Jobs: map[string]*Job{ 67 "en": { 68 Script: Stringorslice{"echo hello world"}, 69 Stage: "greeting", 70 }, 71 "fr": { 72 Script: Stringorslice{"echo bonjour monde"}, 73 Stage: "greeting", 74 }, 75 }, 76 } 77 78 if diff := cmp.Diff(got, want); diff != "" { 79 t.Errorf("Unexpected parsing result") 80 t.Log(diff) 81 } 82 }