github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/cache_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 TestCache(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Cache
    28  	}{
    29  		{
    30  			yaml: `true`,
    31  			want: Cache{
    32  				Timeout: 3,
    33  			},
    34  		},
    35  		{
    36  			yaml: `{}`,
    37  			want: Cache{
    38  				Timeout: 3,
    39  			},
    40  		},
    41  		{
    42  			yaml: `{timeout: 42, directories: [ /go, /node ]}`,
    43  			want: Cache{
    44  				Timeout:     42,
    45  				Directories: Stringorslice{"/go", "/node"},
    46  			},
    47  		},
    48  		{
    49  			yaml: `"apt"`,
    50  			want: Cache{
    51  				Timeout: 3,
    52  				Apt:     true,
    53  			},
    54  		},
    55  		{
    56  			yaml: `"bundler"`,
    57  			want: Cache{
    58  				Timeout: 3,
    59  				Bundler: true,
    60  			},
    61  		},
    62  		{
    63  			yaml: `"cargo"`,
    64  			want: Cache{
    65  				Timeout: 3,
    66  				Cargo:   true,
    67  			},
    68  		},
    69  		{
    70  			yaml: `"ccache"`,
    71  			want: Cache{
    72  				Timeout: 3,
    73  				Ccache:  true,
    74  			},
    75  		},
    76  		{
    77  			yaml: `"cocoapods"`,
    78  			want: Cache{
    79  				Timeout:   3,
    80  				Cocoapods: true,
    81  			},
    82  		},
    83  		{
    84  			yaml: `"npm"`,
    85  			want: Cache{
    86  				Timeout: 3,
    87  				Npm:     true,
    88  			},
    89  		},
    90  		{
    91  			yaml: `"packages"`,
    92  			want: Cache{
    93  				Timeout:  3,
    94  				Packages: true,
    95  			},
    96  		},
    97  		{
    98  			yaml: `"pip"`,
    99  			want: Cache{
   100  				Timeout: 3,
   101  				Pip:     true,
   102  			},
   103  		},
   104  		{
   105  			yaml: `"yarn"`,
   106  			want: Cache{
   107  				Timeout: 3,
   108  				Yarn:    true,
   109  			},
   110  		},
   111  		{
   112  			yaml: `"edge"`,
   113  			want: Cache{
   114  				Timeout: 3,
   115  				Edge:    true,
   116  			},
   117  		},
   118  	}
   119  
   120  	for i, test := range tests {
   121  		got := new(Cache)
   122  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
   123  			t.Error(err)
   124  			return
   125  		}
   126  		if diff := cmp.Diff(got, &test.want); diff != "" {
   127  			t.Errorf("Unexpected parsing results for test %v", i)
   128  			t.Log(diff)
   129  		}
   130  	}
   131  }
   132  
   133  func TestCache_Error(t *testing.T) {
   134  	err := yaml.Unmarshal([]byte("[[]]"), new(Cache))
   135  	if err == nil || err.Error() != "failed to unmarshal cache" {
   136  		t.Errorf("Expect error, got %s", err)
   137  	}
   138  }