Skip to content

Commit cc1fdc8

Browse files
TheFox0x7GiteaBotwxiaoguang
authored
Use test context in tests and new loop system in benchmarks (#33648)
Replace all contexts in tests with go1.24 t.Context() --------- Co-authored-by: Giteabot <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 3bbc482 commit cc1fdc8

File tree

108 files changed

+712
-794
lines changed

Some content is hidden

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

108 files changed

+712
-794
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/auth/oauth2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestOAuth2Application_GenerateClientSecret(t *testing.T) {
2525
func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) {
2626
assert.NoError(b, unittest.PrepareTestDatabase())
2727
app := unittest.AssertExistsAndLoadBean(b, &auth_model.OAuth2Application{ID: 1})
28-
for i := 0; i < b.N; i++ {
28+
for b.Loop() {
2929
_, _ = app.GenerateClientSecret(db.DefaultContext)
3030
}
3131
}

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/unittest/fixtures_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ func BenchmarkFixturesLoader(b *testing.B) {
9999
// BenchmarkFixturesLoader/Internal
100100
// BenchmarkFixturesLoader/Internal-12 1746 670457 ns/op
101101
b.Run("Internal", func(b *testing.B) {
102-
for i := 0; i < b.N; i++ {
102+
for b.Loop() {
103103
require.NoError(b, loaderInternal.Load())
104104
}
105105
})
106106
b.Run("Vendor", func(b *testing.B) {
107107
if loaderVendor == nil {
108108
b.Skip()
109109
}
110-
for i := 0; i < b.N; i++ {
110+
for b.Loop() {
111111
require.NoError(b, loaderVendor.Load())
112112
}
113113
})

0 commit comments

Comments
 (0)