Skip to content

Commit

Permalink
base1: Fix format_bytes() B unit
Browse files Browse the repository at this point in the history
For numbers < 1000 the unit should be "B" (bytes), not simply empty.

This is technically an API break, but most places which use that (like,
memory or storage device sizes) don't usually deal with such small
numbers (except "0"), and cockpit-files already has a hack for this [1].
As cockpit-files, -podman, etc. bundle cockpit.js, this can be fixed at
the time when updating cockpit lib, so it's not a sudden runtime
breakage.

Again, `format_bytes_per_sec()` already gets this right.

[1] https://github.com/cockpit-project/cockpit-files/blob/8e078434f5/src/sidebar.jsx#L80
  • Loading branch information
martinpitt committed Apr 23, 2024
1 parent 597f701 commit 7ad70e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions pkg/base1/test-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ QUnit.test("format_number", function (assert) {

QUnit.test("format_bytes", function (assert) {
const checks = [
[5, 1000, "5"],
[5, 1024, "5"],
[999, 1000, "999"],
[999, 1024, "999"],
[1023, 1024, "1023"],
[5, 1000, "5 B"],
[5, 1024, "5 B"],
[999, 1000, "999 B"],
[999, 1024, "999 B"],
[1023, 1024, "1023 B"],
[1934, undefined, "1.93 kB"],
[1934, 1000, "1.93 kB"],
[2000, 1024, "1.95 KiB"],
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/cockpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,8 @@ function factory() {
}

const byte_suffixes = {
1000: [null, "kB", "MB", "GB", "TB", "PB", "EB", "ZB"],
1024: [null, "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]
1000: ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"],
1024: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]
};

cockpit.format_bytes = function format_bytes(number, ...args) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/storaged/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ QUnit.test("format_fsys_usage", function (assert) {
* means 5, 200 and 5k out of 5k are displayed as "0.01 / 5kB", "0.20 / 5kB" and "5 / 5kB"
*/
const results = [
["5", ["5"]],
["200", ["5", "200"]],
["5 B", ["5"]],
["200 B", ["5", "200"]],
["5 kB", ["0.01", "0.20", "5"]],
["200 kB", ["0.01", "0.20", "5", "200"]],
["5 MB", ["0.01", "0.01", "0.01", "0.20", "5"]],
Expand Down
8 changes: 4 additions & 4 deletions test/verify/check-storage-swap
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestStorageswap(storagelib.StorageCase):

# It should have been started and have a fstab entry
self.click_card_row("GPT partitions", 1)
b.wait_text(self.card_desc("Swap", "Used"), "0")
b.wait_text(self.card_desc("Swap", "Used"), "0 B")
self.assertIn("defaults", m.execute(f"findmnt --fstab -n -o OPTIONS {disk}1"))

# Stopping should set it to noauto
Expand All @@ -54,7 +54,7 @@ class TestStorageswap(storagelib.StorageCase):

# Start it again to test teardown below
b.click(self.card_button("Swap", "Start"))
b.wait_text(self.card_desc("Swap", "Used"), "0")
b.wait_text(self.card_desc("Swap", "Used"), "0 B")
self.assertIn("defaults", m.execute(f"findmnt --fstab -n -o OPTIONS {disk}1"))

# It should have the right partition type
Expand Down Expand Up @@ -86,7 +86,7 @@ class TestStorageswap(storagelib.StorageCase):
# fstab entry
m.execute(f"mkswap -f {disk}")
b.click(self.card_button("Swap", "Start"))
b.wait_text(self.card_desc("Swap", "Used"), "0")
b.wait_text(self.card_desc("Swap", "Used"), "0 B")
testlib.wait(lambda: "defaults" in m.execute(f"findmnt --fstab -n -o OPTIONS {disk}"))

def testEncrypted(self):
Expand All @@ -111,7 +111,7 @@ class TestStorageswap(storagelib.StorageCase):

# It should have been started and have a fstab entry
self.click_card_row("Storage", name=disk)
b.wait_text(self.card_desc("Swap", "Used"), "0")
b.wait_text(self.card_desc("Swap", "Used"), "0 B")
dev = b.text(self.card_desc("Encryption", "Cleartext device"))
testlib.wait(lambda: "defaults" in m.execute(f"findmnt --fstab -n -o OPTIONS {dev}"))

Expand Down

0 comments on commit 7ad70e4

Please sign in to comment.