Skip to content

Commit 54d7595

Browse files
committed
Add markers to linter tests, move linter imports
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 275baa6 commit 54d7595

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

Diff for: setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ filterwarnings =
7474
ignore:The 'asyncio_mode' default value will change to 'strict' in future:DeprecationWarning:pytest
7575
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated::pyreadline
7676
junit_suite_name = colcon-notification
77+
markers =
78+
flake8
79+
linter
7780

7881
[options.entry_points]
7982
colcon_core.event_handler =

Diff for: test/spell_check.words

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ hwnd
1414
isatty
1515
iterdir
1616
libnotify
17+
linter
1718
linux
1819
loadfromfile
1920
lparam
@@ -23,6 +24,7 @@ lstrip
2324
noqa
2425
pathlib
2526
plugin
27+
pydocstyle
2628
pytest
2729
retval
2830
rtype

Diff for: test/test_copyright_license.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from pathlib import Path
55
import sys
66

7+
import pytest
78

9+
10+
@pytest.mark.linter
811
def test_copyright_license():
912
missing = check_files([Path(__file__).parents[1]])
1013
assert not len(missing), \
@@ -25,8 +28,8 @@ def check_files(paths):
2528
if not content:
2629
continue
2730
lines = content.splitlines()
28-
has_copyright = \
29-
any(line for line in lines if line.startswith('# Copyright'))
31+
has_copyright = any(filter(
32+
lambda line: line.startswith('# Copyright'), lines))
3033
has_license = \
3134
'# Licensed under the Apache License, Version 2.0' in lines
3235
if not has_copyright or not has_license:

Diff for: test/test_flake8.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
from pathlib import Path
66
import sys
77

8-
from flake8 import LOG
9-
from flake8.api.legacy import get_style_guide
8+
import pytest
109

1110

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

16+
# avoid debug / info / warning messages from flake8 internals
17+
logging.getLogger('flake8').setLevel(logging.ERROR)
18+
19+
# for some reason the pydocstyle logger changes to an effective level of 1
20+
# set higher level to prevent the output to be flooded with debug messages
21+
logging.getLogger('pydocstyle').setLevel(logging.WARNING)
1522

16-
def test_flake8():
1723
style_guide = get_style_guide(
1824
extend_ignore=['D100', 'D104'],
1925
show_source=True,
@@ -43,9 +49,6 @@ def test_flake8():
4349
if report_tests.total_errors:
4450
report_tests._application.formatter.show_statistics(
4551
report_tests._stats)
46-
print(
47-
'flake8 reported {total_errors} errors'
48-
.format_map(locals()), file=sys.stderr)
52+
print(f'flake8 reported {total_errors} errors', file=sys.stderr)
4953

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

Diff for: test/test_spell_check.py

+10-7
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,7 +14,12 @@ 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):
19+
from scspell import Report
20+
from scspell import SCSPELL_BUILTIN_DICT
21+
from scspell import spell_check
22+
2223
source_filenames = [Path(__file__).parents[1] / 'setup.py'] + \
2324
list(
2425
(Path(__file__).parents[1] / 'colcon_notification')
@@ -36,21 +37,23 @@ def test_spell_check(known_words):
3637

3738
unknown_word_count = len(report.unknown_words)
3839
assert unknown_word_count == 0, \
39-
'Found {unknown_word_count} unknown words: '.format_map(locals()) + \
40+
f'Found {unknown_word_count} unknown words: ' + \
4041
', '.join(sorted(report.unknown_words))
4142

4243
unused_known_words = set(known_words) - report.found_known_words
4344
unused_known_word_count = len(unused_known_words)
4445
assert unused_known_word_count == 0, \
45-
'{unused_known_word_count} words in the word list are not used: ' \
46-
.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))
4748

4849

50+
@pytest.mark.linter
4951
def test_spell_check_word_list_order(known_words):
5052
assert known_words == sorted(known_words), \
5153
'The word list should be ordered alphabetically'
5254

5355

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

0 commit comments

Comments
 (0)