Skip to content

Commit 0b5fd6d

Browse files
authored
Add markers to linter tests, move linter imports (#29)
Test markers can be used to easily (de-)select tests, and colcon exposes mechanisms to do so. Linters are a category of tests that are commonly called out. Additionally, if we move the imports for some of our single-purpose tests into the test function, we can avoid installing the linter dependencies entirely. This is a common case in platform packaging, where linter errors are not actionable and the dependencies are not typically installed.
1 parent a67bf9d commit 0b5fd6d

5 files changed

+34
-26
lines changed

setup.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ exclude =
6060
colcon_core.verb =
6161
coveragepy-result = colcon_coveragepy_result.verb.coveragepy_result:CoveragePyResultVerb
6262

63+
[tool:pytest]
64+
markers =
65+
flake8
66+
linter
67+
6368
[flake8]
6469
import-order-style = google
6570
max-line-length = 99

test/spell_check.words

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake
66
colcon
77
coveragepy
88
iterdir
9+
linter
910
lstrip
1011
nargs
1112
noqa

test/test_copyright_license.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
from pathlib import Path
55
import sys
66

7+
import pytest
78

9+
10+
@pytest.mark.linter
811
def test_copyright_license():
9-
missing = check_files([
10-
Path(__file__).parents[1],
11-
Path(__file__).parents[1] / 'bin' / 'colcon',
12-
])
12+
missing = check_files([Path(__file__).parents[1]])
1313
assert not len(missing), \
1414
'In some files no copyright / license line was found'
1515

test/test_flake8.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
from pathlib import Path
66
import sys
77

8-
from flake8 import LOG
9-
from flake8.api.legacy import get_style_guide
10-
from pydocstyle.utils import log
8+
import pytest
119

1210

13-
# avoid debug and info messages from flake8 internals
14-
LOG.setLevel(logging.WARNING)
11+
@pytest.mark.flake8
12+
@pytest.mark.linter
13+
def test_flake8():
14+
from flake8.api.legacy import get_style_guide
1515

16+
# avoid debug / info / warning messages from flake8 internals
17+
logging.getLogger('flake8').setLevel(logging.ERROR)
1618

17-
def test_flake8():
1819
# for some reason the pydocstyle logger changes to an effective level of 1
1920
# set higher level to prevent the output to be flooded with debug messages
20-
log.setLevel(logging.WARNING)
21+
logging.getLogger('pydocstyle').setLevel(logging.WARNING)
2122

2223
style_guide = get_style_guide(
2324
extend_ignore=['D100', 'D104'],
@@ -48,9 +49,6 @@ def test_flake8():
4849
if report_tests.total_errors:
4950
report_tests._application.formatter.show_statistics(
5051
report_tests._stats)
51-
print(
52-
'flake8 reported {total_errors} errors'
53-
.format_map(locals()), file=sys.stderr)
52+
print(f'flake8 reported {total_errors} errors', file=sys.stderr)
5453

55-
assert not total_errors, \
56-
'flake8 reported {total_errors} errors'.format_map(locals())
54+
assert not total_errors, f'flake8 reported {total_errors} errors'

test/test_spell_check.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
from pathlib import Path
55

66
import pytest
7-
from scspell import Report
8-
from scspell import SCSPELL_BUILTIN_DICT
9-
from scspell import spell_check
10-
117

128
spell_check_words_path = Path(__file__).parent / 'spell_check.words'
139

@@ -18,10 +14,16 @@ def known_words():
1814
return spell_check_words_path.read_text().splitlines()
1915

2016

17+
@pytest.mark.linter
2118
def test_spell_check(known_words):
22-
source_filenames = [
23-
Path(__file__).parents[1] / 'setup.py'] + \
24-
list((Path(__file__).parents[1] / 'colcon_coveragepy_result').glob('**/*.py')) + \
19+
from scspell import Report
20+
from scspell import SCSPELL_BUILTIN_DICT
21+
from scspell import spell_check
22+
23+
source_filenames = [Path(__file__).parents[1] / 'setup.py'] + \
24+
list(
25+
(Path(__file__).parents[1] / 'colcon_coveragepy_result')
26+
.glob('**/*.py')) + \
2527
list((Path(__file__).parents[1] / 'test').glob('**/*.py'))
2628

2729
for source_filename in sorted(source_filenames):
@@ -35,21 +37,23 @@ def test_spell_check(known_words):
3537

3638
unknown_word_count = len(report.unknown_words)
3739
assert unknown_word_count == 0, \
38-
'Found {unknown_word_count} unknown words: '.format_map(locals()) + \
40+
f'Found {unknown_word_count} unknown words: ' + \
3941
', '.join(sorted(report.unknown_words))
4042

4143
unused_known_words = set(known_words) - report.found_known_words
4244
unused_known_word_count = len(unused_known_words)
4345
assert unused_known_word_count == 0, \
44-
'{unused_known_word_count} words in the word list are not used: ' \
45-
.format_map(locals()) + ', '.join(sorted(unused_known_words))
46+
f'{unused_known_word_count} words in the word list are not used: ' + \
47+
', '.join(sorted(unused_known_words))
4648

4749

50+
@pytest.mark.linter
4851
def test_spell_check_word_list_order(known_words):
4952
assert known_words == sorted(known_words), \
5053
'The word list should be ordered alphabetically'
5154

5255

56+
@pytest.mark.linter
5357
def test_spell_check_word_list_duplicates(known_words):
5458
assert len(known_words) == len(set(known_words)), \
5559
'The word list should not contain duplicates'

0 commit comments

Comments
 (0)