github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/yaml/config_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  		t.Run(test, func(t *testing.T) {
    35  			// parse the yaml file
    36  			tmp1, err := ParseFile(test)
    37  			if err != nil {
    38  				t.Error(err)
    39  				return
    40  			}
    41  
    42  			// marshal the yaml file
    43  			tmp2, err := yaml.Marshal(tmp1)
    44  			if err != nil {
    45  				t.Error(err)
    46  				return
    47  			}
    48  
    49  			// unmarshal the yaml file to a map
    50  			got := map[string]interface{}{}
    51  			if err := yaml.Unmarshal(tmp2, &got); err != nil {
    52  				t.Error(err)
    53  				return
    54  			}
    55  
    56  			// parse the golden yaml file and unmarshal
    57  			data, err := ioutil.ReadFile(test + ".golden")
    58  			if err != nil {
    59  				t.Skipf("no golden file")
    60  				return
    61  			}
    62  
    63  			// unmarshal the golden yaml file
    64  			want := map[string]interface{}{}
    65  			if err := yaml.Unmarshal(data, &want); err != nil {
    66  				t.Error(err)
    67  				return
    68  			}
    69  
    70  			// compare the parsed yaml file to the golden yaml file
    71  			if diff := cmp.Diff(got, want); diff != "" {
    72  				t.Errorf("Unexpected parsing result")
    73  				t.Log(diff)
    74  				println(string(tmp2))
    75  			}
    76  		})
    77  	}
    78  }