Skip to content

Commit 9a4d135

Browse files
committed
refactor(tests): use t.Context
1 parent 40faa6d commit 9a4d135

File tree

99 files changed

+254
-336
lines changed

Some content is hidden

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

99 files changed

+254
-336
lines changed

cmd/hook_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"bufio"
88
"bytes"
9-
"context"
109
"strings"
1110
"testing"
1211

@@ -15,7 +14,7 @@ import (
1514

1615
func TestPktLine(t *testing.T) {
1716
// test read
18-
ctx := context.Background()
17+
ctx := t.Context()
1918
s := strings.NewReader("0000")
2019
r := bufio.NewReader(s)
2120
result, err := readPktLine(ctx, r, pktLineTypeFlush)

cmd/migrate_storage_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cmd
55

66
import (
7-
"context"
87
"os"
98
"strings"
109
"testing"
@@ -53,7 +52,7 @@ func TestMigratePackages(t *testing.T) {
5352
assert.NotNil(t, v)
5453
assert.NotNil(t, f)
5554

56-
ctx := context.Background()
55+
ctx := t.Context()
5756

5857
p := t.TempDir()
5958

models/issues/issue_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package issues_test
55

66
import (
7-
"context"
87
"fmt"
98
"sort"
109
"sync"
@@ -326,7 +325,7 @@ func TestCorrectIssueStats(t *testing.T) {
326325
wg.Wait()
327326

328327
// Now we will get all issueID's that match the "Bugs are nasty" query.
329-
issues, err := issues_model.Issues(context.TODO(), &issues_model.IssuesOptions{
328+
issues, err := issues_model.Issues(t.Context(), &issues_model.IssuesOptions{
330329
Paginator: &db.ListOptions{
331330
PageSize: issueAmount,
332331
},

models/renderhelper/repo_comment_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -21,7 +20,7 @@ func TestRepoComment(t *testing.T) {
2120
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2221

2322
t.Run("AutoLink", func(t *testing.T) {
24-
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
23+
rctx := NewRenderContextRepoComment(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2524
rendered, err := markup.RenderString(rctx, `
2625
65f1bf27bc3bf70f64657658635e66094edbcb4d
2726
#1
@@ -36,7 +35,7 @@ func TestRepoComment(t *testing.T) {
3635
})
3736

3837
t.Run("AbsoluteAndRelative", func(t *testing.T) {
39-
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
38+
rctx := NewRenderContextRepoComment(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
4039

4140
// It is Gitea's old behavior, the relative path is resolved to the repo path
4241
// It is different from GitHub, GitHub resolves relative links to current page's path
@@ -56,7 +55,7 @@ func TestRepoComment(t *testing.T) {
5655
})
5756

5857
t.Run("WithCurrentRefPath", func(t *testing.T) {
59-
rctx := NewRenderContextRepoComment(context.Background(), repo1, RepoCommentOptions{CurrentRefPath: "/commit/1234"}).
58+
rctx := NewRenderContextRepoComment(t.Context(), repo1, RepoCommentOptions{CurrentRefPath: "/commit/1234"}).
6059
WithMarkupType(markdown.MarkupName)
6160

6261
// the ref path is only used to render commit message: a commit message is rendered at the commit page with its commit ID path

models/renderhelper/repo_file_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -22,7 +21,7 @@ func TestRepoFile(t *testing.T) {
2221
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2322

2423
t.Run("AutoLink", func(t *testing.T) {
25-
rctx := NewRenderContextRepoFile(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
24+
rctx := NewRenderContextRepoFile(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2625
rendered, err := markup.RenderString(rctx, `
2726
65f1bf27bc3bf70f64657658635e66094edbcb4d
2827
#1
@@ -37,7 +36,7 @@ func TestRepoFile(t *testing.T) {
3736
})
3837

3938
t.Run("AbsoluteAndRelative", func(t *testing.T) {
40-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{CurrentRefPath: "branch/main"}).
39+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{CurrentRefPath: "branch/main"}).
4140
WithMarkupType(markdown.MarkupName)
4241
rendered, err := markup.RenderString(rctx, `
4342
[/test](/test)
@@ -55,7 +54,7 @@ func TestRepoFile(t *testing.T) {
5554
})
5655

5756
t.Run("WithCurrentRefPath", func(t *testing.T) {
58-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{CurrentRefPath: "/commit/1234"}).
57+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{CurrentRefPath: "/commit/1234"}).
5958
WithMarkupType(markdown.MarkupName)
6059
rendered, err := markup.RenderString(rctx, `
6160
[/test](/test)
@@ -68,7 +67,7 @@ func TestRepoFile(t *testing.T) {
6867
})
6968

7069
t.Run("WithCurrentRefPathByTag", func(t *testing.T) {
71-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
70+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{
7271
CurrentRefPath: "/commit/1234",
7372
CurrentTreePath: "my-dir",
7473
}).
@@ -89,7 +88,7 @@ func TestRepoFileOrgMode(t *testing.T) {
8988
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
9089

9190
t.Run("Links", func(t *testing.T) {
92-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
91+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{
9392
CurrentRefPath: "/commit/1234",
9493
CurrentTreePath: "my-dir",
9594
}).WithRelativePath("my-dir/a.org")
@@ -106,7 +105,7 @@ func TestRepoFileOrgMode(t *testing.T) {
106105
})
107106

108107
t.Run("CodeHighlight", func(t *testing.T) {
109-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{}).WithRelativePath("my-dir/a.org")
108+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{}).WithRelativePath("my-dir/a.org")
110109

111110
rendered, err := markup.RenderString(rctx, `
112111
#+begin_src c

models/renderhelper/repo_wiki_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -20,7 +19,7 @@ func TestRepoWiki(t *testing.T) {
2019
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2120

2221
t.Run("AutoLink", func(t *testing.T) {
23-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
22+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2423
rendered, err := markup.RenderString(rctx, `
2524
65f1bf27bc3bf70f64657658635e66094edbcb4d
2625
#1
@@ -35,7 +34,7 @@ func TestRepoWiki(t *testing.T) {
3534
})
3635

3736
t.Run("AbsoluteAndRelative", func(t *testing.T) {
38-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
37+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
3938
rendered, err := markup.RenderString(rctx, `
4039
[/test](/test)
4140
[./test](./test)
@@ -52,7 +51,7 @@ func TestRepoWiki(t *testing.T) {
5251
})
5352

5453
t.Run("PathInTag", func(t *testing.T) {
55-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
54+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
5655
rendered, err := markup.RenderString(rctx, `
5756
<img src="LINK">
5857
<video src="LINK">

models/renderhelper/simple_document_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -16,7 +15,7 @@ import (
1615

1716
func TestSimpleDocument(t *testing.T) {
1817
unittest.PrepareTestEnv(t)
19-
rctx := NewRenderContextSimpleDocument(context.Background(), "/base").WithMarkupType(markdown.MarkupName)
18+
rctx := NewRenderContextSimpleDocument(t.Context(), "/base").WithMarkupType(markdown.MarkupName)
2019
rendered, err := markup.RenderString(rctx, `
2120
65f1bf27bc3bf70f64657658635e66094edbcb4d
2221
#1

models/repo/wiki_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package repo_test
55

66
import (
7-
"context"
87
"path/filepath"
98
"testing"
109

@@ -19,7 +18,7 @@ func TestRepository_WikiCloneLink(t *testing.T) {
1918
assert.NoError(t, unittest.PrepareTestDatabase())
2019

2120
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
22-
cloneLink := repo.WikiCloneLink(context.Background(), nil)
21+
cloneLink := repo.WikiCloneLink(t.Context(), nil)
2322
assert.Equal(t, "ssh://[email protected]:3000/user2/repo1.wiki.git", cloneLink.SSH)
2423
assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
2524
}

models/user/avatar_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package user
55

66
import (
7-
"context"
87
"io"
98
"strings"
109
"testing"
@@ -37,7 +36,7 @@ func TestUserAvatarGenerate(t *testing.T) {
3736
assert.NoError(t, unittest.PrepareTestDatabase())
3837
var err error
3938
tmpDir := t.TempDir()
40-
storage.Avatars, err = storage.NewLocalStorage(context.Background(), &setting.Storage{Path: tmpDir})
39+
storage.Avatars, err = storage.NewLocalStorage(t.Context(), &setting.Storage{Path: tmpDir})
4140
require.NoError(t, err)
4241

4342
u := unittest.AssertExistsAndLoadBean(t, &User{ID: 2})

models/user/user_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package user_test
55

66
import (
7-
"context"
87
"crypto/rand"
98
"fmt"
109
"strings"
@@ -279,7 +278,7 @@ func TestCreateUserCustomTimestamps(t *testing.T) {
279278
err := user_model.CreateUser(db.DefaultContext, user, &user_model.Meta{})
280279
assert.NoError(t, err)
281280

282-
fetched, err := user_model.GetUserByID(context.Background(), user.ID)
281+
fetched, err := user_model.GetUserByID(t.Context(), user.ID)
283282
assert.NoError(t, err)
284283
assert.Equal(t, creationTimestamp, fetched.CreatedUnix)
285284
assert.Equal(t, creationTimestamp, fetched.UpdatedUnix)
@@ -306,7 +305,7 @@ func TestCreateUserWithoutCustomTimestamps(t *testing.T) {
306305

307306
timestampEnd := time.Now().Unix()
308307

309-
fetched, err := user_model.GetUserByID(context.Background(), user.ID)
308+
fetched, err := user_model.GetUserByID(t.Context(), user.ID)
310309
assert.NoError(t, err)
311310

312311
assert.LessOrEqual(t, timestampStart, fetched.CreatedUnix)

0 commit comments

Comments
 (0)