Skip to content

Commit

Permalink
fix: ensure all pytest options are serializable (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu authored and Noah Negin-Ulster committed Dec 30, 2022
1 parent 23cca84 commit e320d7b
Showing 1 changed file with 16 additions and 3 deletions.
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 lru_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:
@lru_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 e320d7b

Please sign in to comment.