Skip to content

Commit

Permalink
refactor(test-snapshot): make it aligned with pyfakefs by using `tr…
Browse files Browse the repository at this point in the history
…y`/`except` instead of checking `Path().exists()` as `pyfakefs` doesn't seem to respect `skip_names` for `Path().exists()`

relevant: pytest-dev/pyfakefs#1021 (comment)
  • Loading branch information
sassanh committed May 27, 2024
1 parent b7e020f commit e1eaaef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Version 0.15.7

- refactor(test-snapshot): make it aligned with `pyfakefs` by using `try`/`except`
instead of checking `Path().exists()` as `pyfakefs` doesn't seem to respect `skip_names`
for `Path().exists()`

## Version 0.15.5

- feat(test-snapshot): while still taking snapshots of the whole state of the
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-redux"
version = "0.15.5"
version = "0.15.7"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <sassanh@gmail.com>"]
license = "Apache-2.0"
Expand Down
12 changes: 7 additions & 5 deletions redux_pytest/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import pytest

pytest.register_assert_rewrite('redux_pytest.fixtures.event_loop')
pytest.register_assert_rewrite('redux_pytest.fixtures.monitor')
pytest.register_assert_rewrite('redux_pytest.fixtures.snapshot')
pytest.register_assert_rewrite('redux_pytest.fixtures.store')
pytest.register_assert_rewrite('redux_pytest.fixtures.wait_for')
pytest.register_assert_rewrite(
'redux_pytest.fixtures.event_loop',
'redux_pytest.fixtures.monitor',
'redux_pytest.fixtures.snapshot',
'redux_pytest.fixtures.store',
'redux_pytest.fixtures.wait_for',
)

from .event_loop import LoopThread, event_loop # noqa: E402
from .monitor import StoreMonitor, store_monitor # noqa: E402
Expand Down
9 changes: 6 additions & 3 deletions redux_pytest/fixtures/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ def take(
)
raise RuntimeError(msg)

from pathlib import Path

filename = self.get_filename(title)
path = self.results_dir / filename
path = Path(self.results_dir / filename)
json_path = path.with_suffix('.jsonc')
mismatch_path = path.with_suffix('.mismatch.jsonc')

new_snapshot = self.json_snapshot(selector=selector)
if self.override:
json_path.write_text(f'// {filename}\n{new_snapshot}\n') # pragma: no cover
else:
old_snapshot = None
if json_path.exists():
try:
old_snapshot = json_path.read_text().split('\n', 1)[1][:-1]
except Exception: # noqa: BLE001
old_snapshot = None
if old_snapshot != new_snapshot:
self._is_failed = True
mismatch_path.write_text( # pragma: no cover
Expand Down

0 comments on commit e1eaaef

Please sign in to comment.