From 9a4d135286c653d21fe08bf0283728550b64c6f9 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Wed, 19 Feb 2025 22:29:09 +0100 Subject: [PATCH 1/2] refactor(tests): use t.Context --- cmd/hook_test.go | 3 +-- cmd/migrate_storage_test.go | 3 +-- models/issues/issue_test.go | 3 +-- models/renderhelper/repo_comment_test.go | 7 +++-- models/renderhelper/repo_file_test.go | 13 +++++---- models/renderhelper/repo_wiki_test.go | 7 +++-- models/renderhelper/simple_document_test.go | 3 +-- models/repo/wiki_test.go | 3 +-- models/user/avatar_test.go | 3 +-- models/user/user_test.go | 5 ++-- models/webhook/webhook_test.go | 13 +++++---- modules/cache/context_test.go | 5 ++-- modules/csv/csv_test.go | 3 +-- modules/git/blame_sha256_test.go | 2 +- modules/git/blame_test.go | 2 +- modules/git/command_race_test.go | 4 +-- modules/git/command_test.go | 15 +++++------ modules/git/commit_info_test.go | 5 ++-- modules/git/commit_submodule_file_test.go | 9 +++---- modules/git/commit_test.go | 3 +-- modules/git/grep_test.go | 15 +++++------ modules/git/notes_test.go | 9 +++---- modules/git/repo_test.go | 7 +++-- modules/git/submodule_test.go | 3 +-- modules/git/url/url_test.go | 10 +++---- modules/globallock/globallock_test.go | 2 +- modules/globallock/locker_test.go | 22 +++++++-------- modules/gtprof/trace_test.go | 2 +- modules/httplib/url_test.go | 4 +-- modules/indexer/code/indexer_test.go | 15 +++++------ modules/indexer/issues/indexer_test.go | 27 +++++++++---------- .../indexer/issues/internal/tests/tests.go | 16 +++++------ modules/indexer/stats/indexer_test.go | 3 +-- modules/lfs/http_client_test.go | 4 +-- modules/lfs/transferadapter_test.go | 7 +++-- modules/log/event_writer_conn_test.go | 3 +-- modules/log/logger_test.go | 9 +++---- modules/markup/console/console_test.go | 3 +-- modules/markup/csv/csv_test.go | 3 +-- modules/markup/render_link_test.go | 3 +-- modules/process/manager_test.go | 8 +++--- modules/queue/base_test.go | 4 +-- modules/queue/manager_test.go | 9 +++---- modules/queue/workerqueue_test.go | 5 ++-- modules/testlogger/testlogger.go | 3 +-- routers/api/actions/ping/ping_test.go | 3 +-- routers/common/errpage_test.go | 3 +-- routers/private/hook_verification_test.go | 3 +-- services/actions/context_test.go | 3 +-- services/auth/oauth2_test.go | 5 ++-- .../auth/source/oauth2/source_sync_test.go | 17 ++++++------ services/gitdiff/gitdiff_test.go | 5 ++-- services/gitdiff/submodule_test.go | 3 +-- services/mailer/mail_test.go | 16 +++++------ services/markup/renderhelper_mention_test.go | 9 +++---- services/migrations/codebase_test.go | 3 +-- services/migrations/gitea_downloader_test.go | 3 +-- services/migrations/gitea_uploader_test.go | 11 ++++---- services/migrations/github_test.go | 3 +-- services/migrations/gitlab_test.go | 5 ++-- services/migrations/gogs_test.go | 3 +-- services/migrations/onedev_test.go | 3 +-- services/pull/check_test.go | 3 +-- services/repository/lfs_test.go | 3 +-- services/webhook/deliver_test.go | 9 +++---- services/webhook/dingtalk_test.go | 3 +-- services/webhook/discord_test.go | 3 +-- services/webhook/feishu_test.go | 3 +-- services/webhook/matrix_test.go | 3 +-- services/webhook/msteams_test.go | 3 +-- services/webhook/packagist_test.go | 5 ++-- services/webhook/slack_test.go | 3 +-- services/webhook/telegram_test.go | 3 +-- tests/e2e/utils_e2e_test.go | 2 +- tests/integration/actions_job_test.go | 17 ++++++------ .../integration/actions_runner_modify_test.go | 3 +-- tests/integration/actions_runner_test.go | 12 ++++----- tests/integration/actions_variables_test.go | 3 +-- .../api_activitypub_person_test.go | 3 +-- .../api_helper_for_declarative_test.go | 3 +-- tests/integration/api_private_serv_test.go | 4 +-- tests/integration/api_pull_test.go | 7 +++-- .../integration/api_repo_file_create_test.go | 5 ++-- .../integration/api_repo_file_update_test.go | 3 +-- .../integration/api_repo_files_change_test.go | 3 +-- tests/integration/auth_ldap_test.go | 13 +++++---- tests/integration/dump_restore_test.go | 7 +++-- .../git_helper_for_declarative_test.go | 6 ++--- tests/integration/git_lfs_ssh_test.go | 3 +-- tests/integration/git_misc_test.go | 3 +-- tests/integration/issue_test.go | 3 +-- tests/integration/lfs_view_test.go | 3 +-- tests/integration/linguist_test.go | 3 +-- .../migration-test/migration_test.go | 9 +++---- tests/integration/mirror_pull_test.go | 3 +-- tests/integration/mirror_push_test.go | 3 +-- tests/integration/pull_merge_test.go | 17 ++++++------ tests/integration/repo_webhook_test.go | 3 +-- tests/integration/wiki_test.go | 3 +-- 99 files changed, 254 insertions(+), 336 deletions(-) diff --git a/cmd/hook_test.go b/cmd/hook_test.go index 91f24ff2b4466..86cd4834f2091 100644 --- a/cmd/hook_test.go +++ b/cmd/hook_test.go @@ -6,7 +6,6 @@ package cmd import ( "bufio" "bytes" - "context" "strings" "testing" @@ -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) diff --git a/cmd/migrate_storage_test.go b/cmd/migrate_storage_test.go index 5d8c867993092..f8fa95a927363 100644 --- a/cmd/migrate_storage_test.go +++ b/cmd/migrate_storage_test.go @@ -4,7 +4,6 @@ package cmd import ( - "context" "os" "strings" "testing" @@ -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() diff --git a/models/issues/issue_test.go b/models/issues/issue_test.go index dbbb1e417901f..3f76a81bb65d4 100644 --- a/models/issues/issue_test.go +++ b/models/issues/issue_test.go @@ -4,7 +4,6 @@ package issues_test import ( - "context" "fmt" "sort" "sync" @@ -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, }, diff --git a/models/renderhelper/repo_comment_test.go b/models/renderhelper/repo_comment_test.go index 01e20b9e02606..776152db96069 100644 --- a/models/renderhelper/repo_comment_test.go +++ b/models/renderhelper/repo_comment_test.go @@ -4,7 +4,6 @@ package renderhelper import ( - "context" "testing" repo_model "code.gitea.io/gitea/models/repo" @@ -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 @@ -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 @@ -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 diff --git a/models/renderhelper/repo_file_test.go b/models/renderhelper/repo_file_test.go index 959648b660b81..29cb45f6f754b 100644 --- a/models/renderhelper/repo_file_test.go +++ b/models/renderhelper/repo_file_test.go @@ -4,7 +4,6 @@ package renderhelper import ( - "context" "testing" repo_model "code.gitea.io/gitea/models/repo" @@ -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 @@ -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) @@ -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) @@ -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", }). @@ -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") @@ -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 diff --git a/models/renderhelper/repo_wiki_test.go b/models/renderhelper/repo_wiki_test.go index beab2570e7c05..b24508f1f2f7e 100644 --- a/models/renderhelper/repo_wiki_test.go +++ b/models/renderhelper/repo_wiki_test.go @@ -4,7 +4,6 @@ package renderhelper import ( - "context" "testing" repo_model "code.gitea.io/gitea/models/repo" @@ -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 @@ -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) @@ -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, `