github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/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  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  func TestPipeline(t *testing.T) {
    26  	tests := []string{
    27  		"testdata/addons/browserstack",
    28  		"testdata/addons/sonar",
    29  		"testdata/golang",
    30  		"testdata/rust",
    31  		"testdata/scala",
    32  		"testdata/smalltalk",
    33  	}
    34  
    35  	for _, test := range tests {
    36  		t.Run(test, func(t *testing.T) {
    37  			// parse the yaml file
    38  			tmp1, err := ParseFile(test + ".yaml")
    39  			if err != nil {
    40  				t.Error(err)
    41  				return
    42  			}
    43  
    44  			// marshal the yaml file
    45  			tmp2, err := yaml.Marshal(tmp1)
    46  			if err != nil {
    47  				t.Error(err)
    48  				return
    49  			}
    50  
    51  			// unmarshal the yaml file to a map
    52  			got := map[string]interface{}{}
    53  			if err := yaml.Unmarshal(tmp2, &got); err != nil {
    54  				t.Error(err)
    55  				return
    56  			}
    57  
    58  			// parse the golden json file and unmarshal
    59  			data, err := ioutil.ReadFile(test + ".json")
    60  			if err != nil {
    61  				t.Error(err)
    62  				return
    63  			}
    64  
    65  			// unmarshal the json file
    66  			want := map[string]interface{}{}
    67  			if err := yaml.Unmarshal(data, &want); err != nil {
    68  				t.Error(err)
    69  				return
    70  			}
    71  
    72  			// compare the parsed yaml to the golden file
    73  			if diff := cmp.Diff(got, want); diff != "" {
    74  				t.Errorf("Unexpected parsing result")
    75  				t.Log(diff)
    76  			}
    77  		})
    78  	}
    79  }