Skip to content

Commit

Permalink
add option to use cache in test context (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
petar authored Sep 18, 2023
1 parent 7205a53 commit 0fa8cb8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion id/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions testutil/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 0fa8cb8

Please sign in to comment.