diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bb93e9f..b08438be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - changing some words and symbols to improve readability and cohesion (#250)[https://github.com/vinifmor/bauh/issues/250] ### Fixes +- General + - not properly converting bit based sizes (KiB, MiB, etc...) to byte based + - Debian - info: crashing when the package size has unexpected symbols [#251](https://github.com/vinifmor/bauh/issues/251) diff --git a/bauh/commons/util.py b/bauh/commons/util.py index 8acd65c7..8f1ab298 100644 --- a/bauh/commons/util.py +++ b/bauh/commons/util.py @@ -30,19 +30,23 @@ def deep_update(source: dict, overrides: dict): def size_to_byte(size: float, unit: str) -> int: lower_unit = unit.lower() + final_size = size + + if unit == 'b' or len(lower_unit) > 1 and lower_unit.endswith('ib'): + final_size /= 8 if lower_unit[0] == 'b': - final_size = size + pass elif lower_unit[0] == 'k': - final_size = size * 1024 + final_size = final_size * 1024 elif lower_unit[0] == 'm': - final_size = size * (1024 ** 2) + final_size = final_size * (1024 ** 2) elif lower_unit[0] == 'g': - final_size = size * (1024 ** 3) + final_size = final_size * (1024 ** 3) elif lower_unit[0] == 't': - final_size = size * (1024 ** 4) + final_size = final_size * (1024 ** 4) else: - final_size = size * (1024 ** 5) + final_size = final_size * (1024 ** 5) return round(final_size) diff --git a/tests/common/test_util.py b/tests/common/test_util.py index 10f45a6c..b5cbc2ce 100644 --- a/tests/common/test_util.py +++ b/tests/common/test_util.py @@ -6,8 +6,28 @@ from bauh.commons.util import size_to_byte class SizeToByteTest(TestCase): def test_must_return_right_number_of_bytes(self): + self.assertEqual(1.0, size_to_byte(1, 'B')) + self.assertEqual(1024, size_to_byte(1, 'K')) self.assertEqual(round(float('58675.2')), size_to_byte(57.3, 'K')) + self.assertEqual(1048576, size_to_byte(1, 'M')) self.assertEqual(round(float('60083404.8')), size_to_byte(57.3, 'M')) + self.assertEqual(1073741824, size_to_byte(1, 'G')) self.assertEqual(round(float('61525406515.2')), size_to_byte(57.3, 'G')) + self.assertEqual(1099511627776, size_to_byte(1, 'T')) self.assertEqual(round(float('63002016271564.8')), size_to_byte(57.3, 'T')) + self.assertEqual(1125899906842624, size_to_byte(1, 'P')) self.assertEqual(round(float('64514064662082350')), size_to_byte(57.3, 'P')) + + def test_must_return_right_number_of_bytes_for_bit_units(self): + self.assertEqual(1, size_to_byte(8, 'b')) + self.assertEqual(2, size_to_byte(16, 'b')) + self.assertEqual(128, size_to_byte(1, 'KiB')) + self.assertEqual(197460, size_to_byte(1542.66, 'KiB')) + self.assertEqual(131072, size_to_byte(1, 'MiB')) + self.assertEqual(546570, size_to_byte(4.17, 'MiB')) + self.assertEqual(134217728, size_to_byte(1, 'GiB')) + self.assertEqual(2691065446, size_to_byte(20.05, 'GiB')) + self.assertEqual(137438953472, size_to_byte(1, 'TiB')) + self.assertEqual(6926923254989, size_to_byte(50.4, 'TiB')) + self.assertEqual(140737488355328, size_to_byte(1, 'PiB')) + self.assertEqual(330733097635021, size_to_byte(2.35, 'PiB'))