Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- CSCI 447 Assignment 5 test case runner
- By Sky Duryee
- @ahdog on Discord
- Run this Python file while cd into the `os` folder
- of your assignment.
- I tried my best but in the worst case
- scenario this could end up deleting TestProgram3.k.
- So be prepared to restore it.
- """
- import tempfile
- import shutil
- import os
- import subprocess
- # constants
- # test output location
- OUTPUT = "test_results.txt"
- # tempfile location
- TEMP = "/tmp/a5_run_all_tests_12032.txt"
- # the starting line number of TestProgram3.k where the tests are
- TEST_START = 27
- # the number of different test cases in TestProgram3.k
- NUM_TESTS = 13
- # runs the test_num'th test.
- # the index of the test is 0-based.
- # returns a string of the output.
- def run_test(test_num) -> str:
- with open("TestProgram3.k", mode="r") as testf:
- lines = list(testf)
- origlines = lines.copy()
- print(lines[0])
- test_index = TEST_START + test_num - 1
- test_name = lines[test_index].replace("--", "").strip()
- print()
- print(f"&&&&&&&&& run_all_tests.py: Running test {test_num}: {test_name} &&&&&&&&&")
- # comment out all test lines
- for i in range(TEST_START-1, TEST_START+NUM_TESTS-1):
- lines[i] = "--" + lines[i]
- #print(f"Lines of file:\n {''.join(lines)}")
- # uncomment the line of that specific test
- lines[test_index] = lines[test_index].replace("--", "")
- with open("TestProgram3.k", mode="w") as testf:
- try:
- # write back to file
- testf.writelines(lines)
- testf.flush()
- # make
- subprocess.run("make", check=True)
- # run program
- # write output to shell and to TEMP
- subprocess.run(f"blitz -g os 2>&1 | tee {TEMP}", shell=True, text=True, check=True, stderr=subprocess.STDOUT)
- # get output from TEMP
- with open(TEMP, mode="r") as tempf:
- return tempf.read()
- finally:
- # write back original data to file when done
- testf.truncate(0)
- testf.seek(0)
- testf.writelines(origlines)
- # run all tests and save to OUTPUT
- with open(OUTPUT, mode="w") as out:
- for i in range(NUM_TESTS):
- test_result: str = run_test(i)
- # remove stuff after **** so it looks like desired output
- end = test_result.rfind("*****")
- assert end != -1
- end += 5
- test_result = test_result[:end]
- # Remove first line so it looks like desired output
- test_result = "\n".join(test_result.splitlines()[1:])
- out.write(test_result)
- out.write("\n\n\n")
- out.flush()
- print("Finished running all tests!")
Advertisement
Add Comment
Please, Sign In to add comment