fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Fabian Boehmpexpects/signals: Decrease a timeout that should be reachedec3b3fe32

master
3.9 KiB135 linesraw
1#!/usr/bin/env python3
2from pexpect_helper import SpawnedProc
3
4sp = SpawnedProc(timeout=10)
5send, sendline, sleep, expect_prompt, expect_re, expect_str = (
6    sp.send,
7    sp.sendline,
8    sp.sleep,
9    sp.expect_prompt,
10    sp.expect_re,
11    sp.expect_str,
12)
13
14from time import sleep
15import os
16import platform
17import signal
18import subprocess
19import sys
20
21if "CI" in os.environ and platform.system() in ["Darwin", "FreeBSD"]:
22    sys.exit(127)
23
24expect_prompt()
25
26# Verify that SIGINT inside a command sub cancels it.
27# Negate the pid to send to the pgroup (which should include sleep).
28sendline("while true; echo (sleep 2000); end")
29sleep(0.5)
30os.kill(-sp.spawn.pid, signal.SIGINT)
31expect_prompt()
32sleep(0.2)
33
34# SIGINT should be ignored by background processes.
35sendline("sleep 1234 &")
36expect_prompt()
37os.kill(-sp.spawn.pid, signal.SIGINT)
38sleep(0.500)
39sendline("jobs")
40expect_prompt("sleep 1234")
41sendline("kill %1")
42expect_prompt()
43
44# Verify that the fish_postexec handler is called after SIGINT.
45sendline("function postexec --on-event fish_postexec; echo fish_postexec spotted; end")
46expect_prompt()
47sendline("read")
48expect_re(r"\r\n?.*read> .*", timeout=10)
49sleep(0.1)
50os.kill(sp.spawn.pid, signal.SIGINT)
51expect_str("fish_postexec spotted", timeout=10)
52expect_prompt()
53
54# Ensure that we catch up.
55sendline("echo 'hello' 'world'")
56expect_prompt("hello world")
57
58# Verify that the fish_kill_signal is set.
59sendline(
60    "functions -e postexec; function postexec --on-event fish_postexec; echo fish_kill_signal $fish_kill_signal; end"
61)
62expect_prompt("fish_kill_signal 0")
63sendline("sleep 5")
64sleep(0.300)
65subprocess.call(["pkill", "-INT", "-P", str(sp.spawn.pid), "sleep"])
66expect_str("fish_kill_signal 2")
67expect_prompt()
68
69sendline("sleep 5")
70sleep(0.200)
71subprocess.call(["pkill", "-TERM", "-P", str(sp.spawn.pid), "sleep"])
72expect_str("fish_kill_signal 15")
73expect_prompt()
74
75# See that open() is only interruptible by SIGINT.
76sendline("mkfifo fifoo")
77expect_prompt()
78sendline("cat >fifoo")
79subprocess.call(["kill", "-WINCH", str(sp.spawn.pid)])
80expect_re("open: ", shouldfail=True, timeout=2)
81subprocess.call(["kill", "-INT", str(sp.spawn.pid)])
82expect_prompt()
83
84# Verify that sending SIGHUP to the shell, such as will happen when the tty is
85# closed by the terminal, terminates the shell and the foreground command and
86# any background commands run from that shell.
87#
88# Save the pids for later to check if they are still running.
89pids = []
90send("sleep 1300 & echo $last_pid\r")
91pids += [expect_re("\\d+\r\n").group().strip()]
92expect_prompt()
93sendline("echo 'catch' 'up' 'A'")
94expect_prompt("catch up A")
95send("sleep 1310 & echo $last_pid\r")
96pids += [expect_re("\\d+\r\n").group().strip()]
97expect_prompt()
98sendline("echo 'catch' 'up' 'B'")
99expect_prompt("catch up B")
100send("sleep 9999999\r")
101sleep(0.500)  # ensure fish kicks off the above sleep before it gets HUP - see #7288
102os.kill(sp.spawn.pid, signal.SIGHUP)
103
104# Verify the spawned fish shell has exited.
105sp.spawn.wait()
106
107# Verify all child processes have been killed. We don't use `-p $pid` because
108# if the shell has a bug the child processes might have been reparented to pid
109# 1 rather than killed.
110proc = subprocess.run(
111    ["pgrep", "-l", "-f", "sleep 13"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
112)
113
114remaining = []
115if proc.returncode == 0:
116    # If any sleeps exist, we check them against our pids,
117    # to avoid false-positives (any other `sleep 13xyz` running on the system)
118    print(proc.stdout)
119    for line in proc.stdout.split(b"\n"):
120        pid = line.split(b" ", maxsplit=1)[0].decode("utf-8")
121        if pid in pids:
122            remaining += [pid]
123
124# Kill any remaining sleeps ourselves, otherwise rerunning this is pointless.
125for pid in remaining:
126    try:
127        os.kill(int(pid), signal.SIGTERM)
128    except ProcessLookupError:
129        continue
130
131if remaining:
132    # We still have processes left over!
133    print("Commands were still running!")
134    print(remaining)
135    sys.exit(1)