fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
b6a1bedab
master
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 = ( 9sp .send , 10sp .sendline , 11sp .sleep , 12sp .expect_prompt , 13sp .expect_re , 14) 15 16 17# Helper to print an error and exit. 18def error_and_exit (text ): 19keys = sp .colors () 20"{RED}Test failed: {NORMAL}{TEXT}" .format (TEXT = text ,** keys )) 21sys .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 : 30sys .exit (127 ) 31if not os .path .exists (exe_path ): 32f" { exe_path } does not exist" ) 33sys .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 : 41os .kill (fish_pid ,signal .SIGKILL ) 42error_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 ): 55pid ,status = os .waitpid (fish_pid ,os .WUNTRACED | os .WNOHANG ) 56if pid == 0 : 57# No process ready yet, loop again. 58sleep (0.1 ) 59elif pid != fish_pid : 60# Some other process exited (??) 61os .kill (fish_pid ,signal .SIGKILL ) 62error_and_exit ( 63"unexpected pid returned from waitpid. Got %d, expected %d" 64% (pid ,fish_pid ) 65 ) 66elif 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. 70os .kill (fish_pid ,signal .SIGKILL ) 71error_and_exit ("pid %d stopped instead of exiting on SIGTERM" % pid ) 72elif not os .WIFSIGNALED (status ): 73error_and_exit ("pid %d did not signal-exit" % pid ) 74elif os .WTERMSIG (status )!= signal .SIGTERM : 75error_and_exit ( 76"pid %d signal-exited with %d instead of %d (SIGTERM)" 77% (os .WTERMSIG (status ),signal .SIGTERM ) 78 ) 79else : 80# Success! 81sys .exit (0 ) 82else : 83# Our loop completed without the process being returned. 84os .kill (fish_pid ,signal .SIGKILL ) 85error_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" )