Skip to content

Commit dc2cc8a

Browse files
authored
Merge pull request #26 from jakkdl/diffing_message_printing
2 parents 59c45d9 + 1579eff commit dc2cc8a

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Tests are automatically generated for files named `trio*.py` in the `tests/` dir
3333

3434
Lines containing `error:` are parsed as expecting an error of the code matching the file name, with everything on the line after the colon `eval`'d and passed as arguments to `flake8_trio.Error_codes[<error_code>].str_format`. The `globals` argument to `eval` contains a `lineno` variable assigned the current line number, and the `flake8_trio.Statement` namedtuple. The first element after `error:` *must* be an integer containing the column where the error on that line originates.
3535

36+
Test files by default filter out all errors not matching the file name, but if there's a line `#INCLUDE TRIO\d\d\d TRIO\d\d\d` those additional error codes are not filtered out and will be an error if encountered.
37+
3638

3739
## Style Guide
3840

tests/test_flake8_trio.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
)
2323

2424

25+
class ParseError(Exception):
26+
...
27+
28+
2529
# These functions are messily cobbled together and their formatting requirements
2630
# should be documented in the readme
2731
#
@@ -43,12 +47,19 @@ def test_eval(test: str, path: str):
4347

4448
assert test in Error_codes.keys(), "error code not defined in flake8_trio.py"
4549

50+
include = [test]
4651
expected: List[Error] = []
4752
with open(os.path.join("tests", path)) as file:
4853
lines = file.readlines()
4954

5055
for lineno, line in enumerate(lines, start=1):
5156
line = line.strip()
57+
58+
if reg_match := re.search(r"(?<=INCLUDE).*", line):
59+
for other_code in reg_match.group().split(" "):
60+
if other_code.strip():
61+
include.append(other_code.strip())
62+
5263
# skip commented out lines
5364
if not line or line[0] == "#":
5465
continue
@@ -77,17 +88,21 @@ def test_eval(test: str, path: str):
7788
), f'invalid column "{col}" @L{lineno}, in "{line}"'
7889

7990
# assert col.isdigit(), f'invalid column "{col}" @L{lineno}, in "{line}"'
80-
expected.append(make_error(test, lineno, int(col), *args))
91+
try:
92+
expected.append(make_error(test, lineno, int(col), *args))
93+
except AttributeError as e:
94+
msg = f'Line {lineno}: Failed to format\n "{Error_codes[test]}"\nwith\n{args}'
95+
raise ParseError(msg) from e
8196

8297
assert expected, f"failed to parse any errors in file {path}"
83-
assert_expected_errors(path, test, *expected)
98+
assert_expected_errors(path, include, *expected)
8499

85100

86-
def assert_expected_errors(test_file: str, include: str, *expected: Error):
101+
def assert_expected_errors(test_file: str, include: Iterable[str], *expected: Error):
87102
filename = Path(__file__).absolute().parent / test_file
88103
plugin = Plugin.from_filename(str(filename))
89104

90-
errors = tuple(sorted(e for e in plugin.run() if include in e[2]))
105+
errors = tuple(sorted(e for e in plugin.run() if any(i in e[2] for i in include)))
91106

92107
assert_correct_lines(errors, expected)
93108
assert_correct_columns(errors, expected)
@@ -146,12 +161,20 @@ def assert_correct_messages(errors: Iterable[Error], expected: Iterable[Error]):
146161
file=sys.stderr,
147162
)
148163
msg_error = True
164+
i = 0
165+
while error_msg[i] == expected_msg[i]:
166+
i += 1
167+
j = -1
168+
while error_msg[j] == expected_msg[j]:
169+
j -= 1
170+
end = None if j == -1 else j + 1
149171
print(
150-
f"* line: {line:3}",
151-
f" actual: {error_msg}",
152-
f"expected: {expected_msg}",
172+
f"* line: {line:3} differs\n",
173+
f" same: {error_msg[:i]}\n",
174+
f" actual: {error_msg[i:end]}\n",
175+
f"expected: {expected_msg[i:end]}\n",
176+
f" end: {error_msg[end:]}\n" if end is not None else "",
153177
"-" * 20,
154-
sep="\n",
155178
file=sys.stderr,
156179
)
157180
assert not msg_error

0 commit comments

Comments
 (0)