Skip to content

Commit

Permalink
encapsulate paths with quotes to escape spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Sep 19, 2024
1 parent 6403f4d commit e6d3a6f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
11 changes: 7 additions & 4 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"regexp"
"runtime"
"strings"
"time"

"dario.cat/mergo"
Expand Down Expand Up @@ -305,9 +306,7 @@ func (c *Config) preprocess() error {
if c.TestDataDir == "" {
c.TestDataDir = "testdata"
}
if err != nil {
return err
}

ed := c.Build.ExcludeDir
for i := range ed {
ed[i] = cleanPath(ed[i])
Expand All @@ -328,6 +327,9 @@ func (c *Config) preprocess() error {
// CMD will not recognize relative path like ./tmp/server
c.Build.Bin, err = filepath.Abs(c.Build.Bin)

// Account for spaces in filepath
c.Build.Bin = fmt.Sprintf("%q", c.Build.Bin)

return err
}

Expand Down Expand Up @@ -363,7 +365,8 @@ func (c *Config) killDelay() time.Duration {
}

func (c *Config) binPath() string {
return filepath.Join(c.Root, c.Build.Bin)
bin := strings.Trim(c.Build.Bin, "\"")
return fmt.Sprintf("%q", filepath.Join(c.Root, bin))
}

func (c *Config) tmpPath() string {
Expand Down
51 changes: 42 additions & 9 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,49 @@ func TestReadConfByName(t *testing.T) {
}

func TestConfPreprocess(t *testing.T) {
t.Setenv(airWd, "_testdata/toml")
df := defaultConfig()
err := df.preprocess()
if err != nil {
t.Fatalf("preprocess error %v", err)
tests := []struct {
name string
space bool
suffix string
}{
{
name: "no spaces",
space: false,
suffix: "/_testdata/toml/tmp/main\"",
},
{
name: "with spaces",
space: true,
suffix: "/_testdata/toml/tmp space/main\"",
},
}
suffix := "/_testdata/toml/tmp/main"
binPath := df.Build.Bin
if !strings.HasSuffix(binPath, suffix) {
t.Fatalf("bin path is %s, but not have suffix %s.", binPath, suffix)

for _, tt := range tests {

oWD, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get currWD: %v", err)
}

t.Setenv(airWd, "_testdata/toml")
df := defaultConfig()
if tt.space {
df.Build.Bin = "./tmp space/main"
}
err = df.preprocess()
if err != nil {
t.Fatalf("%s: preprocess error %v", tt.name, err)
}

binPath := df.Build.Bin
if !strings.HasSuffix(binPath, tt.suffix) {
t.Fatalf("%s: bin path is %s, but not have suffix %s.", tt.name, binPath, tt.suffix)
}

err = os.Chdir(oWD)
if err != nil {
t.Fatalf("failed to change back to original WD: %v", err)
}
}
}

Expand Down

0 comments on commit e6d3a6f

Please sign in to comment.