Port signals test to pexpect

This commit is contained in:
Fabian Homborg 2020-06-13 15:38:54 +02:00
parent c11457f2db
commit 93c3aaf5f4
4 changed files with 77 additions and 62 deletions

77
tests/pexpects/signals.py Normal file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env python3
from pexpect_helper import SpawnedProc
sp = SpawnedProc()
send, sendline, sleep, expect_prompt, expect_re, expect_str = (
sp.send,
sp.sendline,
sp.sleep,
sp.expect_prompt,
sp.expect_re,
sp.expect_str,
)
from time import sleep
import os
import signal
import subprocess
expect_prompt()
sendline("sleep 10 &")
expect_prompt()
send("\x03")
sleep(0.010)
sendline("jobs")
expect_prompt("sleep.10")
sendline("kill %1")
expect_prompt ()
# Verify that the fish_postexec handler is called after SIGINT.
sendline("function postexec --on-event fish_postexec; echo fish_postexec spotted; end")
expect_prompt()
sendline("read")
expect_re("\r\n?read> $")
os.kill(sp.spawn.pid, signal.SIGINT)
expect_str("fish_postexec spotted")
expect_prompt()
# Verify that the fish_kill_signal is set.
sendline("functions -e postexec; function postexec --on-event fish_postexec; echo fish_kill_signal $fish_kill_signal; end")
expect_prompt()
sendline("sleep 5")
sleep(0.100)
subprocess.call(["pkill", "-INT", "sleep", "-P", str(sp.spawn.pid)])
expect_str("fish_kill_signal 2")
expect_prompt()
sendline("sleep 5")
sleep(0.100)
subprocess.call(["pkill", "-TERM", "sleep", "-P", str(sp.spawn.pid)])
expect_str("fish_kill_signal 15")
expect_prompt()
# Verify that sending SIGHUP to the shell, such as will happen when the tty is
# closed by the terminal, terminates the shell and the foreground command and
# any background commands run from that shell.
send("sleep 130 &\r")
expect_prompt()
send("sleep 131 &\r")
expect_prompt()
send("sleep 132\r")
os.kill(sp.spawn.pid, signal.SIGHUP)
# Verify the spawned fish shell has exited.
sp.spawn.wait()
# Verify all child processes have been killed. We don't use `-p $pid` because
# if the shell has a bug the child processes might have been reparented to pid
# 1 rather than killed.
proc = subprocess.run(
["pgrep", "-l", "-f", "sleep 13"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if proc.returncode == 0:
print("Commands still running")
print(proc.stdout)
sys.exit(1)

View file

@ -1,62 +0,0 @@
# vim: set filetype=expect:
#
# Test signal handling for interactive shells.
set pid [spawn $fish -C "sleep 10&"]
expect_prompt
send "\x03"
sleep 0.010
send_line "jobs"
expect_prompt -re {sleep.10} {} unmatched {
puts stderr "background job missing after SIGINT"
}
send_line "kill %1"
expect_prompt
# Verify that the fish_postexec handler is called after SIGINT.
send_line "function postexec --on-event fish_postexec; echo fish_postexec spotted; end"
expect_prompt
send_line read
expect -re "\\r\\n?read> $"
exec -- kill -INT $pid
expect "fish_postexec spotted"
expect_prompt
# Verify that the fish_kill_signal is set.
send_line "functions -e postexec; function postexec --on-event fish_postexec; echo fish_kill_signal \$fish_kill_signal; end"
expect_prompt
send_line "sleep 5"
sleep 0.100
exec -- pkill -INT sleep -P $pid
expect "fish_kill_signal 2"
expect_prompt
send_line "sleep 5"
sleep 0.100
exec -- pkill -TERM sleep -P $pid
expect "fish_kill_signal 15"
expect_prompt
# Verify that sending SIGHUP to the shell, such as will happen when the tty is
# closed by the terminal, terminates the shell and the foreground command and
# any background commands run from that shell.
send "sleep 130 &\r"
expect_prompt
send "sleep 131 &\r"
expect_prompt
send "sleep 132\r"
exec -- kill -HUP $pid
# Verify the spawned fish shell has exited.
catch {expect default exp_continue} output
wait
# Verify all child processes have been killed. We don't use `-p $pid` because
# if the shell has a bug the child processes might have been reparented to pid
# 1 rather than killed.
set status [catch {exec pgrep -l -f "sleep 13"} output]
if {$status == 0} {
puts stderr "Commands spawned by the shell still running after SIGHUP"
puts stderr $output
}