Skip to content

Commit 233fe03

Browse files
authored
test: Use t.Context() instead of context.Background() (#3750)
1 parent b7f068f commit 233fe03

File tree

171 files changed

+1571
-1739
lines changed

Some content is hidden

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

171 files changed

+1571
-1739
lines changed

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ linters:
2424
- tparallel
2525
- unconvert
2626
- unparam
27+
- usetesting
2728
- whitespace
2829
settings:
2930
errorlint:
@@ -126,6 +127,14 @@ linters:
126127
checks:
127128
- "all"
128129
- "-QF1008" # allow embedded field in selector
130+
usetesting:
131+
context-background: true
132+
context-todo: true
133+
os-chdir: true
134+
os-mkdir-temp: true
135+
os-setenv: true
136+
os-create-temp: true
137+
os-temp-dir: true
129138
custom:
130139
sliceofpointers:
131140
type: module

github/actions_artifacts_test.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package github
77

88
import (
9-
"context"
109
"errors"
1110
"fmt"
1211
"net/http"
@@ -36,7 +35,7 @@ func TestActionsService_ListArtifacts(t *testing.T) {
3635
Name: Ptr("TheArtifact"),
3736
ListOptions: ListOptions{Page: 2},
3837
}
39-
ctx := context.Background()
38+
ctx := t.Context()
4039
artifacts, _, err := client.Actions.ListArtifacts(ctx, "o", "r", opts)
4140
if err != nil {
4241
t.Errorf("Actions.ListArtifacts returned error: %v", err)
@@ -66,7 +65,7 @@ func TestActionsService_ListArtifacts_invalidOwner(t *testing.T) {
6665
t.Parallel()
6766
client, _, _ := setup(t)
6867

69-
ctx := context.Background()
68+
ctx := t.Context()
7069
_, _, err := client.Actions.ListArtifacts(ctx, "%", "r", nil)
7170
testURLParseError(t, err)
7271
}
@@ -75,7 +74,7 @@ func TestActionsService_ListArtifacts_invalidRepo(t *testing.T) {
7574
t.Parallel()
7675
client, _, _ := setup(t)
7776

78-
ctx := context.Background()
77+
ctx := t.Context()
7978
_, _, err := client.Actions.ListArtifacts(ctx, "o", "%", nil)
8079
testURLParseError(t, err)
8180
}
@@ -89,7 +88,7 @@ func TestActionsService_ListArtifacts_notFound(t *testing.T) {
8988
w.WriteHeader(http.StatusNotFound)
9089
})
9190

92-
ctx := context.Background()
91+
ctx := t.Context()
9392
artifacts, resp, err := client.Actions.ListArtifacts(ctx, "o", "r", nil)
9493
if err == nil {
9594
t.Error("Expected HTTP 404 response")
@@ -118,7 +117,7 @@ func TestActionsService_ListWorkflowRunArtifacts(t *testing.T) {
118117
})
119118

120119
opts := &ListOptions{Page: 2}
121-
ctx := context.Background()
120+
ctx := t.Context()
122121
artifacts, _, err := client.Actions.ListWorkflowRunArtifacts(ctx, "o", "r", 1, opts)
123122
if err != nil {
124123
t.Errorf("Actions.ListWorkflowRunArtifacts returned error: %v", err)
@@ -148,7 +147,7 @@ func TestActionsService_ListWorkflowRunArtifacts_invalidOwner(t *testing.T) {
148147
t.Parallel()
149148
client, _, _ := setup(t)
150149

151-
ctx := context.Background()
150+
ctx := t.Context()
152151
_, _, err := client.Actions.ListWorkflowRunArtifacts(ctx, "%", "r", 1, nil)
153152
testURLParseError(t, err)
154153
}
@@ -157,7 +156,7 @@ func TestActionsService_ListWorkflowRunArtifacts_invalidRepo(t *testing.T) {
157156
t.Parallel()
158157
client, _, _ := setup(t)
159158

160-
ctx := context.Background()
159+
ctx := t.Context()
161160
_, _, err := client.Actions.ListWorkflowRunArtifacts(ctx, "o", "%", 1, nil)
162161
testURLParseError(t, err)
163162
}
@@ -171,7 +170,7 @@ func TestActionsService_ListWorkflowRunArtifacts_notFound(t *testing.T) {
171170
w.WriteHeader(http.StatusNotFound)
172171
})
173172

174-
ctx := context.Background()
173+
ctx := t.Context()
175174
artifacts, resp, err := client.Actions.ListWorkflowRunArtifacts(ctx, "o", "r", 1, nil)
176175
if err == nil {
177176
t.Error("Expected HTTP 404 response")
@@ -199,7 +198,7 @@ func TestActionsService_GetArtifact(t *testing.T) {
199198
}`)
200199
})
201200

202-
ctx := context.Background()
201+
ctx := t.Context()
203202
artifact, _, err := client.Actions.GetArtifact(ctx, "o", "r", 1)
204203
if err != nil {
205204
t.Errorf("Actions.GetArtifact returned error: %v", err)
@@ -235,7 +234,7 @@ func TestActionsService_GetArtifact_invalidOwner(t *testing.T) {
235234
t.Parallel()
236235
client, _, _ := setup(t)
237236

238-
ctx := context.Background()
237+
ctx := t.Context()
239238
_, _, err := client.Actions.GetArtifact(ctx, "%", "r", 1)
240239
testURLParseError(t, err)
241240
}
@@ -244,7 +243,7 @@ func TestActionsService_GetArtifact_invalidRepo(t *testing.T) {
244243
t.Parallel()
245244
client, _, _ := setup(t)
246245

247-
ctx := context.Background()
246+
ctx := t.Context()
248247
_, _, err := client.Actions.GetArtifact(ctx, "o", "%", 1)
249248
testURLParseError(t, err)
250249
}
@@ -258,7 +257,7 @@ func TestActionsService_GetArtifact_notFound(t *testing.T) {
258257
w.WriteHeader(http.StatusNotFound)
259258
})
260259

261-
ctx := context.Background()
260+
ctx := t.Context()
262261
artifact, resp, err := client.Actions.GetArtifact(ctx, "o", "r", 1)
263262
if err == nil {
264263
t.Error("Expected HTTP 404 response")
@@ -299,7 +298,7 @@ func TestActionsService_DownloadArtifact(t *testing.T) {
299298
http.Redirect(w, r, "https://github.com/artifact", http.StatusFound)
300299
})
301300

302-
ctx := context.Background()
301+
ctx := t.Context()
303302
url, resp, err := client.Actions.DownloadArtifact(ctx, "o", "r", 1, 1)
304303
if err != nil {
305304
t.Errorf("Actions.DownloadArtifact returned error: %v", err)
@@ -355,7 +354,7 @@ func TestActionsService_DownloadArtifact_invalidOwner(t *testing.T) {
355354
client, _, _ := setup(t)
356355
client.RateLimitRedirectionalEndpoints = tc.respectRateLimits
357356

358-
ctx := context.Background()
357+
ctx := t.Context()
359358
_, _, err := client.Actions.DownloadArtifact(ctx, "%", "r", 1, 1)
360359
testURLParseError(t, err)
361360
})
@@ -384,7 +383,7 @@ func TestActionsService_DownloadArtifact_invalidRepo(t *testing.T) {
384383
client, _, _ := setup(t)
385384
client.RateLimitRedirectionalEndpoints = tc.respectRateLimits
386385

387-
ctx := context.Background()
386+
ctx := t.Context()
388387
_, _, err := client.Actions.DownloadArtifact(ctx, "o", "%", 1, 1)
389388
testURLParseError(t, err)
390389
})
@@ -418,7 +417,7 @@ func TestActionsService_DownloadArtifact_StatusMovedPermanently_dontFollowRedire
418417
http.Redirect(w, r, "https://github.com/artifact", http.StatusMovedPermanently)
419418
})
420419

421-
ctx := context.Background()
420+
ctx := t.Context()
422421
_, resp, _ := client.Actions.DownloadArtifact(ctx, "o", "r", 1, 0)
423422
if resp.StatusCode != http.StatusMovedPermanently {
424423
t.Errorf("Actions.DownloadArtifact return status %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
@@ -459,7 +458,7 @@ func TestActionsService_DownloadArtifact_StatusMovedPermanently_followRedirects(
459458
http.Redirect(w, r, "https://github.com/artifact", http.StatusFound)
460459
})
461460

462-
ctx := context.Background()
461+
ctx := t.Context()
463462
url, resp, err := client.Actions.DownloadArtifact(ctx, "o", "r", 1, 1)
464463
if err != nil {
465464
t.Errorf("Actions.DownloadArtifact return error: %v", err)
@@ -507,7 +506,7 @@ func TestActionsService_DownloadArtifact_unexpectedCode(t *testing.T) {
507506
w.WriteHeader(http.StatusNoContent)
508507
})
509508

510-
ctx := context.Background()
509+
ctx := t.Context()
511510
url, resp, err := client.Actions.DownloadArtifact(ctx, "o", "r", 1, 1)
512511
if err == nil {
513512
t.Fatal("Actions.DownloadArtifact should return error on unexpected code")
@@ -533,7 +532,7 @@ func TestActionsService_DeleteArtifact(t *testing.T) {
533532
testMethod(t, r, "DELETE")
534533
})
535534

536-
ctx := context.Background()
535+
ctx := t.Context()
537536
_, err := client.Actions.DeleteArtifact(ctx, "o", "r", 1)
538537
if err != nil {
539538
t.Errorf("Actions.DeleteArtifact return error: %v", err)
@@ -554,7 +553,7 @@ func TestActionsService_DeleteArtifact_invalidOwner(t *testing.T) {
554553
t.Parallel()
555554
client, _, _ := setup(t)
556555

557-
ctx := context.Background()
556+
ctx := t.Context()
558557
_, err := client.Actions.DeleteArtifact(ctx, "%", "r", 1)
559558
testURLParseError(t, err)
560559
}
@@ -563,7 +562,7 @@ func TestActionsService_DeleteArtifact_invalidRepo(t *testing.T) {
563562
t.Parallel()
564563
client, _, _ := setup(t)
565564

566-
ctx := context.Background()
565+
ctx := t.Context()
567566
_, err := client.Actions.DeleteArtifact(ctx, "o", "%", 1)
568567
testURLParseError(t, err)
569568
}
@@ -577,7 +576,7 @@ func TestActionsService_DeleteArtifact_notFound(t *testing.T) {
577576
w.WriteHeader(http.StatusNotFound)
578577
})
579578

580-
ctx := context.Background()
579+
ctx := t.Context()
581580
resp, err := client.Actions.DeleteArtifact(ctx, "o", "r", 1)
582581
if err == nil {
583582
t.Error("Expected HTTP 404 response")

0 commit comments

Comments
 (0)