#!/usr/bin/python # # percent_foreground: tests the %[job name] pluign # # Test the %[job name] to see if it can bring a background task # with teh job name to the foreground. # # %[job name], fg, ctrl-c, ctrl-z, sleep # import sys, imp, atexit sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); import pexpect, shellio, signal, time, os, re, proc_check #Ensure the shell process is terminated def force_shell_termination(shell_process): c.close(force=True) #pulling in the regular expression and other definitions definitions_scriptname = sys.argv[1] plugin_dir = sys.argv[2] def_module = imp.load_source('', definitions_scriptname) logfile = None if hasattr(def_module, 'logfile'): logfile = def_module.logfile #spawn an instance of the shell c = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=c) # Create process and then stop it c.sendline ("sleep 60") proc_check.wait_until_child_is_in_foreground (c) c.sendcontrol ('z'); # Make sure it stopped (jobid, statusmsg, cmdline) = \ shellio.parse_regular_expression (c, def_module.job_status_regex) assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job" # Run our plugin c.sendline ("%sleep"); # Make sure the task sleep 60 was foregrounded assert c.expect_exact(cmdline) == 0, "Shell did not report the job moved into the foreground" # No reason to keep sleeping c.sendintr (); # TEST WITH 2 SLEEPS STOPPED # Create processes and then stop them c.sendline ("sleep 30") proc_check.wait_until_child_is_in_foreground (c) c.sendcontrol ('z'); (jobid, statusmsg, cmdline) = \ shellio.parse_regular_expression (c, def_module.job_status_regex) assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job" # 2nd task c.sendline ("sleep 60") proc_check.wait_until_child_is_in_foreground (c) c.sendcontrol ('z'); (jobid2, statusmsg2, cmdline2) = \ shellio.parse_regular_expression (c, def_module.job_status_regex) assert statusmsg == def_module.jobs_status_msg['stopped'], "Shell did not report stopped job" # Get first sleep c.sendline ("%sleep"); assert c.expect_exact(cmdline) == 0, "Shell did not report the job moved into the foreground" c.sendintr (); # Get 2nd sleep c.sendline ("%sleep"); assert c.expect_exact(cmdline2) == 0, "Shell did not report the job moved into the foreground" c.sendintr (); shellio.success()