#!/usr/bin/python # # group380_CmdLineHist_test: tests the command-line history plugin # # This test enters echo commands in the shell and uses the up # and down arrow keys to confirm that the correct history is # restored. It also tests the 'history' command to confirm the # list of commands that have been executed. # 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, drainpty=True, logfile=logfile, args=['-p', 'plugins/']) atexit.register(force_shell_termination, shell_process=c) # set timeout for all following 'expect*' calls to 2 seconds c.timeout = 2 # ensure that shell prints expected prompt assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" # hit the up arrow key and send line (empty) c.sendline("\033[A") #"\033[A" is the up arrow escape sequence. B is down. # check that the shell ignored the empty command and prints expected prompt assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" # send 'history' command c.sendline("history") assert c.expect("history") == 0, "'history' did not output the correct command line history" # send an echo command to the shell c.sendline("echo CmdLineHist 1") # ensure that the shell prints the expected prompt assert c.expect(def_module.prompt) == 0, "Shell did not print expected prompt" # hit the up arrow key and press enter. c.sendline("\033[A") # check to see if the same echo command executed assert c.expect("CmdLineHist 1") == 0, "Shell did not execute correct command in history" # enter another echo command c.sendline("echo CmdLineHist 2") # hit up arrow key and press enter. c.sendline("\033[A") # confirm command executed is the same as previous command assert c.expect("CmdLineHist 2") == 0, "Shell did not execute correct command" # send the same echo command multiple times c.sendline("echo CmdLineHist 3") c.sendline("echo CmdLineHist 3") c.sendline("echo CmdLineHist 3") c.sendline("echo CmdLineHist 3") # hit the up arrow key twice. c.send("\033[A") c.sendline("\033[A") # confirm that the duplicate commands were not stored. The second command should have been executed. assert c.expect("CmdLineHist 2") == 0, "Shell did not execute correct command after executing the same command consecutively and entering 'up arrow' twice" # enter a previously entered command (not the most previous one) c.sendline("echo CmdLineHist 1") # hit the up arrow key c.sendline("\033[A") # since that command was not the most previous one, it should have been added to history. assert c.expect("CmdLineHist 1") == 0, "Shell did not execute correct command when entering a command that was previously entered (not the most current one)" # enter a few different commands c.sendline("echo CmdLineHist 1") c.sendline("echo CmdLineHist 2") c.sendline("echo CmdLineHist 3") c.sendline("echo CmdLineHist 4") c.sendline("echo CmdLineHist 5") # Hit up arrow four times c.send("\033[A") c.send("\033[A") c.send("\033[A") c.send("\033[A") # Hit down arrow two times c.send("\033[B") c.sendline("\033[B") # Confirm correct command was executed assert c.expect("CmdLineHist 4") == 0, "Shell did not execute the correct command when going up and down the commands" # up arrow once c.send("\033[A") # down arrow twice c.send("\033[B") c.send("\033[B") # up arrow once c.sendline("\033[A") # Confirm command was the most recent command, since going down after it reaches the end should not go anywhere assert c.expect("CmdLineHist 4") == 0, "Shell did not execute the correct format when testing the bounds of the history list" # send 'history' command c.sendline("history") history = " history\r\n" \ " echo CmdLineHist 1\r\n" \ " echo CmdLineHist 2\r\n" \ " echo CmdLineHist 3\r\n" \ " echo CmdLineHist 2\r\n" \ " echo CmdLineHist 1\r\n" \ " echo CmdLineHist 2\r\n" \ " echo CmdLineHist 3\r\n" \ " echo CmdLineHist 4\r\n" \ " echo CmdLineHist 5\r\n" \ " echo CmdLineHist 4\r\n" \ " history\r\n" assert c.expect(history) == 0, "'history' did not report the correct history list" # exit c.sendline("exit") assert c.expect_exact("exit\r\n") == 0, "Shell output extraneous characters" shellio.success()