From 7b743b78050df5428dfd6d1af452531163a8fca2 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 14 Feb 2022 17:48:13 +0000 Subject: [PATCH] feat: initial implementation --- README.md | 4 +++ go.mod | 5 +++ go.sum | 25 ++++++++++++++ gomockctx.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rand.go | 28 ++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 gomockctx.go create mode 100644 rand.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6044aa --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# gomockctx + +Go package with [gomock](https://github.com/golang/mock) helpers for matching +[context.Context](https://pkg.go.dev/context). diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..54784f1 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/romdo/gomockctx + +go 1.17 + +require github.com/golang/mock v1.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d067127 --- /dev/null +++ b/go.sum @@ -0,0 +1,25 @@ +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/gomockctx.go b/gomockctx.go new file mode 100644 index 0000000..d12099d --- /dev/null +++ b/gomockctx.go @@ -0,0 +1,93 @@ +// Package gomockctx contains gomock helpers for matching context.Context +// objects. +package gomockctx + +import ( + "context" + "fmt" + "reflect" + + "github.com/golang/mock/gomock" +) + +type ( + contextKey string + contextValue string +) + +var ctxKey contextKey = "gomockctx context ID" + +func newCtxID() contextValue { + id, err := randString(64) + if err != nil { + panic(err) + } + + return contextValue(id) +} + +func value(ctx context.Context) contextValue { + var value contextValue + v := ctx.Value(ctxKey) + if s, ok := v.(contextValue); ok { + value = s + } + + return value +} + +// New returns a context as a child of the given parent, and with a randomized +// gomockctx ID value set, making it a gomockctx context. This can then be used +// with Is to get a gomock Matcher which returns true for the context from New, +// or any child contexts of it. +// +// If crypto/rand returns an error, this will panic trying to generate the +// gomockctx ID. In practise though, crypto/rand should never return a error. +func New(parent context.Context) context.Context { + return context.WithValue(parent, ctxKey, newCtxID()) +} + +// Is accepts a context with a gomockctx ID value (as returned from New), and +// returns a gomock.Matcher which returns true for the given context, of any +// child contexts of it. +// +// If ctx was not returned from New, the resulting matcher will ALWAYS return +// false. +func Is(ctx context.Context) gomock.Matcher { + return WithValue(ctxKey, value(ctx)) +} + +// WithValue creates a generic gomock context matcher which returns true for any +// context which has the specified key/value. +func WithValue(key interface{}, value interface{}) gomock.Matcher { + return &contextMatcher{ + key: key, + value: value, + } +} + +// ID returns the gomockctx ID value in the given context, or a empty string if +// it not a gomockctx context. +func ID(ctx context.Context) string { + return string(value(ctx)) +} + +type contextMatcher struct { + key interface{} + value interface{} +} + +var _ gomock.Matcher = &contextMatcher{} + +func (e *contextMatcher) Matches(x interface{}) bool { + ctx, ok := x.(context.Context) + if !ok { + return false + } + + return reflect.DeepEqual(e.value, ctx.Value(e.key)) +} + +func (e *contextMatcher) String() string { + return fmt.Sprintf(`context with "%+v" = "%+v"`, e.key, e.value) +} diff --git a/rand.go b/rand.go new file mode 100644 index 0000000..bb7dae1 --- /dev/null +++ b/rand.go @@ -0,0 +1,28 @@ +package gomockctx + +import ( + "crypto/rand" + "math/big" +) + +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz" + + "0123456789" + +// randString returns a cryptographically secure random string of alphanumeric +// characters of n length. +// +// Borrowed from github.com/jimeh/rands package. +func randString(n int) (string, error) { + l := big.NewInt(int64(len(alphabet))) + b := make([]byte, n) + for i := 0; i < n; i++ { + index, err := rand.Int(rand.Reader, l) + if err != nil { + return "", err + } + b[i] = alphabet[index.Int64()] + } + + return string(b), nil +}