|
5 | 5 |
|
6 | 6 | class TestBase(unittest.TestCase): |
7 | 7 | def runStudentCode(self, dirname, name): |
8 | | - res = subprocess.call(['./run_student_code.sh', dirname]) |
9 | | - if res != 0: |
10 | | - raise AssertionError(f'Unable to run student\'s virtual machine translator on {name}.vm!') |
| 8 | + try: |
| 9 | + process = subprocess.run(['./run_student_code.sh', dirname], check=True, text=True, capture_output=True, timeout=30) |
| 10 | + print(f'{process.stdout.strip()}\n{process.stderr.strip()}'.strip()) |
| 11 | + except subprocess.CalledProcessError as err: |
| 12 | + error_message = str(err.stderr).strip() |
| 13 | + raise AssertionError(f'Unable to run student code on {dirname}/{name}.jack: "{error_message}"\n{err.stdout}'.strip()) |
| 14 | + except subprocess.TimeoutExpired as err: |
| 15 | + raise TimeoutError(f'Student code timed out after {err.timeout} seconds:\n{str(err.stdout).strip()}') |
11 | 16 |
|
12 | 17 | def assertValidAssembly(self, dirname, name): |
13 | | - res = subprocess.call(['n2tAssembler', f'/autograder/source/{dirname}/{name}.asm']) |
14 | | - if res != 0: |
15 | | - raise AssertionError(f'Unable to assemble student\'s ASM output!') |
| 18 | + try: |
| 19 | + subprocess.run(['n2tAssembler', f'/autograder/source/{dirname}/{name}.asm'], check=True, text=True, capture_output=True, timeout=30) |
| 20 | + except subprocess.CalledProcessError as err: |
| 21 | + error_message = str(err.stderr).strip() |
| 22 | + raise AssertionError(f'Student\'s ASM is invalid, and could not be assembled: "{error_message}"\n{err.stdout}'.strip()) |
| 23 | + except subprocess.TimeoutExpired as err: |
| 24 | + raise TimeoutError(f'Assembler timed out out after {err.timeout} seconds:\n{str(err.stdout).strip()}') |
16 | 25 |
|
17 | 26 | def runCPUEmulator(self, dirname, name): |
18 | | - res = subprocess.call(['n2tCPUEmulator', f'/autograder/source/{dirname}/{name}.tst']) |
19 | | - if res != 0: |
| 27 | + try: |
| 28 | + subprocess.run(['n2tCPUEmulator', f'/autograder/source/{dirname}/{name}.tst'], check=True, text=True, capture_output=True, timeout=30) |
| 29 | + except subprocess.CalledProcessError as err: |
20 | 30 | diff = subprocess.check_output(['/bin/sh', '-c', f'diff /autograder/source/{dirname}/{name}.cmp /autograder/source/{dirname}/{name}.out --strip-trailing-cr ; exit 0'], text=True) |
21 | 31 | print(f'Files differ!\n{diff}') |
22 | | - raise AssertionError(f'Student\'s ASM did not pass the provided TST file!') |
| 32 | + |
| 33 | + error_message = str(err.stderr).strip() |
| 34 | + raise AssertionError(f'Student\'s ASM did not pass the provided TST file: "{error_message}"\n{err.stdout}'.strip()) |
| 35 | + except subprocess.TimeoutExpired as err: |
| 36 | + raise TimeoutError(f'Emulator timed out out after {err.timeout} seconds:\n{str(err.stdout).strip()}') |
23 | 37 |
|
24 | 38 | def assertCorrectTranslator(self, dirname): |
25 | 39 | _, name = dirname.split('/') |
|
0 commit comments