AHDog

run_all_tests

Mar 2nd, 2024 (edited)
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. """
  2. CSCI 447 Assignment 5 test case runner
  3. By Sky Duryee
  4. @ahdog on Discord
  5.  
  6. Run this Python file while cd into the `os` folder
  7. of your assignment.
  8.  
  9. I tried my best but in the worst case
  10. scenario this could end up deleting TestProgram3.k.
  11. So be prepared to restore it.
  12. """
  13.  
  14. import tempfile
  15. import shutil
  16. import os
  17. import subprocess
  18.  
  19. # constants
  20. # test output location
  21. OUTPUT = "test_results.txt"
  22. # tempfile location
  23. TEMP = "/tmp/a5_run_all_tests_12032.txt"
  24. # the starting line number of TestProgram3.k where the tests are
  25. TEST_START = 27
  26. # the number of different test cases in TestProgram3.k
  27. NUM_TESTS = 13
  28.  
  29. # runs the test_num'th test.
  30. # the index of the test is 0-based.
  31. # returns a string of the output.
  32. def run_test(test_num) -> str:
  33.     with open("TestProgram3.k", mode="r") as testf:
  34.         lines = list(testf)
  35.     origlines = lines.copy()
  36.     print(lines[0])
  37.    
  38.     test_index = TEST_START + test_num - 1
  39.     test_name = lines[test_index].replace("--", "").strip()
  40.     print()
  41.     print(f"&&&&&&&&& run_all_tests.py: Running test {test_num}: {test_name} &&&&&&&&&")
  42.  
  43.     # comment out all test lines
  44.     for i in range(TEST_START-1, TEST_START+NUM_TESTS-1):
  45.         lines[i] = "--" + lines[i]
  46.    
  47.     #print(f"Lines of file:\n {''.join(lines)}")
  48.  
  49.  
  50.     # uncomment the line of that specific test
  51.     lines[test_index] = lines[test_index].replace("--", "")
  52.  
  53.     with open("TestProgram3.k", mode="w") as testf:
  54.         try:
  55.             # write back to file
  56.             testf.writelines(lines)
  57.             testf.flush()
  58.    
  59.             # make
  60.             subprocess.run("make", check=True)
  61.  
  62.             # run program
  63.             # write output to shell and to TEMP
  64.             subprocess.run(f"blitz -g os 2>&1 | tee {TEMP}", shell=True, text=True, check=True, stderr=subprocess.STDOUT)
  65.             # get output from TEMP
  66.             with open(TEMP, mode="r") as tempf:
  67.                 return tempf.read()
  68.         finally:
  69.             # write back original data to file when done
  70.             testf.truncate(0)
  71.             testf.seek(0)
  72.             testf.writelines(origlines)
  73.        
  74. # run all tests and save to OUTPUT
  75. with open(OUTPUT, mode="w") as out:
  76.     for i in range(NUM_TESTS):
  77.         test_result: str = run_test(i)
  78.  
  79.         # remove stuff after **** so it looks like desired output
  80.         end = test_result.rfind("*****")
  81.         assert end != -1
  82.         end += 5
  83.         test_result = test_result[:end]
  84.  
  85.         # Remove first line so it looks like desired output
  86.         test_result = "\n".join(test_result.splitlines()[1:])
  87.  
  88.         out.write(test_result)
  89.         out.write("\n\n\n")
  90.         out.flush()
  91.  
  92. print("Finished running all tests!")
Advertisement
Add Comment
Please, Sign In to add comment