generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: defer snapshot default extension import (#734)
* test: add coverage for bug #719 * fix: defer snapshot default extension import Fixes unable to use pytest's `pythonpath` option with this project's `--snapshot-default-extension` option. Does cause extension import errors to raise later than CLI argument parsing, and therefore emit on stdout, instead of stderr. --------- Co-authored-by: Noah Negin-Ulster <[email protected]>
- Loading branch information
1 parent
3152c56
commit dfd5910
Showing
4 changed files
with
106 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/integration/test_snapshot_option_extension_pythonpath.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import textwrap | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import syrupy | ||
|
||
SUBDIR = "subdir_not_on_default_path" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def cache_clear(): | ||
syrupy.__import_extension.cache_clear() | ||
|
||
|
||
@pytest.fixture | ||
def testfile(pytester): | ||
subdir = pytester.mkpydir(SUBDIR) | ||
|
||
Path( | ||
subdir, | ||
"extension_file.py", | ||
).write_text( | ||
data=textwrap.dedent( | ||
""" | ||
from syrupy.extensions.single_file import SingleFileSnapshotExtension | ||
class MySingleFileExtension(SingleFileSnapshotExtension): | ||
pass | ||
""" | ||
), | ||
encoding="utf-8", | ||
) | ||
|
||
pytester.makepyfile( | ||
test_file=( | ||
""" | ||
def test_default(snapshot): | ||
assert b"default extension serializer" == snapshot | ||
""" | ||
) | ||
) | ||
|
||
return pytester | ||
|
||
|
||
def test_snapshot_default_extension_option_success(testfile): | ||
testfile.makeini( | ||
f""" | ||
[pytest] | ||
pythonpath = | ||
{Path(testfile.path, SUBDIR).as_posix()} | ||
""" | ||
) | ||
|
||
result = testfile.runpytest( | ||
"-v", | ||
"--snapshot-update", | ||
"--snapshot-default-extension", | ||
"extension_file.MySingleFileExtension", | ||
) | ||
result.stdout.re_match_lines((r"1 snapshot generated\.")) | ||
assert Path( | ||
testfile.path, "__snapshots__", "test_file", "test_default.raw" | ||
).exists() | ||
assert not result.ret | ||
|
||
|
||
def test_snapshot_default_extension_option_module_not_found(testfile): | ||
result = testfile.runpytest( | ||
"-v", | ||
"--snapshot-update", | ||
"--snapshot-default-extension", | ||
"extension_file.MySingleFileExtension", | ||
) | ||
result.stdout.re_match_lines((r".*: Module 'extension_file' does not exist.*",)) | ||
assert not Path( | ||
testfile.path, "__snapshots__", "test_file", "test_default.raw" | ||
).exists() | ||
assert result.ret | ||
|
||
|
||
def test_snapshot_default_extension_option_failure(testfile): | ||
testfile.makeini( | ||
f""" | ||
[pytest] | ||
pythonpath = | ||
{Path(testfile.path, SUBDIR).as_posix()} | ||
""" | ||
) | ||
|
||
result = testfile.runpytest( | ||
"-v", | ||
"--snapshot-update", | ||
"--snapshot-default-extension", | ||
"extension_file.DoesNotExistExtension", | ||
) | ||
result.stdout.re_match_lines((r".*: Member 'DoesNotExistExtension' not found.*",)) | ||
assert not Path( | ||
testfile.path, "__snapshots__", "test_file", "test_default.raw" | ||
).exists() | ||
assert result.ret |