github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/normalize_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  )
    22  
    23  func TestNormalize(t *testing.T) {
    24  	tests := []struct {
    25  		before []*Steps
    26  		after  []*Steps
    27  	}{
    28  		// group single step into stage
    29  		{
    30  			before: []*Steps{
    31  				{Step: &Step{Name: "test"}},
    32  			},
    33  			after: []*Steps{
    34  				{
    35  					Stage: &Stage{
    36  						Steps: []*Steps{
    37  							{Step: &Step{Name: "test"}},
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  		// group multiple steps into stage
    44  		{
    45  			before: []*Steps{
    46  				{Step: &Step{Name: "build"}},
    47  				{Step: &Step{Name: "test"}},
    48  			},
    49  			after: []*Steps{
    50  				{
    51  					Stage: &Stage{
    52  						Steps: []*Steps{
    53  							{Step: &Step{Name: "build"}},
    54  							{Step: &Step{Name: "test"}},
    55  						},
    56  					},
    57  				},
    58  			},
    59  		},
    60  		// no change when steps already grouped
    61  		// into top-level stages
    62  		{
    63  			before: []*Steps{
    64  				{Stage: &Stage{Name: "stage1"}},
    65  				{Stage: &Stage{Name: "stage2"}},
    66  			},
    67  			after: []*Steps{
    68  				{Stage: &Stage{Name: "stage1"}},
    69  				{Stage: &Stage{Name: "stage2"}},
    70  			},
    71  		},
    72  		// handle a mix of steps and stages
    73  		{
    74  			before: []*Steps{
    75  				{Step: &Step{Name: "step1"}},
    76  				{Stage: &Stage{Name: "stage1"}},
    77  				{Step: &Step{Name: "step2"}},
    78  				{Step: &Step{Name: "step3"}},
    79  				{Stage: &Stage{Name: "stage2"}},
    80  				{Step: &Step{Name: "step4"}},
    81  				{Step: &Step{Name: "step5"}},
    82  			},
    83  			after: []*Steps{
    84  				{
    85  					Stage: &Stage{
    86  						Steps: []*Steps{
    87  							{Step: &Step{Name: "step1"}},
    88  						},
    89  					},
    90  				},
    91  				{Stage: &Stage{Name: "stage1"}},
    92  				{
    93  					Stage: &Stage{
    94  						Steps: []*Steps{
    95  							{Step: &Step{Name: "step2"}},
    96  							{Step: &Step{Name: "step3"}},
    97  						},
    98  					},
    99  				},
   100  				{Stage: &Stage{Name: "stage2"}},
   101  				{
   102  					Stage: &Stage{
   103  						Steps: []*Steps{
   104  							{Step: &Step{Name: "step4"}},
   105  							{Step: &Step{Name: "step5"}},
   106  						},
   107  					},
   108  				},
   109  			},
   110  		},
   111  	}
   112  
   113  	for i, test := range tests {
   114  		got := new(Config)
   115  		got.Pipelines.Default = test.before
   116  
   117  		want := new(Config)
   118  		want.Pipelines.Default = test.after
   119  
   120  		Normalize(got)
   121  
   122  		if diff := cmp.Diff(got, want); diff != "" {
   123  			t.Errorf("Unexpected parsing results for test %v", i)
   124  			t.Log(diff)
   125  		}
   126  	}
   127  }