def test_comments(program): commands = [ "#This line should get ignored", "# This line should also get ignored", "### This is ignored too", ] bash = handler.ShellHandler("bash") hand = handler.ShellHandler(program) run_commands(bash, hand, commands)
def test_multiple_spaces_cmds(program): cmds = [ "ls -la --color=never | wc", "echo Hello > a.txt", 'echo "." > a.txt', "ls --color=never < a.txt | wc >> a.txt" ] hand = handler.ShellHandler(program) bash = handler.ShellHandler("bash") run_commands(bash, hand, cmds)
def test_nospaces_cmds(program): cmds = [ "ls -la --color=never|wc", 'echo ".">a.txt', "ls --color=never<a.txt", "find ~/ -print -type f|grep --color=never share>>a.txt" ] hand = handler.ShellHandler(program) bash = handler.ShellHandler("bash") run_commands(bash, hand, cmds)
def test_command_execution(program): commands = [ "ls -la --color=never", 'echo "Hello from my Shell"', 'echo "This Is a very Long String' + "A" * 1000 + '"', 'echo "This string contains chars and other things .912345&*<>again()?/_-+ ~#@%"', ] bash = handler.ShellHandler("bash") hand = handler.ShellHandler(program) run_commands(bash, hand, commands)
def test_multiple_pipes(program): commands = [ "ls -la --color=never | sort | uniq | wc", "echo H" + "i" * 8000 + " | wc" * 200, 'echo "." > a1.txt', "ls -la --color=never < a.txt | sort | uniq | grep a >> a1.txt", ] bash = handler.ShellHandler("bash") hand = handler.ShellHandler(program) run_commands(bash, hand, commands)
def test_garbage_command(program): hand = handler.ShellHandler(program) bash = handler.ShellHandler("bash") cmds = [ '"ls --color=never"', # this is a valid command '"echo" "ThisShouldWorkToo"' ] run_commands(bash, hand, cmds) hand = handler.ShellHandler(program) # this has to fail out = hand.do_input("garbage command ls -la --color=never -print | grep | tail") assert "not found" in out, "Garbage is not a valid command" hand.do_end()
def test_command_redirection_simple_pipe(program): commands = [ "ls -la --color=never | wc", "ls -la --color=never | wc > a.txt", "cat a.txt", 'echo "Hello Im Writing to the end of a file" >> a.txt', "cat a.txt", "wc < a.txt", 'echo "." > a.txt', "ls --color=never < a.txt | wc > b.txt", "ls --color=never > a.txt | wc", ] bash = handler.ShellHandler("bash") hand = handler.ShellHandler(program) run_commands(bash, hand, commands)
def test_history(program): handl = handler.ShellHandler(program) # Run 10 commands to fill the history cmds = [ "ls -la --color=never", "echo HelloWorld", 'find ~/ -type f -print | grep --color=never "Permission Denied" > out.txt', "rm out.txt", "echo againHello", "ps aux", "ls -la --color=never | wc", "echo HelloAgain", "ls -la | cat" ] for cmd in cmds: handl.do_input(cmd) history = handl.do_input("history") assert "[10]: history" in history, "'history' command must be part of the history" cmd_result = handl.do_input("again 2") assert cmd_result == "HelloWorld", "Bad again implementation" cmd_result = handl.do_input("again 5 | sort > again.txt") out = handl.do_input("ps aux | sort") out2 = handl.do_input("cat again.txt") assert out == out2 handl.do_input("rm again.txt") cmd_result = handl.do_input("echo 'again 3 This must print'") assert cmd_result == "again 3 This must print", "Quotes must escape again command" handl.do_end()