fix: fix args name in postrender/exec_test.go and error if order in postRendererArgsSlice

Signed-off-by: guofutan <guofutan@tencent.com>
pull/10514/head
guofutan 3 years ago
parent d12170b3f2
commit 04e79e936d

@ -117,7 +117,7 @@ func (o *outputValue) Set(s string) error {
func bindPostRenderFlag(cmd *cobra.Command, varRef *postrender.PostRenderer) { func bindPostRenderFlag(cmd *cobra.Command, varRef *postrender.PostRenderer) {
p := &postRendererOptions{varRef, "", []string{}} p := &postRendererOptions{varRef, "", []string{}}
cmd.Flags().Var(&postRendererString{p}, postRenderFlag, "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path") cmd.Flags().Var(&postRendererString{p}, postRenderFlag, "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
cmd.Flags().Var(&postRendererArgsSlice{p}, postRenderArgsFlag, "the args to an executable to be used for post rendering. (can specify multiple)") cmd.Flags().Var(&postRendererArgsSlice{p}, postRenderArgsFlag, "an argument to the post-renderer (can specify multiple)")
} }
type postRendererOptions struct { type postRendererOptions struct {
@ -164,10 +164,15 @@ func (p *postRendererArgsSlice) Type() string {
} }
func (p *postRendererArgsSlice) Set(val string) error { func (p *postRendererArgsSlice) Set(val string) error {
if val == "" || p.options.binaryPath == "" { if val == "" {
return nil return nil
} }
p.options.args = append(p.options.args, val) p.options.args = append(p.options.args, val)
if p.options.binaryPath == "" {
return nil
}
// overwrite if already create PostRenderer by `post-renderer` flags // overwrite if already create PostRenderer by `post-renderer` flags
pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...) pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...)
if err != nil { if err != nil {

@ -34,7 +34,7 @@ const testingScript = `#!/bin/sh
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
sed s/FOOTEST/BARTEST/g <&0 sed s/FOOTEST/BARTEST/g <&0
else else
sed s/FOOTEST/BARTEST$#/g <&0 sed s/FOOTEST/"$*"/g <&0
fi fi
` `
@ -137,12 +137,12 @@ func TestNewExecWithOneArgsRun(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t)
defer cleanup() defer cleanup()
renderer, err := NewExec(testpath, "FOOTEST") renderer, err := NewExec(testpath, "ARG1")
require.NoError(t, err) require.NoError(t, err)
output, err := renderer.Run(bytes.NewBufferString("FOOTEST")) output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err) is.NoError(err)
is.Contains(output.String(), "BARTEST1") is.Contains(output.String(), "ARG1")
} }
func TestNewExecWithTwoArgsRun(t *testing.T) { func TestNewExecWithTwoArgsRun(t *testing.T) {
@ -154,12 +154,12 @@ func TestNewExecWithTwoArgsRun(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t)
defer cleanup() defer cleanup()
renderer, err := NewExec(testpath, "FOOTEST", "FOOTEST") renderer, err := NewExec(testpath, "ARG1", "ARG2")
require.NoError(t, err) require.NoError(t, err)
output, err := renderer.Run(bytes.NewBufferString("FOOTEST")) output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err) is.NoError(err)
is.Contains(output.String(), "BARTEST2") is.Contains(output.String(), "ARG1 ARG2")
} }
func setupTestingScript(t *testing.T) (filepath string, cleanup func()) { func setupTestingScript(t *testing.T) (filepath string, cleanup func()) {

Loading…
Cancel
Save