Skip to content

Commit 1bc8478

Browse files
committed
Use exists() instead of parsing JSON for snapshot write-once check
The write-once guard in snapshot_base_manifest() was opening and JSON-parsing the snapshot file just to check if it exists. Use build_media_storage.exists() (S3 HEAD request) instead — cheaper and clearer intent. https://claude.ai/code/session_01FgTyr1d9mpwms8emHGNAQf
1 parent f3b67ea commit 1bc8478

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

readthedocs/filetreediff/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,17 @@ def snapshot_base_manifest(external_version: Version, base_version: Version):
177177
manifests. Same approach could extend to all build artifacts if we move
178178
toward content-addressed storage.
179179
"""
180-
if _get_base_manifest_snapshot(external_version) is not None:
180+
snapshot_path = external_version.get_storage_path(
181+
media_type=MEDIA_TYPE_DIFF,
182+
filename=BASE_MANIFEST_SNAPSHOT_FILE_NAME,
183+
)
184+
if build_media_storage.exists(snapshot_path):
181185
return
182186

183187
base_manifest = get_manifest(base_version)
184188
if not base_manifest:
185189
return
186190

187-
snapshot_path = external_version.get_storage_path(
188-
media_type=MEDIA_TYPE_DIFF,
189-
filename=BASE_MANIFEST_SNAPSHOT_FILE_NAME,
190-
)
191191
with build_media_storage.open(snapshot_path, "w") as f:
192192
json.dump(base_manifest.as_dict(), f)
193193

readthedocs/filetreediff/tests/test_filetreediff.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,23 +234,9 @@ def test_snapshot_prevents_false_positives_from_stale_base(self, storage_open):
234234
assert diff.added == []
235235
assert diff.deleted == []
236236

237+
@mock.patch.object(BuildMediaFileSystemStorageTest, "exists", return_value=True)
237238
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
238-
def test_snapshot_is_write_once(self, storage_open):
239+
def test_snapshot_is_write_once(self, storage_open, storage_exists):
239240
"""snapshot_base_manifest is a no-op if a snapshot already exists."""
240-
existing = json.dumps(
241-
{"build": {"id": 1}, "files": {"index.html": {"main_content_hash": "old"}}}
242-
)
243-
244-
@contextmanager
245-
def mock_existing(*a, **kw):
246-
m = mock.MagicMock()
247-
m.read.return_value = existing
248-
yield m
249-
250-
storage_open.return_value = mock_existing()
251241
snapshot_base_manifest(self.pr_version, self.base_version)
252-
253-
for call in storage_open.call_args_list:
254-
args, _ = call
255-
if len(args) > 1:
256-
assert args[1] != "w", "Should not write when snapshot exists"
242+
storage_open.assert_not_called()

0 commit comments

Comments
 (0)