Skip to content

Commit fcab594

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

6 files changed

+140
-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

+72-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,80 @@ 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: sleepCommandPath(), 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+
t.Log("before run: ", getCommandRunner().command)
104+
go getCommandRunner().Run(tt.command)
105+
t.Log("after run: ", getCommandRunner().command)
106+
// wait for the new command process to start
107+
for getCommandRunner().command == nil {
108+
time.Sleep(10 * time.Millisecond)
109+
}
110+
t.Log("after waiting: ", getCommandRunner().command)
111+
}
112+
t.Log("before abort: ", getCommandRunner().command)
113+
result := getCommandRunner().AbortRunningCommand()
114+
t.Log("after abort: ", getCommandRunner().command)
115+
assert.Equal(t, tt.expected, result)
116+
})
117+
}
118+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright (c) 2024 Murex
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package toolchain
26+
27+
func sleepCommandPath() string {
28+
return "sleep"
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright (c) 2024 Murex
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package toolchain
24+
25+
func sleepCommandPath() string {
26+
return "sleep.exe"
27+
}

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)