github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/convert_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 travis
    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 TestConvert(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  			// convert the yaml file from travis to harness
    36  			converter := New()
    37  			tmp1, err := converter.ConvertFile(test)
    38  			if err != nil {
    39  				t.Error(err)
    40  				return
    41  			}
    42  
    43  			// unmarshal the converted yaml file to a map
    44  			got := map[string]interface{}{}
    45  			if err := yaml.Unmarshal(tmp1, &got); err != nil {
    46  				t.Error(err)
    47  				return
    48  			}
    49  
    50  			// parse the golden yaml file
    51  			data, err := ioutil.ReadFile(test + ".golden")
    52  			if err != nil {
    53  				// skip tests with no golden files
    54  				// TODO all tests should have golden files
    55  				return
    56  			}
    57  
    58  			// unmarshal the golden yaml file to a map
    59  			want := map[string]interface{}{}
    60  			if err := yaml.Unmarshal(data, &want); err != nil {
    61  				t.Error(err)
    62  				return
    63  			}
    64  
    65  			// compare the converted yaml to the golden file
    66  			if diff := cmp.Diff(got, want); diff != "" {
    67  				t.Errorf("Unexpected conversion result")
    68  				t.Log(diff)
    69  			}
    70  		})
    71  	}
    72  }