github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/pipeline_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"gopkg.in/yaml.v3"
    10  )
    11  
    12  func TestPipelineYaml(t *testing.T) {
    13  	tests, err := filepath.Glob("testdata/job_keywords/*/*.yaml")
    14  	if err != nil {
    15  		t.Error(err)
    16  		return
    17  	}
    18  
    19  	for _, test := range tests {
    20  		t.Run(test, func(t *testing.T) {
    21  			// parse the yaml file
    22  			tmp1, err := ParseFile(test)
    23  			if err != nil {
    24  				t.Error(err)
    25  				return
    26  			}
    27  
    28  			// marshal the yaml file
    29  			tmp2, err := yaml.Marshal(tmp1)
    30  			if err != nil {
    31  				t.Error(err)
    32  				return
    33  			}
    34  
    35  			// unmarshal the yaml file to a map
    36  			got := map[string]interface{}{}
    37  			if err := yaml.Unmarshal(tmp2, &got); err != nil {
    38  				t.Error(err)
    39  				return
    40  			}
    41  
    42  			// parse the golden yaml file and unmarshal
    43  			data, err := ioutil.ReadFile(test + ".golden")
    44  			if err != nil {
    45  				// skip tests with no golden files
    46  				// TODO these should be re-enabled
    47  				return
    48  			}
    49  
    50  			// unmarshal the golden yaml file
    51  			want := map[string]interface{}{}
    52  			if err := yaml.Unmarshal(data, &want); err != nil {
    53  				t.Error(err)
    54  				return
    55  			}
    56  
    57  			// compare the parsed yaml to the golden file
    58  			if diff := cmp.Diff(got, want); diff != "" {
    59  				t.Errorf("Unexpected parsing result")
    60  				t.Log(diff)
    61  			}
    62  		})
    63  	}
    64  }