github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/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  	"gopkg.in/yaml.v3"
    21  )
    22  
    23  func TestCache_Error(t *testing.T) {
    24  	err := yaml.Unmarshal([]byte("[[]]"), new(Cache))
    25  	if err == nil || err.Error() != "failed to unmarshal cache" {
    26  		t.Errorf("Expect error, got %s", err)
    27  	}
    28  }
    29  
    30  func TestCache_Marshal(t *testing.T) {
    31  	tests := []struct {
    32  		before Cache
    33  		after  string
    34  	}{
    35  		{
    36  			before: Cache{
    37  				Path: "node_modules",
    38  			},
    39  			after: "node_modules\n",
    40  		},
    41  		{
    42  			before: Cache{
    43  				Path: "node_modules",
    44  				Key: &CacheKey{
    45  					Files: []string{"package.json"},
    46  				},
    47  			},
    48  			after: "key:\n    files:\n        - package.json\npath: node_modules\n",
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		after, err := yaml.Marshal(&test.before)
    54  		if err != nil {
    55  			t.Error(err)
    56  			return
    57  		}
    58  		if got, want := string(after), test.after; got != want {
    59  			t.Errorf("want yaml %q, got %q", want, got)
    60  		}
    61  	}
    62  }