Skip to content

Use test context in tests and new loop system in benchmarks #33648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"bufio"
"bytes"
"context"
"strings"
"testing"

Expand All @@ -15,7 +14,7 @@ import (

func TestPktLine(t *testing.T) {
// test read
ctx := context.Background()
ctx := t.Context()
s := strings.NewReader("0000")
r := bufio.NewReader(s)
result, err := readPktLine(ctx, r, pktLineTypeFlush)
Expand Down
3 changes: 1 addition & 2 deletions cmd/migrate_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"context"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestMigratePackages(t *testing.T) {
assert.NotNil(t, v)
assert.NotNil(t, f)

ctx := context.Background()
ctx := t.Context()

p := t.TempDir()

Expand Down
2 changes: 1 addition & 1 deletion models/auth/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestOAuth2Application_GenerateClientSecret(t *testing.T) {
func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) {
assert.NoError(b, unittest.PrepareTestDatabase())
app := unittest.AssertExistsAndLoadBean(b, &auth_model.OAuth2Application{ID: 1})
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = app.GenerateClientSecret(db.DefaultContext)
}
}
Expand Down
3 changes: 1 addition & 2 deletions models/issues/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package issues_test

import (
"context"
"fmt"
"sort"
"sync"
Expand Down Expand Up @@ -326,7 +325,7 @@ func TestCorrectIssueStats(t *testing.T) {
wg.Wait()

// Now we will get all issueID's that match the "Bugs are nasty" query.
issues, err := issues_model.Issues(context.TODO(), &issues_model.IssuesOptions{
issues, err := issues_model.Issues(t.Context(), &issues_model.IssuesOptions{
Paginator: &db.ListOptions{
PageSize: issueAmount,
},
Expand Down
7 changes: 3 additions & 4 deletions models/renderhelper/repo_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package renderhelper

import (
"context"
"testing"

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

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

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

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

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

// the ref path is only used to render commit message: a commit message is rendered at the commit page with its commit ID path
Expand Down
13 changes: 6 additions & 7 deletions models/renderhelper/repo_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package renderhelper

import (
"context"
"testing"

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

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

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

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

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

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

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

rendered, err := markup.RenderString(rctx, `
#+begin_src c
Expand Down
7 changes: 3 additions & 4 deletions models/renderhelper/repo_wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package renderhelper

import (
"context"
"testing"

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

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

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

t.Run("PathInTag", func(t *testing.T) {
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
rendered, err := markup.RenderString(rctx, `
<img src="LINK">
<video src="LINK">
Expand Down
3 changes: 1 addition & 2 deletions models/renderhelper/simple_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package renderhelper

import (
"context"
"testing"

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

func TestSimpleDocument(t *testing.T) {
unittest.PrepareTestEnv(t)
rctx := NewRenderContextSimpleDocument(context.Background(), "/base").WithMarkupType(markdown.MarkupName)
rctx := NewRenderContextSimpleDocument(t.Context(), "/base").WithMarkupType(markdown.MarkupName)
rendered, err := markup.RenderString(rctx, `
65f1bf27bc3bf70f64657658635e66094edbcb4d
#1
Expand Down
3 changes: 1 addition & 2 deletions models/repo/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package repo_test

import (
"context"
"path/filepath"
"testing"

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

repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
cloneLink := repo.WikiCloneLink(context.Background(), nil)
cloneLink := repo.WikiCloneLink(t.Context(), nil)
assert.Equal(t, "ssh://[email protected]:3000/user2/repo1.wiki.git", cloneLink.SSH)
assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
}
Expand Down
4 changes: 2 additions & 2 deletions models/unittest/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func BenchmarkFixturesLoader(b *testing.B) {
// BenchmarkFixturesLoader/Internal
// BenchmarkFixturesLoader/Internal-12 1746 670457 ns/op
b.Run("Internal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
require.NoError(b, loaderInternal.Load())
}
})
b.Run("Vendor", func(b *testing.B) {
if loaderVendor == nil {
b.Skip()
}
for i := 0; i < b.N; i++ {
for b.Loop() {
require.NoError(b, loaderVendor.Load())
}
})
Expand Down
3 changes: 1 addition & 2 deletions models/user/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package user

import (
"context"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -37,7 +36,7 @@ func TestUserAvatarGenerate(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
var err error
tmpDir := t.TempDir()
storage.Avatars, err = storage.NewLocalStorage(context.Background(), &setting.Storage{Path: tmpDir})
storage.Avatars, err = storage.NewLocalStorage(t.Context(), &setting.Storage{Path: tmpDir})
require.NoError(t, err)

u := unittest.AssertExistsAndLoadBean(t, &User{ID: 2})
Expand Down
7 changes: 3 additions & 4 deletions models/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package user_test

import (
"context"
"crypto/rand"
"fmt"
"strings"
Expand Down Expand Up @@ -201,7 +200,7 @@ func BenchmarkHashPassword(b *testing.B) {
pass := "password1337"
u := &user_model.User{Passwd: pass}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
u.SetPassword(pass)
}
}
Expand Down Expand Up @@ -279,7 +278,7 @@ func TestCreateUserCustomTimestamps(t *testing.T) {
err := user_model.CreateUser(db.DefaultContext, user, &user_model.Meta{})
assert.NoError(t, err)

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

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

fetched, err := user_model.GetUserByID(context.Background(), user.ID)
fetched, err := user_model.GetUserByID(t.Context(), user.ID)
assert.NoError(t, err)

assert.LessOrEqual(t, timestampStart, fetched.CreatedUnix)
Expand Down
13 changes: 6 additions & 7 deletions models/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package webhook

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -245,7 +244,7 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 0))
unittest.AssertNotExistsBean(t, hookTask)
}

Expand All @@ -261,7 +260,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 0))
unittest.AssertExistsAndLoadBean(t, hookTask)
}

Expand All @@ -278,7 +277,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1))
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 1))
unittest.AssertExistsAndLoadBean(t, hookTask)
}

Expand All @@ -295,7 +294,7 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
unittest.AssertNotExistsBean(t, hookTask)
}

Expand All @@ -311,7 +310,7 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
unittest.AssertExistsAndLoadBean(t, hookTask)
}

Expand All @@ -328,6 +327,6 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test
assert.NoError(t, err)
unittest.AssertExistsAndLoadBean(t, hookTask)

assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
unittest.AssertExistsAndLoadBean(t, hookTask)
}
5 changes: 2 additions & 3 deletions modules/cache/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
package cache

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestWithCacheContext(t *testing.T) {
ctx := WithCacheContext(context.Background())
ctx := WithCacheContext(t.Context())

v := GetContextData(ctx, "empty_field", "my_config1")
assert.Nil(t, v)
Expand Down Expand Up @@ -52,7 +51,7 @@ func TestWithCacheContext(t *testing.T) {
}

func TestWithNoCacheContext(t *testing.T) {
ctx := context.Background()
ctx := t.Context()

const field = "system_setting"

Expand Down
3 changes: 1 addition & 2 deletions modules/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package csv

import (
"bytes"
"context"
"encoding/csv"
"io"
"strconv"
Expand Down Expand Up @@ -231,7 +230,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimiters`,
}

for n, c := range cases {
delimiter := determineDelimiter(markup.NewRenderContext(context.Background()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
delimiter := determineDelimiter(markup.NewRenderContext(t.Context()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
}
}
Expand Down
Loading
Loading