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

fix: ensure all pytest options are serializable #667

Merged
merged 1 commit into from
Dec 29, 2022
Merged
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
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 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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We attempt to import the option to validate it as early as possible.

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,
)