#!/usr/bin/python # # Block header comment # # import sys, imp, atexit, os, getpass, socket 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) #preserve old config file if it exists config_exists = os.path.isfile('config.fig') if config_exists: old_config = open('config.fig', 'r+') temp = open('temp_config', 'w+') temp.write(old_config.read()) temp.close() old_config.close() #run tests #test prompt 1 config = open('config.fig', 'w+') config.write('PROMPT="A custom prompt>"') config.close() d = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=d) d.timeout = 2 assert d.expect(def_module.prompt1) == 0, "Assert the prompt1 displayed correctly" #test prompt 2 config = open('config.fig', 'w+') config.write('PROMPT="//u//h//w>"') config.close() e = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=e) e.timeout = 2 assert e.expect(def_module.prompt2) == 0, "Assert the prompt2 displayed correctly" #test prompt 3 config = open('config.fig', 'w+') config.write('PROMPT="/h:/w>/u@"') config.close() f = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=f) f.timeout = 2 assert f.expect(def_module.prompt3) == 0, "Assert the prompt3 displayed correctly" #test default prompt os.remove('config.fig') g = pexpect.spawn(def_module.shell + plugin_dir, drainpty=True, logfile=logfile) atexit.register(force_shell_termination, shell_process=g) g.timeout = 2 assert g.expect(def_module.prompt_default) == 0, "Assert the default_prompt displayed correctly" #restore old config file if config_exists: old_config = open('temp_config', 'r+') config = open('config.fig', 'w+') config.write(old_config.read()) old_config.close() config.close() os.remove('temp_config') shellio.success()