@@ -26,11 +26,11 @@ import (
26
26
"fmt"
27
27
"github.com/stretchr/testify/assert"
28
28
"testing"
29
+ "time"
29
30
)
30
31
31
32
func Test_command_result_outcome (t * testing.T ) {
32
-
33
- testFlags := []struct {
33
+ testCases := []struct {
34
34
status CommandStatus
35
35
expectedPassed bool
36
36
expectedFailed bool
@@ -39,11 +39,80 @@ func Test_command_result_outcome(t *testing.T) {
39
39
{"fail" , false , true },
40
40
{"unknown" , false , false },
41
41
}
42
- for _ , tt := range testFlags {
42
+ for _ , tt := range testCases {
43
43
t .Run (fmt .Sprint (tt .status , "_status" ), func (t * testing.T ) {
44
44
result := CommandResult {Status : tt .status }
45
45
assert .Equal (t , tt .expectedPassed , result .Passed ())
46
46
assert .Equal (t , tt .expectedFailed , result .Failed ())
47
47
})
48
48
}
49
49
}
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
+ }
0 commit comments