Skip to content

Commit

Permalink
Fix the issue with empty array replacement
Browse files Browse the repository at this point in the history
Prior to this change, if there is only one element in an array that is
a reference to an empty array, the original array becomes nil after
replacement, but it should be an empty array instead of nil.

Fixes tektoncd#5149

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Sep 6, 2022
1 parent 6a0575d commit 4b78091
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (arrayOrString *ArrayOrString) ApplyReplacements(stringReplacements map[str
if arrayOrString.Type == ParamTypeString {
arrayOrString.StringVal = substitution.ApplyReplacements(arrayOrString.StringVal, stringReplacements)
} else {
var newArrayVal []string
newArrayVal := []string{}
for _, v := range arrayOrString.ArrayVal {
newArrayVal = append(newArrayVal, substitution.ApplyArrayReplacements(v, stringReplacements, arrayReplacements)...)
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/apis/pipeline/v1beta1/param_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,18 @@ func TestArrayOrString_ApplyReplacements(t *testing.T) {
},
expectedOutput: v1beta1.NewArrayOrString("firstvalue", "array", "value", "lastvalue", "asdf", "sdfsd"),
}, {
name: "empty array replacement",
name: "empty array replacement without extra elements",
args: args{
// input: v1beta1.NewArrayOrString("$(arraykey)"),
input: &v1beta1.ArrayOrString{
Type: v1beta1.ParamTypeArray,
ArrayVal: []string{"$(arraykey)"},
},
arrayReplacements: map[string][]string{"arraykey": {}},
},
expectedOutput: &v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{}},
}, {
name: "empty array replacement with extra elements",
args: args{
input: v1beta1.NewArrayOrString("firstvalue", "$(arraykey)", "lastvalue"),
stringReplacements: map[string]string{"some": "value", "anotherkey": "value"},
Expand Down

0 comments on commit 4b78091

Please sign in to comment.