generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid reporting crash for snapshot dir outside pytest dir (#621)
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def testcases(testdir, tmp_path): | ||
dirname = tmp_path.joinpath("__snapshots__") | ||
testdir.makeconftest( | ||
f""" | ||
import pytest | ||
from syrupy.extensions.amber import AmberSnapshotExtension | ||
class CustomSnapshotExtension(AmberSnapshotExtension): | ||
@property | ||
def _dirname(self): | ||
return {str(dirname)!r} | ||
@pytest.fixture | ||
def snapshot(snapshot): | ||
return snapshot.use_extension(CustomSnapshotExtension) | ||
""" | ||
) | ||
return { | ||
"zero": ( | ||
""" | ||
def test_do_it(snapshot): | ||
pass | ||
""" | ||
), | ||
"one": ( | ||
""" | ||
def test_do_it(snapshot): | ||
assert snapshot == 'passed1' | ||
""" | ||
), | ||
"two": ( | ||
""" | ||
def test_do_it(snapshot): | ||
assert snapshot == 'passed1' | ||
assert snapshot == 'passed2' | ||
""" | ||
), | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def generate_snapshots(testdir, testcases): | ||
testdir.makepyfile(test_file=testcases["two"]) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
return result, testdir, testcases | ||
|
||
|
||
def test_generated_snapshots(generate_snapshots): | ||
result = generate_snapshots[0] | ||
result.stdout.re_match_lines((r"2 snapshots generated\.")) | ||
assert "snapshots unused" not in result.stdout.str() | ||
assert result.ret == 0 | ||
|
||
|
||
def test_unmatched_snapshots(generate_snapshots): | ||
_, testdir, testcases = generate_snapshots | ||
testdir.makepyfile(test_file=testcases["one"]) | ||
result = testdir.runpytest("-v") | ||
result.stdout.re_match_lines((r"1 snapshot passed. 1 snapshot unused\.")) | ||
assert result.ret == 1 | ||
|
||
|
||
def test_updated_snapshots_partial_delete(generate_snapshots): | ||
_, testdir, testcases = generate_snapshots | ||
testdir.makepyfile(test_file=testcases["one"]) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
result.stdout.re_match_lines(r"1 snapshot passed. 1 unused snapshot deleted\.") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_updated_snapshots_full_delete(generate_snapshots): | ||
_, testdir, testcases = generate_snapshots | ||
testdir.makepyfile(test_file=testcases["zero"]) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
result.stdout.re_match_lines(r"2 unused snapshots deleted\.") | ||
assert result.ret == 0 |