-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make export --format respect "/" in template actions
During tk export path separators are temporarily replaced with BEL in order to protect paths from being mixed with template output. But when there is a slash in a template action (e.g. {{index .metadata.labels "app.kubernetes.io/name"}}, that slash also got replaced, and the template did not work as expected. This fix ensures that the BEL replacement only occurs within the text portions of the template, and the action blocks are being preserved. Fixes #568
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"go.formatTool": "gofmt" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tanka | ||
|
||
import "testing" | ||
|
||
func Test_replaceTmplText(t *testing.T) { | ||
type args struct { | ||
s string | ||
old string | ||
new string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"text only", args{"a", "a", "b"}, "b"}, | ||
{"action blocks", args{"{{a}}{{.}}", "a", "b"}, "{{a}}{{.}}"}, | ||
{"mixed", args{"a{{a}}a{{a}}a", "a", "b"}, "b{{a}}b{{a}}b"}, | ||
{"invalid template format handled as text", args{"a}}a{{a", "a", "b"}, "b}}b{{b"}, | ||
{ | ||
name: "keep path separator in action block", | ||
args: args{`{{index .metadata.labels "app.kubernetes.io/name"}}/{{.metadata.name}}`, "/", BelRune}, | ||
want: "{{index .metadata.labels \"app.kubernetes.io/name\"}}\u0007{{.metadata.name}}", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := replaceTmplText(tt.args.s, tt.args.old, tt.args.new); got != tt.want { | ||
t.Errorf("replaceInTmplText() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |