Skip to content

Commit

Permalink
all(test): prefer t.Context where possible
Browse files Browse the repository at this point in the history
t.Context was added in Go 1.24 to provide a context
that is valid only for the lifetime of a test function.

This change updates test functions to use this context where possible.

Cases where it can't be used:

- rapid tests
- t.Cleanup functions
  • Loading branch information
abhinav committed Feb 15, 2025
1 parent f280938 commit a67417c
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 171 deletions.
4 changes: 2 additions & 2 deletions branch_submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestBranchSubmit_listChangeTemplates(t *testing.T) {
t.Run("NoTimeout", func(t *testing.T) {
log := logutil.TestLogger(t)
ctx := context.Background()
ctx := t.Context()
tmpl := &forge.ChangeTemplate{}
svc := &spiceTemplateServiceStub{
ListChangeTemplatesF: func(ctx context.Context, _ string, _ forge.Repository) ([]*forge.ChangeTemplate, error) {
Expand All @@ -34,7 +34,7 @@ func TestBranchSubmit_listChangeTemplates(t *testing.T) {

t.Run("Timeout", func(t *testing.T) {
log := logutil.TestLogger(t)
ctx := context.Background()
ctx := t.Context()

svc := &spiceTemplateServiceStub{
ListChangeTemplatesF: func(ctx context.Context, _ string, _ forge.Repository) ([]*forge.ChangeTemplate, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.abhg.dev/gs

go 1.23.4
go 1.24.0

require (
github.com/alecthomas/kong v1.8.1
Expand Down
14 changes: 6 additions & 8 deletions internal/forge/github/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package github

import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
Expand Down Expand Up @@ -75,11 +74,10 @@ func TestAuthHasGitHubToken(t *testing.T) {
Log: log.New(&logBuffer),
}

ctx := context.Background()
view := &ui.FileView{W: io.Discard}

t.Run("AuthenticationFlow", func(t *testing.T) {
_, err := f.AuthenticationFlow(ctx, view)
_, err := f.AuthenticationFlow(t.Context(), view)
require.Error(t, err)
assert.ErrorContains(t, err, "already authenticated")
assert.Contains(t, logBuffer.String(), "Already authenticated")
Expand Down Expand Up @@ -181,7 +179,7 @@ func TestDeviceFlowAuthenticator(t *testing.T) {
DeviceAuthURL: srv.URL + "/device/code",
TokenURL: srv.URL + "/oauth/access_token",
},
}).Authenticate(context.Background(), &ui.FileView{W: io.Discard})
}).Authenticate(t.Context(), &ui.FileView{W: io.Discard})
require.NoError(t, err)

assert.Equal(t, "my-token", tok.AccessToken)
Expand All @@ -207,7 +205,7 @@ func TestAuthenticationFlow_PAT(t *testing.T) {
uitest.RunScripts(t, func(t testing.TB, ts *testscript.TestScript, view ui.InteractiveView) {
wantToken := strings.TrimSpace(ts.ReadFile("want_token"))

got, err := new(Forge).AuthenticationFlow(context.Background(), view)
got, err := new(Forge).AuthenticationFlow(t.Context(), view)
require.NoError(t, err)

assert.Equal(t, &AuthenticationToken{
Expand All @@ -228,7 +226,7 @@ func TestAuthCLI(t *testing.T) {
runCmd: func(*exec.Cmd) error {
return nil
},
}).Authenticate(context.Background(), discardView)
}).Authenticate(t.Context(), discardView)
require.NoError(t, err)

f := Forge{
Expand All @@ -253,7 +251,7 @@ func TestAuthCLI(t *testing.T) {
Stderr: []byte("great sadness"),
}
},
}).Authenticate(context.Background(), discardView)
}).Authenticate(t.Context(), discardView)
require.Error(t, err)
assert.ErrorContains(t, err, "not authenticated")
assert.ErrorContains(t, err, "great sadness")
Expand All @@ -265,7 +263,7 @@ func TestAuthCLI(t *testing.T) {
runCmd: func(*exec.Cmd) error {
return errors.New("gh not found")
},
}).Authenticate(context.Background(), discardView)
}).Authenticate(t.Context(), discardView)
require.Error(t, err)
assert.ErrorContains(t, err, "gh not found")
})
Expand Down
5 changes: 2 additions & 3 deletions internal/forge/github/comment_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package github

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -128,7 +127,7 @@ func TestListChangeComments(t *testing.T) {
defer srv.Close()

repo, err := newRepository(
context.Background(), new(Forge),
t.Context(), new(Forge),
"owner", "repo",
logutil.TestLogger(t),
githubv4.NewEnterpriseClient(srv.URL, nil),
Expand All @@ -138,7 +137,7 @@ func TestListChangeComments(t *testing.T) {

prID := PR{Number: 1, GQLID: "prID"}

ctx := context.Background()
ctx := t.Context()
var bodies []string
for comment, err := range repo.ListChangeComments(ctx, &prID, tt.opts) {
require.NoError(t, err)
Expand Down
Loading

0 comments on commit a67417c

Please sign in to comment.