From 22c8a11a51e6433c1bd322c7ed36e809425bec9c Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 13 Mar 2018 20:00:25 -0700 Subject: [PATCH] add non-functional tests --- test/spell_check.words | 23 +++++++++++++++++ test/test_copyright_license.py | 39 ++++++++++++++++++++++++++++ test/test_flake8.py | 47 ++++++++++++++++++++++++++++++++++ test/test_spell_check.py | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 test/spell_check.words create mode 100644 test/test_copyright_license.py create mode 100644 test/test_flake8.py create mode 100644 test/test_spell_check.py diff --git a/test/spell_check.words b/test/spell_check.words new file mode 100644 index 0000000..8616a7e --- /dev/null +++ b/test/spell_check.words @@ -0,0 +1,23 @@ +argcomplete +args +basepath +bool +buildfile +cmake +colcon +config +cpu +ctest +dict +github +https +jobserver +kislyuk +makeflags +noqa +pragma +rtype +str +todo +txt +xml diff --git a/test/test_copyright_license.py b/test/test_copyright_license.py new file mode 100644 index 0000000..21f5ace --- /dev/null +++ b/test/test_copyright_license.py @@ -0,0 +1,39 @@ +# Copyright 2016-2018 Dirk Thomas +# Licensed under the Apache License, Version 2.0 + +from pathlib import Path +import sys + + +def test_copyright_licence(): + missing = check_files([Path(__file__).parents[1]]) + assert not len(missing), \ + 'In some files no copyright / license line was found' + + +def check_files(paths): + missing = [] + for path in paths: + if path.is_dir(): + for p in sorted(path.iterdir()): + if p.name.startswith('.'): + continue + if p.name.endswith('.py') or p.is_dir(): + missing += check_files([p]) + if path.is_file(): + content = path.read_text() + if not content: + continue + lines = content.splitlines() + has_copyright = \ + any(l for l in lines if l.startswith('# Copyright')) + has_license = \ + '# Licensed under the Apache License, Version 2.0' in lines + if not has_copyright or not has_license: + print( + 'Could not find copyright / license in:', path, + file=sys.stderr) + missing .append(path) + else: + print('Found copyright / license in:', path) + return missing diff --git a/test/test_flake8.py b/test/test_flake8.py new file mode 100644 index 0000000..040ce4f --- /dev/null +++ b/test/test_flake8.py @@ -0,0 +1,47 @@ +# Copyright 2016-2018 Dirk Thomas +# Licensed under the Apache License, Version 2.0 + +import logging +from pathlib import Path +import sys + +from flake8 import LOG +from flake8.api.legacy import get_style_guide + + +# avoid debug and info messages from flake8 internals +LOG.setLevel(logging.WARN) + + +def test_flake8(): + style_guide = get_style_guide( + ignore=['D100', 'D104'], + show_source=True, + ) + style_guide_tests = get_style_guide( + ignore=['D100', 'D101', 'D102', 'D103', 'D104', 'D105', 'D107'], + show_source=True, + ) + + stdout = sys.stdout + sys.stdout = sys.stderr + # implicitly calls report_errors() + report = style_guide.check_files([ + str(Path(__file__).parents[1] / 'colcon_cmake'), + ]) + report_tests = style_guide_tests.check_files([ + str(Path(__file__).parents[1] / 'test'), + ]) + sys.stdout = stdout + + total_errors = report.total_errors + report_tests.total_errors + if total_errors: # pragma: no cover + # output summary with per-category counts + print() + report._application.formatter.show_statistics(report._stats) + print( + 'flake8 reported {total_errors} errors' + .format_map(locals()), file=sys.stderr) + + assert not report.total_errors, \ + 'flake8 reported {total_errors} errors'.format_map(locals()) diff --git a/test/test_spell_check.py b/test/test_spell_check.py new file mode 100644 index 0000000..8d1f395 --- /dev/null +++ b/test/test_spell_check.py @@ -0,0 +1,46 @@ +# Copyright 2016-2018 Dirk Thomas +# Licensed under the Apache License, Version 2.0 + +from pathlib import Path + +from pylint.lint import Run +import pytest + + +spell_check_words_path = Path(__file__).parent / 'spell_check.words' + + +def test_spell_check(): + global spell_check_words_path + + try: + import enchant # noqa: F401 + except ImportError: # pragma: no cover + pytest.skip( + "Skipping spell checking tests since 'enchant' was not found") + + try: + Run([ + '--disable=all', + '--enable=spelling', + '--spelling-dict=en_US', + '--ignore-comments=no', + '--spelling-private-dict-file=' + + str(spell_check_words_path), + str(Path(__file__).parents[1] / 'colcon_cmake'), + ] + [ + str(p) for p in + (Path(__file__).parents[1] / 'test').glob('**/*.py') + ]) + except SystemExit as e: + assert not e.code, 'Some spell checking errors' + else: + assert False, 'The pylint API is supposed to raise a SystemExit' + + +def test_spell_check_word_list(): + global spell_check_words_path + with spell_check_words_path.open('r') as h: + lines = h.read().splitlines() + assert lines == sorted(lines), \ + 'The word list should be ordered alphabetically'