From b14bae70d14b5caa983208cdbb1fb93819f3707c Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Mon, 20 May 2024 11:20:49 -0700 Subject: [PATCH 1/6] Update developer configuration --- .config/requirements-dev.in | 8 -------- .config/requirements-test.in | 9 +++++++++ .gitignore | 3 +++ .vscode/extensions.json | 10 +++++++--- .vscode/settings.json | 19 +++++++++++-------- pyproject.toml | 27 +++++++++++++++++++-------- tox.ini | 18 ++++++++++++------ 7 files changed, 61 insertions(+), 33 deletions(-) delete mode 100644 .config/requirements-dev.in diff --git a/.config/requirements-dev.in b/.config/requirements-dev.in deleted file mode 100644 index fe0aabf..0000000 --- a/.config/requirements-dev.in +++ /dev/null @@ -1,8 +0,0 @@ -black -mypy -pre-commit -pydoclint -pytest -ruff -toml-sort -types-PyYAML diff --git a/.config/requirements-test.in b/.config/requirements-test.in index d6333f2..873ddc4 100644 --- a/.config/requirements-test.in +++ b/.config/requirements-test.in @@ -1,4 +1,13 @@ pytest coverage[toml] +coverage-enable-subprocess +mypy +pip-tools +pre-commit +pydoclint +pytest pytest-xdist +ruff +toml-sort tox +types-PyYAML diff --git a/.gitignore b/.gitignore index 1b1aca1..f409493 100644 --- a/.gitignore +++ b/.gitignore @@ -168,4 +168,7 @@ cython_debug/ # Version created and populated by setuptools_scm /src/*/_version.py +# Coverage files +*.lcov + .DS_Store diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 37de469..d26386e 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,9 +1,13 @@ { "recommendations": [ + "charliermarsh.ruff", + "esbenp.prettier-vscode", + "markis.code-coverage", "ms-python.black-formatter", + "ms-python.debugpy", + "ms-python.mypy-type-checker", "ms-python.pylint", - "charliermarsh.ruff", - "matangover.mypy", - "esbenp.prettier-vscode" + "ms-python.python", + "streetsidesoftware.code-spell-checker" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 95b3f85..8aa05b9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,18 +1,21 @@ { "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true, // Override the pyproject.toml and disable xdist to enable test debugging - "python.testing.pytestArgs": ["-n0", "--dist", "no"], + "python.testing.pytestArgs": ["tests"], "[python]": { - "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll": "explicit" + "source.fixAll": "explicit", + "source.organizeImports": "explicit" }, - "editor.defaultFormatter": "charliermarsh.ruff" + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.formatOnSave": true }, - "mypy.runUsingActiveInterpreter": true, - "mypy.targets": ["src", "tests"], "[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" - } + }, + "mypy-type-checker.importStrategy": "fromEnvironment", + "mypy-type-checker.preferDaemon": true, + "mypy-type-checker.reportingScope": "workspace", + "python.testing.pytestEnabled": true, + "markiscodecoverage.searchCriteria": "coverage.lcov" } diff --git a/pyproject.toml b/pyproject.toml index f442f98..626569d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,9 @@ documentation = "https://ansible.readthedocs.io/projects/dev-environment/" homepage = "https://github.com/ansible/ansible-dev-environment" repository = "https://github.com/ansible/ansible-dev-environment" +[tool.coverage.paths] +source = ["src"] + [tool.coverage.report] exclude_lines = ["if TYPE_CHECKING:", "pragma: no cover"] # Increase it just so it would pass on any single-python run @@ -52,12 +55,12 @@ skip_covered = true skip_empty = true [tool.coverage.run] -concurrency = ["multiprocessing", "thread"] +source = ["src"] # Do not use branch until bug is fixes: # https://github.com/nedbat/coveragepy/issues/605 # branch = true parallel = true -source = ["src"] +concurrency = ["multiprocessing", "thread"] [tool.mypy] files = ["src", "tests"] @@ -308,33 +311,41 @@ disable = [ "unsubscriptable-object" ] +[tool.pytest.ini_options] +addopts = "-ra --showlocals --durations=10" +tmp_path_retention_policy = "failed" +verbosity_assertions = 2 + [tool.ruff] builtins = ["__"] fix = true line-length = 100 -select = ["ALL"] target-version = "py310" -[tool.ruff.flake8-pytest-style] +[tool.ruff.lint] +select = ["ALL"] + +[tool.ruff.lint.flake8-pytest-style] parametrize-values-type = "tuple" -[tool.ruff.isort] +[tool.ruff.lint.isort] lines-after-imports = 2 # Ensures consistency for cases when there's variable vs function/class definitions after imports lines-between-types = 1 # Separate import/from with 1 line -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] # SLF001: Allow private member access in tests # S101 Allow assert in tests # S602 Allow shell in test # T201 Allow print in tests "tests/**" = ["SLF001", "S101", "S602", "T201"] +# SIM108, file is generated +"_version.py" = ["SIM108"] -[tool.ruff.pydocstyle] +[tool.ruff.lint.pydocstyle] convention = "pep257" [tool.setuptools.dynamic] dependencies = {file = [".config/requirements.in"]} -optional-dependencies.dev = {file = [".config/requirements-dev.in"]} optional-dependencies.docs = {file = [".config/requirements-docs.in"]} optional-dependencies.test = {file = [".config/requirements-test.in"]} diff --git a/tox.ini b/tox.ini index f1bff98..0cfd44a 100644 --- a/tox.ini +++ b/tox.ini @@ -25,19 +25,25 @@ pass_env = TERM USER set_env = + COVERAGE_FILE = {env:COVERAGE_FILE:{envdir}/.coverage.{envname}} COVERAGE_PROCESS_START = {toxinidir}/pyproject.toml FORCE_COLOR = 1 PIP_CONSTRAINT = {toxinidir}/.config/constraints.txt PRE_COMMIT_COLOR = always TERM = xterm-256color commands_pre = - sh -c "rm -f .coverage* coverage.xml 2>/dev/null || true" -commands = - coverage run -m pytest {posargs} + # safety measure to assure we do not accidentally run tests with broken dependencies + {envpython} -m pip check + # cleaning needed to prevent errors between runs + sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true"commands = commands_post = - py,py{39,310,311,312}: sh -c "coverage combine .coverage.*" - py,py{39,310,311,312}: coverage xml - py,py{39,310,311,312}: coverage report + # We add coverage options but not making them mandatory as we do not want to force + # pytest users to run coverage when they just want to run a single test with `pytest -k test` + coverage run -m pytest {posargs:-n auto} + # needed for upload to codecov.io + {py,py310,py311,py312,py313}: sh -c "coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors" + # lcov needed for vscode integration due to https://github.com/ryanluker/vscode-coverage-gutters/issues/403 + # We fail if files are modified at the end git diff --exit-code allowlist_externals = git From 86c412a9eab40dca8555777fb6c467d513f36f9c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 18:21:34 +0000 Subject: [PATCH 2/6] chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tox.ini | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tox.ini b/tox.ini index 0cfd44a..7f1ad82 100644 --- a/tox.ini +++ b/tox.ini @@ -32,18 +32,11 @@ set_env = PRE_COMMIT_COLOR = always TERM = xterm-256color commands_pre = - # safety measure to assure we do not accidentally run tests with broken dependencies {envpython} -m pip check - # cleaning needed to prevent errors between runs sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true"commands = commands_post = - # We add coverage options but not making them mandatory as we do not want to force - # pytest users to run coverage when they just want to run a single test with `pytest -k test` coverage run -m pytest {posargs:-n auto} - # needed for upload to codecov.io {py,py310,py311,py312,py313}: sh -c "coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors" - # lcov needed for vscode integration due to https://github.com/ryanluker/vscode-coverage-gutters/issues/403 - # We fail if files are modified at the end git diff --exit-code allowlist_externals = git From d516ab441ccddfcce13b1725c312c69cd3a3b510 Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Mon, 20 May 2024 11:29:31 -0700 Subject: [PATCH 3/6] Fix commands --- .vscode/settings.json | 14 +++++++------- tox.ini | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8aa05b9..abc3697 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,7 @@ { - "python.testing.unittestEnabled": false, - // Override the pyproject.toml and disable xdist to enable test debugging - "python.testing.pytestArgs": ["tests"], + "[jsonc]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, "[python]": { "editor.codeActionsOnSave": { "source.fixAll": "explicit", @@ -10,12 +10,12 @@ "editor.defaultFormatter": "ms-python.black-formatter", "editor.formatOnSave": true }, - "[jsonc]": { - "editor.defaultFormatter": "esbenp.prettier-vscode" - }, + "markiscodecoverage.searchCriteria": "coverage.lcov", "mypy-type-checker.importStrategy": "fromEnvironment", "mypy-type-checker.preferDaemon": true, "mypy-type-checker.reportingScope": "workspace", + // Override the pyproject.toml and disable xdist to enable test debugging + "python.testing.pytestArgs": ["tests"], "python.testing.pytestEnabled": true, - "markiscodecoverage.searchCriteria": "coverage.lcov" + "python.testing.unittestEnabled": false } diff --git a/tox.ini b/tox.ini index 7f1ad82..3d3acc7 100644 --- a/tox.ini +++ b/tox.ini @@ -36,7 +36,12 @@ commands_pre = sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true"commands = commands_post = coverage run -m pytest {posargs:-n auto} - {py,py310,py311,py312,py313}: sh -c "coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors" + {py,py310,py311,py312,py313}: sh -c "< Date: Mon, 20 May 2024 15:25:36 -0700 Subject: [PATCH 4/6] Fix tox --- .config/requirements-test.in | 3 +-- .vscode/settings.json | 1 - tox.ini | 17 +++++++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.config/requirements-test.in b/.config/requirements-test.in index 873ddc4..b697209 100644 --- a/.config/requirements-test.in +++ b/.config/requirements-test.in @@ -1,6 +1,5 @@ -pytest +black coverage[toml] -coverage-enable-subprocess mypy pip-tools pre-commit diff --git a/.vscode/settings.json b/.vscode/settings.json index abc3697..77ac4e9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,7 +14,6 @@ "mypy-type-checker.importStrategy": "fromEnvironment", "mypy-type-checker.preferDaemon": true, "mypy-type-checker.reportingScope": "workspace", - // Override the pyproject.toml and disable xdist to enable test debugging "python.testing.pytestArgs": ["tests"], "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false diff --git a/tox.ini b/tox.ini index 3d3acc7..b6b46b3 100644 --- a/tox.ini +++ b/tox.ini @@ -33,15 +33,16 @@ set_env = TERM = xterm-256color commands_pre = {envpython} -m pip check - sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true"commands = -commands_post = + python -c 'import pathlib; pathlib.Path("{env_site_packages_dir}/cov.pth").write_text("import coverage; coverage.process_startup()")' + sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true" +commands = coverage run -m pytest {posargs:-n auto} - {py,py310,py311,py312,py313}: sh -c "< Date: Mon, 20 May 2024 22:26:17 +0000 Subject: [PATCH 5/6] chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tox.ini | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tox.ini b/tox.ini index b6b46b3..2d2f6cf 100644 --- a/tox.ini +++ b/tox.ini @@ -37,12 +37,12 @@ commands_pre = sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true" commands = coverage run -m pytest {posargs:-n auto} - {py,py310,py311,py312,py313}: sh -c "\ - coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && \ - coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && \ - COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && \ - COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors \ - " + {py,py310,py311,py312,py313}: sh -c " \ + coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && \ + coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && \ + COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && \ + COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors \ + " git diff --exit-code allowlist_externals = git From 68471e21e18c4e31a484d61ac607a5fc9fbf237e Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Tue, 21 May 2024 13:50:38 -0700 Subject: [PATCH 6/6] Final fix for coverage error --- pyproject.toml | 6 ++---- tests/conftest.py | 19 +++++++++++++++++++ tox.ini | 6 ++---- 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 tests/conftest.py diff --git a/pyproject.toml b/pyproject.toml index 626569d..f6eae69 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,9 +40,6 @@ documentation = "https://ansible.readthedocs.io/projects/dev-environment/" homepage = "https://github.com/ansible/ansible-dev-environment" repository = "https://github.com/ansible/ansible-dev-environment" -[tool.coverage.paths] -source = ["src"] - [tool.coverage.report] exclude_lines = ["if TYPE_CHECKING:", "pragma: no cover"] # Increase it just so it would pass on any single-python run @@ -55,7 +52,7 @@ skip_covered = true skip_empty = true [tool.coverage.run] -source = ["src"] +source_pkgs = ["ansible_dev_environment"] # Do not use branch until bug is fixes: # https://github.com/nedbat/coveragepy/issues/605 # branch = true @@ -313,6 +310,7 @@ disable = [ [tool.pytest.ini_options] addopts = "-ra --showlocals --durations=10" +testpaths = "tests" tmp_path_retention_policy = "failed" verbosity_assertions = 2 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0feff89 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +"""Global conftest.py for pytest. + +The root package import below happens before the pytest workers are forked, so it +picked up by the initial coverage process for a source match. + +Without it, coverage reports the following false positive error: + +CoverageWarning: No data was collected. (no-data-collected) + +This works in conjunction with the coverage source_pkg set to the package such that +a `coverage run --debug trace` shows the source package and file match. + +<...> +Imported source package 'ansible_dev_environment' as '/**/src//__init__.py' +<...> +Tracing '/**/src//__init__.py' +""" + +import ansible_dev_environment # noqa: F401 diff --git a/tox.ini b/tox.ini index 2d2f6cf..baee09c 100644 --- a/tox.ini +++ b/tox.ini @@ -32,18 +32,16 @@ set_env = PRE_COMMIT_COLOR = always TERM = xterm-256color commands_pre = - {envpython} -m pip check python -c 'import pathlib; pathlib.Path("{env_site_packages_dir}/cov.pth").write_text("import coverage; coverage.process_startup()")' - sh -c "rm -f {envdir}/.coverage.* 2>/dev/null || true" + sh -c "rm -f {envdir}/.coverage* 2>/dev/null || true" commands = coverage run -m pytest {posargs:-n auto} {py,py310,py311,py312,py313}: sh -c " \ - coverage combine -a -q --data-file={envdir}/.coverage {envdir}/.coverage.* && \ + coverage combine -q --data-file={envdir}/.coverage {envdir}/.coverage.* && \ coverage xml --data-file={envdir}/.coverage -o {envdir}/coverage.xml --ignore-errors --fail-under=0 && \ COVERAGE_FILE={envdir}/.coverage coverage lcov --fail-under=0 --ignore-errors -q && \ COVERAGE_FILE={envdir}/.coverage coverage report --fail-under=0 --ignore-errors \ " - git diff --exit-code allowlist_externals = git rm