Skip to content

Commit

Permalink
fix: ensure all pytest options are serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Negin-Ulster committed Dec 29, 2022
1 parent 4c25bf2 commit 343e9f5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
38 changes: 37 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ codecov = '^2.1.12'
invoke = '^1.7.3'
coverage = { version = '^6.5.0', extras = ['toml'] }
pytest-benchmark = '^4.0.0'
pytest-xdist = '^3.1.0'

[tool.poetry.group.dev.dependencies]
isort = '^5.10.1'
Expand Down
19 changes: 16 additions & 3 deletions src/syrupy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import sys
from functools import cache
from gettext import gettext
from typing import (
Any,
Expand Down Expand Up @@ -30,13 +31,21 @@
_syrupy: Optional["SnapshotSession"] = None


def __default_extension_option(value: str) -> Any:
@cache
def __import_extension(value: Optional[str]) -> Any:
if not value:
return DEFAULT_EXTENSION
try:
return import_module_member(value)
except FailedToLoadModuleMember as e:
raise argparse.ArgumentTypeError(e)


def __default_extension_option(value: Optional[str]) -> Any:
__import_extension(value)
return value


def pytest_addoption(parser: Any) -> None:
"""
Exposes snapshot plugin configuration to pytest.
Expand Down Expand Up @@ -64,13 +73,17 @@ def pytest_addoption(parser: Any) -> None:
dest="include_snapshot_details",
help="Include details of unused snapshots in the final report",
)

# We lazy evaluate the default extension since pytest-xdist requires
# all pytest options to be serializable.
group.addoption(
"--snapshot-default-extension",
type=__default_extension_option,
default=DEFAULT_EXTENSION,
default=None,
dest="default_extension",
help="Specify the default snapshot extension",
)

group.addoption(
"--snapshot-no-colors",
action="store_true",
Expand Down Expand Up @@ -175,7 +188,7 @@ def pytest_terminal_summary(
def snapshot(request: Any) -> "SnapshotAssertion":
return SnapshotAssertion(
update_snapshots=request.config.option.update_snapshots,
extension_class=request.config.option.default_extension,
extension_class=__import_extension(request.config.option.default_extension),
test_location=PyTestLocation(request.node),
session=request.session.config._syrupy,
)

0 comments on commit 343e9f5

Please sign in to comment.