Skip to content

Commit 7e7cd2d

Browse files
authored
fix: use T.Context in tests (#2768)
T.Context returns a context that is canceled just before Cleanup-registered functions are called, and is intended to be used in tests. See https://pkg.go.dev/testing#T.Context.
1 parent 87a1005 commit 7e7cd2d

File tree

10 files changed

+18
-24
lines changed

10 files changed

+18
-24
lines changed

internal/automation/cloudbuild_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestRunCloudBuildTrigger(t *testing.T) {
7878
},
7979
} {
8080
t.Run(test.name, func(t *testing.T) {
81-
ctx := context.Background()
81+
ctx := t.Context()
8282
client := &mockCloudBuildClient{
8383
runError: test.runError,
8484
buildTriggers: make([]*cloudbuildpb.BuildTrigger, 0),
@@ -144,7 +144,7 @@ func TestFindTriggerIdByName(t *testing.T) {
144144
},
145145
} {
146146
t.Run(test.name, func(t *testing.T) {
147-
ctx := context.Background()
147+
ctx := t.Context()
148148
client := &mockCloudBuildClient{
149149
runError: test.runError,
150150
buildTriggers: test.buildTriggers,
@@ -209,7 +209,7 @@ func TestRunCloudBuildTriggerByName(t *testing.T) {
209209
},
210210
} {
211211
t.Run(test.name, func(t *testing.T) {
212-
ctx := context.Background()
212+
ctx := t.Context()
213213
client := &mockCloudBuildClient{
214214
runError: test.runError,
215215
buildTriggers: test.buildTriggers,

internal/automation/trigger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestRunCommandWithClient(t *testing.T) {
184184
},
185185
} {
186186
t.Run(test.name, func(t *testing.T) {
187-
ctx := context.Background()
187+
ctx := t.Context()
188188
client := &mockCloudBuildClient{
189189
runError: test.runError,
190190
buildTriggers: test.buildTriggers,
@@ -334,7 +334,7 @@ func TestRunCommandWithConfig(t *testing.T) {
334334
},
335335
} {
336336
t.Run(test.name, func(t *testing.T) {
337-
ctx := context.Background()
337+
ctx := t.Context()
338338
client := &mockCloudBuildClient{
339339
runError: test.runError,
340340
buildTriggers: buildTriggers,

internal/container/java/execv/execv_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package execv
1616

1717
import (
18-
"context"
1918
"errors"
2019
"os/exec"
2120
"strings"
@@ -56,7 +55,7 @@ func TestRun(t *testing.T) {
5655
}
5756
for _, tt := range tests {
5857
t.Run(tt.name, func(t *testing.T) {
59-
err := Run(context.Background(), tt.args, ".")
58+
err := Run(t.Context(), tt.args, ".")
6059
if (err != nil) != tt.wantErr {
6160
t.Fatalf("Run() error = %v, wantErr %v", err, tt.wantErr)
6261
}

internal/container/java/generate/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ java_gapic_library(
274274
if err != nil {
275275
t.Fatalf("failed to create generate config: %v", err)
276276
}
277-
if err := Generate(context.Background(), cfg); (err != nil) != tt.wantErr {
277+
if err := Generate(t.Context(), cfg); (err != nil) != tt.wantErr {
278278
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
279279
}
280280
if protocRunCount != tt.wantProtocRunCount {

internal/docker/docker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ func TestReleaseStageRequestContent(t *testing.T) {
10201020
Output: filepath.Join(tmpDir, "output"),
10211021
LibrarianConfig: &config.LibrarianConfig{},
10221022
}
1023-
if err := d.ReleaseStage(context.Background(), req); err != nil {
1023+
if err := d.ReleaseStage(t.Context(), req); err != nil {
10241024
t.Fatalf("d.ReleaseStage() failed: %v", err)
10251025
}
10261026
}

internal/librarian/command_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package librarian
1616

1717
import (
18-
"context"
1918
"errors"
2019
"fmt"
2120
"os"
@@ -1564,7 +1563,7 @@ func TestCommitAndPush(t *testing.T) {
15641563
prBodyBuilder: test.prBodyBuilder,
15651564
}
15661565

1567-
err := commitAndPush(context.Background(), commitInfo)
1566+
err := commitAndPush(t.Context(), commitInfo)
15681567

15691568
if test.wantErr {
15701569
if err == nil {
@@ -1746,7 +1745,7 @@ func TestAddLabelsToPullRequest(t *testing.T) {
17461745
t.Run(test.name, func(t *testing.T) {
17471746
client := test.mockGithubClient
17481747
prMetadata := test.prMetadata
1749-
err := addLabelsToPullRequest(context.Background(), client, test.wantPullRequestLabels, &prMetadata)
1748+
err := addLabelsToPullRequest(t.Context(), client, test.wantPullRequestLabels, &prMetadata)
17501749

17511750
if test.wantErr {
17521751
if err == nil {

internal/librarian/generate_command_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package librarian
1616

1717
import (
18-
"context"
1918
"errors"
2019
"os"
2120
"path/filepath"
@@ -358,7 +357,7 @@ func TestRunConfigureCommand(t *testing.T) {
358357
}
359358
}
360359

361-
_, err := r.runConfigureCommand(context.Background(), outputDir)
360+
_, err := r.runConfigureCommand(t.Context(), outputDir)
362361

363362
if test.wantErr {
364363
if err == nil {
@@ -949,7 +948,7 @@ func TestGenerateScenarios(t *testing.T) {
949948
}
950949
}
951950

952-
err := r.run(context.Background())
951+
err := r.run(t.Context())
953952
if test.wantErr {
954953
if err == nil {
955954
t.Fatalf("%s should return error", test.name)
@@ -1066,7 +1065,7 @@ func TestGenerateSingleLibraryCommand(t *testing.T) {
10661065
}
10671066
}
10681067

1069-
status, err := r.generateSingleLibrary(context.Background(), r.library, r.workRoot)
1068+
status, err := r.generateSingleLibrary(t.Context(), r.library, r.workRoot)
10701069
if test.wantErr {
10711070
if err == nil {
10721071
t.Fatalf("%s should return error", test.name)

internal/librarian/librarian_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package librarian
1616

1717
import (
1818
"bytes"
19-
"context"
2019
"fmt"
2120
"io"
2221
"log/slog"
@@ -92,7 +91,7 @@ func TestVerboseFlag(t *testing.T) {
9291
// Reset logger to default for test isolation.
9392
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
9493

95-
_ = Run(context.Background(), test.args...)
94+
_ = Run(t.Context(), test.args...)
9695

9796
// Restore stderr and read the output.
9897
w.Close()
@@ -119,7 +118,7 @@ func TestVerboseFlag(t *testing.T) {
119118
}
120119

121120
func TestGenerate_DefaultBehavior(t *testing.T) {
122-
ctx := context.Background()
121+
ctx := t.Context()
123122

124123
// 1. Set up a mock repository with a state file
125124
repo := newTestGitRepo(t)

internal/librarian/state_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package librarian
1616

1717
import (
18-
"context"
1918
"fmt"
2019
"os"
2120
"path/filepath"
@@ -75,7 +74,7 @@ libraries:
7574
- src/b
7675
apis:
7776
- path: a/b/v1
78-
service_config:
77+
service_config:
7978
`,
8079
source: "/non-existed-path",
8180
want: nil,
@@ -478,7 +477,7 @@ func TestLoadRepoStateFromGitHub(t *testing.T) {
478477
},
479478
} {
480479
t.Run(test.name, func(t *testing.T) {
481-
got, err := loadRepoStateFromGitHub(context.Background(), test.ghClient, test.branch)
480+
got, err := loadRepoStateFromGitHub(t.Context(), test.ghClient, test.branch)
482481
if test.wantErr {
483482
if err == nil {
484483
t.Fatal("loadRepoStateFromGitHub() should fail")

system_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package librarian
1616

1717
import (
18-
"context"
1918
"fmt"
2019
"log/slog"
2120
"os"
@@ -79,7 +78,7 @@ func TestGetRawContentSystem(t *testing.T) {
7978
}
8079

8180
client := github.NewClient(testToken, repo)
82-
got, err := client.GetRawContent(context.Background(), test.path, "main")
81+
got, err := client.GetRawContent(t.Context(), test.path, "main")
8382

8483
if test.wantErr {
8584
if err == nil {

0 commit comments

Comments
 (0)