Skip to content

Commit cab6d16

Browse files
committed
Update tests for go1.23
- Extract `func testContext(t *testing.T) context.Context` with two different implementations. g1.24 and above will continue to use `(*testing.T).Context` as before, while go1.23 gets a hand-rolled approximation. - Update all callsites of `(*testing.T).Context` to use `testContext` instead. - Once the module Go version is updated to go1.24 or later, this change can be reversed.
1 parent 0055a93 commit cab6d16

22 files changed

+59
-30
lines changed

context_new_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build go1.24
2+
3+
package unstructured
4+
5+
import (
6+
"context"
7+
"testing"
8+
)
9+
10+
// textContext mimics [*testing.T.Context], which was added in go1.24.
11+
func testContext(t *testing.T) context.Context {
12+
return t.Context()
13+
}

context_old_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !go1.24
2+
3+
package unstructured
4+
5+
import (
6+
"context"
7+
"testing"
8+
)
9+
10+
// textContext mimics [*testing.T.Context], which was added in go1.24.
11+
func testContext(t *testing.T) context.Context {
12+
t.Helper()
13+
ctx, cancel := context.WithCancel(context.Background())
14+
t.Cleanup(cancel)
15+
return ctx
16+
}

destination_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestCreateDestination(t *testing.T) {
3232
w.Write(response)
3333
}
3434

35-
destination, err := client.CreateDestination(t.Context(), CreateDestinationRequest{
35+
destination, err := client.CreateDestination(testContext(t), CreateDestinationRequest{
3636
Name: "test_destination_name",
3737

3838
Config: &S3ConnectorConfig{

destination_delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestDeleteDestination(t *testing.T) {
2727
w.Write(response)
2828
}
2929

30-
err := client.DeleteDestination(t.Context(), "b25d4161-77a0-4e08-b65e-86f398ce15ad")
30+
err := client.DeleteDestination(testContext(t), "b25d4161-77a0-4e08-b65e-86f398ce15ad")
3131
if err != nil {
3232
t.Fatalf("failed to delete destination: %v", err)
3333
}

destination_get_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestGetDestination(t *testing.T) {
4040
w.Write(response)
4141
}
4242

43-
destination, err := client.GetDestination(t.Context(), id)
43+
destination, err := client.GetDestination(testContext(t), id)
4444
if err != nil {
4545
t.Fatalf("failed to get destination: %v", err)
4646
}
@@ -70,7 +70,7 @@ func TestGetDestinationNotFound(t *testing.T) {
7070
http.Error(w, "destination ID "+r.PathValue("id")+" not found", http.StatusNotFound)
7171
}
7272

73-
_, err := client.GetDestination(t.Context(), id)
73+
_, err := client.GetDestination(testContext(t), id)
7474
if err == nil {
7575
t.Fatalf("expected error, got nil")
7676
}

destination_list_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestListDestinations(t *testing.T) {
3737
w.Write(response)
3838
}
3939

40-
destinations, err := client.ListDestinations(t.Context(), "")
40+
destinations, err := client.ListDestinations(testContext(t), "")
4141
if err != nil {
4242
t.Fatalf("failed to list destinations: %v", err)
4343
}
@@ -74,7 +74,7 @@ func TestListDestinationsEmpty(t *testing.T) {
7474
w.Write([]byte("[]"))
7575
}
7676

77-
destinations, err := client.ListDestinations(t.Context(), "")
77+
destinations, err := client.ListDestinations(testContext(t), "")
7878
if err != nil {
7979
t.Fatalf("failed to list destinations: %v", err)
8080
}
@@ -107,7 +107,7 @@ func TestListDestinationsErrorCode(t *testing.T) {
107107
w.WriteHeader(code)
108108
}
109109

110-
_, err := client.ListDestinations(t.Context(), "")
110+
_, err := client.ListDestinations(testContext(t), "")
111111
if err == nil {
112112
t.Fatalf("expected error, got nil")
113113
}

destination_update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestUpdateDestination(t *testing.T) {
4040
w.Write(response)
4141
}
4242

43-
updated, err := client.UpdateDestination(t.Context(), UpdateDestinationRequest{
43+
updated, err := client.UpdateDestination(testContext(t), UpdateDestinationRequest{
4444
ID: id,
4545
Config: &S3ConnectorConfig{
4646
RemoteURL: "s3://mock-s3-connector",

job_cancel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestCancelJob(t *testing.T) {
3030
w.Write(response)
3131
}
3232

33-
err := client.CancelJob(t.Context(), id)
33+
err := client.CancelJob(testContext(t), id)
3434
if err != nil {
3535
t.Fatalf("failed to cancel job: %v", err)
3636
}

job_get_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestGetJob(t *testing.T) {
3636
w.Write(response)
3737
}
3838

39-
job, err := client.GetJob(t.Context(), id)
39+
job, err := client.GetJob(testContext(t), id)
4040
if err != nil {
4141
t.Fatalf("failed to get job: %v", err)
4242
}
@@ -62,7 +62,7 @@ func TestGetJobNotFound(t *testing.T) {
6262
http.Error(w, "job ID "+r.PathValue("id")+" not found", http.StatusNotFound)
6363
}
6464

65-
_, err := client.GetJob(t.Context(), id)
65+
_, err := client.GetJob(testContext(t), id)
6666
if err == nil {
6767
t.Fatalf("expected error, got nil")
6868
}
@@ -102,7 +102,7 @@ func TestGetJobError(t *testing.T) {
102102
w.WriteHeader(code)
103103
}
104104

105-
_, err := client.GetJob(t.Context(), id)
105+
_, err := client.GetJob(testContext(t), id)
106106
if err == nil {
107107
t.Fatalf("expected error, got nil")
108108
}

jobs_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestListJobs(t *testing.T) {
3030
w.Write(response)
3131
}
3232

33-
jobs, err := client.ListJobs(t.Context(), &ListJobsRequest{})
33+
jobs, err := client.ListJobs(testContext(t), &ListJobsRequest{})
3434
if err != nil {
3535
t.Fatalf("failed to list jobs: %v", err)
3636
}

0 commit comments

Comments
 (0)