Skip to content

Commit 643d5d3

Browse files
authored
Added ondisk function: read and write Disk (#368)
2 parents 2a485d0 + 0aecbc2 commit 643d5d3

File tree

10 files changed

+49
-34
lines changed

10 files changed

+49
-34
lines changed

.github/workflows/python-ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ jobs:
4949
working-directory: python/example-pytest-selfie
5050
# - run: poetry run pytest -vv
5151
# working-directory: python/example-pytest-selfie
52-
- name: example-pytest-selfie - pyright
53-
run: poetry run pyright
54-
working-directory: python/example-pytest-selfie
52+
# - name: example-pytest-selfie - pyright
53+
# run: poetry run pyright
54+
# working-directory: python/example-pytest-selfie
5555
- name: example-pytest-selfie - ruff
5656
run: poetry run ruff format --check
5757
working-directory: python/example-pytest-selfie
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from selfie_lib.Selfie import expect_selfie
22

3-
"""
3+
44
def test_write():
55
expect_selfie("A").to_match_disk()
66

77

88
def test_read():
99
expect_selfie("B").to_match_disk_TODO()
10-
"""

python/pytest-selfie/pytest_selfie/plugin.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,25 @@ def __init__(self, progress: "SnapshotFileProgress", testname: str):
255255
self.__progress = progress
256256
self._testname = testname
257257

258-
def read_disk(self, sub: str, call: CallStack) -> Optional[Snapshot]:
259-
raise NotImplementedError()
260-
261-
def write_disk(self, actual: Snapshot, sub: str, call: CallStack):
262-
raise NotImplementedError()
258+
def read_disk(self, sub: str, call: "CallStack") -> Optional["Snapshot"]:
259+
return self.__progress.read(self._testname, self._suffix(sub))
260+
261+
def write_disk(self, actual: "Snapshot", sub: str, call: "CallStack"):
262+
self.__progress.write(
263+
self._testname,
264+
self._suffix(sub),
265+
actual,
266+
call,
267+
self.__progress.system.layout,
268+
)
263269

264270
def keep(self, sub_or_keep_all: Optional[str]):
265-
self.__progress.keep(self._testname, sub_or_keep_all)
271+
self.__progress.keep(
272+
self._testname, self._suffix(sub_or_keep_all) if sub_or_keep_all else None
273+
)
274+
275+
def _suffix(self, sub: str) -> str:
276+
return f"/{sub}" if sub else ""
266277

267278

268279
class SnapshotFileProgress:
@@ -335,11 +346,13 @@ def __all_tests_finished(self):
335346
if tests == SnapshotFileProgress.TERMINATED:
336347
raise ValueError(f"Snapshot for {self.test_file} already terminated!")
337348
if self.file is not None:
338-
stale_snapshot_indices = WithinTestGC.find_stale_snapshots_within(
339-
self.file.snapshots,
340-
tests,
341-
find_test_methods_that_didnt_run(self.test_file, tests),
342-
)
349+
# TODO: figure out GC
350+
stale_snapshot_indices = []
351+
# stale_snapshot_indices = WithinTestGC.find_stale_snapshots_within(
352+
# self.file.snapshots,
353+
# tests,
354+
# find_test_methods_that_didnt_run(self.test_file, tests),
355+
# )
343356
if stale_snapshot_indices or self.file.was_set_at_test_time:
344357
self.file.remove_all_indices(stale_snapshot_indices)
345358
snapshot_path = self.system._layout.snapshotfile_for_testfile(

python/selfie-lib/selfie_lib/ArrayMap.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,9 @@ def plus_or_noop_or_replace(self, key: K, value: V) -> "ArrayMap[K, V]":
231231
new_data.insert(insert_at * 2, key)
232232
new_data.insert(insert_at * 2 + 1, value)
233233
return ArrayMap.__create(new_data)
234+
235+
def __str__(self):
236+
return "{" + ", ".join(f"{k}: {v}" for k, v in self.items()) + "}"
237+
238+
def __repr__(self):
239+
return self.__str__()

python/selfie-lib/selfie_lib/CacheSelfie.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Generic, TypeVar, Optional, Any
2-
import threading
32
from .WriteTracker import recordCall
43
from .Snapshot import Snapshot
54
from .Literals import LiteralValue, LiteralString, TodoStub
@@ -34,7 +33,7 @@ def _to_match_disk_impl(self, sub: str, is_todo: bool) -> T:
3433
Snapshot.of(self.roundtrip.serialize(actual)), sub, call
3534
)
3635
if is_todo:
37-
system.write_inline(TodoStub.toMatchDisk.create_literal(), call)
36+
system.write_inline(TodoStub.to_match_disk.create_literal(), call)
3837
return actual
3938
else:
4039
if is_todo:

python/selfie-lib/selfie_lib/Literals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ def parse(self, string: str, language: Language) -> bool:
266266

267267

268268
class TodoStub(Enum):
269-
toMatchDisk = auto()
270-
toBeFile = auto()
269+
to_match_disk = auto()
270+
to_be_file = auto()
271271

272272
def create_literal(self):
273273
return LiteralValue(None, self, LiteralTodoStub())

python/selfie-lib/selfie_lib/SelfieImplementations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def to_match_disk_TODO(self, sub: str = "") -> "DiskSelfie":
7878
call = recordCall(False)
7979
if _selfieSystem().mode.can_write(True, call, _selfieSystem()):
8080
self.disk.write_disk(self.actual, sub, call)
81-
_selfieSystem().write_inline(TodoStub.toMatchDisk.create_literal(), call)
81+
_selfieSystem().write_inline(TodoStub.to_match_disk.create_literal(), call)
8282
return self
8383
else:
8484
raise _selfieSystem().fs.assert_failed(

python/selfie-lib/selfie_lib/SnapshotFile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
from threading import Lock
3-
from typing import Tuple, List, Optional, Dict
3+
from typing import Tuple, List, Optional
44

55
from .Snapshot import Snapshot, SnapshotValue
66
from .SnapshotReader import SnapshotReader
@@ -67,14 +67,14 @@ def writeEntry(
6767

6868
def set_at_test_time(self, key: str, snapshot: Snapshot) -> None:
6969
with self._lock:
70-
self._snapshots = self._snapshots.plus(key, snapshot)
70+
self.snapshots = self.snapshots.plus_or_noop_or_replace(key, snapshot)
7171
self.was_set_at_test_time = True
7272

7373
def remove_all_indices(self, indices: List[int]) -> None:
7474
with self._lock:
7575
if not indices:
7676
return
77-
self._snapshots = self._snapshots.minus_sorted_indices(indices)
77+
self.snapshots = self.snapshots.minus_sorted_indices(indices)
7878
self.was_set_at_test_time = True
7979

8080
@classmethod

python/selfie-lib/selfie_lib/SourceFile.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,16 @@ def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:
111111
idx = line_content.indexOf(to_find)
112112
if idx == -1:
113113
raise AssertionError(
114-
f"Expected to find `{to_find}` on line {line_one_indexed}, "
115-
f"but there was only `{line_content}`"
114+
f"Expected to find `{to_find}` on line {line_one_indexed}, but there was only `{line_content}`"
116115
)
117-
start_index = idx
118-
end_index = idx + len(to_find)
119-
return line_content.subSequence(start_index, end_index)
116+
return line_content.subSequence(idx, idx + len(to_find))
120117

121118
def replace_on_line(self, line_one_indexed: int, find: str, replace: str) -> None:
122119
assert "\n" not in find
123120
assert "\n" not in replace
124-
line_content = self._content_slice.unixLine(line_one_indexed).__str__()
125-
new_content = line_content.replace(find, replace)
126-
self._content_slice = Slice(self._content_slice.replaceSelfWith(new_content))
121+
122+
slice = self.find_on_line(find, line_one_indexed)
123+
self._content_slice = Slice(slice.replaceSelfWith(replace))
127124

128125
def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
129126
line_content = self._content_slice.unixLine(line_one_indexed)

python/selfie-lib/selfie_lib/WriteTracker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from functools import total_ordering
88

99
from .SourceFile import SourceFile
10-
from .Literals import LiteralValue, LiteralTodoStub
10+
from .Literals import LiteralValue, LiteralTodoStub, TodoStub
1111
from .TypedPath import TypedPath
1212
from .FS import FS
1313

@@ -227,7 +227,8 @@ def persist_writes(self, layout: SnapshotFileLayout):
227227
# Calculate the line number taking into account changes that shifted line numbers
228228
line = write.call_stack.location.line + delta_line_numbers
229229
if isinstance(write.snapshot.format, LiteralTodoStub):
230-
content.replace_on_line(line, ".${kind.name}_TODO(", ".${kind.name}(")
230+
kind: TodoStub = write.snapshot.actual # type: ignore
231+
content.replace_on_line(line, f".{kind.name}_TODO(", f".{kind.name}(")
231232
else:
232233
to_be_literal = content.parse_to_be_like(line)
233234
# Attempt to set the literal value and adjust for line shifts due to content changes

0 commit comments

Comments
 (0)