Created
May 3, 2023 18:04
-
-
Save imjasonh/95ccf4fbafd65551ef3581d1887bbf10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package must | |
import ( | |
"testing" | |
"github.com/google/go-containerregistry/pkg/name" | |
v1 "github.com/google/go-containerregistry/pkg/v1" | |
) | |
// must.Testing(t).Digest(img) | |
// | |
// or | |
// | |
// var must = must.Testing(t) | |
// must.Digest(img) | |
func Testing(t *testing.T) Muster { | |
return testMuster{t} | |
} | |
type digester interface { | |
Digest() (v1.Hash, error) | |
} | |
type Muster interface { | |
Digest(digester) v1.Hash | |
Reference(string) name.Reference | |
Tag(string) name.Tag | |
} | |
type testMuster struct { | |
t *testing.T | |
} | |
// func TestFoo(t *testing.T) { | |
// must := must.Testing(t) | |
// must.Digest(img) | |
// must.ParseReference("gcr.io/foo/bar") | |
// must.Tag("gcr.io/foo/bar:baz") | |
// } | |
func (m testMuster) Digest(i digester) v1.Hash { return Digest(m.t, i) } | |
func (m testMuster) Reference(s string) name.Reference { return ParseReference(m.t, s) } | |
func (m testMuster) Tag(s string) name.Tag { return Tag(m.t, s) } | |
// var must = must.New() | |
// | |
// func main() { | |
// must.Digest(img) | |
// must.ParseReference("gcr.io/foo/bar") | |
// must.Tag("gcr.io/foo/bar:baz") | |
// } | |
func New() Muster { | |
return panicMuster{} | |
} | |
type panicMuster struct{} | |
func (m panicMuster) Digest(i digester) v1.Hash { | |
h, err := i.Digest() | |
if err != nil { | |
panic(err) | |
} | |
return h | |
} | |
func (m panicMuster) Reference(s string) name.Reference { | |
r, err := name.ParseReference(s) | |
if err != nil { | |
panic(err) | |
} | |
return r | |
} | |
func (m panicMuster) Tag(s string) name.Tag { | |
r, err := name.NewTag(s) | |
if err != nil { | |
panic(err) | |
} | |
return r | |
} | |
////////////////////// | |
// must.Digest(t, img) | |
// must.ParseReference(t, "gcr.io/foo/bar") | |
// must.Tag(t, "gcr.io/foo/bar:baz") | |
func Digest(t *testing.T, i interface { | |
Digest() (v1.Hash, error) | |
}) v1.Hash { | |
t.Helper() | |
h, err := i.Digest() | |
if err != nil { | |
t.Fatalf("Digest(): %v", err) | |
} | |
return h | |
} | |
func ParseReference(t *testing.T, s string) name.Reference { | |
t.Helper() | |
r, err := name.ParseReference(s) | |
if err != nil { | |
t.Fatalf("ParseReference(%q): %v", s, err) | |
} | |
return r | |
} | |
func Tag(t *testing.T, s string) name.Tag { | |
t.Helper() | |
r, err := name.NewTag(s) | |
if err != nil { | |
t.Fatalf("NewTag(%q): %v", s, err) | |
} | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment