github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/perms_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 TestPerms(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Permissions
    28  	}{
    29  		{
    30  			yaml: `{}`,
    31  			want: Permissions{},
    32  		},
    33  		{
    34  			yaml: `{ actions: read }`,
    35  			want: Permissions{
    36  				Actions: "read",
    37  			},
    38  		},
    39  		{
    40  			yaml: `read-all`,
    41  			want: Permissions{
    42  				ReadAll: true,
    43  			},
    44  		},
    45  		{
    46  			yaml: `write-all`,
    47  			want: Permissions{
    48  				WriteAll: true,
    49  			},
    50  		},
    51  	}
    52  
    53  	for i, test := range tests {
    54  		got := new(Permissions)
    55  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    56  			t.Log(test.yaml)
    57  			t.Error(err)
    58  			return
    59  		}
    60  		if diff := cmp.Diff(got, &test.want); diff != "" {
    61  			t.Log(test.yaml)
    62  			t.Errorf("Unexpected parsing results for test %v", i)
    63  			t.Log(diff)
    64  		}
    65  	}
    66  }
    67  
    68  func TestPerms_Marshal(t *testing.T) {
    69  	tests := []struct {
    70  		before Permissions
    71  		after  string
    72  	}{
    73  		{
    74  			before: Permissions{ReadAll: true},
    75  			after:  "read-all\n",
    76  		},
    77  		{
    78  			before: Permissions{WriteAll: true},
    79  			after:  "write-all\n",
    80  		},
    81  		{
    82  			before: Permissions{Actions: "write"},
    83  			after:  "actions: write\n",
    84  		},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		after, err := yaml.Marshal(&test.before)
    89  		if err != nil {
    90  			t.Error(err)
    91  			return
    92  		}
    93  		if got, want := string(after), test.after; got != want {
    94  			t.Errorf("want yaml %q, got %q", want, got)
    95  		}
    96  	}
    97  }
    98  
    99  func TestPerms_Error(t *testing.T) {
   100  	err := yaml.Unmarshal([]byte("[[]]"), new(Permissions))
   101  	if err == nil || err.Error() != "failed to unmarshal permissions" {
   102  		t.Errorf("Expect error, got %s", err)
   103  	}
   104  }