1from abc
import ABC, abstractmethod
2from collections.abc
import ByteString
3from enum
import Enum, auto
4from typing
import Optional
6from .CommentTracker
import CommentTracker
8from .Literals
import LiteralValue
9from .Snapshot
import Snapshot
10from .TypedPath
import TypedPath
11from .WriteTracker
import CallStack, SnapshotFileLayout
15 from .WriteTracker
import CallStack
18 def read_disk(self, sub: str, call: CallStack) -> Optional[Snapshot]:
22 def write_disk(self, actual: Snapshot, sub: str, call: CallStack):
26 def keep(self, sub_or_keep_all: Optional[str]):
33 def fs(self) -> FS: ...
37 def mode(self) -> "Mode": ...
41 def layout(self) -> SnapshotFileLayout: ...
46 Returns true if the sourcecode for the given call has a writable annotation.
51 def write_inline(self, literal_value: LiteralValue, call: CallStack) ->
None:
53 Indicates that the following value should be written into test sourcecode.
59 self, path: TypedPath, data: ByteString, call: CallStack
62 Writes the given bytes to the given file, checking for duplicate writes.
69 Returns the DiskStorage for the test associated with this thread, else error.
78 if selfieSystem
is None:
80 "Selfie system not initialized, make sure that `pytest-selfie` is installed and that you are running tests with `pytest`."
85def _initSelfieSystem(system: SnapshotSystem):
87 if selfieSystem
is not None:
88 raise Exception(
"Selfie system already initialized")
92def _clearSelfieSystem(system: SnapshotSystem):
94 if selfieSystem
is not system:
95 raise Exception(
"This was not the running system!")
104 def can_write(self, is_todo: bool, call: CallStack, system: SnapshotSystem) -> bool:
105 if self == Mode.interactive:
106 return is_todo
or system.source_file_has_writable_comment(call)
107 elif self == Mode.readonly:
108 if system.source_file_has_writable_comment(call):
109 layout = system.layout
110 path = layout.sourcefile_for_call(call.location)
111 comment, line = CommentTracker.commentString(path)
112 raise system.fs.assert_failed(
113 f
"Selfie is in readonly mode, so `{comment}` is illegal at {call.location.with_line(line).ide_link(layout)}"
116 elif self == Mode.overwrite:
119 raise ValueError(f
"Unknown mode: {self}")
122 return self.
msg(
"Snapshot not found")
125 return self.
msg(f
"Snapshot not found: no such file {file}")
129 "Snapshot mismatch (error msg could be better https://github.com/diffplug/selfie/issues/501)"
141 if 33 <= b <= 126
and b != 61:
142 result.append(chr(b))
145 result.append(f
"={b:02X}")
146 return "".join(result)
148 def msg(self, headline: str) -> str:
149 if self == Mode.interactive:
152 "- update this snapshot by adding `_TODO` to the function name\n"
153 "- update all snapshots in this file by adding `#selfieonce` or `#SELFIEWRITE`"
155 elif self == Mode.readonly:
157 elif self == Mode.overwrite:
158 return f
"{headline}\n(didn't expect this to ever happen in overwrite mode)"
160 raise ValueError(f
"Unknown mode: {self}")
keep(self, Optional[str] sub_or_keep_all)
write_disk(self, Snapshot actual, str sub, CallStack call)
Optional[Snapshot] read_disk(self, str sub, CallStack call)
str msg_snapshot_not_found(self)
bool can_write(self, bool is_todo, CallStack call, SnapshotSystem system)
str msg(self, str headline)
str msg_snapshot_mismatch(self, str expected, str actual)
str msg_snapshot_not_found_no_such_file(self, file)
str _to_quoted_printable(self, bytes byte_data)
str msg_snapshot_mismatch_binary(self, bytes expected, bytes actual)
None write_to_be_file(self, TypedPath path, ByteString data, CallStack call)
SnapshotFileLayout layout(self)
DiskStorage disk_thread_local(self)
bool source_file_has_writable_comment(self, CallStack call)
None write_inline(self, LiteralValue literal_value, CallStack call)
"SnapshotSystem" _selfieSystem()