From b0306ea8744ee2a34814352c9943d37dfe6c857c Mon Sep 17 00:00:00 2001 From: Olivier Ramonat Date: Tue, 26 Mar 2024 11:36:49 +0100 Subject: [PATCH] Use e3 pytest plugin See https://github.com/AdaCore/e3-core/pull/703 Remove conftest and coverage files coming from e3-core that are available in the e3 pytest plugin. --- pyproject.toml | 11 +++++ src/e3/aws/dynamodb/__init__.py | 4 +- tests/conftest.py | 34 --------------- tests/coverage/base.rc | 23 ---------- tests/coverage/darwin.rc | 4 -- tests/coverage/linux.rc | 4 -- tests/coverage/windows.rc | 3 -- tests/fix-coverage-paths.py | 33 -------------- tests/gen-cov-config.py | 76 --------------------------------- tox.ini | 11 +---- 10 files changed, 16 insertions(+), 187 deletions(-) delete mode 100644 tests/conftest.py delete mode 100644 tests/coverage/base.rc delete mode 100644 tests/coverage/darwin.rc delete mode 100644 tests/coverage/linux.rc delete mode 100644 tests/coverage/windows.rc delete mode 100644 tests/fix-coverage-paths.py delete mode 100644 tests/gen-cov-config.py diff --git a/pyproject.toml b/pyproject.toml index 01181f24..5825f6ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,6 +51,17 @@ check = [ [tool.setuptools.dynamic] version = {file = "VERSION"} +[tool.coverage.report] +exclude_also = [ + "if tries < max_tries:" +] + +[tool.coverage.run] +branch = false + +[tool.coverage.html] +title = "e3 aws coverage report" + [tool.mypy] # Ensure mypy works with namespace in which there is no toplevel # __init__.py. Explicit_package_bases means that that mypy_path diff --git a/src/e3/aws/dynamodb/__init__.py b/src/e3/aws/dynamodb/__init__.py index 44f046a2..e610ef89 100644 --- a/src/e3/aws/dynamodb/__init__.py +++ b/src/e3/aws/dynamodb/__init__.py @@ -144,7 +144,9 @@ def batch_get_items( res.extend(response.get("Responses", {table_name: []})[table_name]) logger.debug(f"Get_item response: {response}") unprocessed = response["UnprocessedKeys"] - if len(unprocessed) > 0: + if len(unprocessed) > 0: # all: no cover" + # Testing this case is difficult as it requires a table + # with more than 16MB of data batch_keys = unprocessed unprocessed_count = sum( [len(batch_key["Keys"]) for batch_key in batch_keys.values()] # type: ignore diff --git a/tests/conftest.py b/tests/conftest.py deleted file mode 100644 index c489e62e..00000000 --- a/tests/conftest.py +++ /dev/null @@ -1,34 +0,0 @@ -from __future__ import absolute_import, division, print_function - -import logging -import tempfile - -import e3.log -import pytest -from e3.env import Env -from e3.fs import rm -from e3.os.fs import cd - -# Activate full debug logs -e3.log.activate(level=logging.DEBUG, e3_debug=True) - - -@pytest.fixture(autouse=True) -def env_protect(request): - """Protection against environment change. - - The fixture is enabled for all tests and does the following: - - * store/restore env between each tests - * create a temporary directory and do a cd to it before each - test. The directory is automatically removed when test ends - """ - Env().store() - tempd = tempfile.mkdtemp() - cd(tempd) - - def restore_env(): - Env().restore() - rm(tempd, True) - - request.addfinalizer(restore_env) diff --git a/tests/coverage/base.rc b/tests/coverage/base.rc deleted file mode 100644 index 4f84be96..00000000 --- a/tests/coverage/base.rc +++ /dev/null @@ -1,23 +0,0 @@ -[run] -branch = False -# we can probably activate it once we have more coverage - -[report] -fail_under = 60 -# ideally should be around 90, work in progress -exclude_lines = - all: no cover - # os-specific - defensive code - # + -only and : no cover - # + py2-only or py3-only - if TYPE_CHECKING: - # testing this option is hard since it would require a table with - # more that 16MB of data - if len(unprocessed) > 0: - if tries < max_tries: - - - -[html] -title = e3 coverage report diff --git a/tests/coverage/darwin.rc b/tests/coverage/darwin.rc deleted file mode 100644 index a728d1b7..00000000 --- a/tests/coverage/darwin.rc +++ /dev/null @@ -1,4 +0,0 @@ -[run] -omit = - */os/windows/* - */os/unix/constant.py diff --git a/tests/coverage/linux.rc b/tests/coverage/linux.rc deleted file mode 100644 index a728d1b7..00000000 --- a/tests/coverage/linux.rc +++ /dev/null @@ -1,4 +0,0 @@ -[run] -omit = - */os/windows/* - */os/unix/constant.py diff --git a/tests/coverage/windows.rc b/tests/coverage/windows.rc deleted file mode 100644 index fc251567..00000000 --- a/tests/coverage/windows.rc +++ /dev/null @@ -1,3 +0,0 @@ -[run] -omit = - */os/unix/* diff --git a/tests/fix-coverage-paths.py b/tests/fix-coverage-paths.py deleted file mode 100644 index 1c7583c0..00000000 --- a/tests/fix-coverage-paths.py +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python -# strip .tox/*/lib/python*/site-packages paths from coverage data -# show only paths corresponding to the real source files -# From https://github.com/danilobellini/pytest-doctest-custom/ - - -import os -import sys - -from coverage.sqldata import CoverageData -from coverage.files import PathAliases -from tempfile import NamedTemporaryFile - - -def fix_paths(site_pkg_dir: str, cov_data_file: str) -> None: - site_pkg_dir = os.path.abspath(site_pkg_dir) - - paths = PathAliases() - paths.add(site_pkg_dir, "src") - - old_cov_file = NamedTemporaryFile() - old_cov_file.close() - os.rename(cov_data_file, old_cov_file.name) - - old_coverage_data = CoverageData(old_cov_file.name) - old_coverage_data.read() - new_coverage_data = CoverageData(cov_data_file) - new_coverage_data.update(old_coverage_data, aliases=paths) - new_coverage_data.write() - - -if __name__ == "__main__": - fix_paths(sys.argv[1], sys.argv[2]) diff --git a/tests/gen-cov-config.py b/tests/gen-cov-config.py deleted file mode 100644 index 0798fe71..00000000 --- a/tests/gen-cov-config.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python -from __future__ import annotations -from argparse import ArgumentParser -import os - -from e3.env import Env - -from configparser import ConfigParser - - -def main(coverage_rc: str, omit_list_filename: str | None = None) -> None: - os_name = Env().build.os.name - test_dir = os.path.abspath(os.path.dirname(__file__)) - - config = ConfigParser() - base_conf, target_conf = ( - os.path.join(test_dir, "coverage", "%s.rc" % name) for name in ("base", os_name) - ) - - with open(coverage_rc, "w") as dest: - config.read(base_conf) - config.read(target_conf) - - # exclude lines is built with: base.rc config - exclude_lines = config.get("report", "exclude_lines").splitlines() - - # add all -only patterns - exclude_lines += [ - "%s-only" % o - for o in ("darwin", "linux", "solaris", "windows", "bsd", "aix") - if o != os_name - ] - # exclude this specific os - exclude_lines.append("%s: no cover" % os_name) - - # special case for unix - if os_name != "windows": - exclude_lines.append("unix: no cover") - - config.set("report", "exclude_lines", "\n".join(exclude_lines)) - - # If the user gave a file with a list of "omit" entries, - # read it, and append its contents to the run.omit option. - - if omit_list_filename is not None: - if config.has_option("run", "omit"): - omit = config.get("run", "omit").splitlines() - else: - omit = [] - with open(omit_list_filename) as f: - for line in f: - omit.append(line) - config.set("run", "omit", "\n".join(omit)) - - config.write(dest) - - -if __name__ == "__main__": - parser = ArgumentParser(description="Generate a coverage config file") - parser.add_argument( - "coverage_rc_filename", help="The name of the coverage configuration file." - ) - parser.add_argument( - "--omit-from-file", - dest="omit_list_filename", - help=( - "The name of the file providing a list of files which should" - " be excluded in the coverage report, with each line being" - " a glob pattern matching the files that should be omitted." - " This omit list is in addition to the list of files to" - " be omitted by default." - ), - ) - - args = parser.parse_args() - main(args.coverage_rc_filename, omit_list_filename=args.omit_list_filename) diff --git a/tox.ini b/tox.ini index d221f6f7..4ea12312 100644 --- a/tox.ini +++ b/tox.ini @@ -13,14 +13,10 @@ passenv = APPVEYOR*,CI,GITHUB_*,CODECOV_* # Run testsuite with coverage when '-cov' is in the env name commands= - {envpython} {toxinidir}/tests/gen-cov-config.py {toxinidir}/.coveragerc - py.test --ignore=build -vv \ + pytest --ignore=build -vv --e3 \ + cov: --e3-cov-rewrite {envsitepackagesdir} src \ cov: --cov {envsitepackagesdir}/e3/aws --cov-report= --cov-fail-under=0 \ [] - cov: {envpython} {toxinidir}/tests/fix-coverage-paths.py \ - cov: {envsitepackagesdir} {toxinidir}/.coverage - cov: coverage html --fail-under=0 - cov: coverage report codecov: codecov [testenv:check] @@ -39,9 +35,6 @@ ignore = E123,E133,E241,E242,W503,W504 [pydocstyle] ignore = D100,D101,D102,D102,D103,D104,D105,D203,D403,D213 -[pytest] -addopts = --failed-first - [flake8] exclude = .git,__pycache__,build,dist,.tox ignore = B905, C901, E203, E266, E501, W503,D100,D101,D102,D102,D103,D104,D105,D106,D107,D203,D403,D213