Skip to content

Commit

Permalink
fix: properly format paths with spaces in config.Build.Bin (#666) (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkashin authored Mar 5, 2025
1 parent 1068416 commit a830e5a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
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 @@ func joinPath(root, path string) string {

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)
}

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)
})
}

0 comments on commit a830e5a

Please sign in to comment.