Skip to content

Commit 47d9694

Browse files
pieternclaude
andauthored
Use t.Context() instead of context.Background() in tests (#4655)
## Summary Go 1.24 added `t.Context()` / `b.Context()` which return a context scoped to the test lifetime. This replaces all `context.Background()` usage in tests and test helpers with the scoped alternative. - Replace ~1000 occurrences across ~290 files - Add `Context()` to the `TestingT` interface - Add a ruleguard lint rule to prevent `context.Background()` in `_test.go` files ## Test plan Tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4a3346f commit 47d9694

File tree

295 files changed

+1071
-1291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

295 files changed

+1071
-1291
lines changed

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestApplySomeChangeReturnsDiagnostics(t *testing.T) {
134134
}
135135

136136
func TestApplySomeChangeFixesThings(t *testing.T) {
137-
ctx := context.Background()
137+
ctx := t.Context()
138138
b, err := ...some operation...
139139
require.NoError(t, err)
140140
...
@@ -202,7 +202,7 @@ log.Errorf(ctx, "...")
202202
```
203203

204204
Note that the 'ctx' variable here is something that should be passed in as
205-
an argument by the caller. We should not use context.Background() like we do in tests.
205+
an argument by the caller. In tests, use `t.Context()` (or `b.Context()` for benchmarks) instead of `context.Background()`. This is enforced by a lint rule.
206206

207207
Use cmdio.LogString to print to stdout:
208208

acceptance/acceptance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func runTest(t *testing.T,
567567
timeout = time.Duration(float64(timeout) * config.TimeoutCIMultiplier)
568568
}
569569

570-
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
570+
ctx, cancelFunc := context.WithTimeout(t.Context(), timeout)
571571
defer cancelFunc()
572572
args := []string{"bash", "-euo", "pipefail", EntryPointScript}
573573
cmd := exec.CommandContext(ctx, args[0], args[1:]...)

acceptance/dbr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func runDbrTests(ctx context.Context, t *testing.T, w *databricks.WorkspaceClien
325325

326326
// runDbrAcceptanceTests is the main entry point for running DBR acceptance tests.
327327
func runDbrAcceptanceTests(t *testing.T, config dbrTestConfig) {
328-
ctx := context.Background()
328+
ctx := t.Context()
329329
uniqueID := uuid.New().String()
330330

331331
w, f, testDir := setupDbrTestDir(ctx, t, uniqueID)

acceptance/internal/cmd_server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package internal
22

33
import (
4-
"context"
54
"encoding/json"
65
"os"
76
"strings"
@@ -27,7 +26,7 @@ func StartCmdServer(t *testing.T) *testserver.Server {
2726
// Change current working directory to match the callsite.
2827
defer chdir(t, q.Get("cwd"))()
2928

30-
c := testcli.NewRunner(t, context.Background(), args...)
29+
c := testcli.NewRunner(t, t.Context(), args...)
3130
c.Verbose = false
3231
stdout, stderr, err := c.Run()
3332
result := map[string]any{

acceptance/internal/prepare_server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package internal
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -86,7 +85,7 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o
8685
w, err := databricks.NewWorkspaceClient()
8786
require.NoError(t, err)
8887

89-
user, err := w.CurrentUser.Me(context.Background())
88+
user, err := w.CurrentUser.Me(t.Context())
9089
require.NoError(t, err, "Failed to get current user")
9190

9291
cfg := w.Config

bundle/apps/validate_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package apps
22

33
import (
4-
"context"
54
"path/filepath"
65
"testing"
76

@@ -50,7 +49,7 @@ func TestAppsValidateSameSourcePath(t *testing.T) {
5049

5150
bundletest.SetLocation(b, ".", []dyn.Location{{File: filepath.Join(tmpDir, "databricks.yml")}})
5251

53-
diags := bundle.ApplySeq(context.Background(), b, mutator.TranslatePaths(), Validate())
52+
diags := bundle.ApplySeq(t.Context(), b, mutator.TranslatePaths(), Validate())
5453
require.Len(t, diags, 1)
5554
require.Equal(t, "Duplicate app source code path", diags[0].Summary)
5655
require.Contains(t, diags[0].Detail, "has the same source code path as app resource")
@@ -86,7 +85,7 @@ func TestAppsValidateBothSourceCodePathAndGitSource(t *testing.T) {
8685

8786
bundletest.SetLocation(b, ".", []dyn.Location{{File: filepath.Join(tmpDir, "databricks.yml")}})
8887

89-
diags := bundle.ApplySeq(context.Background(), b, mutator.TranslatePaths(), Validate())
88+
diags := bundle.ApplySeq(t.Context(), b, mutator.TranslatePaths(), Validate())
9089
require.Len(t, diags, 1)
9190
require.Equal(t, "Both source_code_path and git_source fields are set", diags[0].Summary)
9291
require.Contains(t, diags[0].Detail, "should have either source_code_path or git_source field, not both")

bundle/bundle_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bundle
22

33
import (
4-
"context"
54
"io/fs"
65
"os"
76
"path/filepath"
@@ -22,7 +21,7 @@ import (
2221

2322
// mustLoad calls MustLoad and returns the bundle and collected diagnostics
2423
func mustLoad(t *testing.T) (*Bundle, []diag.Diagnostic) {
25-
ctx := logdiag.InitContext(context.Background())
24+
ctx := logdiag.InitContext(t.Context())
2625
logdiag.SetCollect(ctx, true)
2726
b := MustLoad(ctx)
2827
diags := logdiag.FlushCollected(ctx)
@@ -31,27 +30,27 @@ func mustLoad(t *testing.T) (*Bundle, []diag.Diagnostic) {
3130

3231
// tryLoad calls TryLoad and returns the bundle and collected diagnostics
3332
func tryLoad(t *testing.T) (*Bundle, []diag.Diagnostic) {
34-
ctx := logdiag.InitContext(context.Background())
33+
ctx := logdiag.InitContext(t.Context())
3534
logdiag.SetCollect(ctx, true)
3635
b := TryLoad(ctx)
3736
diags := logdiag.FlushCollected(ctx)
3837
return b, diags
3938
}
4039

4140
func TestLoadNotExists(t *testing.T) {
42-
b, err := Load(context.Background(), "/doesntexist")
41+
b, err := Load(t.Context(), "/doesntexist")
4342
assert.ErrorIs(t, err, fs.ErrNotExist)
4443
assert.Nil(t, b)
4544
}
4645

4746
func TestLoadExists(t *testing.T) {
48-
b, err := Load(context.Background(), "./tests/basic")
47+
b, err := Load(t.Context(), "./tests/basic")
4948
assert.NoError(t, err)
5049
assert.NotNil(t, b)
5150
}
5251

5352
func TestBundleLocalStateDir(t *testing.T) {
54-
ctx := context.Background()
53+
ctx := t.Context()
5554
projectDir := t.TempDir()
5655
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
5756
require.NoError(t, err)
@@ -75,7 +74,7 @@ func TestBundleLocalStateDir(t *testing.T) {
7574
}
7675

7776
func TestBundleLocalStateDirOverride(t *testing.T) {
78-
ctx := context.Background()
77+
ctx := t.Context()
7978
projectDir := t.TempDir()
8079
bundleTmpDir := t.TempDir()
8180
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package loader_test
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/databricks/cli/bundle"
@@ -12,15 +11,15 @@ import (
1211

1312
func TestEntryPointNoRootPath(t *testing.T) {
1413
b := &bundle.Bundle{}
15-
diags := bundle.Apply(context.Background(), b, loader.EntryPoint())
14+
diags := bundle.Apply(t.Context(), b, loader.EntryPoint())
1615
require.Error(t, diags.Error())
1716
}
1817

1918
func TestEntryPoint(t *testing.T) {
2019
b := &bundle.Bundle{
2120
BundleRootPath: "testdata/basic",
2221
}
23-
diags := bundle.Apply(context.Background(), b, loader.EntryPoint())
22+
diags := bundle.Apply(t.Context(), b, loader.EntryPoint())
2423
require.NoError(t, diags.Error())
2524
assert.Equal(t, "loader_test", b.Config.Bundle.Name)
2625
}

bundle/config/loader/process_include_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package loader_test
22

33
import (
4-
"context"
54
"path/filepath"
65
"testing"
76

@@ -31,7 +30,7 @@ func TestProcessInclude(t *testing.T) {
3130
assert.Equal(t, "foo", b.Config.Workspace.Host)
3231

3332
// Apply the mutator and assert that the host value has been updated
34-
diags := bundle.Apply(context.Background(), b, m)
33+
diags := bundle.Apply(t.Context(), b, m)
3534
require.NoError(t, diags.Error())
3635
assert.Equal(t, "bar", b.Config.Workspace.Host)
3736
}
@@ -55,7 +54,7 @@ func TestProcessIncludeFormatMatch(t *testing.T) {
5554
}
5655

5756
m := loader.ProcessInclude(filepath.Join(b.BundleRootPath, fileName), fileName)
58-
diags := bundle.Apply(context.Background(), b, m)
57+
diags := bundle.Apply(t.Context(), b, m)
5958
assert.Empty(t, diags)
6059
})
6160
}
@@ -210,7 +209,7 @@ func TestProcessIncludeFormatNotMatch(t *testing.T) {
210209
}
211210

212211
m := loader.ProcessInclude(filepath.Join(b.BundleRootPath, fileName), fileName)
213-
diags := bundle.Apply(context.Background(), b, m)
212+
diags := bundle.Apply(t.Context(), b, m)
214213
require.Len(t, diags, 1)
215214
assert.Equal(t, expectedDiags, diags)
216215
})

bundle/config/loader/process_root_includes_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package loader_test
22

33
import (
4-
"context"
54
"runtime"
65
"testing"
76

@@ -18,7 +17,7 @@ func TestProcessRootIncludesEmpty(t *testing.T) {
1817
b := &bundle.Bundle{
1918
BundleRootPath: ".",
2019
}
21-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
20+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
2221
require.NoError(t, diags.Error())
2322
}
2423

@@ -38,7 +37,7 @@ func TestProcessRootIncludesAbs(t *testing.T) {
3837
},
3938
},
4039
}
41-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
40+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
4241
require.True(t, diags.HasError())
4342
assert.ErrorContains(t, diags.Error(), "must be relative paths")
4443
}
@@ -57,7 +56,7 @@ func TestProcessRootIncludesSingleGlob(t *testing.T) {
5756
testutil.Touch(t, b.BundleRootPath, "a.yml")
5857
testutil.Touch(t, b.BundleRootPath, "b.yml")
5958

60-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
59+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
6160
require.NoError(t, diags.Error())
6261
assert.Equal(t, []string{"a.yml", "b.yml"}, b.Config.Include)
6362
}
@@ -76,7 +75,7 @@ func TestProcessRootIncludesMultiGlob(t *testing.T) {
7675
testutil.Touch(t, b.BundleRootPath, "a1.yml")
7776
testutil.Touch(t, b.BundleRootPath, "b1.yml")
7877

79-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
78+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
8079
require.NoError(t, diags.Error())
8180
assert.Equal(t, []string{"a1.yml", "b1.yml"}, b.Config.Include)
8281
}
@@ -94,7 +93,7 @@ func TestProcessRootIncludesRemoveDups(t *testing.T) {
9493

9594
testutil.Touch(t, b.BundleRootPath, "a.yml")
9695

97-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
96+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
9897
require.NoError(t, diags.Error())
9998
assert.Equal(t, []string{"a.yml"}, b.Config.Include)
10099
}
@@ -108,7 +107,7 @@ func TestProcessRootIncludesNotExists(t *testing.T) {
108107
},
109108
},
110109
}
111-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
110+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
112111
require.True(t, diags.HasError())
113112
assert.ErrorContains(t, diags.Error(), "notexist.yml defined in 'include' section does not match any files")
114113
}
@@ -172,7 +171,7 @@ func TestProcessRootIncludesGlobInRootPath(t *testing.T) {
172171
BundleRootPath: test.root,
173172
}
174173

175-
diags := bundle.Apply(context.Background(), b, loader.ProcessRootIncludes())
174+
diags := bundle.Apply(t.Context(), b, loader.ProcessRootIncludes())
176175
require.True(t, diags.HasError())
177176
assert.Len(t, diags, 1)
178177
assert.Equal(t, test.diag, diags[0])

0 commit comments

Comments
 (0)