github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/pipeline_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  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"gopkg.in/yaml.v3"
    24  )
    25  
    26  func TestPipeline(t *testing.T) {
    27  	tests, err := filepath.Glob("testdata/*/*.yaml")
    28  	if err != nil {
    29  		t.Error(err)
    30  		return
    31  	}
    32  
    33  	for _, test := range tests {
    34  
    35  		switch test {
    36  		case "testdata/matrix/example-3.yaml",
    37  			"testdata/matrix/example-8.yaml":
    38  			// skip these tests due to unsupported syntax
    39  			// TODO these should be eventually re-enabled
    40  			continue
    41  		}
    42  
    43  		t.Run(test, func(t *testing.T) {
    44  
    45  			// parse the yaml file
    46  			tmp1, err := ParseFile(test)
    47  			if err != nil {
    48  				t.Error(err)
    49  				return
    50  			}
    51  
    52  			// marshal the yaml file
    53  			tmp2, err := yaml.Marshal(tmp1)
    54  			if err != nil {
    55  				t.Error(err)
    56  				return
    57  			}
    58  
    59  			// unmarshal the yaml file to a map
    60  			got := map[string]interface{}{}
    61  			if err := yaml.Unmarshal(tmp2, &got); err != nil {
    62  				t.Error(err)
    63  				return
    64  			}
    65  
    66  			// parse the golden yaml file and unmarshal
    67  			data, err := ioutil.ReadFile(test + ".golden")
    68  			if err != nil {
    69  				// skip tests with no golden files
    70  				// TODO these should be re-enabled
    71  				return
    72  			}
    73  
    74  			// unmarshal the golden yaml file
    75  			want := map[string]interface{}{}
    76  			if err := yaml.Unmarshal(data, &want); err != nil {
    77  				t.Error(err)
    78  				return
    79  			}
    80  
    81  			// compare the parsed yaml to the golden file
    82  			if diff := cmp.Diff(got, want); diff != "" {
    83  				t.Errorf("Unexpected parsing result")
    84  				t.Log(diff)
    85  			}
    86  		})
    87  	}
    88  }