Skip to content

Commit 4eb4cd9

Browse files
committed
Make test runner return a TestResult, rather than Optional
1 parent ac97dd1 commit 4eb4cd9

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

dragon_runner/harness.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ def __init__(self, config: Config, cli_args: CLIArgs):
1515
self.cli_args: CLIArgs = cli_args
1616
self.failures: List[TestResult] = []
1717

18-
def post_run_log(self):
19-
"""Report failures to stdout."""
20-
if self.failures != []:
21-
log(f"Failure Summary: ({len(self.failures)} tests)")
22-
for result in self.failures:
23-
result.log()
24-
2518
def process_test_result(self, test_result: Optional[TestResult], counters: Dict[str, int]):
2619
"""
2720
Process each test result.
@@ -43,7 +36,10 @@ def pre_executable_hook(self):
4336

4437
def post_executable_hook(self):
4538
"""Hook to run after iterating through an executable"""
46-
pass
39+
if self.failures != []:
40+
log(f"Failure Summary: ({len(self.failures)} tests)")
41+
for result in self.failures:
42+
result.log()
4743

4844
def iterate(self):
4945
"""
@@ -70,7 +66,7 @@ def iterate(self):
7066
counters = {"pass_count": 0, "test_count": 0}
7167
self.pre_subpackage_hook(spkg)
7268
for test in spkg.tests:
73-
test_result: Optional[TestResult] = tc_runner.run(test, exe)
69+
test_result: TestResult = tc_runner.run(test, exe)
7470
self.process_test_result(test_result, counters)
7571
self.post_subpackage_hook(counters)
7672
log("Subpackage Passed: ", counters["pass_count"], "/", counters["test_count"], indent=3)
@@ -100,9 +96,7 @@ def process_test_result(self, test_result: Optional[TestResult], counters: Dict[
10096
"""
10197
Override the hook for regular run-specific implementation of counting passes
10298
"""
103-
if not test_result:
104-
log("[ERROR] Failed to receive test result", indent=7)
105-
elif test_result.did_pass:
99+
if test_result.did_pass:
106100
counters["pass_count"] += 1
107101
test_result.log(args=self.cli_args)
108102
else:
@@ -126,10 +120,7 @@ def process_test_result(self,
126120
toolchain_name = context["toolchain_name"]
127121
a_pkg_name = context["a_pkg_name"]
128122

129-
if not test_result:
130-
log(f"Failed to run test {test.stem}")
131-
context["exit_status"] = False
132-
elif test_result.did_pass:
123+
if test_result.did_pass:
133124
print(Fore.GREEN + '.' + Fore.RESET, end='')
134125
counters['pass_count'] += 1
135126
else:
@@ -251,6 +242,11 @@ def post_executable_hook(self):
251242
indent=4)
252243
self.leak_tests = []
253244
self.test_count = 0 # reset for each executable
245+
246+
if self.failures != []:
247+
log(f"Failure Summary: ({len(self.failures)} tests)")
248+
for result in self.failures:
249+
result.log()
254250

255251
def process_test_result(self, test_result: Optional[TestResult], counters: Dict[str, int]):
256252
"""
@@ -286,9 +282,7 @@ def process_test_result(self, test_result: Optional[TestResult], counters: Dict[
286282
"""
287283
Override the hook for regular run-specific implementation of counting passes
288284
"""
289-
if not test_result:
290-
log("Failed to receive test result")
291-
elif test_result.did_pass:
285+
if test_result.did_pass:
292286
counters["pass_count"] += 1
293287
test_result.log(args=self.cli_args)
294288
else:

dragon_runner/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def main():
1414
args: CLIArgs = parse_cli_args()
1515
log(args, level=1)
1616

17-
# dragon-runner can also be used as a frontend for grading scripts
17+
# dragon-runner can also be used as a loader for grading & other scripts
1818
if args.is_script_mode():
1919
print(f"Use dragon-runner as a loader for script: {args.mode}")
2020
loader = Loader(args.mode, args.script_args)

dragon_runner/runner.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ class TestResult:
6969
execution time, and error information.
7070
"""
7171
__test__ = False # pytest gets confused when classes start with 'Test'
72-
def __init__(self, test:TestFile):
72+
def __init__(self, test:TestFile, did_pass:bool=False):
7373
# required fields
7474
self.test = test
75-
self.did_pass: bool = False
75+
self.did_pass: bool = did_pass
7676
self.did_timeout: bool = False
7777
self.error_test: bool = False
7878
self.memory_leak: bool = False
@@ -186,13 +186,13 @@ def resolve_command(self, step: Step, params: MagicParams) -> Command:
186186
command.args[0] = os.path.abspath(exe)
187187
return command
188188

189-
def run(self, test: TestFile, exe: Executable) -> Optional[TestResult]:
189+
def run(self, test: TestFile, exe: Executable) -> TestResult:
190190
"""
191191
run each step of the toolchain for a given test and executable
192192
"""
193193
input_file = test.path
194194
expected = test.expected_out if isinstance(test.expected_out, bytes) else b''
195-
tr = TestResult(test=test)
195+
tr = TestResult(test=test, did_pass=False)
196196

197197
for index, step in enumerate(self.tc):
198198

@@ -241,7 +241,7 @@ def run(self, test: TestFile, exe: Executable) -> Optional[TestResult]:
241241
tr.memory_leak = True
242242

243243
if child_process.returncode != 0 and \
244-
child_process.returncode not in self.reserved_exit_codes:
244+
child_process.returncode not in self.reserved_exit_codes:
245245
"""
246246
A step in the toolchain has returned a non-zero exit status. If "allowError"
247247
is specified in the config, we can perform a lenient diff based on CompileTime
@@ -296,6 +296,9 @@ def run(self, test: TestFile, exe: Executable) -> Optional[TestResult]:
296296
If $OUTPUT is not supplied, we create a temporary pipe.
297297
"""
298298
input_file = output_file or make_tmp_file(child_process.stdout)
299+
300+
# this code should be unreachable for well-defined toolchains
301+
return tr
299302

300303
@staticmethod
301304
def replace_env_vars(cmd: Command) -> Command:

tests/configs/ValgrindConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"testDir": "../packages/MemoryLeaks",
33
"testedExecutablePaths": {
4-
"clang": "/usr/bin/clang"
4+
"clang": "/usr/bin/clang",
5+
"clang2": "/usr/bin/clang"
56
},
67
"toolchains": {
78
"LLVM": [

0 commit comments

Comments
 (0)