github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/trigger_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  // child1:
    25  //   trigger:
    26  //     include: .child-pipeline.yml
    27  
    28  // child2:
    29  //   trigger:
    30  //     include: .child-pipeline.yml
    31  //     forward:
    32  //       pipeline_variables: true
    33  
    34  // child3:
    35  //   trigger:
    36  //     include: .child-pipeline.yml
    37  //     forward:
    38  //       yaml_variables: false
    39  
    40  func TestTrigger(t *testing.T) {
    41  	tests := []struct {
    42  		yaml string
    43  		want Trigger
    44  	}{
    45  		{
    46  			yaml: `"my-group/my-project"`,
    47  			want: Trigger{
    48  				Project: "my-group/my-project",
    49  			},
    50  		},
    51  		{
    52  			yaml: `{ "include": ".child-pipeline.yml" }`,
    53  			want: Trigger{
    54  				Include: ".child-pipeline.yml",
    55  			},
    56  		},
    57  		{
    58  			yaml: `{ "include": ".child-pipeline.yml", "forward": { "pipeline_variables": true } }`,
    59  			want: Trigger{
    60  				Include: ".child-pipeline.yml",
    61  				Forward: &Forward{
    62  					PipelineVariables: true,
    63  				},
    64  			},
    65  		},
    66  	}
    67  
    68  	for i, test := range tests {
    69  		got := new(Trigger)
    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 TestTrigger_Error(t *testing.T) {
    82  	err := yaml.Unmarshal([]byte("[]"), new(Trigger))
    83  	if err == nil || err.Error() != "failed to unmarshal trigger" {
    84  		t.Errorf("Expect error, got %s", err)
    85  	}
    86  }