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