Skip to content

Commit

Permalink
Merge pull request #84 from 15r10nk/xdist
Browse files Browse the repository at this point in the history
feat: check in inline-snapshot is used in combination with xdist
  • Loading branch information
15r10nk authored May 6, 2024
2 parents 9060935 + 08b2866 commit 340ef3e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
5 changes: 4 additions & 1 deletion inline_snapshot/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def list(self) -> Set[str]:
return set()

def persist(self, name):
file = self._lookup_path(name)
try:
file = self._lookup_path(name)
except HashError:
return
if file.stem.endswith("-new"):
stem = file.stem[:-4]
file.rename(file.with_name(stem + file.suffix))
Expand Down
23 changes: 22 additions & 1 deletion inline_snapshot/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def pytest_addoption(parser):
)


def xdist_running(config):
return (
hasattr(config.option, "numprocesses")
and config.option.numprocesses is not None
)


def pytest_configure(config):
flags = config.option.inline_snapshot.split(",")
flags = [flag for flag in flags if flag]
Expand All @@ -44,7 +51,12 @@ def pytest_configure(config):
f"--inline-snapshot-disable can not be combined with other flags ({','.join(flags)})"
)

_inline_snapshot._active = not config.option.inline_snapshot_disable
if xdist_running(config) and flags:
raise pytest.UsageError(f"inline-snapshot can not be combined with xdist")

_inline_snapshot._active = not (
config.option.inline_snapshot_disable or xdist_running(config)
)

_inline_snapshot._update_flags = _inline_snapshot.Flags(set(flags))

Expand Down Expand Up @@ -116,6 +128,14 @@ def pytest_assertrepr_compare(config, op, left, right):


def pytest_terminal_summary(terminalreporter, exitstatus, config):

if xdist_running(config):
terminalreporter.section("inline snapshot")
terminalreporter.write(
"INFO: inline-snapshot was disabled because you used xdist\n"
)
return

if not _inline_snapshot._active:
return

Expand Down Expand Up @@ -157,6 +177,7 @@ def report(flag, message):
)

if _inline_snapshot._update_flags.change_something():

count = {"create": 0, "fix": 0, "trim": 0, "update": 0}

for snapshot in _inline_snapshot.snapshots.values():
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,29 @@ def test_sub_snapshot():
================================================================== 1 passed, 1 error in <time> ==================================================================
"""
)


def test_persist_unknown_external(project):
project.setup(
"""\
from inline_snapshot import external, snapshot
def test_sub_snapshot():
external("123*.png")
assert 1==snapshot(2)
"""
)

result = project.run("--inline-snapshot=fix")

assert project.source == snapshot(
"""\
from inline_snapshot import external, snapshot
def test_sub_snapshot():
external("123*.png")
assert 1==snapshot(1)
"""
)

assert result.ret == 0
39 changes: 39 additions & 0 deletions tests/test_xdist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from inline_snapshot import snapshot


def test_xdist(project):

project.setup(
"""\
def test_a():
assert 1==snapshot()
"""
)

result = project.run("--inline-snapshot=create", "-n=auto")

assert "\n".join(result.stderr.lines).strip() == snapshot(
"ERROR: inline-snapshot can not be combined with xdist"
)

assert result.ret == 4


def test_xdist_disabled(project):

project.setup(
"""\
def test_a():
assert 1==snapshot(1)
"""
)

result = project.run("-n=auto")

assert result.report == snapshot(
"INFO: inline-snapshot was disabled because you used xdist"
)

assert result.ret == 0

0 comments on commit 340ef3e

Please sign in to comment.