Added util.ByteSizeNoLimit: a byte size of -1 means no limit, and any other negative size is rejected

ParseByteSize accepts "-1" or "-1B" as ByteSizeNoLimit, and String gives it
back as "-1". A setting for which no limit is unsafe can reject it.
This commit is contained in:
bwilliams committed 2026-10-15 15:57:30 +01:00
1 parent e19c8c2b69
commit 49b824d5a2
2 files changed
+44 -3

No files matched your search

+24 -1
View File
@@ -1,6 +1,9 @@
package util
import "testing"
import (
"strings"
"testing"
)
func TestByteSizeString(t *testing.T) {
tests := []struct {
@@ -25,3 +28,23 @@ func TestByteSizeString(t *testing.T) {
}
}
}
// -1 is ByteSizeNoLimit, parsed and formatted as "-1"; no other size may be
// negative.
func TestByteSizeNoLimit(t *testing.T) {
for _, text := range []string{"-1", "-1B", "-1b", " -1 "} {
size, err := ParseByteSize(text)
if err != nil || size != ByteSizeNoLimit {
t.Errorf("ParseByteSize(%q) = %d, %v; want ByteSizeNoLimit", text, size, err)
}
}
if got := ByteSizeNoLimit.String(); got != "-1" {
t.Errorf("ByteSizeNoLimit.String() = %q; want -1", got)
}
for _, text := range []string{"-2", "-1KB", "-0.5", "-1KiB", "-100"} {
size, err := ParseByteSize(text)
if err == nil || !strings.Contains(err.Error(), "only -1 (no limit)") {
t.Errorf("ParseByteSize(%q) = %d, %v; want a negative size error", text, size, err)
}
}
}