Selfie
Loading...
Searching...
No Matches
pytest_selfie.plugin.PytestSnapshotSystem Class Reference
Inheritance diagram for pytest_selfie.plugin.PytestSnapshotSystem:
selfie_lib.SnapshotSystem.SnapshotSystem

Public Member Functions

 __init__ (self, SelfieSettingsAPI settings)
 
 planning_to_run (self, TypedPath testfile, str testname)
 
 mark_path_as_written (self, TypedPath path)
 
 test_start (self, TypedPath testfile, str testname)
 
 test_failed (self, TypedPath testfile, str testname)
 
 test_finish (self, TypedPath testfile, str testname)
 
 finished_all_tests (self)
 
Mode mode (self)
 
FS fs (self)
 
SnapshotFileLayout layout (self)
 
DiskStorage disk_thread_local (self)
 
bool source_file_has_writable_comment (self, CallStack call)
 
 write_inline (self, LiteralValue literal_value, CallStack call)
 
None write_to_be_file (self, TypedPath path, "ByteString" data, CallStack call)
 

Public Attributes

 layout_pytest
 
 layout
 

Detailed Description

Definition at line 150 of file plugin.py.

Constructor & Destructor Documentation

◆ __init__()

pytest_selfie.plugin.PytestSnapshotSystem.__init__ (   self,
SelfieSettingsAPI  settings 
)

Definition at line 151 of file plugin.py.

151 def __init__(self, settings: SelfieSettingsAPI):
152 self.__fs = FSImplementation()
153 self.__mode = settings.calc_mode()
154 self.layout_pytest = PytestSnapshotFileLayout(self.__fs, settings)
155 self.__comment_tracker = CommentTracker()
156 self.__inline_write_tracker = InlineWriteTracker()
157 self.__toBeFileWriteTracker = ToBeFileWriteTracker()
158
159 self.__progress_per_file: defaultdict[TypedPath, SnapshotFileProgress] = (
160 _keydefaultdict(lambda key: SnapshotFileProgress(self, key)) # type: ignore
161 ) # type: ignore
162 # the test which is running right now, if any
163 self.__in_progress: Optional[SnapshotFileProgress] = None
164 # double-checks that we don't have any tests in progress
165 self.check_for_invalid_state: AtomicReference[Optional[ArraySet[TypedPath]]] = (
166 AtomicReference(ArraySet.empty())
167 )
168

Member Function Documentation

◆ disk_thread_local()

DiskStorage pytest_selfie.plugin.PytestSnapshotSystem.disk_thread_local (   self)
Returns the DiskStorage for the test associated with this thread, else error.

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 236 of file plugin.py.

236 def disk_thread_local(self) -> DiskStorage:
237 if (
238 self.__in_progress is None
239 or self.__in_progress.testname_in_progress is None
240 ):
241 raise RuntimeError("No test in progress")
242 return DiskStoragePytest(
243 self.__in_progress, self.__in_progress.testname_in_progress
244 )
245

◆ finished_all_tests()

pytest_selfie.plugin.PytestSnapshotSystem.finished_all_tests (   self)

Definition at line 208 of file plugin.py.

208 def finished_all_tests(self):
209 snapshotsFilesWrittenToDisk = self.check_for_invalid_state.get_and_update(
210 lambda _: None
211 )
212 if snapshotsFilesWrittenToDisk is None:
213 raise RuntimeError("finished_all_tests() was called more than once.")
214
215 if self.mode != Mode.readonly:
216 if self.__inline_write_tracker.hasWrites():
217 self.__inline_write_tracker.persist_writes(self.layout)
218
219 for path in self.__comment_tracker.paths_with_once():
220 source = SourceFile(path.name, self.fs.file_read(path))
221 source.remove_selfie_once_comments()
222 self.fs.file_write(path, source.as_string)
223

◆ fs()

FS pytest_selfie.plugin.PytestSnapshotSystem.fs (   self)

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 229 of file plugin.py.

229 def fs(self) -> FS:
230 return self.__fs
231

◆ layout()

SnapshotFileLayout pytest_selfie.plugin.PytestSnapshotSystem.layout (   self)

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 233 of file plugin.py.

233 def layout(self) -> SnapshotFileLayout:
234 return self.layout_pytest
235

◆ mark_path_as_written()

pytest_selfie.plugin.PytestSnapshotSystem.mark_path_as_written (   self,
TypedPath  path 
)

Definition at line 173 of file plugin.py.

173 def mark_path_as_written(self, path: TypedPath):
174 def update_fun(arg: Optional[ArraySet[TypedPath]]):
175 if arg is None:
176 raise RuntimeError(
177 "Snapshot file is being written after all tests were finished."
178 )
179 return arg.plusOrThis(path)
180
181 self.check_for_invalid_state.update_and_get(update_fun)
182

◆ mode()

Mode pytest_selfie.plugin.PytestSnapshotSystem.mode (   self)

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 225 of file plugin.py.

225 def mode(self) -> Mode:
226 return self.__mode
227

◆ planning_to_run()

pytest_selfie.plugin.PytestSnapshotSystem.planning_to_run (   self,
TypedPath  testfile,
str  testname 
)

Definition at line 169 of file plugin.py.

169 def planning_to_run(self, testfile: TypedPath, testname: str): # noqa: ARG002
170 progress = self.__progress_per_file[testfile]
171 progress.finishes_expected += 1
172

◆ source_file_has_writable_comment()

bool pytest_selfie.plugin.PytestSnapshotSystem.source_file_has_writable_comment (   self,
CallStack  call 
)
Returns true if the sourcecode for the given call has a writable annotation.

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 246 of file plugin.py.

246 def source_file_has_writable_comment(self, call: CallStack) -> bool:
247 return self.__comment_tracker.hasWritableComment(call, self.layout)
248

◆ test_failed()

pytest_selfie.plugin.PytestSnapshotSystem.test_failed (   self,
TypedPath  testfile,
str  testname 
)

Definition at line 191 of file plugin.py.

191 def test_failed(self, testfile: TypedPath, testname: str):
192 self.__assert_inprogress(testfile)
193 self.__in_progress.test_failed(testname) # type: ignore
194

◆ test_finish()

pytest_selfie.plugin.PytestSnapshotSystem.test_finish (   self,
TypedPath  testfile,
str  testname 
)

Definition at line 195 of file plugin.py.

195 def test_finish(self, testfile: TypedPath, testname: str):
196 self.__assert_inprogress(testfile)
197 self.__in_progress.test_finish(testname) # type: ignore
198 self.__in_progress = None
199

◆ test_start()

pytest_selfie.plugin.PytestSnapshotSystem.test_start (   self,
TypedPath  testfile,
str  testname 
)

Definition at line 183 of file plugin.py.

183 def test_start(self, testfile: TypedPath, testname: str):
184 if self.__in_progress:
185 raise RuntimeError(
186 f"Test already in progress. {self.__in_progress.test_file} is running, can't start {testfile}"
187 )
188 self.__in_progress = self.__progress_per_file[testfile]
189 self.__in_progress.test_start(testname)
190

◆ write_inline()

pytest_selfie.plugin.PytestSnapshotSystem.write_inline (   self,
LiteralValue  literal_value,
CallStack  call 
)
Indicates that the following value should be written into test sourcecode.

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 249 of file plugin.py.

249 def write_inline(self, literal_value: LiteralValue, call: CallStack):
250 self.__inline_write_tracker.record(literal_value, call, self.layout)
251

◆ write_to_be_file()

None pytest_selfie.plugin.PytestSnapshotSystem.write_to_be_file (   self,
TypedPath  path,
"ByteString"  data,
CallStack   call 
)
Writes the given bytes to the given file, checking for duplicate writes.

Reimplemented from selfie_lib.SnapshotSystem.SnapshotSystem.

Definition at line 252 of file plugin.py.

254 ) -> None:
255 # Directly write to disk using ToBeFileWriteTracker
256 self.__toBeFileWriteTracker.writeToDisk(
257 path, bytes(data), call, self.layout_pytest
258 )
259
260

Member Data Documentation

◆ layout

pytest_selfie.plugin.PytestSnapshotSystem.layout

Definition at line 217 of file plugin.py.

◆ layout_pytest

pytest_selfie.plugin.PytestSnapshotSystem.layout_pytest

Definition at line 154 of file plugin.py.


The documentation for this class was generated from the following file: