From 0fa8cb8f54c3409d62d1d22a682171b6da75e8d7 Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Mon, 18 Sep 2023 10:05:33 -0700 Subject: [PATCH] add option to use cache in test context (#19) --- id/init_test.go | 2 +- testutil/ctx.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/id/init_test.go b/id/init_test.go index 6eb8d40..fd57ee4 100644 --- a/id/init_test.go +++ b/id/init_test.go @@ -9,7 +9,7 @@ import ( ) func TestInit(t *testing.T) { - ctx := testutil.NewCtx() + ctx := testutil.NewCtx(t, false) testID := NewTestID(ctx, t, git.MainBranch, true) Init(ctx, testID.OwnerAddress()) if err := must.Try(func() { Init(ctx, testID.OwnerAddress()) }); err == nil { diff --git a/testutil/ctx.go b/testutil/ctx.go index 60cd2f0..d0645ed 100644 --- a/testutil/ctx.go +++ b/testutil/ctx.go @@ -2,16 +2,24 @@ package testutil import ( "context" + "math/rand" + "path/filepath" "strconv" "sync" + "testing" "github.com/gov4git/lib4git/git" ) -func NewCtx() context.Context { +func NewCtx(t *testing.T, useCache bool) context.Context { ctx := git.WithAuth(context.Background(), nil) ctx = git.WithTTL(ctx, nil) - return context.WithValue(ctx, counterKey{}, &counter{}) + ctx = context.WithValue(ctx, counterKey{}, &counter{}) + if useCache { + return git.WithCache(ctx, filepath.Join(t.TempDir(), "cache-"+strconv.FormatUint(uint64(rand.Int63()), 36))) + } else { + return ctx + } } type counterKey struct{}