Skip to content

Commit 4ab7b79

Browse files
fix incorrect linode-cli obj du number formatting (#480)
1 parent 2a0b31c commit 4ab7b79

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

linodecli/plugins/obj/helpers.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,11 @@ def _denominate(total):
106106
"""
107107
Coverts bucket size to human readable bytes.
108108
"""
109-
total = float(total)
110-
denomination = ["KB", "MB", "GB", "TB"]
111-
for x in denomination:
112-
if total > 1024:
113-
total = total / 1024
109+
for unit in ("KB", "MB", "GB", "TB"):
110+
total = total / 1024
114111
if total < 1024:
115-
total = round(total, 2)
116-
total = str(total) + " " + x
117112
break
118-
return total
113+
return f"{round(total, 2)} {unit}"
119114

120115

121116
# helper functions for output

tests/unit/test_plugin_obj.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pytest import CaptureFixture
22

3-
from linodecli.plugins.obj import get_obj_args_parser, print_help
3+
from linodecli.plugins.obj import get_obj_args_parser, helpers, print_help
44

55

66
def test_print_help(capsys: CaptureFixture):
@@ -12,3 +12,24 @@ def test_print_help(capsys: CaptureFixture):
1212
"See --help for individual commands for more information"
1313
in captured_text.out
1414
)
15+
16+
17+
def test_helpers_denominate():
18+
assert helpers._denominate(0) == "0.0 KB"
19+
assert helpers._denominate(1) == "0.0 KB"
20+
assert helpers._denominate(12) == "0.01 KB"
21+
assert helpers._denominate(123) == "0.12 KB"
22+
assert helpers._denominate(1000) == "0.98 KB"
23+
24+
assert helpers._denominate(1024) == "1.0 KB"
25+
assert helpers._denominate(1024**2) == "1.0 MB"
26+
assert helpers._denominate(1024**3) == "1.0 GB"
27+
assert helpers._denominate(1024**4) == "1.0 TB"
28+
assert helpers._denominate(1024**5) == "1024.0 TB"
29+
30+
assert helpers._denominate(102400) == "100.0 KB"
31+
assert helpers._denominate(1024000) == "1000.0 KB"
32+
assert helpers._denominate((1024**2) // 10) == "102.4 KB"
33+
34+
assert helpers._denominate(123456789) == "117.74 MB"
35+
assert helpers._denominate(1e23) == "90949470177.29 TB"

0 commit comments

Comments
 (0)