x/build/blob: more tests

This commit is contained in:
Blake Mizerany 2024-04-04 11:43:07 -07:00
parent 45ca3c80e8
commit 2751c26da7
2 changed files with 10 additions and 8 deletions

View File

@ -7,6 +7,8 @@ import (
"strings"
)
const MaxRefLength = 255
type PartKind int
// Levels of concreteness
@ -224,7 +226,7 @@ func Parts(s string) iter.Seq2[PartKind, string] {
s = s[len("https://"):]
}
if len(s) > 255 || len(s) == 0 {
if len(s) > MaxRefLength || len(s) == 0 {
return
}

View File

@ -5,11 +5,6 @@ import (
"testing"
)
// test refs
const (
refTooLong = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
var testRefs = map[string]Ref{
"mistral:latest": {name: "mistral", tag: "latest"},
"mistral": {name: "mistral"},
@ -38,6 +33,9 @@ var testRefs = map[string]Ref{
"file:///etc/passwd": {},
"file:///etc/passwd:latest": {},
"file:///etc/passwd:latest+u": {},
strings.Repeat("a", MaxRefLength): {name: strings.Repeat("a", MaxRefLength)},
strings.Repeat("a", MaxRefLength+1): {},
}
func TestRefParts(t *testing.T) {
@ -48,7 +46,7 @@ func TestRefParts(t *testing.T) {
}
}
func TestParseRefWithAndWithoutPrefixes(t *testing.T) {
func TestParseRef(t *testing.T) {
for s, want := range testRefs {
for _, prefix := range []string{"", "https://", "http://"} {
// We should get the same results with or without the
@ -104,9 +102,11 @@ func TestRefFull(t *testing.T) {
func TestParseRefAllocs(t *testing.T) {
// test allocations
var r Ref
allocs := testing.AllocsPerRun(1000, func() {
ParseRef("example.com/mistral:7b+Q4_0")
r = ParseRef("example.com/mistral:7b+Q4_0")
})
_ = r
if allocs > 0 {
t.Errorf("ParseRef allocs = %v; want 0", allocs)
}