github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_coverity_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 TestCoverity(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Coverity
    28  	}{
    29  		// bool value
    30  		{
    31  			yaml: `true`,
    32  			want: Coverity{
    33  				Enabled: true,
    34  			},
    35  		},
    36  		// struct value
    37  		{
    38  			yaml: `{ enabled: true, notification_email: janecitizen@mail.com }`,
    39  			want: Coverity{
    40  				Enabled: true,
    41  				NotificationEmail: &Secure{
    42  					Decrypted: "janecitizen@mail.com",
    43  				},
    44  			},
    45  		},
    46  		// struct value
    47  		{
    48  			yaml: `{ enabled: true, notification_email: { secure: mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0 } }`,
    49  			want: Coverity{
    50  				Enabled: true,
    51  				NotificationEmail: &Secure{
    52  					Encrypted: "mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0",
    53  				},
    54  			},
    55  		},
    56  		// project string
    57  		{
    58  			yaml: `{ enabled: true, project: my_github/my_project }`,
    59  			want: Coverity{
    60  				Enabled: true,
    61  				Project: &CoverityProject{
    62  					Name: "my_github/my_project",
    63  				},
    64  			},
    65  		},
    66  		// project struct
    67  		{
    68  			yaml: `{ enabled: true, project: { name: my_github/my_project, version: 1.0, description: my project } }`,
    69  			want: Coverity{
    70  				Enabled: true,
    71  				Project: &CoverityProject{
    72  					Name:        "my_github/my_project",
    73  					Version:     "1.0",
    74  					Description: "my project",
    75  				},
    76  			},
    77  		},
    78  	}
    79  
    80  	for i, test := range tests {
    81  		got := new(Coverity)
    82  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    83  			t.Log(test.yaml)
    84  			t.Error(err)
    85  			return
    86  		}
    87  		if diff := cmp.Diff(got, &test.want); diff != "" {
    88  			t.Log(test.yaml)
    89  			t.Errorf("Unexpected parsing results for test %v", i)
    90  			t.Log(diff)
    91  		}
    92  	}
    93  }
    94  
    95  func TestCoverity_Error(t *testing.T) {
    96  	err := yaml.Unmarshal([]byte("[[]]"), new(Coverity))
    97  	if err == nil || err.Error() != "failed to unmarshal coverity_scan" {
    98  		t.Errorf("Expect error, got %s", err)
    99  	}
   100  }
   101  
   102  func TestCoveritProject_Error(t *testing.T) {
   103  	err := yaml.Unmarshal([]byte("[[]]"), new(CoverityProject))
   104  	if err == nil || err.Error() != "failed to unmarshal coverity project" {
   105  		t.Errorf("Expect error, got %s", err)
   106  	}
   107  }