#!/usr/bin/python # # Block header comment # # import sys, imp, atexit, time, os 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] #plugin_dir = "./plugins" 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) # set timeout for all following 'expect*' calls to 2 seconds c.timeout = 10 #######CREATE A FILE TO TEST WITH######### filename = "test.c"; target = open (filename, 'w') ## a will append, w will over-write #providing the content for the file target.write("int main(int argc, char** argv) {\n") target.write("testEveryNth();\n") target.write("return 0;") target.write("}\n") target.write("void testEveryNth() {\n") target.write("uint64_t Value = 12345;\n") target.write("uint8_t N = 2;\n") target.write("uint64_t Result = everyNth(Value, N);\n") target.write("return 0;\n") target.write("}\n") target.close() # run the autoformat without the -o switch c.sendline("af ./test.c") #have to sleep for 5 seconds while vim works time.sleep(5) #read the output file output = open ("a.txt", "r+") str = output.read() output.close #assert it was formatted correctly assert str == "int main(int argc, char** argv) {\n testEveryNth();\n return 0;}\n void testEveryNth() {\n uint64_t Value = 12345;\n uint8_t N = 2;\n uint64_t Result = everyNth(Value, N);\n return 0;\n }\n", "Unimplemented functionality" #run with the -o switch c.sendline("af -o out.txt ./test.c") #have to sleep for 5 seconds while vim works time.sleep(5) #read the output file output2 = open ("out.txt", "r+") str = output2.read() output2.close #assert the file was formatted correctly assert str == "int main(int argc, char** argv) {\n testEveryNth();\n return 0;}\n void testEveryNth() {\n uint64_t Value = 12345;\n uint8_t N = 2;\n uint64_t Result = everyNth(Value, N);\n return 0;\n }\n", "Unimplemented functionality" #remove the created text file os.remove("./a.txt") #try to format a nonexistnet file c.sendline("af imaginary.c") time.sleep(5) #assert an output file was not created, nothing was formatted assert False == os.path.isfile("a.txt") #try to format a nonexistent file with the -o switch c.sendline("af -o wut.txt imaginary.c") time.sleep(5) #assert a file was not created assert False == os.path.isfile("wut.txt") #remove test files os.remove("./test.c") os.remove("./out.txt") shellio.success()