Skip to content

Commit

Permalink
Add markers to linter tests, move linter imports
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cottsay committed Feb 7, 2024
1 parent 4ce1fb9 commit 2493c4d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ exclude =

[tool:pytest]
junit_suite_name = colcon-bazel
markers =
flake8
linter
python_classes = !TestPackageArguments

[options.entry_points]
Expand Down
2 changes: 2 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ https
iterdir
karg
kislyuk
linter
lstrip
mickael
nargs
noqa
noshow
pathlib
plugin
pydocstyle
pyparsing
pytest
returncode
Expand Down
3 changes: 3 additions & 0 deletions test/test_copyright_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from pathlib import Path
import sys

import pytest


@pytest.mark.linter
def test_copyright_license():
missing = check_files([Path(__file__).parents[1]])
assert not len(missing), \
Expand Down
23 changes: 13 additions & 10 deletions test/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
from pathlib import Path
import sys

from flake8 import LOG
from flake8.api.legacy import get_style_guide
import pytest


# avoid debug and info messages from flake8 internals
LOG.setLevel(logging.WARN)
@pytest.mark.flake8
@pytest.mark.linter
def test_flake8():
from flake8.api.legacy import get_style_guide

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

# for some reason the pydocstyle logger changes to an effective level of 1
# set higher level to prevent the output to be flooded with debug messages
logging.getLogger('pydocstyle').setLevel(logging.WARNING)

def test_flake8():
style_guide = get_style_guide(
extend_ignore=['D100', 'D104'],
show_source=True,
Expand Down Expand Up @@ -44,9 +50,6 @@ def test_flake8():
if report_tests.total_errors:
report_tests._application.formatter.show_statistics(
report_tests._stats)
print(
'flake8 reported {total_errors} errors'
.format_map(locals()), file=sys.stderr)
print(f'flake8 reported {total_errors} errors', file=sys.stderr)

assert not total_errors, \
'flake8 reported {total_errors} errors'.format_map(locals())
assert not total_errors, f'flake8 reported {total_errors} errors'
17 changes: 10 additions & 7 deletions test/test_spell_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from pathlib import Path

import pytest
from scspell import Report
from scspell import SCSPELL_BUILTIN_DICT
from scspell import spell_check


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

Expand All @@ -18,7 +14,12 @@ def known_words():
return spell_check_words_path.read_text().splitlines()


@pytest.mark.linter
def test_spell_check(known_words):
from scspell import Report
from scspell import SCSPELL_BUILTIN_DICT
from scspell import spell_check

source_filenames = [Path(__file__).parents[1] / 'setup.py'] + \
list(
(Path(__file__).parents[1] / 'colcon_bazel')
Expand All @@ -36,21 +37,23 @@ def test_spell_check(known_words):

unknown_word_count = len(report.unknown_words)
assert unknown_word_count == 0, \
'Found {unknown_word_count} unknown words: '.format_map(locals()) + \
f'Found {unknown_word_count} unknown words: ' + \
', '.join(sorted(report.unknown_words))

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


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


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

0 comments on commit 2493c4d

Please sign in to comment.