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
  • Loading branch information
chuangw6 authored and jtcheng committed Sep 2, 2022
1 parent 43570c1 commit 3a83640
Show file tree
Hide file tree
Showing 2 changed files with 9 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
9 changes: 8 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,14 @@ 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)"),
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 3a83640

Please sign in to comment.