Skip to content

Commit e8581cc

Browse files
committed
refactor(update): ensure temp directory cleared on failure
1 parent 70264b8 commit e8581cc

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/update.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,17 @@ def run_update(strategy: str):
177177

178178
prepare_temp_dir()
179179

180-
download_url = get_download_url()
181-
_ensure_disk_space(download_url, parallel=strategy == "PARALLEL")
180+
try:
181+
download_url = get_download_url()
182+
_ensure_disk_space(download_url, parallel=strategy == "PARALLEL")
182183

183-
index_file = _download_verified_index()
184+
index_file = _download_verified_index()
184185

185-
extract_index(index_file)
186+
extract_index(index_file)
186187

187-
logging.info("Activating new index")
188-
index.activate(os.path.join(config.TEMP_DIR, "photon_data"))
189-
clear_temp_dir()
188+
logging.info("Activating new index")
189+
index.activate(os.path.join(config.TEMP_DIR, "photon_data"))
190190

191-
logging.info("Update pipeline completed successfully.")
191+
logging.info("Update pipeline completed successfully.")
192+
finally:
193+
clear_temp_dir()

tests/test_update.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,28 @@ def boom():
321321
update.run_update("SEQUENTIAL")
322322

323323

324+
def test_run_update_clears_temp_dir_on_failure(fake_dirs: Path, monkeypatch: pytest.MonkeyPatch):
325+
monkeypatch.setattr(config, "SKIP_MD5_CHECK", True)
326+
_make_pipeline_patches(monkeypatch)
327+
328+
cleared = {"n": 0}
329+
330+
def fake_clear():
331+
cleared["n"] += 1
332+
333+
monkeypatch.setattr(update, "clear_temp_dir", fake_clear)
334+
335+
def boom():
336+
raise update.DownloadError("download died")
337+
338+
monkeypatch.setattr(update, "download_index", boom)
339+
340+
with pytest.raises(update.DownloadError, match="download died"):
341+
update.run_update("SEQUENTIAL")
342+
343+
assert cleared["n"] == 1
344+
345+
324346
def test_run_update_checksum_mismatch_prevents_activation(fake_dirs: Path, monkeypatch: pytest.MonkeyPatch):
325347
monkeypatch.setattr(config, "SKIP_MD5_CHECK", False)
326348
_make_pipeline_patches(monkeypatch)

0 commit comments

Comments
 (0)