Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve comment and add test
Browse files Browse the repository at this point in the history
MartinBelthle committed Jan 14, 2025
1 parent 869a8bb commit 464b1fe
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 5 additions & 4 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
@@ -189,10 +189,11 @@ def get_disk_usage(path: t.Union[str, Path]) -> int:
with contextlib.suppress(FileNotFoundError, PermissionError):
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total_size += entry.stat().st_size
elif entry.is_dir():
total_size += get_disk_usage(path=str(entry.path))
with contextlib.suppress(FileNotFoundError, PermissionError):
if entry.is_file():
total_size += entry.stat().st_size
elif entry.is_dir():
total_size += get_disk_usage(path=str(entry.path))
return total_size


13 changes: 10 additions & 3 deletions tests/study/test_service.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from pathlib import Path
from unittest.mock import patch

@@ -56,11 +55,15 @@ def test_gest_disk_usage_exceptions(tmp_path: Path) -> None:
"""
This test ensures that the 'get_disk_usage' function handles exceptions appropriately.
"""
for folder in ["study", "study2"]:
(tmp_path / folder).mkdir()
file_path = tmp_path / folder / "file.txt"
file_path.write_bytes(b"10")

with patch("os.scandir", side_effect=FileNotFoundError("File doesn't exist")):
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 0

(tmp_path / "file.txt").touch()
with patch("os.DirEntry.is_file", side_effect=FileNotFoundError("File doesn't exist")):
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 0
@@ -69,11 +72,15 @@ def test_gest_disk_usage_exceptions(tmp_path: Path) -> None:
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 0

(tmp_path / "study").mkdir()
with patch("os.DirEntry.is_dir", side_effect=FileNotFoundError("File doesn't exist")):
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 0

with patch("os.scandir", side_effect=PermissionError("Access denied")):
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 0

# Mocks the case where the 2nd file raises an exception. We should have the first file size in the result
with patch("os.DirEntry.is_file", side_effect=[False, True, False, FileNotFoundError("File doesn't exist")]):
disk_usage = get_disk_usage(tmp_path)
assert disk_usage == 2

0 comments on commit 464b1fe

Please sign in to comment.