github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/script_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  const mockPipe = `name: Send alert to Opsgenie
    25  pipe: atlassian/opsgenie-send-alert:latest
    26  variables:
    27      GENIE_KEY: $GENIE_KEY
    28      MESSAGE: Wake up!
    29  `
    30  
    31  func TestScript(t *testing.T) {
    32  	tests := []struct {
    33  		yaml string
    34  		want Script
    35  	}{
    36  		{
    37  			yaml: `"echo hello world"`,
    38  			want: Script{
    39  				Text: "echo hello world",
    40  			},
    41  		},
    42  		{
    43  			yaml: mockPipe,
    44  			want: Script{
    45  				Pipe: &Pipe{
    46  					Image: "atlassian/opsgenie-send-alert:latest",
    47  					Name:  "Send alert to Opsgenie",
    48  					Variables: map[string]string{
    49  						"GENIE_KEY": "$GENIE_KEY",
    50  						"MESSAGE":   "Wake up!",
    51  					},
    52  				},
    53  			},
    54  		},
    55  	}
    56  
    57  	for i, test := range tests {
    58  		got := new(Script)
    59  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    60  			t.Log(test.yaml)
    61  			t.Error(err)
    62  			return
    63  		}
    64  		if diff := cmp.Diff(got, &test.want); diff != "" {
    65  			t.Log(test.yaml)
    66  			t.Errorf("Unexpected parsing results for test %v", i)
    67  			t.Log(diff)
    68  		}
    69  	}
    70  }
    71  
    72  func TestScript_Error(t *testing.T) {
    73  	err := yaml.Unmarshal([]byte("[[]]"), new(Script))
    74  	if err == nil || err.Error() != "failed to unmarshal script" {
    75  		t.Errorf("Expect error, got %s", err)
    76  	}
    77  }
    78  
    79  func TestScript_Marshal(t *testing.T) {
    80  	tests := []struct {
    81  		before Script
    82  		after  string
    83  	}{
    84  		{
    85  			before: Script{Text: "echo hello world"},
    86  			after:  "echo hello world\n",
    87  		},
    88  		{
    89  			before: Script{
    90  				Pipe: &Pipe{
    91  					Image: "atlassian/opsgenie-send-alert:latest",
    92  					Name:  "Send alert to Opsgenie",
    93  					Variables: map[string]string{
    94  						"GENIE_KEY": "$GENIE_KEY",
    95  						"MESSAGE":   "Wake up!",
    96  					},
    97  				},
    98  			},
    99  			after: mockPipe,
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		after, err := yaml.Marshal(&test.before)
   105  		if err != nil {
   106  			t.Error(err)
   107  			return
   108  		}
   109  		if got, want := string(after), test.after; got != want {
   110  			t.Errorf("want yaml %q, got %q", want, got)
   111  		}
   112  	}
   113  }