diff --git a/e2e/ls_test.go b/e2e/ls_test.go index 9f3f09dfe..6b61a2f25 100644 --- a/e2e/ls_test.go +++ b/e2e/ls_test.go @@ -431,6 +431,7 @@ func TestListS3ObjectsWithDashH(t *testing.T) { putFile(t, s3client, bucket, "testfile1.txt", strings.Repeat("this is a file content", 10000)) putFile(t, s3client, bucket, "testfile2.txt", strings.Repeat("this is also a file content", 10000)) + putFile(t, s3client, bucket, "testfile3.txt", "this is a small file") cmd := s5cmd("ls", "-H", "s3://"+bucket) result := icmd.RunCmd(cmd) @@ -440,6 +441,7 @@ func TestListS3ObjectsWithDashH(t *testing.T) { assertLines(t, result.Stdout(), map[int]compareFunc{ 0: match(`^ 214.8K testfile1.txt$`), 1: match(`^ 263.7K testfile2.txt$`), + 2: match(`^ 20B testfile3.txt$`), }, trimMatch(dateRe), alignment(true)) } diff --git a/strutil/strutil.go b/strutil/strutil.go index 49c66183d..fd7d4f6c5 100644 --- a/strutil/strutil.go +++ b/strutil/strutil.go @@ -31,7 +31,7 @@ func HumanizeBytes(b int64) string { } } if suffix == "" { - return strconv.FormatInt(b, 10) + return strconv.FormatInt(b, 10) + "B" } return fmt.Sprintf("%.1f%s", float64(b)/float64(div), suffix) diff --git a/strutil/strutil_test.go b/strutil/strutil_test.go index 9884279fd..090659be9 100644 --- a/strutil/strutil_test.go +++ b/strutil/strutil_test.go @@ -43,6 +43,32 @@ func TestCapitalizeFirstLetter(t *testing.T) { } } +func TestHumanizeBytes(t *testing.T) { + tests := []struct { + b int64 + want string + }{ + {0, "0B"}, + {1, "1B"}, + {999, "999B"}, + {1023, "1023B"}, + {1024, "1024B"}, + {1025, "1.0K"}, + {1536, "1.5K"}, + {1 << 20, "1024.0K"}, + {(1 << 20) + 1, "1.0M"}, + {5 * (1 << 30), "5.0G"}, + {3 * (1 << 40), "3.0T"}, + } + for _, tt := range tests { + t.Run(tt.want, func(t *testing.T) { + if got := HumanizeBytes(tt.b); got != tt.want { + t.Errorf("HumanizeBytes(%d) = %q, want %q", tt.b, got, tt.want) + } + }) + } +} + func Test_WildCardToRegexp(t *testing.T) { t.Parallel() tests := []struct {