Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import (

func TestImport(t *testing.T) {
runTest := func(t *testing.T, dir zbstore.Directory, realStoreDir string) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

const fileContent = "Hello, World!\n"
exportBuffer := new(bytes.Buffer)
Expand Down Expand Up @@ -281,8 +280,7 @@ func TestDelete(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

realStoreDir := t.TempDir()
server, client, err := backendtest.NewServer(ctx, t, dir, &backendtest.Options{
Expand Down
6 changes: 2 additions & 4 deletions internal/backend/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ func TestExport(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Run("RPC", func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

dir := backendtest.NewStoreDirectory(t)
records, importData, err := generateImport(dir)
Expand Down Expand Up @@ -216,8 +215,7 @@ func TestExport(t *testing.T) {
}

t.Run(mapTestName, func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

var dir zbstore.Directory
var realDir string
Expand Down
36 changes: 12 additions & 24 deletions internal/backend/realize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const (
)

func TestRealizeSingleDerivation(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -123,8 +122,7 @@ func TestRealizeSingleDerivation(t *testing.T) {
}

func TestRealizeReuse(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -221,8 +219,7 @@ func TestRealizeReuse(t *testing.T) {
}

func TestRealizeDisableReuse(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -318,8 +315,7 @@ func TestRealizeDisableReuse(t *testing.T) {
}

func TestRealizeMultiStep(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -419,8 +415,7 @@ func TestRealizeMultiStep(t *testing.T) {
}

func TestRealizeReferenceToDep(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -568,8 +563,7 @@ func TestRealizeReferenceToDep(t *testing.T) {
}

func TestRealizeSelfReference(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const inputContent = "Hello, World!\n"
Expand Down Expand Up @@ -654,8 +648,7 @@ func TestRealizeSelfReference(t *testing.T) {
}

func TestRealizeFixed(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

exportBuffer := new(bytes.Buffer)
Expand Down Expand Up @@ -770,8 +763,7 @@ func TestRealizeFixed(t *testing.T) {
}

func TestRealizeFailure(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

exportBuffer := new(bytes.Buffer)
Expand Down Expand Up @@ -883,8 +875,7 @@ func TestRealizeFailure(t *testing.T) {
}

func TestRealizeNoOutput(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

exportBuffer := new(bytes.Buffer)
Expand Down Expand Up @@ -999,8 +990,7 @@ func TestRealizeCores(t *testing.T) {
tests := []int{1, 2}
for _, n := range tests {
t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

exportBuffer := new(bytes.Buffer)
Expand Down Expand Up @@ -1075,8 +1065,7 @@ func TestRealizeCores(t *testing.T) {
}

func TestRealizeFetchURL(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

const fileContent = "Hello, World!\n"
Expand Down Expand Up @@ -1149,8 +1138,7 @@ func TestRealizeFetchURL(t *testing.T) {
}

func TestRealizeSignature(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
dir := backendtest.NewStoreDirectory(t)

testKey := ed25519.PrivateKey{
Expand Down
33 changes: 11 additions & 22 deletions internal/frontend/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func TestLuaToGo(t *testing.T) {
},
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -150,8 +149,7 @@ func TestGetenv(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

const wantKey = "BAR"
Expand Down Expand Up @@ -199,8 +197,7 @@ func TestGetenv(t *testing.T) {
}

func TestStringMethod(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -238,8 +235,7 @@ func TestStringMethod(t *testing.T) {
}

func TestImportFromDerivation(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -281,8 +277,7 @@ func TestImportFromDerivation(t *testing.T) {
}

func TestImportExitStore(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -321,8 +316,7 @@ func TestImportExitStore(t *testing.T) {
}

func TestImportCycle(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -397,8 +391,7 @@ func TestImportCycle(t *testing.T) {
}

func TestExtract(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -437,8 +430,7 @@ func TestExtract(t *testing.T) {
}

t.Run("Full", func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

drv, ok := results[0].(*Derivation)
if !ok {
Expand Down Expand Up @@ -475,8 +467,7 @@ func TestExtract(t *testing.T) {
})

t.Run("Stripped", func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

drv, ok := results[1].(*Derivation)
if !ok {
Expand Down Expand Up @@ -514,8 +505,7 @@ func TestExtract(t *testing.T) {
}

func TestNewState(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -561,8 +551,7 @@ func TestNewState(t *testing.T) {

// BenchmarkNewState measures the performance of spinning up a new interpreter.
func BenchmarkNewState(b *testing.B) {
ctx, cancel := testcontext.New(b)
defer cancel()
ctx := testcontext.New(b)
storeDir := backendtest.NewStoreDirectory(b)

di := new(zbstorerpc.DeferredImporter)
Expand Down
3 changes: 1 addition & 2 deletions internal/frontend/lazy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestLazy(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down
24 changes: 8 additions & 16 deletions internal/frontend/path_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func TestPath(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -96,8 +95,7 @@ func TestPath(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -180,8 +178,7 @@ func TestPath(t *testing.T) {
})

t.Run("Directory", func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -239,8 +236,7 @@ func TestPath(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -314,8 +310,7 @@ func TestPath(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -381,8 +376,7 @@ func TestPath(t *testing.T) {
})

t.Run("FilteredDirectory", func(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -433,8 +427,7 @@ func TestPath(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down Expand Up @@ -492,8 +485,7 @@ func TestReadFile(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)
storeDir := backendtest.NewStoreDirectory(t)

di := new(zbstorerpc.DeferredImporter)
Expand Down
3 changes: 1 addition & 2 deletions internal/remotestore/httpstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import (
)

func TestHTTPStore(t *testing.T) {
ctx, cancel := testcontext.New(t)
defer cancel()
ctx := testcontext.New(t)

cacheFileRoot, err := os.OpenRoot(filepath.Join("testdata", "cache"))
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions internal/testcontext/testcontext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024 The zb Authors
// SPDX-License-Identifier: MIT

// Package testcontext provides a function that creates a test-scoped [context.Context].
package testcontext

import (
Expand All @@ -13,14 +14,15 @@ import (

// New returns a context that associates the test logger with the test
// and obeys the test's deadline if present.
func New(tb testing.TB) (context.Context, context.CancelFunc) {
func New(tb testing.TB) context.Context {
ctx := tb.Context()
cancel := context.CancelFunc(func() {})
if d, ok := deadline(tb); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, d)
tb.Cleanup(cancel)
}
ctx = testlog.WithTB(ctx, tb)
return ctx, cancel
return ctx
}

func deadline(x any) (deadline time.Time, ok bool) {
Expand Down
Loading