Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Jan 14, 2021
1 parent 8c513ec commit db0fa67
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
7 changes: 7 additions & 0 deletions flake8_codes/_codes/_adhoc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# built-in
import re
from contextlib import suppress
from importlib import import_module
from pathlib import Path
from typing import Dict

from ._registry import registry
from ._default import extract_default


@registry.add
Expand Down Expand Up @@ -78,6 +80,11 @@ def extract_flake8_pytest_style() -> Dict[str, str]:
def extract_flake8_annotations_complexity() -> Dict[str, str]:
from flake8_annotations_complexity.checker import AnnotationsComplexityChecker

with suppress(ImportError):
codes = extract_default('flake8_annotations_complexity.ast_helpers')
if codes:
return codes

code, message = AnnotationsComplexityChecker._error_message_template.split(' ', maxsplit=1)
return {code: message}

Expand Down
1 change: 1 addition & 0 deletions flake8_codes/_plugins/_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

ALIASES = {
'flake-mutable': ('M511', ),
'flake8-annotations-complexity': ('TAE00', ),
'flake8-bandit': ('S', ),
'flake8-django': ('DJ', ), # they say `DJ0` prefix but codes have `DJ10`
'flake8-future-import': ('FI', ),
Expand Down
Empty file added tests/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions tests/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
KNOWN_PLUGINS = (
'dlint',
'flake8-2020',
'flake8-alfred',
'flake8-annotations-complexity',
# 'flake8-bandit',
'flake8-black',
'flake8-broken-line',
'flake8-builtins',
'flake8-coding',
'flake8-cognitive-complexity',
'flake8-comprehensions',
'flake8-debugger',
'flake8-docstrings',
'flake8-eradicate',
'flake8-executable',
'flake8-expression-complexity',
'flake8-fixme',
'flake8-functions',
'flake8-logging-format',
'flake8-mutable',
'flake8-mypy',
'flake8-pep3101',
'flake8-pie',
'flake8-print',
'flake8-printf-formatting',
'flake8-pyi',
'flake8-quotes',
'flake8-requirements',
'flake8-rst-docstrings',
'flake8-spellcheck',
'flake8-sql',
'flake8-strict',
'flake8-string-format',
'flake8-todo',
'flake8-use-fstring',
'flake8-variables-names',

# framework-specific
'flake8-django',
'flake8-scrapy',
'pandas-vet',

# tests
'flake8-aaa',
'flake8-mock',
'flake8-pytest',
'flake8-pytest-style',

# PyCQA
'flake8-bugbear',
'flake8-commas',
'mccabe',
'pep8-naming',

# imports
'flake8-future-import',
'flake8-import-order',
'flake8-isort',
'flake8-absolute-import',
'flake8-tidy-imports',

# built-in in flake8
'pycodestyle',
'pyflakes',
)
32 changes: 32 additions & 0 deletions tests/test_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# external
import pytest

# project
from ._constants import KNOWN_PLUGINS
from flake8_codes._codes import extract
from flake8_codes._codes._default import extract_default
from flake8_codes._codes._registry import registry


@pytest.mark.parametrize('plugin_name', KNOWN_PLUGINS)
def test_smoke_extract(plugin_name):
codes = extract(plugin_name)
assert codes

for code, msg in codes.items():
assert type(code) is str, 'bad code type'
assert type(msg) is str, 'bad message type'

# that's not exactly true but all plugins follow this convention
assert code[0].isalpha(), 'code must start from letter'
assert code[0].isupper(), 'code must be uppercase'


@pytest.mark.parametrize('plugin_name', KNOWN_PLUGINS)
def test_no_custom_extractor_needed(plugin_name):
extractor = registry.get(plugin_name)
if extractor is None:
return
custom_codes = extractor()
default_codes = extract_default(plugin_name)
assert default_codes != custom_codes
23 changes: 23 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# built-in
from unittest.mock import patch

# external
import pytest
from flake8.main.application import Application

from flake8_codes._codes import extract
from flake8_codes._plugins import get_installed
from ._constants import KNOWN_PLUGINS


@patch('sys.argv', ['flake8'])
@pytest.mark.parametrize('plugin_name', KNOWN_PLUGINS)
def test_smoke_prefixes(plugin_name):
app = Application()
plugins = {plugin.name: plugin for plugin in get_installed(app=app)}
plugin = plugins[plugin_name]

codes = extract(plugin_name)
for code in codes:
print(plugin_name, code, plugin.codes)
assert code.startswith(tuple(plugin.codes))

0 comments on commit db0fa67

Please sign in to comment.