sp.expect_re, ) # Helper to print an error and exit. def error_and_exit(text): keys = sp.colors() print("{RED}Test failed: {NORMAL}{TEXT}".format(TEXT=text, **keys)) sys.exit(1) fish_pid = sp.spawn.pid # Launch fish_test_helper. expect_prompt() exe_path = os.environ.get("fish_test_helper") sp.sendline(exe_path + " nohup_wait") # We expect it to transfer tty ownership to fish_test_helper. sleep(0.1) tty_owner = os.tcgetpgrp(sp.spawn.child_fd) if fish_pid == tty_owner: os.kill(fish_pid, signal.SIGKILL) error_and_exit( "Test failed: outer fish %d did not transfer tty owner to fish_test_helper" % (fish_pid) ) # Now we are going to tell fish to exit. # It must not hang. But it might hang when trying to restore the tty. os.kill(fish_pid, signal.SIGTERM)
#!/usr/bin/env python3 from pexpect_helper import SpawnedProc import os import signal import tempfile from time import sleep # Ensure that on-exit handlers run even if we get SIGHUP. with tempfile.NamedTemporaryFile(mode="r", encoding="utf8") as tf: sp = SpawnedProc() ghoti_pid = sp.spawn.pid sp.expect_prompt() sp.sendline( "function myexit --on-event ghoti_exit; /bin/echo $ghoti_pid > {filename}; end".format( filename=tf.name ) ) sp.expect_prompt() os.kill(ghoti_pid, signal.SIGHUP) # Note that ghoti's SIGHUP handling in interactive mode races with the call to select. # So actually close its stdin, like a terminal emulator would do. sp.spawn.close(force=False) sp.spawn.wait() tf.seek(0) line = tf.readline().strip() if line != str(ghoti_pid): colors = sp.colors() print( """{RED}Failed to find pid written by exit handler{RESET}""".format( **colors )
import subprocess import sys import time sp = SpawnedProc(args=["-d", "reader"]) sp.expect_prompt() # Verify we correctly diable mouse tracking. # Five char sequence. sp.send("\x1b[tDE") sp.expect_str("reader: Disabling mouse tracking") # Six char sequence. sp.send("\x1b[MABC") sp.expect_str("reader: Disabling mouse tracking") # Nine char sequences. sp.send("\x1b[TABCDEF") sp.expect_str("reader: Disabling mouse tracking") # Extended SGR sequences. sp.send("\x1b[<fooM") sp.expect_str("reader: Disabling mouse tracking") sp.send("\x1b[<foobarm") sp.expect_str("reader: Disabling mouse tracking") sp.sendline("echo done") sp.expect_prompt("done")