From c533f5c758f464b5a9e95d5812efb26e63c01913 Mon Sep 17 00:00:00 2001 From: cidrblock Date: Wed, 13 Apr 2022 06:01:59 -0700 Subject: [PATCH] Linting and test config --- .flake8 | 33 +++++++++++++++++++ .gitignore | 3 ++ pyproject.toml | 12 +++++++ tests/unit/compat/builtins.py | 2 +- tests/unit/compat/mock.py | 12 +++---- tests/unit/compat/unittest.py | 4 +-- tests/unit/plugins/action/test_fact_diff.py | 2 +- tests/unit/plugins/action/test_update_fact.py | 2 +- tests/unit/plugins/action/test_validate.py | 2 +- tox.ini | 12 ++----- 10 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 .flake8 create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..23ec73f --- /dev/null +++ b/.flake8 @@ -0,0 +1,33 @@ +[flake8] + +builtins = _ + +# Print the total number of errors: +count = true + +# Don't even try to analyze these: +extend-exclude = + # project env vars + .env, + # GitHub configs + .github, + # Cache files of pytest + .pytest_cache, + # Occasional virtualenv dirs + .venv + venv + # VS Code + .vscode, + +# IMPORTANT: avoid using ignore option, always use extend-ignore instead +# Completely and unconditionally ignore the following errors: +extend-ignore = + E203, # annoy black by allowing white space before : https://github.com/psf/black/issues/315 + E402, # ansible requires import after doc strings + E501, # given the ansible_collections this is nearly impossible + +# Accessibility/large fonts and PEP8 unfriendly: +max-line-length = 100 + +# Count the number of occurrences of each error/warning code and print a report: +statistics = true \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4e54cab..6ec57ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ /tests/output/ /changelogs/.plugin-cache.yaml +# In case a collection directory is present, ignore it +/collections/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..6a88cc9 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,12 @@ + +[tool.black] +line-length = 79 + +[tool.pytest.ini_options] +addopts = "-n auto" +testpaths = [ + "tests", +] +filterwarnings = [ + 'ignore:AnsibleCollectionFinder has already been configured', +] \ No newline at end of file diff --git a/tests/unit/compat/builtins.py b/tests/unit/compat/builtins.py index bfc8adf..c71cc5d 100644 --- a/tests/unit/compat/builtins.py +++ b/tests/unit/compat/builtins.py @@ -27,7 +27,7 @@ __metaclass__ = type # One unittest needs to import builtins via __import__() so we need to have # the string that represents it try: - import __builtin__ + import __builtin__ # pyright: ignore[reportMissingImports] # noqa F401 except ImportError: BUILTINS = "builtins" else: diff --git a/tests/unit/compat/mock.py b/tests/unit/compat/mock.py index d6e23b7..76302fe 100644 --- a/tests/unit/compat/mock.py +++ b/tests/unit/compat/mock.py @@ -23,7 +23,7 @@ __metaclass__ = type """ Compat module for Python3.x's unittest.mock module """ -import _io +import _io # pyright: ignore[reportMissingImports] import sys # Python 2.7 @@ -35,12 +35,12 @@ try: # Allow wildcard import because we really do want to import all of mock's # symbols into this compat shim # pylint: disable=wildcard-import,unused-wildcard-import - from unittest.mock import * + from unittest.mock import * # noqa F403 except ImportError: # Python 2 # pylint: disable=wildcard-import,unused-wildcard-import try: - from mock import * + from mock import * # pyright: ignore[reportMissingModuleSource] # noqa F403 except ImportError: print("You need the mock library installed on python2.x to run tests") @@ -54,7 +54,7 @@ if sys.version_info >= (3,) and sys.version_info < (3, 4, 4): # Retrieve lines from read_data via a generator so that separate calls to # readline, read, and readlines are properly interleaved sep = b"\n" if isinstance(read_data, bytes) else "\n" - data_as_list = [l + sep for l in read_data.split(sep)] + data_as_list = [line + sep for line in read_data.split(sep)] if data_as_list[-1] == sep: # If the last line ended in a newline, the list comprehension will have an @@ -107,9 +107,9 @@ if sys.version_info >= (3,) and sys.version_info < (3, 4, 4): ) if mock is None: - mock = MagicMock(name="open", spec=open) + mock = MagicMock(name="open", spec=open) # noqa F405 - handle = MagicMock(spec=file_spec) + handle = MagicMock(spec=file_spec) # noqa F405 handle.__enter__.return_value = handle _data = _iterate_read_data(read_data) diff --git a/tests/unit/compat/unittest.py b/tests/unit/compat/unittest.py index df3379b..b4354cf 100644 --- a/tests/unit/compat/unittest.py +++ b/tests/unit/compat/unittest.py @@ -32,8 +32,8 @@ import sys if sys.version_info < (2, 7): try: # Need unittest2 on python2.6 - from unittest2 import * + from unittest2 import * # noqa F403 except ImportError: print("You need unittest2 installed on python2.6.x to run tests") else: - from unittest import * + from unittest import * # noqa F403 diff --git a/tests/unit/plugins/action/test_fact_diff.py b/tests/unit/plugins/action/test_fact_diff.py index facd720..402dfaa 100644 --- a/tests/unit/plugins/action/test_fact_diff.py +++ b/tests/unit/plugins/action/test_fact_diff.py @@ -19,7 +19,7 @@ from ansible_collections.ansible.utils.plugins.action.fact_diff import ( try: from unittest.mock import MagicMock # pylint:disable=syntax-error except ImportError: - from mock import MagicMock + from mock import MagicMock # pyright: ignore[reportMissingModuleSource] class TestUpdate_Fact(unittest.TestCase): diff --git a/tests/unit/plugins/action/test_update_fact.py b/tests/unit/plugins/action/test_update_fact.py index f0c4451..9eeb11b 100644 --- a/tests/unit/plugins/action/test_update_fact.py +++ b/tests/unit/plugins/action/test_update_fact.py @@ -20,7 +20,7 @@ from ansible_collections.ansible.utils.plugins.action.update_fact import ( try: from unittest.mock import MagicMock # pylint:disable=syntax-error except ImportError: - from mock import MagicMock + from mock import MagicMock # pyright: ignore[reportMissingModuleSource] VALID_DATA = { diff --git a/tests/unit/plugins/action/test_validate.py b/tests/unit/plugins/action/test_validate.py index 9cb48ef..702b793 100644 --- a/tests/unit/plugins/action/test_validate.py +++ b/tests/unit/plugins/action/test_validate.py @@ -19,7 +19,7 @@ from ansible_collections.ansible.utils.plugins.action.validate import ( try: from unittest.mock import MagicMock # pylint:disable=syntax-error except ImportError: - from mock import MagicMock + from mock import MagicMock # pyright: ignore[reportMissingModuleSource] DATA = { diff --git a/tox.ini b/tox.ini index a95eb6f..5c919c3 100644 --- a/tox.ini +++ b/tox.ini @@ -10,22 +10,14 @@ deps = -r{toxinidir}/requirements.txt [testenv:black] install_command = pip install {opts} {packages} commands = - black -v -l79 {toxinidir} + black -v {toxinidir} [testenv:linters] install_command = pip install {opts} {packages} commands = - black -v -l79 --check {toxinidir} + black -v --check {toxinidir} flake8 {posargs} [testenv:venv] commands = {posargs} -[flake8] -# E123, E125 skipped as they are invalid PEP-8. - -show-source = True -ignore = E123,E125,E402,W503 -max-line-length = 160 -builtins = _ -exclude = .git,.tox,tests/unit/compat/