github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/concurrency_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 TestConcurrency(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Concurrency
    28  	}{
    29  		// string value
    30  		{
    31  			yaml: `staging_environment`,
    32  			want: Concurrency{
    33  				Group: "staging_environment",
    34  			},
    35  		},
    36  		// string value
    37  		{
    38  			yaml: `ci-${{ github.ref }}`,
    39  			want: Concurrency{
    40  				Group: "ci-${{ github.ref }}",
    41  			},
    42  		},
    43  		// struct value
    44  		{
    45  			yaml: `
    46  group: ${{ github.ref }}
    47  cancel-in-progress: true
    48  `,
    49  			want: Concurrency{
    50  				Group:            "${{ github.ref }}",
    51  				CancelInProgress: true,
    52  			},
    53  		},
    54  	}
    55  
    56  	for i, test := range tests {
    57  		got := new(Concurrency)
    58  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    59  			t.Log(test.yaml)
    60  			t.Error(err)
    61  			return
    62  		}
    63  		if diff := cmp.Diff(got, &test.want); diff != "" {
    64  			t.Log(test.yaml)
    65  			t.Errorf("Unexpected parsing results for test %v", i)
    66  			t.Log(diff)
    67  		}
    68  	}
    69  }
    70  
    71  func TestConcurrency_Error(t *testing.T) {
    72  	err := yaml.Unmarshal([]byte("[[]]"), new(Concurrency))
    73  	if err == nil || err.Error() != "failed to unmarshal concurrency" {
    74  		t.Errorf("Expect error, got %s", err)
    75  	}
    76  }