Skip to content

Commit ed434c0

Browse files
authored
Merge pull request #413 from buildkite/fix-custom-runner-cmd-interpolation
Fix custom runner cmd interpolation
2 parents 5b03618 + 8b0753a commit ed434c0

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

internal/runner/command.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"os/exec"
77
"os/signal"
8-
"strings"
98
"syscall"
109
)
1110

@@ -18,7 +17,7 @@ func runAndForwardSignal(cmd *exec.Cmd) error {
1817
finishCh := make(chan struct{})
1918
defer close(finishCh)
2019

21-
fmt.Println(strings.Join(cmd.Args, " "))
20+
fmt.Println(cmd.String())
2221
fmt.Println("")
2322

2423
if err := cmd.Start(); err != nil {

internal/runner/custom.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os/exec"
7-
"slices"
7+
"strings"
88

99
"github.com/buildkite/test-engine-client/internal/debug"
1010
"github.com/buildkite/test-engine-client/internal/plan"
@@ -87,7 +87,7 @@ func (r Custom) Run(result *RunResult, testCases []plan.TestCase, retry bool) er
8787
tests, parseErr := parseTestEngineTestResult(r.ResultPath)
8888

8989
if parseErr != nil {
90-
fmt.Printf("Buildkite Test Engine Client: Failed to read json output: %v", parseErr)
90+
fmt.Println("Buildkite Test Engine Client: Failed to read json output:", parseErr)
9191
return err
9292
}
9393

@@ -108,15 +108,12 @@ func (r Custom) Run(result *RunResult, testCases []plan.TestCase, retry bool) er
108108
}
109109

110110
func (r Custom) commandNameAndArgs(cmd string, testCases []string) (string, []string, error) {
111+
cmd = strings.Replace(cmd, "{{testExamples}}", strings.Join(testCases, " "), 1)
112+
111113
words, err := shellquote.Split(cmd)
112114
if err != nil {
113115
return "", []string{}, err
114116
}
115-
idx := slices.Index(words, "{{testExamples}}")
116-
117-
if idx >= 0 {
118-
words = slices.Replace(words, idx, idx+1, testCases...)
119-
}
120117

121118
return words[0], words[1:], nil
122119
}

internal/runner/custom_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,48 @@ func TestCustom_GetFiles(t *testing.T) {
8080
}
8181

8282
func TestCustom_CommandNameAndArgs(t *testing.T) {
83-
custom, err := NewCustom(RunnerConfig{
84-
TestCommand: "bin/test -- {{testExamples}}",
85-
TestFilePattern: "tests/**/test_*.sh",
86-
})
87-
88-
if err != nil {
89-
t.Fatalf("Failed to create Custom runner: %v", err)
90-
}
91-
9283
testCases := []string{"tests/test_a.sh", "tests/test_b.sh"}
9384

94-
gotName, gotArgs, err := custom.commandNameAndArgs(custom.TestCommand, testCases)
95-
if err != nil {
96-
t.Errorf("Custom.cmdNameAndArgs() error = %v", err)
97-
}
85+
commands := []struct {
86+
command string
87+
wantName string
88+
wantArgs []string
89+
}{
90+
{
91+
command: "bin/test -- {{testExamples}}",
92+
wantName: "bin/test",
93+
wantArgs: []string{"--", "tests/test_a.sh", "tests/test_b.sh"},
94+
},
95+
{
96+
command: "rake test TEST_FILES='{{testExamples}}'",
97+
wantName: "rake",
98+
wantArgs: []string{"test", "TEST_FILES=tests/test_a.sh tests/test_b.sh"},
99+
},
100+
}
101+
102+
for _, tc := range commands {
103+
104+
custom, err := NewCustom(RunnerConfig{
105+
TestCommand: tc.command,
106+
TestFilePattern: "tests/**/test_*.sh",
107+
})
98108

99-
wantName := "bin/test"
100-
wantArgs := []string{"--", "tests/test_a.sh", "tests/test_b.sh"}
109+
if err != nil {
110+
t.Fatalf("Failed to create Custom runner: %v", err)
111+
}
101112

102-
if gotName != wantName {
103-
t.Errorf("Custom.cmdNameAndArgs() name = %v, want %v", gotName, wantName)
104-
}
113+
gotName, gotArgs, err := custom.commandNameAndArgs(custom.TestCommand, testCases)
114+
if err != nil {
115+
t.Errorf("Custom.cmdNameAndArgs(%q, testCases) error = %v", tc.command, err)
116+
}
117+
118+
if gotName != tc.wantName {
119+
t.Errorf("Custom.cmdNameAndArgs(%q, testCases) name = %v, want %v", tc.command, gotName, tc.wantName)
120+
}
105121

106-
if diff := cmp.Diff(gotArgs, wantArgs); diff != "" {
107-
t.Errorf("Custom.cmdNameAndArgs() args diff (-got +want):\n%s", diff)
122+
if diff := cmp.Diff(gotArgs, tc.wantArgs); diff != "" {
123+
t.Errorf("Custom.cmdNameAndArgs(%q, testCases) args diff (-got +want):\n%s", tc.command, diff)
124+
}
108125
}
109126
}
110127

internal/runner/pytest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (p Pytest) Run(result *RunResult, testCases []plan.TestCase, retry bool) er
7979
tests, parseErr := parseTestEngineTestResult(p.ResultPath)
8080

8181
if parseErr != nil {
82-
fmt.Printf("Buildkite Test Engine Client: Failed to read json output, failed tests will not be retried: %v", parseErr)
82+
fmt.Println("Buildkite Test Engine Client: Failed to read json output, failed tests will not be retried:", parseErr)
8383
return err
8484
}
8585

internal/runner/pytest_pants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (p PytestPants) Run(result *RunResult, testCases []plan.TestCase, retry boo
8080
tests, parseErr := parseTestEngineTestResult(p.ResultPath)
8181

8282
if parseErr != nil {
83-
fmt.Printf("Buildkite Test Engine Client: Failed to read json output, failed tests will not be retried: %v", parseErr)
83+
fmt.Println("Buildkite Test Engine Client: Failed to read json output, failed tests will not be retried:", parseErr)
8484
return err
8585
}
8686

0 commit comments

Comments
 (0)