github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/params_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 circle
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestReplaceParams(t *testing.T) {
    22  	tests := []struct {
    23  		before string
    24  		after  string
    25  	}{
    26  		// no params
    27  		{
    28  			before: "foo\nbar\nbaz\n",
    29  			after:  "foo\nbar\nbaz\n",
    30  		},
    31  		// open bracket only
    32  		{
    33  			before: "foo\nbar<<baz\n",
    34  			after:  "foo\nbar<<baz\n",
    35  		},
    36  		// close bracket only
    37  		{
    38  			before: "foo\nbar>>baz\n",
    39  			after:  "foo\nbar>>baz\n",
    40  		},
    41  		// open bracket before close bracket
    42  		{
    43  			before: "foo\n>>bar<<baz\n",
    44  			after:  "foo\n>>bar<<baz\n",
    45  		},
    46  		// unknown param
    47  		{
    48  			before: "foo\n<< foo.bar >>\nbaz\n",
    49  			after:  "foo\n<< foo.bar >>\nbaz\n",
    50  		},
    51  		{
    52  			before: "foo\n<< pipeline.id >>\nbaz\n",
    53  			after:  "foo\n<+pipeline.identifier>\nbaz\n",
    54  		},
    55  		// pipeline parameter
    56  		{
    57  			before: "foo\n<< parameters.message >>\nbaz\n",
    58  			after:  "foo\n<+inputs.message>\nbaz\n",
    59  		},
    60  		// job parameter
    61  		{
    62  			before: "foo\n<< pipeline.parameters.message >>\nbaz\n",
    63  			after:  "foo\n<+inputs.message>\nbaz\n",
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		t.Run(test.before, func(t *testing.T) {
    69  			out := replaceParams([]byte(test.before), params)
    70  			if got, want := string(out), test.after; got != want {
    71  				t.Errorf("Got replace params %q, want %q", got, want)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestExpandParams(t *testing.T) {
    78  	tests := []struct {
    79  		before string
    80  		after  string
    81  	}{
    82  		// no params
    83  		{
    84  			before: "foo\nbar\nbaz\n",
    85  			after:  "foo\nbar\nbaz\n",
    86  		},
    87  		// open bracket only
    88  		{
    89  			before: "foo\nbar<<baz\n",
    90  			after:  "foo\nbar<<baz\n",
    91  		},
    92  		// close bracket only
    93  		{
    94  			before: "foo\nbar>>baz\n",
    95  			after:  "foo\nbar>>baz\n",
    96  		},
    97  		// open bracket before close bracket
    98  		{
    99  			before: "foo\n>>bar<<baz\n",
   100  			after:  "foo\n>>bar<<baz\n",
   101  		},
   102  		// unknown param
   103  		{
   104  			before: "foo\n<< foo.bar >>\nbaz\n",
   105  			after:  "foo\n<< foo.bar >>\nbaz\n",
   106  		},
   107  		// known parameter
   108  		{
   109  			before: "foo\n<< pipeline.branch >>\nbaz\n",
   110  			after:  "foo\nmain\nbaz\n",
   111  		},
   112  	}
   113  
   114  	for _, test := range tests {
   115  		t.Run(test.before, func(t *testing.T) {
   116  			out := expandParams([]byte(test.before), map[string]string{"pipeline.branch": "main"})
   117  			if got, want := string(out), test.after; got != want {
   118  				t.Errorf("Got expanded param %q, want %q", got, want)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func TestExractParam(t *testing.T) {
   125  	tests := []struct {
   126  		before string
   127  		after  string
   128  	}{
   129  		// no params
   130  		{
   131  			before: "foo\nbar\nbaz\n",
   132  			after:  "",
   133  		},
   134  		// open bracket only
   135  		{
   136  			before: "foo\nbar<<baz\n",
   137  			after:  "",
   138  		},
   139  		// close bracket only
   140  		{
   141  			before: "foo\nbar>>baz\n",
   142  			after:  "",
   143  		},
   144  		// open bracket before close bracket
   145  		{
   146  			before: "foo\n>>bar<<baz\n",
   147  			after:  "",
   148  		},
   149  		// known parameter
   150  		{
   151  			before: "foo\n<< pipeline.id >>\nbaz\n",
   152  			after:  "pipeline.id",
   153  		},
   154  	}
   155  
   156  	for _, test := range tests {
   157  		t.Run(test.before, func(t *testing.T) {
   158  			out := extractParam(test.before)
   159  			if got, want := out, test.after; got != want {
   160  				t.Errorf("Extracted param %q, wanted %q", got, want)
   161  			}
   162  		})
   163  	}
   164  }