1
1
package timeout
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"os/exec"
6
7
"runtime"
@@ -15,11 +16,17 @@ var (
15
16
shellflag = "-c"
16
17
)
17
18
19
+ const stubCmd = "testdata/stubcmd"
20
+
18
21
func init () {
19
22
if runtime .GOOS == "windows" {
20
23
shellcmd = "cmd"
21
24
shellflag = "/c"
22
25
}
26
+ err := exec .Command ("go" , "build" , "-o" , stubCmd , "testdata/stubcmd.go" ).Run ()
27
+ if err != nil {
28
+ panic (err )
29
+ }
23
30
}
24
31
25
32
func TestRun (t * testing.T ) {
@@ -63,38 +70,38 @@ func TestRunSimple(t *testing.T) {
63
70
},
64
71
{
65
72
name : "timed out" ,
66
- cmd : exec .Command (shellcmd , shellflag , " sleep 3" ),
73
+ cmd : exec .Command (shellcmd , shellflag , fmt . Sprintf ( "%s - sleep 3", stubCmd ) ),
67
74
duration : 1 * time .Second ,
68
75
signal : os .Interrupt ,
69
76
expectedExit : 124 ,
70
77
},
71
78
{
72
79
name : "timed out with preserve status" ,
73
- cmd : exec .Command (shellcmd , shellflag , " sleep 3" ),
80
+ cmd : exec .Command (shellcmd , shellflag , fmt . Sprintf ( "%s - sleep 3", stubCmd ) ),
74
81
duration : time .Duration (0.1 * float64 (time .Second )),
75
82
preserveStatus : true ,
76
83
expectedExit : 128 + 15 ,
77
84
skipOnWin : true ,
78
85
},
79
86
{
80
- name : "preserve status (signal handled )" ,
81
- cmd : exec .Command ("perl " , "testdata/exit_with_23.pl " ),
87
+ name : "preserve status (signal trapd )" ,
88
+ cmd : exec .Command (stubCmd , "-trap " , "SIGTERM" , "-trap-exit" , "23" , "-sleep" , "3 " ),
82
89
duration : 1 * time .Second ,
83
90
preserveStatus : true ,
84
91
expectedExit : 23 ,
85
92
skipOnWin : true ,
86
93
},
87
94
{
88
95
name : "kill after" ,
89
- cmd : exec .Command ("perl " , "testdata/ignore_sigterm.pl " ),
96
+ cmd : exec .Command (stubCmd , "-trap " , "SIGTERM" , "-sleep" , "3 " ),
90
97
duration : 1 * time .Second ,
91
98
killAfter : 1 * time .Second ,
92
99
signal : syscall .SIGTERM ,
93
100
expectedExit : exitKilled ,
94
101
},
95
102
{
96
- name : "ignore sigterm but exited before kill after" ,
97
- cmd : exec .Command ("perl " , "testdata/ignore_sigterm.pl " ),
103
+ name : "trap sigterm but exited before kill after" ,
104
+ cmd : exec .Command (stubCmd , "-trap " , "SIGTERM" , "-sleep" , "3 " ),
98
105
duration : 1 * time .Second ,
99
106
killAfter : 5 * time .Second ,
100
107
signal : syscall .SIGTERM ,
@@ -109,8 +116,8 @@ func TestRunSimple(t *testing.T) {
109
116
skipOnWin : true ,
110
117
},
111
118
{
112
- name : "command cannnot be invoked" ,
113
- cmd : exec .Command ("testdata/ignore_sigterm.pl-xxxxxxxxxxxxxxxxxxxxx " ),
119
+ name : "command cannnot be invoked (command not found) " ,
120
+ cmd : exec .Command ("testdata/command-not-found " ),
114
121
duration : 1 * time .Second ,
115
122
expectedExit : 127 , // TODO cmd should return 125 on win
116
123
skipOnWin : true ,
0 commit comments