github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/variable_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 TestVariable(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Variable
    28  	}{
    29  		{
    30  			yaml: `"https://example.com/"`,
    31  			want: Variable{
    32  				Value:  "https://example.com/",
    33  				Expand: toPointerBool(true),
    34  			},
    35  		},
    36  		{
    37  			yaml: `{ "description": "The deployment note." }`,
    38  			want: Variable{
    39  				Desc:   "The deployment note.",
    40  				Expand: toPointerBool(true),
    41  			},
    42  		},
    43  		{
    44  			yaml: `{ "description": "The deployment target.", "value": "staging" }`,
    45  			want: Variable{
    46  				Value:  "staging",
    47  				Desc:   "The deployment target.",
    48  				Expand: toPointerBool(true),
    49  			},
    50  		},
    51  		{
    52  			yaml: `{ "options": [ "production", "staging", "development" ], "value": "staging" }`,
    53  			want: Variable{
    54  				Value:   "staging",
    55  				Options: []string{"production", "staging", "development"},
    56  				Expand:  toPointerBool(true),
    57  			},
    58  		},
    59  		{
    60  			yaml: `{ "value": "value3 $VAR1", "expand": false }`,
    61  			want: Variable{
    62  				Value:  "value3 $VAR1",
    63  				Expand: toPointerBool(false),
    64  			},
    65  		},
    66  	}
    67  
    68  	for i, test := range tests {
    69  		got := new(Variable)
    70  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    71  			t.Error(err)
    72  			return
    73  		}
    74  		if diff := cmp.Diff(got, &test.want); diff != "" {
    75  			t.Errorf("Unexpected parsing results for test %v", i)
    76  			t.Log(diff)
    77  		}
    78  	}
    79  }
    80  
    81  func TestVariable_Error(t *testing.T) {
    82  	err := yaml.Unmarshal([]byte("[]"), new(Variable))
    83  	if err == nil || err.Error() != "failed to unmarshal variable" {
    84  		t.Errorf("Expect error, got %s", err)
    85  	}
    86  }
    87  
    88  func toPointerBool(b bool) *bool {
    89  	return &b
    90  }
    91  
    92  // variables:
    93  //   DEPLOY_NOTE:
    94  //     description: "The deployment note. Explain the reason for this deployment."
    95  
    96  // variables:
    97  //   DEPLOY_ENVIRONMENT:
    98  //     value: "staging"
    99  //     description: "The deployment target. Change this variable to 'canary' or 'production' if needed."
   100  
   101  // variables:
   102  //   DEPLOY_ENVIRONMENT:
   103  //     value: "staging"
   104  //     options:
   105  //       - "production"
   106  //       - "staging"
   107  //       - "canary"
   108  //     description: "The deployment target. Set to 'staging' by default."
   109  
   110  // variables:
   111  //   VAR1: value1
   112  //   VAR2: value2 $VAR1
   113  //   VAR3:
   114  //     value: value3 $VAR1
   115  //     expand: false