Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fsutil/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def convert_size_bytes_to_string(size: int) -> str:
units = SIZE_UNITS
factor = 0
factor_limit = len(units) - 1
while (size_num >= 1024) and (factor <= factor_limit):
while (size_num >= 1024) and (factor < factor_limit):
size_num /= 1024
factor += 1
size_units = units[factor]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ def test_convert_size_bytes_to_string(size_bytes, expected_output):
assert fsutil.convert_size_bytes_to_string(size_bytes) == expected_output


@pytest.mark.parametrize(
"size_bytes, expected_output",
[
(2**80, "1.00 YB"),
(2**90, "1024.00 YB"),
(2**100, "1048576.00 YB"),
],
)
def test_convert_size_bytes_to_string_saturates_at_largest_unit(
size_bytes, expected_output
):
# Sizes at or above 1024 YB must clamp to the YB unit, not raise IndexError.
assert fsutil.convert_size_bytes_to_string(size_bytes) == expected_output


@pytest.mark.parametrize(
"size_string, expected_output",
[
Expand Down
Loading