Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutmut #21

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions mutmut_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os.path


def pre_mutation(context):
line = context.current_source_line.strip()
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)
23 changes: 23 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ def docs(session):
session.install("poetry")
session.run("poetry", "install", "--with=doc")
session.run("mkdocs", "build", *session.posargs)


@nox.session(python="python3.10")
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")
85 changes: 84 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", "\\.\\.\\."]

Expand Down Expand Up @@ -51,6 +54,7 @@ python = "^3.7"
coverage = "^7.2.3"
coverage-enable-subprocess = "^1.0"
hypothesis = "^6.75.5"
mutmut = "^2.4.3"
mypy = "^1.2.0"
pytest = "^7.1"
pytest-subtests = "^0.11.0"
Expand Down
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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("")
65 changes: 35 additions & 30 deletions tests/test_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,83 +255,83 @@ def test_mutable_values(check_update):
assert (
check_update(
"""
l=[1,2]
l=[1,[2]]
assert l==snapshot()
l.append(3)
l[1].append(4)
assert l==snapshot()
""",
flags="create",
number=2,
)
== snapshot(
"""
l=[1,2]
assert l==snapshot([1, 2])
l.append(3)
assert l==snapshot([1, 2, 3])
l=[1,[2]]
assert l==snapshot([1, [2]])
l[1].append(4)
assert l==snapshot([1, [2, 4]])
"""
)
)

assert (
check_update(
"""
l=[1,2]
l=[1,[2]]
assert l<=snapshot()
l.append(3)
l[1].append(4)
assert l<=snapshot()
""",
flags="create",
number=2,
)
== snapshot(
"""
l=[1,2]
assert l<=snapshot([1, 2])
l.append(3)
assert l<=snapshot([1, 2, 3])
l=[1,[2]]
assert l<=snapshot([1, [2]])
l[1].append(4)
assert l<=snapshot([1, [2, 4]])
"""
)
)

assert (
check_update(
"""
l=[1,2]
l=[1,[2]]
assert l>=snapshot()
l.append(3)
l[1].append(4)
assert l>=snapshot()
""",
flags="create",
number=2,
)
== snapshot(
"""
l=[1,2]
assert l>=snapshot([1, 2])
l.append(3)
assert l>=snapshot([1, 2, 3])
l=[1,[2]]
assert l>=snapshot([1, [2]])
l[1].append(4)
assert l>=snapshot([1, [2, 4]])
"""
)
)

assert (
check_update(
"""
l=[1,2]
l=[1,[2]]
assert l in snapshot()
l.append(3)
l[1].append(4)
assert l in snapshot()
""",
flags="create",
number=2,
)
== snapshot(
"""
l=[1,2]
assert l in snapshot([[1, 2]])
l.append(3)
assert l in snapshot([[1, 2, 3]])
l=[1,[2]]
assert l in snapshot([[1, [2]]])
l[1].append(4)
assert l in snapshot([[1, [2, 4]]])
"""
)
)
Expand Down Expand Up @@ -661,12 +661,17 @@ def test_plain(check_update):
def test_string_update(check_update):
# black --preview wraps strings to keep the line length.
# string concatenation should produce updates.
assert (
check_update(
'assert "ab" == snapshot("a" "b")', reported_flags="", flags="update"
)
== 'assert "ab" == snapshot("a" "b")'
)

for prefix in ("", "b"):
for quote in "'\"":
stmt = f'assert {prefix}"ab" == snapshot({prefix}{quote}a{quote} {prefix}{quote}b{quote})'

assert check_update(stmt, reported_flags="", flags="update") == stmt

stmt = f'assert {prefix}"ab" == snapshot({prefix}{quote*3}a{quote*3} {prefix}{quote*3}b{quote*3})'
result_stmt = f'assert {prefix}"ab" == snapshot({prefix}"ab")'

assert check_update(stmt, flags="update") == result_stmt

assert (
check_update(
Expand Down