Skip to content

Commit c909baf

Browse files
authored
Merge pull request #1168 from PyCQA/improve-coverage
improve coverage
2 parents c5165e1 + dc633e2 commit c909baf

File tree

8 files changed

+55
-79
lines changed

8 files changed

+55
-79
lines changed

.coveragerc

Lines changed: 0 additions & 40 deletions
This file was deleted.

pycodestyle.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
7474
DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704,W503,W504'
7575
try:
76-
if sys.platform == 'win32':
76+
if sys.platform == 'win32': # pragma: win32 cover
7777
USER_CONFIG = os.path.expanduser(r'~\.pycodestyle')
78-
else:
78+
else: # pragma: win32 no cover
7979
USER_CONFIG = os.path.join(
8080
os.getenv('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'),
8181
'pycodestyle'
@@ -150,11 +150,11 @@
150150
DUNDER_REGEX = re.compile(r"^__([^\s]+)__(?::\s*[a-zA-Z.0-9_\[\]\"]+)? = ")
151151
BLANK_EXCEPT_REGEX = re.compile(r"except\s*:")
152152

153-
if sys.version_info >= (3, 12):
153+
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
154154
FSTRING_START = tokenize.FSTRING_START
155155
FSTRING_MIDDLE = tokenize.FSTRING_MIDDLE
156156
FSTRING_END = tokenize.FSTRING_END
157-
else:
157+
else: # pragma: <3.12 cover
158158
FSTRING_START = FSTRING_MIDDLE = FSTRING_END = -1
159159

160160
_checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
@@ -863,14 +863,14 @@ def missing_whitespace(logical_line, tokens):
863863
for token_type, text, start, end, line in tokens:
864864
if token_type == tokenize.OP and text in {'[', '(', '{'}:
865865
brace_stack.append(text)
866-
elif token_type == FSTRING_START:
866+
elif token_type == FSTRING_START: # pragma: >=3.12 cover
867867
brace_stack.append('f')
868868
elif token_type == tokenize.NAME and text == 'lambda':
869869
brace_stack.append('l')
870870
elif brace_stack:
871871
if token_type == tokenize.OP and text in {']', ')', '}'}:
872872
brace_stack.pop()
873-
elif token_type == FSTRING_END:
873+
elif token_type == FSTRING_END: # pragma: >=3.12 cover
874874
brace_stack.pop()
875875
elif (
876876
brace_stack[-1] == 'l' and
@@ -889,7 +889,7 @@ def missing_whitespace(logical_line, tokens):
889889
if text == ':' and brace_stack[-1:] == ['[']:
890890
pass
891891
# 3.12+ fstring format specifier
892-
elif text == ':' and brace_stack[-2:] == ['f', '{']:
892+
elif text == ':' and brace_stack[-2:] == ['f', '{']: # pragma: >=3.12 cover # noqa: E501
893893
pass
894894
# tuple (and list for some reason?)
895895
elif text == ',' and next_char in ')]':
@@ -1960,7 +1960,7 @@ def build_tokens_line(self):
19601960
continue
19611961
if token_type == tokenize.STRING:
19621962
text = mute_string(text)
1963-
elif token_type == FSTRING_MIDDLE:
1963+
elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover
19641964
text = 'x' * len(text)
19651965
if prev_row:
19661966
(start_row, start_col) = start

setup.cfg

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@ select =
99
ignore = E226,E24,W504
1010
max_line_length = 79
1111
max_doc_length = 72
12+
13+
[coverage:run]
14+
plugins = covdefaults
15+
omit =
16+
# TODO: separate the tests from the test data
17+
testsuite/E*.py
18+
testsuite/W*.py
19+
testsuite/latin-1.py
20+
testsuite/noqa.py
21+
testsuite/python*.py
22+
testsuite/utf-8-bom.py
23+
parallel = true
24+
25+
[coverage:report]
26+
fail_under = 93

testsuite/support.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,45 @@ def error(self, line_number, offset, text, check):
4040
self.counters[code] = 1
4141
detailed_code = '%s:%s:%s' % (code, line_number, offset + 1)
4242
# Don't care about expected errors or warnings
43-
if code in self.expected or detailed_code in self.expected:
44-
return
45-
self._deferred_print.append(
46-
(line_number, offset, detailed_code, text[5:], check.__doc__))
47-
self.file_errors += 1
48-
self.total_errors += 1
49-
return code
43+
if code not in self.expected and detailed_code not in self.expected: # pragma: no cover # noqa: E501
44+
err = (line_number, offset, detailed_code, text[5:], check.__doc__)
45+
self._deferred_print.append(err)
46+
self.file_errors += 1
47+
self.total_errors += 1
48+
return code
5049

5150
def get_file_results(self):
5251
# Check if the expected errors were found
5352
label = '%s:%s:1' % (self.filename, self.line_offset)
5453
for extended_code in self.expected:
5554
code = extended_code.split(':')[0]
56-
if not self.counters.get(code):
55+
if not self.counters.get(code): # pragma: no cover
5756
self.file_errors += 1
5857
self.total_errors += 1
5958
print('%s: error %s not found' % (label, extended_code))
6059
else:
6160
self.counters[code] -= 1
6261
for code, extra in sorted(self.counters.items()):
6362
if code not in self._benchmark_keys:
64-
if extra and code in self.expected:
63+
if extra and code in self.expected: # pragma: no cover
6564
self.file_errors += 1
6665
self.total_errors += 1
6766
print('%s: error %s found too many times (+%d)' %
6867
(label, code, extra))
6968
# Reset counters
7069
del self.counters[code]
71-
if self._verbose and not self.file_errors:
70+
if self._verbose and not self.file_errors: # pragma: no cover
7271
print('%s: passed (%s)' %
7372
(label, ' '.join(self.expected) or 'Okay'))
7473
self.counters['test cases'] += 1
75-
if self.file_errors:
74+
if self.file_errors: # pragma: no cover
7675
self.counters['failed tests'] += 1
7776
return super(TestReport, self).get_file_results()
7877

7978
def print_results(self):
8079
results = ("%(physical lines)d lines tested: %(files)d files, "
8180
"%(test cases)d test cases%%s." % self.counters)
82-
if self.total_errors:
81+
if self.total_errors: # pragma: no cover
8382
print(results % ", %s failures" % self.total_errors)
8483
else:
8584
print(results % "")
@@ -127,20 +126,20 @@ def selftest(options):
127126
checker.check_all()
128127
error = None
129128
if code == 'Okay':
130-
if len(counters) > len(options.benchmark_keys):
129+
if len(counters) > len(options.benchmark_keys): # pragma: no cover # noqa: E501
131130
codes = [key for key in counters
132131
if key not in options.benchmark_keys]
133132
error = "incorrectly found %s" % ', '.join(codes)
134-
elif not counters.get(code):
133+
elif not counters.get(code): # pragma: no cover
135134
error = "failed to find %s" % code
136135
# Keep showing errors for multiple tests
137136
for key in set(counters) - set(options.benchmark_keys):
138137
del counters[key]
139138
count_all += 1
140139
if not error:
141-
if options.verbose:
140+
if options.verbose: # pragma: no cover
142141
print("%s: %s" % (code, source))
143-
else:
142+
else: # pragma: no cover
144143
count_failed += 1
145144
print("pycodestyle.py: %s:" % error)
146145
for line in checker.lines:
@@ -176,7 +175,7 @@ def run_tests(filename):
176175
if ver_match:
177176
test_against_version = tuple(int(val or 0)
178177
for val in ver_match.groups())
179-
if sys.version_info < test_against_version:
178+
if sys.version_info < test_against_version: # pragma: no cover
180179
return
181180
lines = readlines(filename) + ['#:\n']
182181
line_offset = 0
@@ -220,7 +219,7 @@ def run_tests(style):
220219
count_passed = done_d + done_s - count_failed
221220
print("%d passed and %d failed." % (count_passed, count_failed))
222221
print("Test failed." if count_failed else "Test passed.")
223-
if count_failed:
222+
if count_failed: # pragma: no cover
224223
sys.exit(1)
225224
if options.testsuite:
226225
init_tests(style)

testsuite/test_E101.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_E101(self):
1212
'\tprint(1) # tabs\n'
1313
' print(2) # spaces\n'
1414
)
15-
if sys.version_info >= (3, 12):
15+
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
1616
self.assertEqual(errors, ['W191:2:1', 'E901:3:28'])
17-
else:
17+
else: # pragma: <3.12 cover
1818
self.assertEqual(errors, ['W191:2:1', 'E101:3:1'])

testsuite/test_E901.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class E901Test(unittest.TestCase):
99
def test_closing_brace(self):
1010
errors = errors_from_src('}\n')
11-
if sys.version_info < (3, 12):
11+
if sys.version_info < (3, 12): # pragma: <3.12 cover
1212
self.assertEqual(errors, ['E901:2:1'])
13-
else:
13+
else: # pragma: >=3.12 cover
1414
self.assertEqual(errors, [])
1515

1616
def test_unclosed_brace(self):
@@ -22,8 +22,8 @@ def lasting(self, duration=300):
2222
progress = self._progress.setdefault('foo', {}
2323
'''
2424
errors = errors_from_src(src)
25-
if sys.version_info < (3, 12):
25+
if sys.version_info < (3, 12): # pragma: <3.12 cover
2626
expected = ['E122:4:1', 'E251:5:13', 'E251:5:15']
27-
else:
27+
else: # pragma: >=3.12 cover
2828
expected = ['E122:4:1', 'E251:5:13', 'E251:5:15', 'E901:5:1'] # noqa: E501
2929
self.assertEqual(errors, expected)

testsuite/test_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ def test_check_nullbytes(self):
329329
count_errors = pep8style.input_file('stdin', lines=['\x00\n'])
330330

331331
stdout = sys.stdout.getvalue()
332-
if sys.version_info < (3, 11, 4):
332+
if sys.version_info < (3, 11, 4): # pragma: <3.11 cover
333333
expected = ["stdin:1:1: E901 ValueError: source code string cannot contain null bytes"] # noqa: E501
334-
elif sys.version_info < (3, 12):
334+
elif sys.version_info < (3, 12): # pragma: <3.12 cover # pragma: >=3.11 cover # noqa: E501
335335
expected = ["stdin:1:1: E901 SyntaxError: source code string cannot contain null bytes"] # noqa: E501
336-
else:
336+
else: # pragma: >=3.12 cover
337337
expected = [
338338
"stdin:1:1: E901 SyntaxError: source code string cannot contain null bytes", # noqa: E501
339339
"stdin:1:1: E901 TokenError: source code cannot contain null bytes", # noqa: E501
@@ -353,17 +353,17 @@ def test_styleguide_unmatched_triple_quotes(self):
353353
pep8style.input_file('stdin', lines=lines)
354354
stdout = sys.stdout.getvalue()
355355

356-
if sys.version_info < (3, 10):
356+
if sys.version_info < (3, 10): # pragma: <3.10 cover
357357
expected = [
358358
'stdin:2:5: E901 TokenError: EOF in multi-line string',
359359
'stdin:2:26: E901 SyntaxError: EOF while scanning triple-quoted string literal', # noqa: E501
360360
]
361-
elif sys.version_info < (3, 12):
361+
elif sys.version_info < (3, 12): # pragma: >=3.10 cover # pragma: <3.12 cover # noqa: E501
362362
expected = [
363363
'stdin:2:5: E901 TokenError: EOF in multi-line string',
364364
'stdin:2:6: E901 SyntaxError: unterminated triple-quoted string literal (detected at line 2)', # noqa: E501
365365
]
366-
else:
366+
else: # pragma: >=3.12 cover
367367
expected = [
368368
'stdin:2:6: E901 SyntaxError: unterminated triple-quoted string literal (detected at line 2)', # noqa: E501
369369
'stdin:2:6: E901 TokenError: EOF in multi-line string',

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ envlist = py, pypy3
88
skip_missing_interpreters = True
99

1010
[testenv]
11-
deps = coverage
11+
deps =
12+
covdefaults
13+
coverage
1214
commands =
1315
python -m pycodestyle --statistics pycodestyle.py
1416
coverage run -m pycodestyle --max-doc-length=72 --testsuite testsuite

0 commit comments

Comments
 (0)