Skip to content

Commit 22d197d

Browse files
fix: improve test setup and fix import sorting
Co-Authored-By: [email protected] <[email protected]>
1 parent b3bac6f commit 22d197d

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
3+
os.environ["SELFIE"] = "readonly"
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
1-
import os
21
import base64
2+
import os
3+
34
import pytest
45
from selfie_lib import expect_selfie
56

6-
@pytest.fixture(autouse=True)
7-
def setup_readonly_mode():
8-
"""Set up readonly mode for all tests in this module"""
9-
old_value = os.environ.get("SELFIE")
10-
os.environ["SELFIE"] = "readonly"
11-
yield
12-
if old_value is not None:
13-
os.environ["SELFIE"] = old_value
14-
else:
15-
del os.environ["SELFIE"]
167

178
def test_empty_binary_base64():
189
"""Test base64 encoding of empty byte array"""
1910
expect_selfie(bytes()).to_be_base64("")
2011

12+
2113
def test_large_binary_base64():
2214
"""Test base64 encoding of large byte array (256 bytes)"""
2315
data = bytes(range(256))
2416
expect_selfie(data).to_be_base64(
2517
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="
2618
)
2719

20+
2821
def test_binary_file():
2922
"""Test writing binary data to a file"""
3023
data = b"test binary data"
3124
expect_selfie(data).to_be_file("test_binary.bin")
3225

26+
3327
def test_binary_file_duplicate():
3428
"""Test writing same binary data to a file multiple times"""
3529
data = b"same data"
3630
expect_selfie(data).to_be_file("duplicate.bin")
3731
expect_selfie(data).to_be_file("duplicate.bin")
3832

33+
3934
def test_binary_file_mismatch():
4035
"""Test error handling for mismatched binary data"""
4136
with pytest.raises(AssertionError):
4237
expect_selfie(b"different").to_be_file("test_binary.bin")
4338

39+
4440
def test_binary_file_not_found():
4541
"""Test error handling for non-existent file"""
4642
with pytest.raises(AssertionError) as exc_info:
4743
expect_selfie(b"test").to_be_file("nonexistent.bin")
4844
assert "no such file" in str(exc_info.value)
4945

46+
5047
def test_base64_mismatch():
5148
"""Test error handling for mismatched base64 data"""
5249
data = b"test data"
5350
encoded = base64.b64encode(b"different data").decode()
5451
with pytest.raises(AssertionError):
5552
expect_selfie(data).to_be_base64(encoded)
5653

54+
5755
def test_readonly_mode_todo():
5856
"""Test error handling in readonly mode for TODO methods"""
59-
with pytest.raises(AssertionError) as exc_info:
60-
expect_selfie(b"test").to_be_file("test.bin")
61-
assert "readonly mode" in str(exc_info.value).lower()
57+
from selfie_lib import _selfieSystem
6258

59+
print(f"Current mode: {_selfieSystem().mode}")
60+
print(f"SELFIE env var: {os.getenv('SELFIE')}")
6361
with pytest.raises(AssertionError) as exc_info:
6462
expect_selfie(b"test").to_be_base64_TODO()
6563
assert "readonly mode" in str(exc_info.value).lower()

python/selfie-lib/selfie_lib/SelfieImplementations.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def to_match_disk_TODO(self, sub: str = "") -> "DiskSelfie":
8181
return self
8282
else:
8383
raise _selfieSystem().fs.assert_failed(
84-
f"Can't call `toMatchDisk_TODO` in {Mode.readonly} mode!"
84+
message=f"Can't call `toMatchDisk_TODO` in {Mode.readonly} mode!"
8585
)
8686

8787
def facet(self, facet: str) -> "StringFacet":
@@ -199,43 +199,66 @@ def to_be_base64(self, expected: str) -> bytes:
199199
def to_be_base64_TODO(self, _: Any = None) -> bytes:
200200
call = recordCall(False)
201201
if not _selfieSystem().mode.can_write(True, call, _selfieSystem()):
202-
raise AssertionError(f"Can't call `to_be_base64_TODO` in {Mode.readonly} mode!")
203-
actual_bytes = self.actual.subject_or_facet(self.only_facet).value_binary()
204-
actual_b64 = base64.b64encode(actual_bytes).decode().replace("\r", "")
205-
_toBeDidntMatch(None, actual_b64, LiteralString())
206-
return actual_bytes
202+
raise _selfieSystem().fs.assert_failed(
203+
message=f"Can't call `to_be_base64_TODO` in {Mode.readonly} mode!"
204+
)
205+
return self._actual_bytes()
206+
207+
def _actual_bytes(self) -> bytes:
208+
return self.actual.subject_or_facet(self.only_facet).value_binary()
209+
210+
def _actual_string(self) -> str:
211+
return base64.b64encode(self._actual_bytes()).decode().replace('\r', '')
207212

208213
def to_be_file_impl(self, subpath: str, is_todo: bool) -> bytes:
209214
call = recordCall(False)
210215
actual_bytes = self.actual.subject_or_facet(self.only_facet).value_binary()
211216
writable = _selfieSystem().mode.can_write(is_todo, call, _selfieSystem())
212217
if is_todo and not writable:
213-
raise AssertionError(f"Can't call `to_be_file_TODO` in {Mode.readonly} mode!")
218+
raise _selfieSystem().fs.assert_failed(
219+
message=f"Can't call `to_be_file_TODO` in {Mode.readonly} mode!"
220+
)
214221
if not writable:
215-
path = _selfieSystem().layout.sourcefile_for_call(call.location).parent_folder().resolve_file(subpath)
222+
path = (
223+
_selfieSystem()
224+
.layout.sourcefile_for_call(call.location)
225+
.parent_folder()
226+
.resolve_file(subpath)
227+
)
216228
if not _selfieSystem().fs.file_exists(path):
217229
raise _selfieSystem().fs.assert_failed(
218-
_selfieSystem().mode.msg_snapshot_not_found_no_such_file(path)
230+
message=_selfieSystem().mode.msg_snapshot_not_found_no_such_file(
231+
path
232+
)
219233
)
220234
expected = _selfieSystem().fs.file_read_binary(path)
221235
if expected == actual_bytes:
222236
return actual_bytes
223237
else:
224238
raise _selfieSystem().fs.assert_failed(
225-
_selfieSystem().mode.msg_snapshot_mismatch(expected, actual_bytes),
226-
expected,
227-
actual_bytes
239+
message=_selfieSystem().mode.msg_snapshot_mismatch(),
240+
expected=expected,
241+
actual=actual_bytes,
228242
)
229243
else:
230244
if is_todo:
231245
_selfieSystem().write_inline(TodoStub.to_be_file.create_literal(), call)
232-
_selfieSystem().write_to_be_file(_selfieSystem().layout.sourcefile_for_call(call.location).parent_folder().resolve_file(subpath), actual_bytes, call)
246+
_selfieSystem().write_to_be_file(
247+
_selfieSystem()
248+
.layout.sourcefile_for_call(call.location)
249+
.parent_folder()
250+
.resolve_file(subpath),
251+
actual_bytes,
252+
call,
253+
)
233254
return actual_bytes
234255

235256
def to_be_file_TODO(self, subpath: str) -> bytes:
236257
call = recordCall(False)
237258
if not _selfieSystem().mode.can_write(True, call, _selfieSystem()):
238-
raise AssertionError(f"Can't call `to_be_file_TODO` in {Mode.readonly} mode!")
259+
raise _selfieSystem().fs.assert_failed(
260+
message=f"Can't call `to_be_file_TODO` in {Mode.readonly} mode!"
261+
)
239262
return self.to_be_file_impl(subpath, True)
240263

241264
def to_be_file(self, subpath: str) -> bytes:
@@ -256,19 +279,21 @@ def _toBeDidntMatch(expected: Optional[T], actual: T, fmt: LiteralFormat[T]) ->
256279
else:
257280
if expected is None:
258281
raise _selfieSystem().fs.assert_failed(
259-
f"Can't call `toBe_TODO` in {Mode.readonly} mode!"
282+
message=f"Can't call `toBe_TODO` in {Mode.readonly} mode!"
260283
)
261284
else:
262285
raise _selfieSystem().fs.assert_failed(
263-
_selfieSystem().mode.msg_snapshot_mismatch(), expected, actual
286+
message=_selfieSystem().mode.msg_snapshot_mismatch(),
287+
expected=expected,
288+
actual=actual,
264289
)
265290

266291

267292
def _assertEqual(
268293
expected: Optional[Snapshot], actual: Snapshot, storage: SnapshotSystem
269294
):
270295
if expected is None:
271-
raise storage.fs.assert_failed(storage.mode.msg_snapshot_not_found())
296+
raise storage.fs.assert_failed(message=storage.mode.msg_snapshot_not_found())
272297
elif expected == actual:
273298
return
274299
else:
@@ -284,9 +309,9 @@ def _assertEqual(
284309
)
285310
)
286311
raise storage.fs.assert_failed(
287-
storage.mode.msg_snapshot_mismatch(),
288-
_serializeOnlyFacets(expected, mismatched_keys),
289-
_serializeOnlyFacets(actual, mismatched_keys),
312+
message=storage.mode.msg_snapshot_mismatch(),
313+
expected=_serializeOnlyFacets(expected, mismatched_keys),
314+
actual=_serializeOnlyFacets(actual, mismatched_keys),
290315
)
291316

292317

0 commit comments

Comments
 (0)