@@ -24,13 +24,14 @@ package toolchain
24
24
25
25
import (
26
26
"fmt"
27
+ "github.com/murex/tcr/utils"
27
28
"github.com/stretchr/testify/assert"
28
29
"testing"
30
+ "time"
29
31
)
30
32
31
33
func Test_command_result_outcome (t * testing.T ) {
32
-
33
- testFlags := []struct {
34
+ testCases := []struct {
34
35
status CommandStatus
35
36
expectedPassed bool
36
37
expectedFailed bool
@@ -39,11 +40,81 @@ func Test_command_result_outcome(t *testing.T) {
39
40
{"fail" , false , true },
40
41
{"unknown" , false , false },
41
42
}
42
- for _ , tt := range testFlags {
43
+ for _ , tt := range testCases {
43
44
t .Run (fmt .Sprint (tt .status , "_status" ), func (t * testing.T ) {
44
45
result := CommandResult {Status : tt .status }
45
46
assert .Equal (t , tt .expectedPassed , result .Passed ())
46
47
assert .Equal (t , tt .expectedFailed , result .Failed ())
47
48
})
48
49
}
49
50
}
51
+
52
+ func Test_run_command (t * testing.T ) {
53
+ testCases := []struct {
54
+ desc string
55
+ command Command
56
+ expectedStatus CommandStatus
57
+ }{
58
+ {
59
+ "unknown command" ,
60
+ Command {Path : "unknown-command" },
61
+ CommandStatusFail ,
62
+ },
63
+ {
64
+ "passing command" ,
65
+ Command {Path : "true" },
66
+ CommandStatusPass ,
67
+ },
68
+ {
69
+ "failing command" ,
70
+ Command {Path : "false" },
71
+ CommandStatusFail ,
72
+ },
73
+ }
74
+
75
+ for _ , tt := range testCases {
76
+ t .Run (tt .desc , func (t * testing.T ) {
77
+ result := getCommandRunner ().Run (& tt .command )
78
+ assert .Equal (t , tt .expectedStatus , result .Status )
79
+ })
80
+ }
81
+ }
82
+
83
+ func Test_abort_command (t * testing.T ) {
84
+ // this test fails on Windows CI for an unexplained reason.
85
+ // This seems to be related to command.Process never being set when running a command,
86
+ // but no explanation why this happens on Windows CI while this works as expected
87
+ // on local Windows as well as on Unix CIs.
88
+ utils .SkipOnWindowsCI (t )
89
+
90
+ testCases := []struct {
91
+ desc string
92
+ command * Command
93
+ expected bool
94
+ }{
95
+ {
96
+ "with no running command" ,
97
+ nil ,
98
+ false ,
99
+ },
100
+ {
101
+ "with running command" ,
102
+ & Command {Path : "sleep" , Arguments : []string {"5" }},
103
+ true ,
104
+ },
105
+ }
106
+
107
+ for _ , tt := range testCases {
108
+ t .Run (tt .desc , func (t * testing.T ) {
109
+ if tt .command != nil {
110
+ go getCommandRunner ().Run (tt .command )
111
+ // wait for the new command process to start
112
+ for getCommandRunner ().command == nil {
113
+ time .Sleep (10 * time .Millisecond )
114
+ }
115
+ }
116
+ result := getCommandRunner ().AbortRunningCommand ()
117
+ assert .Equal (t , tt .expected , result )
118
+ })
119
+ }
120
+ }
0 commit comments