github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/rules_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  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"gopkg.in/yaml.v3"
    22  )
    23  
    24  func TestChange(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Change
    28  	}{
    29  		{
    30  			yaml: `"Dockerfile"`,
    31  			want: Change{
    32  				Paths: []string{"Dockerfile"},
    33  			},
    34  		},
    35  		{
    36  			yaml: `["Dockerfile"]`,
    37  			want: Change{
    38  				Paths: []string{"Dockerfile"},
    39  			},
    40  		},
    41  		{
    42  			yaml: `{"paths": ["Dockerfile"] }`,
    43  			want: Change{
    44  				Paths: []string{"Dockerfile"},
    45  			},
    46  		},
    47  		{
    48  			yaml: `{"paths": ["Dockerfile"], "compare_to": "refs/heads/branch1" }`,
    49  			want: Change{
    50  				Paths:     []string{"Dockerfile"},
    51  				CompareTo: "refs/heads/branch1",
    52  			},
    53  		},
    54  	}
    55  
    56  	for i, test := range tests {
    57  		got := new(Change)
    58  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    59  			t.Error(err)
    60  			return
    61  		}
    62  		if diff := cmp.Diff(got, &test.want); diff != "" {
    63  			t.Errorf("Unexpected parsing results for test %v", i)
    64  			t.Log(diff)
    65  		}
    66  	}
    67  }
    68  
    69  func TestChange_Error(t *testing.T) {
    70  	err := yaml.Unmarshal([]byte(`[ { "foo": "bar" } ]`), new(Change))
    71  	if err == nil || err.Error() != "failed to unmarshal rules:changes" {
    72  		t.Errorf("Expect error, got %s", err)
    73  	}
    74  }