fork/fish-shell

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

ccoVeilleFix typos in commentsb6a1bedab

master
2.6 KiB89 linesraw
1#!/usr/bin/env python3
2from pexpect_helper import SpawnedProc
3import sys
4import signal
5import os
6
7sp = SpawnedProc()
8send, sendline, sleep, expect_prompt, expect_re = (
9    sp.send,
10    sp.sendline,
11    sp.sleep,
12    sp.expect_prompt,
13    sp.expect_re,
14)
15
16
17# Helper to print an error and exit.
18def error_and_exit(text):
19    keys = sp.colors()
20    print("{RED}Test failed: {NORMAL}{TEXT}".format(TEXT=text, **keys))
21    sys.exit(1)
22
23
24fish_pid = sp.spawn.pid
25
26# Launch fish_test_helper.
27expect_prompt()
28exe_path = os.environ.get("fish_test_helper")
29if not exe_path:
30    sys.exit(127)
31if not os.path.exists(exe_path):
32    print(f"{exe_path} does not exist")
33    sys.exit(1)
34
35sp.sendline(exe_path + " nohup_wait")
36
37# We expect it to transfer tty ownership to fish_test_helper.
38sleep(1)
39tty_owner = os.tcgetpgrp(sp.spawn.child_fd)
40if fish_pid == tty_owner:
41    os.kill(fish_pid, signal.SIGKILL)
42    error_and_exit(
43        "Test failed: outer fish %d did not transfer tty owner to fish_test_helper"
44        % (fish_pid)
45    )
46
47
48# Now we are going to tell fish to exit.
49# It must not hang. But it might hang when trying to restore the tty.
50os.kill(fish_pid, signal.SIGTERM)
51
52# Loop a bit until the process exits (correct) or stops (incorrect).
53# When it exits it should be due to the SIGTERM that we sent it.
54for i in range(50):
55    pid, status = os.waitpid(fish_pid, os.WUNTRACED | os.WNOHANG)
56    if pid == 0:
57        # No process ready yet, loop again.
58        sleep(0.1)
59    elif pid != fish_pid:
60        # Some other process exited (??)
61        os.kill(fish_pid, signal.SIGKILL)
62        error_and_exit(
63            "unexpected pid returned from waitpid. Got %d, expected %d"
64            % (pid, fish_pid)
65        )
66    elif os.WIFSTOPPED(status):
67        # Our pid stopped instead of exiting.
68        # Probably it stopped because of its tcsetpgrp call during exit.
69        # Kill it and report a failure.
70        os.kill(fish_pid, signal.SIGKILL)
71        error_and_exit("pid %d stopped instead of exiting on SIGTERM" % pid)
72    elif not os.WIFSIGNALED(status):
73        error_and_exit("pid %d did not signal-exit" % pid)
74    elif os.WTERMSIG(status) != signal.SIGTERM:
75        error_and_exit(
76            "pid %d signal-exited with %d instead of %d (SIGTERM)"
77            % (os.WTERMSIG(status), signal.SIGTERM)
78        )
79    else:
80        # Success!
81        sys.exit(0)
82else:
83    # Our loop completed without the process being returned.
84    os.kill(fish_pid, signal.SIGKILL)
85    error_and_exit("fish with pid %d hung after SIGTERM" % fish_pid)
86
87# Should never get here.
88os.kill(fish_pid, signal.SIGKILL)
89error_and_exit("unknown, should be unreachable")