github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/harness/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  	"bytes"
    19  	"encoding/json"
    20  	"io/ioutil"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestParse(t *testing.T) {
    27  	// unmarshal the test pipeline.
    28  	got, err := ParseFile("testdata/pipeline.yaml")
    29  	if err != nil {
    30  		t.Error(err)
    31  		return
    32  	}
    33  
    34  	// unmarshal the expected json struct.
    35  	src, _ := ioutil.ReadFile("testdata/pipeline.golden.json")
    36  	want := new(Config)
    37  	if err := json.Unmarshal(src, want); err != nil {
    38  		t.Error(err)
    39  		return
    40  	}
    41  
    42  	// compare the test with the expected output.
    43  	// if they do not match, write the diff to the logs
    44  	// and fail the test.
    45  	if diff := cmp.Diff(got, want); len(diff) != 0 {
    46  		t.Errorf(diff)
    47  	}
    48  }
    49  
    50  // dump json data to the test logs.
    51  func debug(t *testing.T, v interface{}) {
    52  	buf := new(bytes.Buffer)
    53  	enc := json.NewEncoder(buf)
    54  	enc.SetIndent("", "  ")
    55  	enc.Encode(v)
    56  	t.Log(buf.String())
    57  }