#!/usr/bin/python # # Tests the functionality of the convertnum esh plugin. # Written for CS 3214 Spring 2015. # # To run this test on your own shell, simply run: # # python /web/courses/cs3214/spring2015/projects/student-plugins/rosan_nickwp54/magic8/magic8_test.py eshoutput.py # # from the directory in which your "esh" and your "eshoutput.py" is located. # # @author rosan # @author nickwp54 ########## # Set up # ########## import sys, imp, atexit, os sys.path.append("/home/courses/cs3214/software/pexpect-dpty/"); import pexpect, shellio, signal, time, os, re, proc_check # Determine the path this file is in thisdir = os.path.dirname(os.path.realpath(__file__)) #Ensure the shell process is terminated def force_shell_termination(shell_process): c.close(force=True) # pulling in the regular expression and other definitions # this should be the eshoutput.py file of the hosting shell, see usage above definitions_scriptname = sys.argv[1] def_module = imp.load_source('', definitions_scriptname) # you can define logfile=open("log.txt", "w") in your eshoutput.py if you want logging! logfile = None if hasattr(def_module, 'logfile'): logfile = def_module.logfile #spawn an instance of the shell, note the -p flags c = pexpect.spawn(def_module.shell, drainpty=True, logfile=logfile, args=['-p', thisdir]) atexit.register(force_shell_termination, shell_process=c) # set timeout for all following 'expect*' calls to 2 seconds c.timeout = 2 ################ # Actual tests # ################ # ensure that shell prints expected prompt assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt (1)" # test initializing magic8 c.sendline("magic8") c.sendline("Will I pass this test?"); # Expect the random out put from the magic8 assert c.expect(".*\r\n") == 0, \ "expected magic8 %s" % ("random answer") # Continue using the magic8 c.sendline("y"); assert c.expect_exact("What is your question?:") == 0, "What is your question?:" # Ask another question c.sendline("Does the magic8 even work?"); assert c.expect_exact("Would you like to ask again? [y/n]:") == 0, "Would you like to ask again? [y/n]:" # Put in the wrong input this time c.sendline("no"); assert c.expect_exact("Please enter y or n: ") == 0, "Please enter y or n: " # Put in the wrong input again c.sendline("yes"); assert c.expect_exact("Please enter y or n: ") == 0, "Please enter y or n: " # Put in n to discontinue the magic8 and exit c.sendline("n") assert c.expect(def_module.prompt) == 0, "Exit magic 8" #Exit the test shellio.success()