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: Properly handle paths with spaces in generated command #749

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ func (e *Engine) runBin() error {
case <-killCh:
return
default:
command := strings.Join(append([]string{e.config.Build.Bin}, e.runArgs...), " ")
formattedBin := formatPath(e.config.Build.Bin)
command := strings.Join(append([]string{formattedBin}, e.runArgs...), " ")
cmd, stdout, stderr, err := e.startCmd(command)
if err != nil {
e.mainLog("failed to start %s, error: %s", e.config.rel(e.config.binPath()), err.Error())
Expand Down
14 changes: 14 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,17 @@

return filepath.Join(root, path)
}

func formatPath(path string) string {
if !filepath.IsAbs(path) || !strings.Contains(path, " ") {
return path
}

quotedPath := fmt.Sprintf(`"%s"`, path)

if runtime.GOOS == PlatformWindows {
return fmt.Sprintf(`& %s`, quotedPath)
}

Check warning on line 442 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L441-L442

Added lines #L441 - L442 were not covered by tests

return quotedPath
}
84 changes: 84 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,87 @@ func TestJoinPathAbsolute(t *testing.T) {

assert.Equal(t, result, path)
}

func TestFormatPath(t *testing.T) {
type testCase struct {
name string
path string
expected string
}

runTests := func(t *testing.T, tests []testCase) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := formatPath(tt.path)
if result != tt.expected {
t.Errorf("formatPath(%q) = %q, want %q", tt.path, result, tt.expected)
}
})
}
}

t.Run("PathPlatformSpecific", func(t *testing.T) {
if runtime.GOOS == PlatformWindows {
// Windows-specific tests
tests := []testCase{
{
name: "Windows style absolute path with spaces",
path: `C:\My Documents\My Project\tmp\app.exe`,
expected: `& "C:\My Documents\My Project\tmp\app.exe"`,
},
{
name: "Windows style relative path with spaces",
path: `My Project\tmp\app.exe`,
expected: `My Project\tmp\app.exe`,
},
{
name: "Windows style absolute path without spaces",
path: `C:\Documents\Project\tmp\app.exe`,
expected: `C:\Documents\Project\tmp\app.exe`,
},
}
runTests(t, tests)
} else {
// Unix-specific tests
tests := []testCase{
{
name: "Unix style absolute path with spaces",
path: `/usr/local/my project/tmp/main`,
expected: `"/usr/local/my project/tmp/main"`,
},
{
name: "Unix style relative path with spaces",
path: "./my project/tmp/main",
expected: "./my project/tmp/main",
},
{
name: "Unix style absolute path without spaces",
path: `/usr/local/project/tmp/main`,
expected: `/usr/local/project/tmp/main`,
},
}
runTests(t, tests)
}
})

t.Run("CommonCases", func(t *testing.T) {
tests := []testCase{
{
name: "Empty path",
path: "",
expected: "",
},
{
name: "Simple path",
path: "main.go",
expected: "main.go",
},
{
name: "TestShouldIncludeIncludedFile",
path: "sh main.sh",
expected: "sh main.sh",
},
}
runTests(t, tests)
})
}
Loading