From b5e053ca9253f95dc083bdcc128e37b0d25c9f47 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Thu, 25 Apr 2024 13:53:00 -0700 Subject: [PATCH] bug: expand interpreter args to multiple arguments --- pkg/engine/cmd.go | 6 ++++++ pkg/tests/runner_test.go | 16 ++++++++++++++++ pkg/tests/testdata/TestShlex/test.gpt | 2 ++ 3 files changed, 24 insertions(+) create mode 100644 pkg/tests/testdata/TestShlex/test.gpt diff --git a/pkg/engine/cmd.go b/pkg/engine/cmd.go index 65c249b4..c3fe9751 100644 --- a/pkg/engine/cmd.go +++ b/pkg/engine/cmd.go @@ -211,6 +211,12 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T cmdArgs = append(cmdArgs, f.Name()) } + // This is a bit hacky, but we want ARGS="x y" to expand to "x" and "y" not "x y" if used as arguments + cmdArgs, err = shlex.Split(strings.Join(cmdArgs, " ")) + if err != nil { + return nil, nil, err + } + // This is a workaround for Windows, where the command interpreter is constructed with unix style paths // It converts unix style paths to windows style paths if runtime.GOOS == "windows" { diff --git a/pkg/tests/runner_test.go b/pkg/tests/runner_test.go index 95f3db8d..199668a4 100644 --- a/pkg/tests/runner_test.go +++ b/pkg/tests/runner_test.go @@ -559,3 +559,19 @@ func TestExport(t *testing.T) { require.NoError(t, err) assert.Equal(t, "TEST RESULT CALL: 3", x) } + +func TestShlex(t *testing.T) { + runner := tester.NewRunner(t) + + runner.RespondWith(tester.Result{ + Func: types.CompletionFunctionCall{ + Name: "transient", + }, + }) + x, err := runner.Run("", `{ +"args":" x y z " +}`) + require.NoError(t, err) + // The fact the white space is all perfect means that the string didn't get passed as one arg but multiple + assert.Equal(t, "x y z\n", x) +} diff --git a/pkg/tests/testdata/TestShlex/test.gpt b/pkg/tests/testdata/TestShlex/test.gpt new file mode 100644 index 00000000..dc635e09 --- /dev/null +++ b/pkg/tests/testdata/TestShlex/test.gpt @@ -0,0 +1,2 @@ + +#!/usr/bin/env echo ${ARGS} \ No newline at end of file