Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit ba8329a

Browse files
authored
InlineWriteTracker (#45)
2 parents e6c5905 + 4d6d4c4 commit ba8329a

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

python/selfie-lib/selfie_lib/CommentTracker.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
from typing import Dict, Iterable, Tuple
1+
from typing import Dict, Iterable, Tuple, Sequence, TypeVar, Callable
2+
from abc import ABC, abstractmethod
23
from enum import Enum, auto
34
import threading
4-
from selfie_lib.TypedPath import TypedPath
5-
from selfie_lib.Slice import Slice
6-
7-
8-
# Placeholder implementations for CallStack, SnapshotFileLayout, and FS
9-
class CallStack:
10-
pass
115

12-
13-
class SnapshotFileLayout:
14-
def sourcePathForCall(self, location) -> "TypedPath":
15-
# Placeholder return or raise NotImplementedError
16-
raise NotImplementedError("sourcePathForCall is not implemented")
6+
from selfie_lib.Slice import Slice
7+
from selfie_lib.TypedPath import TypedPath
8+
from selfie_lib.WriteTracker import CallStack, SnapshotFileLayout
179

1810

1911
class WritableComment(Enum):

python/selfie-lib/selfie_lib/WriteTracker.py

+69-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
1-
from typing import List, Optional, Generic, TypeVar, Dict
2-
from selfie_lib.CommentTracker import SnapshotFileLayout
1+
from typing import List, Optional, Generic, TypeVar, Dict, cast, Callable, Sequence
32
from abc import ABC, abstractmethod
43
import inspect, threading
54
from functools import total_ordering
65

6+
from selfie_lib.SourceFile import SourceFile
7+
from selfie_lib.Literals import LiteralValue
8+
from selfie_lib.TypedPath import TypedPath
9+
10+
711
T = TypeVar("T")
812
U = TypeVar("U")
913

1014

15+
class FS(ABC):
16+
@abstractmethod
17+
def file_walk(self, typed_path, walk: Callable[[Sequence["TypedPath"]], T]) -> T:
18+
pass
19+
20+
def file_read(self, typed_path) -> str:
21+
return self.file_read_binary(typed_path).decode()
22+
23+
def file_write(self, typed_path, content: str):
24+
self.file_write_binary(typed_path, content.encode())
25+
26+
@abstractmethod
27+
def file_read_binary(self, typed_path) -> bytes:
28+
pass
29+
30+
@abstractmethod
31+
def file_write_binary(self, typed_path, content: bytes):
32+
pass
33+
34+
@abstractmethod
35+
def assert_failed(self, message: str, expected=None, actual=None) -> Exception:
36+
pass
37+
38+
39+
class SnapshotFileLayout:
40+
def __init__(self, fs: FS):
41+
self.fs = fs
42+
43+
def sourcePathForCall(self, location) -> "TypedPath":
44+
raise NotImplementedError("sourcePathForCall is not implemented")
45+
46+
1147
@total_ordering
1248
class CallLocation:
1349
def __init__(self, file_name: Optional[str], line: int):
@@ -132,3 +168,34 @@ def recordInternal(
132168
class DiskWriteTracker(WriteTracker[T, U]):
133169
def record(self, key: T, snapshot: U, call: CallStack, layout: SnapshotFileLayout):
134170
super().recordInternal(key, snapshot, call, layout)
171+
172+
173+
class InlineWriteTracker(WriteTracker[CallLocation, LiteralValue]):
174+
def record(
175+
self,
176+
key: CallLocation,
177+
snapshot: LiteralValue,
178+
call: CallStack,
179+
layout: SnapshotFileLayout,
180+
):
181+
super().recordInternal(key, snapshot, call, layout)
182+
183+
file = layout.sourcePathForCall(key)
184+
if snapshot.expected is not None:
185+
content = SourceFile(file.name, layout.fs.file_read(file))
186+
try:
187+
snapshot = cast(LiteralValue, snapshot)
188+
parsed_value = content.parse_to_be_like(key.line).parse_literal(
189+
snapshot.format
190+
)
191+
except Exception as e:
192+
raise AssertionError(
193+
f"Error while parsing the literal at {key.ide_link(layout)}. Please report this error at https://github.com/diffplug/selfie",
194+
e,
195+
)
196+
if parsed_value != snapshot.expected:
197+
raise layout.fs.assert_failed(
198+
f"Selfie cannot modify the literal at {key.ide_link(layout)} because Selfie has a parsing bug. Please report this error at https://github.com/diffplug/selfie",
199+
snapshot.expected,
200+
parsed_value,
201+
)

0 commit comments

Comments
 (0)