Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: run_sh doesn't remove new lines from input #1642

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/kurtosis-tech/stacktrace"
"github.com/xtgo/uuid"
"go.starlark.net/starlark"
"strings"
)

const (
Expand Down Expand Up @@ -253,7 +252,6 @@ func getCommandToRun(builtin *RunShCapabilities) (string, error) {
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred while replacing runtime values in run_sh")
}
commandWithNoNewLines := strings.ReplaceAll(maybeSubCommandWithRuntimeValues, newlineChar, " ")
h4ck3rk3y marked this conversation as resolved.
Show resolved Hide resolved

return commandWithNoNewLines, nil
return maybeSubCommandWithRuntimeValues, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
runshTest = "run-sh-test"
runshStarlarkSimple = `
def run(plan):
result1 = plan.run_sh(run="echo kurtosis")
result1 = plan.run_sh(run="echo kurtosis | tr -d '\n'")
result2 = plan.run_sh(run="mkdir -p /src/{0} && cd /src/{0} && echo $(pwd)".format(result1.output))
plan.verify(result2.output, "==", "/src/kurtosis\n")
`
Expand Down Expand Up @@ -45,13 +45,23 @@ def run(plan):
files_artifacts = result1.files_artifacts
result2 = plan.run_sh(files={"/temp": files_artifacts[0]}, run="cat /temp/data/kurtosis.txt")
plan.verify(result2.output, "==", "kurtosis\n")
`
runShWithNewLineRemoval = `
def run(plan):
result = plan.run_sh(run="mkdir -p /src && echo kurtosis > /src/tech.txt", store=["/src/tech.txt"])
file_artifacts = result.files_artifacts
result2 = plan.run_sh(run="cat /temp/tech.txt", files={"/temp": file_artifacts[0]})
plan.verify(result2.output, "==", "kurtosis\n")
result2 = plan.run_sh(run="cat /temp/tech.txt | tr -d '\n'", files={"/temp": file_artifacts[0]})
plan.verify(result2.output, "==", "kurtosis")
`
)

func TestStarlark_RunshTaskSimple(t *testing.T) {
ctx := context.Background()
runResult, _ := test_helpers.SetupSimpleEnclaveAndRunScript(t, ctx, runshTest, runshStarlarkSimple)
expectedOutput := "Command returned with exit code '0' and the following output:\n--------------------\nkurtosis\n\n--------------------\nCommand returned with exit code '0' and the following output:\n--------------------\n/src/kurtosis\n\n--------------------\nVerification succeeded. Value is '\"/src/kurtosis\\n\"'.\n"
runResult, err := test_helpers.SetupSimpleEnclaveAndRunScript(t, ctx, runshTest, runshStarlarkSimple)
require.Nil(t, err)
expectedOutput := "Command returned with exit code '0' and the following output: kurtosis\nCommand returned with exit code '0' and the following output:\n--------------------\n/src/kurtosis\n\n--------------------\nVerification succeeded. Value is '\"/src/kurtosis\\n\"'.\n"
require.Equal(t, expectedOutput, string(runResult.RunOutput))
}

Expand Down Expand Up @@ -85,3 +95,9 @@ func TestStarlark_RunshFileArtifactWithoutParentDir(t *testing.T) {
expectedOutput := "Command returned with exit code '0' with no output\nCommand returned with exit code '0' and the following output:\n--------------------\nkurtosis\n\n--------------------\nVerification succeeded. Value is '\"kurtosis\\n\"'.\n"
require.Equal(t, expectedOutput, string(runResult.RunOutput))
}

func TestStarlark_RunShWithNewLineRemovalPipe(t *testing.T) {
ctx := context.Background()
_, err := test_helpers.SetupSimpleEnclaveAndRunScript(t, ctx, runshTest, runShWithNewLineRemoval)
require.Nil(t, err)
}