Skip to content

Commit 16e68cc

Browse files
committed
feat(test-snapshot): the selector function can signal the monitor it should ignore a particular snapshot of the state by returning None
1 parent e1eaaef commit 16e68cc

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.15.8
4+
5+
- feat(test-snapshot): the `selector` function can signal the `monitor` it should
6+
ignore a particular snapshot of the state by returning `None`
7+
38
## Version 0.15.7
49

510
- refactor(test-snapshot): make it aligned with `pyfakefs` by using `try`/`except`

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.15.7"
3+
version = "0.15.8"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <[email protected]>"]
66
license = "Apache-2.0"

redux_pytest/fixtures/snapshot.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
from collections import defaultdict
9+
from distutils.util import strtobool
910
from pathlib import Path
1011
from typing import TYPE_CHECKING, Any, Callable, Generic, cast
1112

@@ -97,9 +98,9 @@ def take(
9798
if self.override:
9899
json_path.write_text(f'// {filename}\n{new_snapshot}\n') # pragma: no cover
99100
else:
100-
try:
101+
if json_path.exists():
101102
old_snapshot = json_path.read_text().split('\n', 1)[1][:-1]
102-
except Exception: # noqa: BLE001
103+
else:
103104
old_snapshot = None
104105
if old_snapshot != new_snapshot:
105106
self._is_failed = True
@@ -114,7 +115,9 @@ def monitor(self: StoreSnapshot[State], selector: Callable[[State], Any]) -> Non
114115
"""Monitor the state of the store and take snapshots."""
115116

116117
@self.store.autorun(selector=selector)
117-
def _(state: State) -> None:
118+
def _(state: object | None) -> None:
119+
if state is None:
120+
return
118121
self.take(selector=lambda _: state)
119122

120123
def close(self: StoreSnapshot[State]) -> None:
@@ -137,7 +140,8 @@ def store_snapshot(request: SubRequest, store: Store) -> StoreSnapshot:
137140
'--override-store-snapshots',
138141
default=cast(
139142
Any,
140-
os.environ.get('REDUX_TEST_OVERRIDE_SNAPSHOTS', '0') == '1',
143+
strtobool(os.environ.get('REDUX_TEST_OVERRIDE_SNAPSHOTS', 'false'))
144+
== 1,
141145
),
142146
)
143147
is True

redux_pytest/fixtures/wait_for.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,26 @@ def __call__( # noqa: PLR0913
127127

128128
args['wait'] = wait or wait_exponential(multiplier=0.5)
129129

130+
if run_async:
131+
132+
def async_decorator(check: Callable[[], None]) -> AsyncWaiter:
133+
async def async_wrapper() -> None:
134+
async for attempt in AsyncRetrying(**args):
135+
with attempt:
136+
check()
137+
138+
return async_wrapper
139+
140+
return async_decorator(check) if check else async_decorator
141+
130142
def decorator(check: Callable[[], None]) -> Waiter:
131143
@retry(**args)
132144
def wrapper() -> None:
133145
check()
134146

135147
return wrapper
136148

137-
def async_decorator(check: Callable[[], None]) -> AsyncWaiter:
138-
async def async_wrapper() -> None:
139-
async for attempt in AsyncRetrying(**args):
140-
with attempt:
141-
check()
142-
143-
return async_wrapper
144-
145-
if check:
146-
return async_decorator(check) if run_async else decorator(check)
147-
148-
return async_decorator if run_async else decorator
149+
return decorator(check) if check else decorator
149150

150151

151152
@pytest.fixture()

0 commit comments

Comments
 (0)