Bläddra i källkod

Merge pull request #6754 from Goryudyuma/6740-fix-display

fix: The correct number is displayed
Djordje Lukic 6 år sedan
förälder
incheckning
98932e9cb4
2 ändrade filer med 22 tillägg och 1 borttagningar
  1. 1 1
      compose/cli/utils.py
  2. 21 0
      tests/unit/cli/utils_test.py

+ 1 - 1
compose/cli/utils.py

@@ -137,7 +137,7 @@ def human_readable_file_size(size):
     if order >= len(suffixes):
         order = len(suffixes) - 1
 
-    return '{0:.3g} {1}'.format(
+    return '{0:.4g} {1}'.format(
         size / float(1 << (order * 10)),
         suffixes[order]
     )

+ 21 - 0
tests/unit/cli/utils_test.py

@@ -3,6 +3,7 @@ from __future__ import unicode_literals
 
 import unittest
 
+from compose.cli.utils import human_readable_file_size
 from compose.utils import unquote_path
 
 
@@ -21,3 +22,23 @@ class UnquotePathTest(unittest.TestCase):
         assert unquote_path('""hello""') == '"hello"'
         assert unquote_path('"hel"lo"') == 'hel"lo'
         assert unquote_path('"hello""') == 'hello"'
+
+
+class HumanReadableFileSizeTest(unittest.TestCase):
+    def test_100b(self):
+        assert human_readable_file_size(100) == '100 B'
+
+    def test_1kb(self):
+        assert human_readable_file_size(1024) == '1 kB'
+
+    def test_1023b(self):
+        assert human_readable_file_size(1023) == '1023 B'
+
+    def test_units(self):
+        assert human_readable_file_size((2 ** 10) ** 0) == '1 B'
+        assert human_readable_file_size((2 ** 10) ** 1) == '1 kB'
+        assert human_readable_file_size((2 ** 10) ** 2) == '1 MB'
+        assert human_readable_file_size((2 ** 10) ** 3) == '1 GB'
+        assert human_readable_file_size((2 ** 10) ** 4) == '1 TB'
+        assert human_readable_file_size((2 ** 10) ** 5) == '1 PB'
+        assert human_readable_file_size((2 ** 10) ** 6) == '1 EB'