Skip to content

Commit 0fa8cb8

Browse files
authored
add option to use cache in test context (#19)
1 parent 7205a53 commit 0fa8cb8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

id/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestInit(t *testing.T) {
12-
ctx := testutil.NewCtx()
12+
ctx := testutil.NewCtx(t, false)
1313
testID := NewTestID(ctx, t, git.MainBranch, true)
1414
Init(ctx, testID.OwnerAddress())
1515
if err := must.Try(func() { Init(ctx, testID.OwnerAddress()) }); err == nil {

testutil/ctx.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ package testutil
22

33
import (
44
"context"
5+
"math/rand"
6+
"path/filepath"
57
"strconv"
68
"sync"
9+
"testing"
710

811
"github.com/gov4git/lib4git/git"
912
)
1013

11-
func NewCtx() context.Context {
14+
func NewCtx(t *testing.T, useCache bool) context.Context {
1215
ctx := git.WithAuth(context.Background(), nil)
1316
ctx = git.WithTTL(ctx, nil)
14-
return context.WithValue(ctx, counterKey{}, &counter{})
17+
ctx = context.WithValue(ctx, counterKey{}, &counter{})
18+
if useCache {
19+
return git.WithCache(ctx, filepath.Join(t.TempDir(), "cache-"+strconv.FormatUint(uint64(rand.Int63()), 36)))
20+
} else {
21+
return ctx
22+
}
1523
}
1624

1725
type counterKey struct{}

0 commit comments

Comments
 (0)