Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting sonarcloud compliance issue #1697

Merged
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
43 changes: 43 additions & 0 deletions src/rez/tests/test_utils_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


"""
unit tests for 'utils.formatting' module
"""
from rez.tests.util import TestBase
from rez.utils import formatting


class TestFormatting(TestBase):
def test_readable_units(self):
readable_units = formatting._readable_units(
0,
formatting.memory_divs
)
self.assertEqual(readable_units, "0 bytes")

readable_units = formatting._readable_units(
1024,
formatting.memory_divs
)
self.assertEqual(readable_units, "1 Kb")

readable_units = formatting._readable_units(
1200,
formatting.memory_divs
)
self.assertEqual(readable_units, "1.2 Kb")

readable_units = formatting._readable_units(
-30000,
formatting.memory_divs
)
self.assertEqual(readable_units, "-29 Kb")

readable_units = formatting._readable_units(
1024,
formatting.memory_divs,
plural_aware=True
)
self.assertEqual(readable_units, "1 K")
8 changes: 6 additions & 2 deletions src/rez/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rez.exceptions import PackageRequestError
from pprint import pformat
from enum import Enum
import math
import os
import os.path
import re
Expand Down Expand Up @@ -363,7 +364,9 @@ def readable_time_duration(secs):


def readable_memory_size(bytes_):
"""Convert number of bytes into human readable form, eg '1.2 Kb'.
"""Convert number of bytes into human-readable form.

This method rounds to 1 decimal place eg '1.2 Kb'.
"""
return _readable_units(bytes_, memory_divs)

Expand All @@ -382,7 +385,8 @@ def _readable_units(value, divs, plural_aware=False):
rounding = 0 if f > threshold else 1
f = round(f, rounding)
f = int(f * 10) / 10.0
if plural_aware and f == 1.0:
is_one = math.isclose(f, 1.0, rel_tol=1e-09, abs_tol=1e-09)
if plural_aware and is_one:
unit = unit[:-1]
txt = "%g %s" % (f, unit)
break
Expand Down