Skip to content

Commit 8a2294f

Browse files
author
Owen Jones
committed
Make CsvsaExpectation a context manager
This gives us more context around assertion failures
1 parent 7cdc8e9 commit 8a2294f

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

regression/CSVSA/TestBasic/test_basic.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
def test_tolerate_unreachable_functions():
88
csvsa_driver = CsvsaDriver(folder_name).with_test_function('tolerate_unreachable_functions')
9-
csvsa_expectation = csvsa_driver.run()
10-
11-
csvsa_expectation.check_successful_run()
12-
csvsa_expectation.check_does_not_match("a \. f\(\)")
13-
csvsa_expectation.check_does_not_match("a \. g\(\)")
14-
csvsa_expectation.check_does_not_match("a \. f\(\)")
15-
csvsa_expectation.check_does_not_match("a \. g\(\)")
9+
with csvsa_driver.run() as csvsa_expectation:
10+
csvsa_expectation.check_successful_run()
11+
csvsa_expectation.check_does_not_match("a . f()")
12+
csvsa_expectation.check_does_not_match("a . g()")

regression/CSVSA/csvsa_driver.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from regression.regex_expectation import RegexExpectation
55

66

7+
78
class CsvsaExpectation(RegexExpectation):
89
"""Encapsulate the output of CsvsaDriver"""
910

10-
def __init__(self, goto_functions, return_code):
11+
def __init__(self, goto_functions, return_code, cmdline):
1112
"""Get the state at the end of test_function"""
12-
RegexExpectation.__init__(self, goto_functions, return_code, compatibility_mode=False, check_verification=False)
13+
RegexExpectation.__init__(self, goto_functions, return_code, compatibility_mode=False,
14+
check_verification=False, cmdline=cmdline)
1315

1416

1517
class CsvsaDriver:
@@ -42,4 +44,4 @@ def run(self):
4244
executable_runner = ExecutableRunner(cmd)
4345
(stdout, _, return_code) = executable_runner.run(pipe_stderr_to_stdout=True)
4446

45-
return CsvsaExpectation(stdout, return_code)
47+
return CsvsaExpectation(stdout, return_code, " ".join(cmd))

regression/regex_expectation.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import re
2+
import sys
23

34

45
class RegexExpectation:
56
"""Encapsulate the output of CsvsaDriver"""
67

7-
def __init__(self, text, return_code, compatibility_mode=True, check_verification=False):
8+
def __init__(self, text, return_code, compatibility_mode=True, check_verification=False, cmdline=""):
89
"""Get the state at the end of test_function"""
910
if return_code < 0:
1011
self.exit_code = 0
@@ -19,6 +20,7 @@ def __init__(self, text, return_code, compatibility_mode=True, check_verificatio
1920
self.split_text = text.splitlines()
2021
# check_verification indicates whether a successful run should include "VERIFICATION SUCCESSFUL" in it
2122
self.check_verification = check_verification
23+
self.cmdline = cmdline
2224

2325
def check_successful_run(self):
2426
self.check_does_not_match_regex("^warning: ignoring")
@@ -38,3 +40,10 @@ def check_matches_regex(self, x):
3840

3941
def check_does_not_match_regex(self, x):
4042
assert all(not re.search(x, line) for line in self.split_text)
43+
44+
def __enter__(self):
45+
return self
46+
47+
def __exit__(self, exc_type, exc_value, traceback):
48+
if exc_value is not None:
49+
print("Failure may relate to command: ", self.cmdline, file=sys.stderr)

0 commit comments

Comments
 (0)