Skip to content

Commit

Permalink
fix: allow for single quotes in run_python (#2172)
Browse files Browse the repository at this point in the history
Closes #2171
  • Loading branch information
h4ck3rk3y authored Feb 16, 2024
1 parent 26ad571 commit 4048368
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ func getPythonCommandToRun(builtin *RunPythonCapabilities) (string, error) {
maybePythonArgumentsWithRuntimeValueReplaced = append(maybePythonArgumentsWithRuntimeValueReplaced, maybePythonArgumentWithRuntimeValueReplaced)
}
argumentsAsString := strings.Join(maybePythonArgumentsWithRuntimeValueReplaced, spaceDelimiter)

runEscaped := strings.ReplaceAll(builtin.run, `"`, `\"`)
if len(argumentsAsString) > 0 {
return fmt.Sprintf("python -c '%s' %s", builtin.run, argumentsAsString), nil
return fmt.Sprintf(`python -c "%s" %s`, runEscaped, argumentsAsString), nil
}
return fmt.Sprintf("python -c '%s'", builtin.run), nil
return fmt.Sprintf(`python -c "%s"`, runEscaped), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ print(sys.argv[1])
packages = ["requests"],
args = ["Kurtosis"]
)
`
runPythonWithSingleQuotesTest = "run-python-single-quote"
runPythonWithSingleQuotesScript = `
def run(plan):
python_script = """
print('kurtosis')
"""
plan.run_python(
run = python_script,
)
`
)

Expand All @@ -54,3 +65,11 @@ func TestStarlark_RunPythonWithExternalPacakges(t *testing.T) {
expectedOutput := "Command returned with exit code '0' and the following output:\n--------------------\n200\nKurtosis\n\n--------------------\n"
require.Equal(t, expectedOutput, string(runResult.RunOutput))
}

func TestStarlark_RunPythonWithSingleQuotes(t *testing.T) {
ctx := context.Background()
runResult, err := test_helpers.SetupSimpleEnclaveAndRunScript(t, ctx, runPythonWithSingleQuotesTest, runPythonWithSingleQuotesScript)
require.Nil(t, err)
expectedOutput := "Command returned with exit code '0' and the following output:\n--------------------\nkurtosis\n\n--------------------\n"
require.Equal(t, expectedOutput, string(runResult.RunOutput))
}

0 comments on commit 4048368

Please sign in to comment.