From 5df89afea34f8e870a0670146f9a52b91402240e Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Wed, 19 Jul 2023 12:21:58 +0200 Subject: [PATCH] test: config mutmut --- mutmut_config.py | 25 ++++++++++++++++++++++++- noxfile.py | 16 ++++++++++++++++ pyproject.toml | 3 +++ tests/conftest.py | 19 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/mutmut_config.py b/mutmut_config.py index b0cb9350..4e83aff3 100644 --- a/mutmut_config.py +++ b/mutmut_config.py @@ -1,4 +1,27 @@ +import os.path + + def pre_mutation(context): line = context.current_source_line.strip() - if line.strip().startswith(("assert False", "@overload", "= TypeVar")): + if line.startswith(("assert False", "@overload")): context.skip = True + + if "= TypeVar" in line: + context.skip = True + + return + """Extract the coverage contexts if possible and only run the tests + matching this data.""" + if not context.config.coverage_data: + # mutmut was run without ``--use-coverage`` + return + fname = os.path.abspath(context.filename) + contexts_for_file = context.config.coverage_data.get(fname, {}) + contexts_for_line = contexts_for_file.get(context.current_line_index, []) + print(line) + print(contexts_for_line) + + context.config.test_command += " ".join( + repr(arg) for arg in contexts_for_line if arg + ) + print(context.config.test_command) diff --git a/noxfile.py b/noxfile.py index 4f8efe08..86d47283 100644 --- a/noxfile.py +++ b/noxfile.py @@ -49,4 +49,20 @@ def docs(session): def mutmut(session): session.install("poetry") session.run("poetry", "install", "--with=dev") + + coverage = False + + if coverage: + session.run("coverage", "erase") + + session.env["COVERAGE_PROCESS_START"] = str( + Path(__file__).parent / "pyproject.toml" + ) + session.env["TOP"] = str(Path(__file__).parent) + session.run("pytest", "--doctest-modules", "inline_snapshot", "tests") + session.run("coverage", "combine") + session.run("mutmut", "run", *session.posargs) + + if coverage: + session.run("coverage", "erase") diff --git a/pyproject.toml b/pyproject.toml index 1a1bb7f3..ef838bf8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,9 @@ version_files = [ ] version_provider = "poetry" +[tool.coverage.html] +show_contexts = true + [tool.coverage.report] exclude_lines = ["pragma: no cover", "assert False", "raise NotImplemented", "\\.\\.\\."] diff --git a/tests/conftest.py b/tests/conftest.py index 694d7d58..746cc678 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,20 @@ pytest_plugins = "pytester" + +import pytest +import coverage + +import os + +ctx_key = "COVERAGE_CONTEXT" + + +@pytest.fixture(autouse=True) +def coverage_context(request): + ctx = request.node.nodeid + coverage.Coverage.current().switch_context(request.node.nodeid) + + os.environ[ctx_key] = ctx + + yield + + coverage.Coverage.current().switch_context("")