github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_homebrew_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 TestHomebrew(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Homebrew
    28  	}{
    29  		// string value
    30  		{
    31  			yaml: `true`,
    32  			want: Homebrew{
    33  				Update: true,
    34  			},
    35  		},
    36  		// string value
    37  		{
    38  			yaml: `"beanstalk"`,
    39  			want: Homebrew{
    40  				Update:   true,
    41  				Packages: []string{"beanstalk"},
    42  			},
    43  		},
    44  		// string slice value
    45  		{
    46  			yaml: `["beanstalk"]`,
    47  			want: Homebrew{
    48  				Update:   true,
    49  				Packages: []string{"beanstalk"},
    50  			},
    51  		},
    52  		// struct value
    53  		{
    54  			yaml: `{packages: ["beanstalk"], taps: homebrew/cask-versions, casks: java8 }`,
    55  			want: Homebrew{
    56  				Update:   false,
    57  				Packages: []string{"beanstalk"},
    58  				Taps:     []string{"homebrew/cask-versions"},
    59  				Casks:    []string{"java8"},
    60  			},
    61  		},
    62  		// struct value with brewfile bool
    63  		{
    64  			yaml: `{packages: ["beanstalk"], brewfile: true }`,
    65  			want: Homebrew{
    66  				Update:   false,
    67  				Packages: []string{"beanstalk"},
    68  				Brewfile: "Brewfile",
    69  			},
    70  		},
    71  		// struct value with brewfile path
    72  		{
    73  			yaml: `{packages: ["beanstalk"], brewfile: Brewfile.travis }`,
    74  			want: Homebrew{
    75  				Update:   false,
    76  				Packages: []string{"beanstalk"},
    77  				Brewfile: "Brewfile.travis",
    78  			},
    79  		},
    80  	}
    81  
    82  	for i, test := range tests {
    83  		got := new(Homebrew)
    84  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    85  			t.Log(test.yaml)
    86  			t.Error(err)
    87  			return
    88  		}
    89  		if diff := cmp.Diff(got, &test.want); diff != "" {
    90  			t.Log(test.yaml)
    91  			t.Errorf("Unexpected parsing results for test %v", i)
    92  			t.Log(diff)
    93  		}
    94  	}
    95  }
    96  
    97  func TestHomebrew_Error(t *testing.T) {
    98  	err := yaml.Unmarshal([]byte("[[]]"), new(Homebrew))
    99  	if err == nil || err.Error() != "failed to unmarshal homebrew" {
   100  		t.Errorf("Expect error, got %s", err)
   101  	}
   102  }