Support arguments in post render

Signed-off-by: Jan-Otto Kröpke <joe@adorsys.de>
pull/9047/head
Jan-Otto Kröpke 5 years ago
parent d7ba5710c6
commit 3a9bbd7c76
No known key found for this signature in database
GPG Key ID: 97D8606EB9E7A864

@ -21,8 +21,8 @@ import (
"io" "io"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/mattn/go-shellwords"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -36,7 +36,15 @@ type execRender struct {
// contain any separators, it will search in $PATH, otherwise it will resolve // contain any separators, it will search in $PATH, otherwise it will resolve
// any relative paths to a fully qualified path // any relative paths to a fully qualified path
func NewExec(command string) (PostRenderer, error) { func NewExec(command string) (PostRenderer, error) {
commandList := strings.Split(command, " ") parser := shellwords.NewParser()
parser.ParseEnv = false
parser.ParseBacktick = false
commandList, err := parser.Parse(command)
if err != nil {
return nil, err
}
binaryPath, args := commandList[0], commandList[1:] binaryPath, args := commandList[0], commandList[1:]
fullPath, err := getFullPath(binaryPath) fullPath, err := getFullPath(binaryPath)

@ -22,6 +22,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -37,7 +38,7 @@ sed s/FOOTEST/${1:-BARTEST}/g <&0
func TestGetFullPath(t *testing.T) { func TestGetFullPath(t *testing.T) {
is := assert.New(t) is := assert.New(t)
t.Run("full path resolves correctly", func(t *testing.T) { t.Run("full path resolves correctly", func(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t, "post-render-test.sh")
defer cleanup() defer cleanup()
fullPath, err := getFullPath(testpath) fullPath, err := getFullPath(testpath)
@ -46,7 +47,7 @@ func TestGetFullPath(t *testing.T) {
}) })
t.Run("relative path resolves correctly", func(t *testing.T) { t.Run("relative path resolves correctly", func(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t, "post-render-test.sh")
defer cleanup() defer cleanup()
currentDir, err := os.Getwd() currentDir, err := os.Getwd()
@ -59,7 +60,7 @@ func TestGetFullPath(t *testing.T) {
}) })
t.Run("binary in PATH resolves correctly", func(t *testing.T) { t.Run("binary in PATH resolves correctly", func(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t, "post-render-test.sh")
defer cleanup() defer cleanup()
realPath := os.Getenv("PATH") realPath := os.Getenv("PATH")
@ -113,7 +114,7 @@ func TestExecRun(t *testing.T) {
t.Skip("skipping on windows") t.Skip("skipping on windows")
} }
is := assert.New(t) is := assert.New(t)
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t, "post-render-test.sh")
defer cleanup() defer cleanup()
renderer, err := NewExec(testpath) renderer, err := NewExec(testpath)
@ -130,7 +131,7 @@ func TestExecRunWithArgs(t *testing.T) {
t.Skip("skipping on windows") t.Skip("skipping on windows")
} }
is := assert.New(t) is := assert.New(t)
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t, "post-render-test.sh")
defer cleanup() defer cleanup()
renderer, err := NewExec(testpath + " ARGUMENT") renderer, err := NewExec(testpath + " ARGUMENT")
@ -141,12 +142,47 @@ func TestExecRunWithArgs(t *testing.T) {
is.Contains(output.String(), "ARGUMENT") is.Contains(output.String(), "ARGUMENT")
} }
func setupTestingScript(t *testing.T) (filepath string, cleanup func()) { func TestExecRunWithArgsAndSpaceInCommandUsingBackslash(t *testing.T) {
if runtime.GOOS == "windows" {
// the actual Run test uses a basic sed example, so skip this test on windows
t.Skip("skipping on windows")
}
is := assert.New(t)
testpath, cleanup := setupTestingScript(t, "post render test.sh")
testpathEscaped := strings.ReplaceAll(testpath, " ", "\\ ")
defer cleanup()
renderer, err := NewExec(testpathEscaped + " ARGUMENT")
require.NoError(t, err)
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err)
is.Contains(output.String(), "ARGUMENT")
}
func TestExecRunWithArgsAndSpaceInCommandUsingQuotes(t *testing.T) {
if runtime.GOOS == "windows" {
// the actual Run test uses a basic sed example, so skip this test on windows
t.Skip("skipping on windows")
}
is := assert.New(t)
testpath, cleanup := setupTestingScript(t, "post render test.sh")
defer cleanup()
renderer, err := NewExec("'" + testpath + "' ARGUMENT")
require.NoError(t, err)
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err)
is.Contains(output.String(), "ARGUMENT")
}
func setupTestingScript(t *testing.T, fileName string) (filepath string, cleanup func()) {
t.Helper() t.Helper()
tempdir := ensure.TempDir(t) tempdir := ensure.TempDir(t)
f, err := ioutil.TempFile(tempdir, "post-render-test.sh") f, err := ioutil.TempFile(tempdir, fileName)
if err != nil { if err != nil {
t.Fatalf("unable to create tempfile for testing: %s", err) t.Fatalf("unable to create tempfile for testing: %s", err)
} }

Loading…
Cancel
Save