github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/import_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 TestImports(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Imports
    28  	}{
    29  		// test string value
    30  		{
    31  			yaml: `"./import.yml@v1"`,
    32  			want: Imports{
    33  				Items: []*Import{
    34  					{Source: "./import.yml@v1"},
    35  				},
    36  			},
    37  		},
    38  		// test string slice value
    39  		{
    40  			yaml: `[ "./import.yml@v1" ]`,
    41  			want: Imports{
    42  				Items: []*Import{
    43  					{Source: "./import.yml@v1"},
    44  				},
    45  			},
    46  		},
    47  		// test map value
    48  		{
    49  			yaml: `{ source: "./import.yml@v1", mode: "merge", "if": "branch = master" }`,
    50  			want: Imports{
    51  				Items: []*Import{
    52  					{Source: "./import.yml@v1", Mode: "merge", If: "branch = master"},
    53  				},
    54  			},
    55  		},
    56  		// test map slice value
    57  		{
    58  			yaml: `[{ source: "./import.yml@v1", mode: "merge", "if": "branch = master" }]`,
    59  			want: Imports{
    60  				Items: []*Import{
    61  					{Source: "./import.yml@v1", Mode: "merge", If: "branch = master"},
    62  				},
    63  			},
    64  		},
    65  	}
    66  
    67  	for i, test := range tests {
    68  		got := new(Imports)
    69  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    70  			t.Log(test.yaml)
    71  			t.Error(err)
    72  			return
    73  		}
    74  		if diff := cmp.Diff(got, &test.want); diff != "" {
    75  			t.Log(test.yaml)
    76  			t.Errorf("Unexpected parsing results for test %v", i)
    77  			t.Log(diff)
    78  		}
    79  	}
    80  }
    81  
    82  func TestImport_Error(t *testing.T) {
    83  	err := yaml.Unmarshal([]byte("[[]]"), new(Import))
    84  	if err == nil || err.Error() != "failed to unmarshal import" {
    85  		t.Errorf("Expect error, got %s", err)
    86  	}
    87  }
    88  
    89  func TestImports_Error(t *testing.T) {
    90  	err := yaml.Unmarshal([]byte("[[]]"), new(Imports))
    91  	if err == nil || err.Error() != "failed to unmarshal imports" {
    92  		t.Errorf("Expect error, got %s", err)
    93  	}
    94  }