Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecfcbae

Browse files
committed
testscript: add kill command
This allows sending a termination signal to backgrounded commands.
1 parent 2c88e7f commit ecfcbae

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

testscript/cmd.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var scriptCmds = map[string]func(*TestScript, bool, []string){
3636
"exec": (*TestScript).cmdExec,
3737
"exists": (*TestScript).cmdExists,
3838
"grep": (*TestScript).cmdGrep,
39+
"kill": (*TestScript).cmdKill,
3940
"mkdir": (*TestScript).cmdMkdir,
4041
"mv": (*TestScript).cmdMv,
4142
"rm": (*TestScript).cmdRm,
@@ -492,7 +493,69 @@ func (ts *TestScript) cmdUNIX2DOS(neg bool, args []string) {
492493
}
493494
}
494495

495-
// Tait waits for background commands to exit, setting stderr and stdout to their result.
496+
// cmdKill kills background commands.
497+
func (ts *TestScript) cmdKill(neg bool, args []string) {
498+
signals := map[string]os.Signal{
499+
"INT": os.Interrupt,
500+
"KILL": os.Kill,
501+
}
502+
var (
503+
name string
504+
signal os.Signal
505+
)
506+
switch len(args) {
507+
case 0:
508+
case 1, 2:
509+
sig, ok := strings.CutPrefix(args[0], "-")
510+
if ok {
511+
signal, ok = signals[sig]
512+
if !ok {
513+
ts.Fatalf("unknown signal: %s", sig)
514+
}
515+
} else {
516+
name = args[0]
517+
break
518+
}
519+
if len(args) == 2 {
520+
name = args[1]
521+
}
522+
default:
523+
ts.Fatalf("usage: kill [-SIGNAL] [name]")
524+
}
525+
if neg {
526+
ts.Fatalf("unsupported: ! kill")
527+
}
528+
if signal == nil {
529+
signal = os.Kill
530+
}
531+
if name != "" {
532+
ts.killBackgroundOne(name, signal)
533+
} else {
534+
ts.killBackground(signal)
535+
}
536+
}
537+
538+
func (ts *TestScript) killBackgroundOne(bgName string, signal os.Signal) {
539+
bg := ts.findBackground(bgName)
540+
if bg == nil {
541+
ts.Fatalf("unknown background process %q", bgName)
542+
}
543+
err := bg.cmd.Process.Signal(signal)
544+
if err != nil {
545+
ts.Fatalf("unexpected error terminating background command %q: %v", bgName, err)
546+
}
547+
}
548+
549+
func (ts *TestScript) killBackground(signal os.Signal) {
550+
for bgName, bg := range ts.background {
551+
err := bg.cmd.Process.Signal(signal)
552+
if err != nil {
553+
ts.Fatalf("unexpected error terminating background command %q: %v", bgName, err)
554+
}
555+
}
556+
}
557+
558+
// cmdWait waits for background commands to exit, setting stderr and stdout to their result.
496559
func (ts *TestScript) cmdWait(neg bool, args []string) {
497560
if len(args) > 1 {
498561
ts.Fatalf("usage: wait [name]")

testscript/doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ The predefined commands are:
179179
The file's content must (or must not) match the regular expression pattern.
180180
For positive matches, -count=N specifies an exact number of matches to require.
181181
182+
- kill [-SIGNAL] [command]
183+
Terminate all 'exec' and 'go' commands started in the background (with the '&'
184+
token) by sending an termination signal. Recognized signals are KILL and INT.
185+
If no signal is specified, KILL is sent.
186+
187+
If a command argument is specified, it terminates only that command.
188+
182189
- mkdir path...
183190
Create the listed directories, if they do not already exists.
184191

testscript/testdata/kill.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[!exec:sleep] skip
2+
3+
# This test depends on sleep exiting with a non-success status when being
4+
# terminated by an interrupt (kill on Windows) signal.
5+
6+
! exec sleep 10 &test_sleep&
7+
8+
# Set a timeout. If the kill below fails, this sleep will still be running
9+
# before the test exits and so the test will fail when it completes.
10+
! exec sleep 5 &
11+
12+
kill -KILL test_sleep
13+
wait test_sleep

0 commit comments

Comments
 (0)