Skip to content

Commit 6f5e015

Browse files
committed
Add test cases on toolchain command runner
1 parent cfa8cd2 commit 6f5e015

File tree

4 files changed

+91
-13
lines changed

4 files changed

+91
-13
lines changed

src/engine/tcr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func (tcr *TCREngine) RunTCRCycle() {
524524

525525
// AbortCommand triggers interruption of an ongoing TCR cycle operation
526526
func (tcr *TCREngine) AbortCommand() {
527-
tcr.toolchain.AbortExecution()
527+
_ = tcr.toolchain.AbortExecution()
528528
}
529529

530530
func (tcr *TCREngine) createTCREvent(testResult toolchain.TestCommandResult) (event events.TCREvent) {

src/toolchain/command_runner.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ const (
6464
)
6565

6666
// commandRunner singleton instance
67-
var commandRunner = getCommandRunner()
67+
var commandRunner = &CommandRunner{
68+
command: nil,
69+
}
6870

6971
func getCommandRunner() *CommandRunner {
70-
return &CommandRunner{
71-
command: nil,
72-
}
72+
return commandRunner
7373
}
7474

7575
// Run launches the execution of the provided command
@@ -123,13 +123,15 @@ func (*CommandRunner) reportCommandTrace(readCloser io.ReadCloser) {
123123
}
124124

125125
// AbortRunningCommand triggers aborting of any command that is currently running
126-
func (r *CommandRunner) AbortRunningCommand() {
126+
func (r *CommandRunner) AbortRunningCommand() bool {
127127
if r.command == nil || r.command.Process == nil {
128128
report.PostWarning("There is no command running at this time")
129-
return
129+
return false
130130
}
131131
report.PostWarning("Aborting command: \"", r.command.String(), "\"")
132132
_ = r.command.Process.Kill()
133133
// Calling Kill() may be a bit too brutal (may leave children process alive)
134134
//_ = r.command.Process.Signal(os.Kill)
135+
r.command = nil
136+
return true
135137
}

src/toolchain/command_runner_test.go

+79-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import (
2626
"fmt"
2727
"github.com/stretchr/testify/assert"
2828
"testing"
29+
"time"
2930
)
3031

3132
func Test_command_result_outcome(t *testing.T) {
32-
33-
testFlags := []struct {
33+
testCases := []struct {
3434
status CommandStatus
3535
expectedPassed bool
3636
expectedFailed bool
@@ -39,11 +39,87 @@ func Test_command_result_outcome(t *testing.T) {
3939
{"fail", false, true},
4040
{"unknown", false, false},
4141
}
42-
for _, tt := range testFlags {
42+
for _, tt := range testCases {
4343
t.Run(fmt.Sprint(tt.status, "_status"), func(t *testing.T) {
4444
result := CommandResult{Status: tt.status}
4545
assert.Equal(t, tt.expectedPassed, result.Passed())
4646
assert.Equal(t, tt.expectedFailed, result.Failed())
4747
})
4848
}
4949
}
50+
51+
func Test_run_command(t *testing.T) {
52+
testCases := []struct {
53+
desc string
54+
command Command
55+
expectedStatus CommandStatus
56+
}{
57+
{
58+
"unknown command",
59+
Command{Path: "unknown-command"},
60+
CommandStatusFail,
61+
},
62+
{
63+
"passing command",
64+
Command{Path: "true"},
65+
CommandStatusPass,
66+
},
67+
{
68+
"failing command",
69+
Command{Path: "false"},
70+
CommandStatusFail,
71+
},
72+
}
73+
74+
for _, tt := range testCases {
75+
t.Run(tt.desc, func(t *testing.T) {
76+
result := getCommandRunner().Run(&tt.command)
77+
assert.Equal(t, tt.expectedStatus, result.Status)
78+
})
79+
}
80+
}
81+
82+
func Test_abort_command(t *testing.T) {
83+
testCases := []struct {
84+
desc string
85+
command *Command
86+
expected bool
87+
}{
88+
{
89+
"with no running command",
90+
nil,
91+
false,
92+
},
93+
{
94+
"with running command",
95+
&Command{Path: "sleep", Arguments: []string{"10"}},
96+
true,
97+
},
98+
}
99+
100+
for _, tt := range testCases {
101+
t.Run(tt.desc, func(t *testing.T) {
102+
if tt.command != nil {
103+
logProcess(t, "before run")
104+
go getCommandRunner().Run(tt.command)
105+
logProcess(t, "after run")
106+
// wait for the new command process to start
107+
for getCommandRunner().command == nil {
108+
time.Sleep(10 * time.Millisecond)
109+
}
110+
logProcess(t, "after waiting")
111+
}
112+
logProcess(t, "before abort")
113+
result := getCommandRunner().AbortRunningCommand()
114+
logProcess(t, "after abort")
115+
assert.Equal(t, tt.expected, result)
116+
})
117+
}
118+
}
119+
120+
func logProcess(t *testing.T, stage string) {
121+
t.Log(stage, ": ", getCommandRunner().command)
122+
if getCommandRunner().command != nil {
123+
t.Log("== process: ", getCommandRunner().command.Process)
124+
}
125+
}

src/toolchain/toolchain.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type (
7070
checkTestCommand() error
7171
runsOnPlatform(osName OsName, archName ArchName) bool
7272
CheckCommandAccess(cmdPath string) (string, error)
73-
AbortExecution()
73+
AbortExecution() bool
7474
}
7575
)
7676

@@ -164,8 +164,8 @@ func (tchn Toolchain) RunTests() TestCommandResult {
164164
}
165165

166166
// AbortExecution asks the toolchain to abort any command currently executing
167-
func (Toolchain) AbortExecution() {
168-
commandRunner.AbortRunningCommand()
167+
func (Toolchain) AbortExecution() bool {
168+
return commandRunner.AbortRunningCommand()
169169
}
170170

171171
// BuildCommandPath returns the build command path for this toolchain

0 commit comments

Comments
 (0)