Skip to content

Commit 7e637bb

Browse files
committed
refactor(cache-downloader): Use pathlib for file existence check
Replaces os.path.isfile with pathlib.Path.is_file in the download_dependency function to improve code readability and adhere to project standards. Updates unit tests to maintain functionality while ensuring consistent use of context managers for patching.
1 parent 7379d1c commit 7e637bb

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

dockerfiles/cache-downloader/cache-downloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def download_dependency(
1919
version_path = os.path.join(BASE_PATH, release_name, f"python{python_version}")
2020
done_file = os.path.join(version_path, f"{python_version}-done")
2121

22-
if os.path.isfile(done_file):
22+
if Path(done_file).is_file():
2323
print(
2424
f"{datetime.datetime.now()}: {release_name} python{python_version} already cached, skipping download"
2525
)

tests/unit/test_cache_downloader.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ def test_skips_download_when_done_file_exists(self, tmp_path):
181181
version_path.mkdir(parents=True)
182182
(version_path / f"{py_ver}-done").write_text("ok")
183183

184-
with patch.object(_mod, "BASE_PATH", str(tmp_path)):
185-
with patch.object(_mod.subprocess, "Popen") as mock_popen:
186-
download_dependency(release, py_ver, "fake-bucket")
184+
with patch.object(_mod, "BASE_PATH", str(tmp_path)), patch.object(
185+
_mod.subprocess, "Popen"
186+
) as mock_popen:
187+
download_dependency(release, py_ver, "fake-bucket")
187188

188189
mock_popen.assert_not_called()
189190

@@ -203,11 +204,10 @@ def test_downloads_when_done_file_missing(self, tmp_path):
203204
mock_tar.communicate.return_value = (b"", b"")
204205
mock_tar.returncode = 0
205206

206-
with patch.object(_mod, "BASE_PATH", str(tmp_path)):
207-
with patch.object(
208-
_mod.subprocess, "Popen", side_effect=[mock_aws, mock_tar]
209-
) as mock_popen:
210-
download_dependency(release, py_ver, "fake-bucket")
207+
with patch.object(_mod, "BASE_PATH", str(tmp_path)), patch.object(
208+
_mod.subprocess, "Popen", side_effect=[mock_aws, mock_tar]
209+
) as mock_popen:
210+
download_dependency(release, py_ver, "fake-bucket")
211211

212212
assert mock_popen.call_count == 2
213213
assert (version_path / f"{py_ver}-done").exists()

0 commit comments

Comments
 (0)