loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[TESTS] Move `CreateDeclarativeRepo` to more accessible location' (#5108) from gusted/forgejo-chore-declerative-repo into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5108
Reviewed-by: Gergely Nagy <algernon@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>

Otto 32735b1e d34d8ec2

+196 -258
-68
services/webhook/sourcehut/builds_test.go
··· 6 6 import ( 7 7 "context" 8 8 "testing" 9 - "time" 10 9 11 - "code.gitea.io/gitea/models/db" 12 - repo_model "code.gitea.io/gitea/models/repo" 13 - unit_model "code.gitea.io/gitea/models/unit" 14 - user_model "code.gitea.io/gitea/models/user" 15 10 webhook_model "code.gitea.io/gitea/models/webhook" 16 11 "code.gitea.io/gitea/modules/git" 17 12 "code.gitea.io/gitea/modules/json" ··· 19 14 api "code.gitea.io/gitea/modules/structs" 20 15 "code.gitea.io/gitea/modules/test" 21 16 webhook_module "code.gitea.io/gitea/modules/webhook" 22 - repo_service "code.gitea.io/gitea/services/repository" 23 - files_service "code.gitea.io/gitea/services/repository/files" 24 17 "code.gitea.io/gitea/services/webhook/shared" 25 18 26 19 "github.com/stretchr/testify/assert" ··· 391 384 require.NoError(t, err) 392 385 assert.Equal(t, "json test", body.Variables.Note) 393 386 } 394 - 395 - func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, enabledUnits, disabledUnits []unit_model.Type, files []*files_service.ChangeRepoFile) (*repo_model.Repository, string) { 396 - t.Helper() 397 - 398 - // Create a new repository 399 - repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{ 400 - Name: name, 401 - Description: "Temporary Repo", 402 - AutoInit: true, 403 - Gitignores: "", 404 - License: "WTFPL", 405 - Readme: "Default", 406 - DefaultBranch: "main", 407 - }) 408 - require.NoError(t, err) 409 - assert.NotEmpty(t, repo) 410 - t.Cleanup(func() { 411 - repo_service.DeleteRepository(db.DefaultContext, owner, repo, false) 412 - }) 413 - 414 - if enabledUnits != nil || disabledUnits != nil { 415 - units := make([]repo_model.RepoUnit, len(enabledUnits)) 416 - for i, unitType := range enabledUnits { 417 - units[i] = repo_model.RepoUnit{ 418 - RepoID: repo.ID, 419 - Type: unitType, 420 - } 421 - } 422 - 423 - err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, units, disabledUnits) 424 - require.NoError(t, err) 425 - } 426 - 427 - var sha string 428 - if len(files) > 0 { 429 - resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{ 430 - Files: files, 431 - Message: "add files", 432 - OldBranch: "main", 433 - NewBranch: "main", 434 - Author: &files_service.IdentityOptions{ 435 - Name: owner.Name, 436 - Email: owner.Email, 437 - }, 438 - Committer: &files_service.IdentityOptions{ 439 - Name: owner.Name, 440 - Email: owner.Email, 441 - }, 442 - Dates: &files_service.CommitDateOptions{ 443 - Author: time.Now(), 444 - Committer: time.Now(), 445 - }, 446 - }) 447 - require.NoError(t, err) 448 - assert.NotEmpty(t, resp) 449 - 450 - sha = resp.Commit.SHA 451 - } 452 - 453 - return repo, sha 454 - }
+3 -3
tests/integration/actions_route_test.go
··· 37 37 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 38 38 39 39 // create the repo 40 - repo, _, f := CreateDeclarativeRepo(t, user2, "actionsTestRepo", 40 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "actionsTestRepo", 41 41 []unit_model.Type{unit_model.TypeActions}, nil, 42 42 []*files_service.ChangeRepoFile{ 43 43 { ··· 120 120 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 121 121 122 122 // create the repo 123 - repo, _, f := CreateDeclarativeRepo(t, user2, "", 123 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "", 124 124 []unit_model.Type{unit_model.TypeActions}, nil, 125 125 []*files_service.ChangeRepoFile{ 126 126 { ··· 153 153 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 154 154 155 155 // create the repo 156 - repo, _, f := CreateDeclarativeRepo(t, user2, "", 156 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "", 157 157 []unit_model.Type{unit_model.TypeActions}, nil, 158 158 []*files_service.ChangeRepoFile{ 159 159 {
+4 -3
tests/integration/actions_trigger_test.go
··· 29 29 release_service "code.gitea.io/gitea/services/release" 30 30 repo_service "code.gitea.io/gitea/services/repository" 31 31 files_service "code.gitea.io/gitea/services/repository/files" 32 + "code.gitea.io/gitea/tests" 32 33 33 34 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" ··· 40 41 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the forked repo 41 42 42 43 // create the base repo 43 - baseRepo, _, f := CreateDeclarativeRepo(t, user2, "repo-pull-request-target", 44 + baseRepo, _, f := tests.CreateDeclarativeRepo(t, user2, "repo-pull-request-target", 44 45 []unit_model.Type{unit_model.TypeActions}, nil, nil, 45 46 ) 46 47 defer f() ··· 197 198 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 198 199 199 200 // create the repo 200 - repo, _, f := CreateDeclarativeRepo(t, user2, "skip-ci", 201 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "skip-ci", 201 202 []unit_model.Type{unit_model.TypeActions}, nil, 202 203 []*files_service.ChangeRepoFile{ 203 204 { ··· 403 404 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 404 405 405 406 // create the repo 406 - repo, sha, f := CreateDeclarativeRepo(t, user2, "repo-workflow-dispatch", 407 + repo, sha, f := tests.CreateDeclarativeRepo(t, user2, "repo-workflow-dispatch", 407 408 []unit_model.Type{unit_model.TypeActions}, nil, 408 409 []*files_service.ChangeRepoFile{ 409 410 {
+1 -1
tests/integration/api_push_mirror_test.go
··· 152 152 assert.False(t, srcRepo.HasWiki()) 153 153 session := loginUser(t, user.Name) 154 154 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) 155 - pushToRepo, _, f := CreateDeclarativeRepoWithOptions(t, user, DeclarativeRepoOptions{ 155 + pushToRepo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ 156 156 Name: optional.Some("push-mirror-test"), 157 157 AutoInit: optional.Some(false), 158 158 EnabledUnits: optional.Some([]unit.Type{unit.TypeCode}),
+5 -5
tests/integration/api_quota_use_test.go
··· 292 292 env.cleanups = append(env.cleanups, userCleanup) 293 293 294 294 // Create a repository 295 - repo, _, repoCleanup := CreateDeclarativeRepoWithOptions(t, env.User.User, DeclarativeRepoOptions{}) 295 + repo, _, repoCleanup := tests.CreateDeclarativeRepoWithOptions(t, env.User.User, tests.DeclarativeRepoOptions{}) 296 296 env.Repo = repo 297 297 env.cleanups = append(env.cleanups, repoCleanup) 298 298 ··· 487 487 defer tests.PrintCurrentTest(t)() 488 488 489 489 // Create a template repository 490 - template, _, cleanup := CreateDeclarativeRepoWithOptions(t, env.User.User, DeclarativeRepoOptions{ 490 + template, _, cleanup := tests.CreateDeclarativeRepoWithOptions(t, env.User.User, tests.DeclarativeRepoOptions{ 491 491 IsTemplate: optional.Some(true), 492 492 }) 493 493 defer cleanup() ··· 524 524 525 525 t.Run("#/repos/{username}/{reponame}", func(t *testing.T) { 526 526 // Lets create a new repo to play with. 527 - repo, _, repoCleanup := CreateDeclarativeRepoWithOptions(t, env.User.User, DeclarativeRepoOptions{}) 527 + repo, _, repoCleanup := tests.CreateDeclarativeRepoWithOptions(t, env.User.User, tests.DeclarativeRepoOptions{}) 528 528 defer repoCleanup() 529 529 530 530 // Drop the quota to 0 ··· 1209 1209 defer tests.PrintCurrentTest(t)() 1210 1210 1211 1211 // Create a repository to transfer 1212 - repo, _, cleanup := CreateDeclarativeRepoWithOptions(t, env.User.User, DeclarativeRepoOptions{}) 1212 + repo, _, cleanup := tests.CreateDeclarativeRepoWithOptions(t, env.User.User, tests.DeclarativeRepoOptions{}) 1213 1213 defer cleanup() 1214 1214 1215 1215 // Initiate repo transfer ··· 1244 1244 defer env.SetRuleLimit(t, "deny-all", -1)() 1245 1245 1246 1246 // Create a repository to transfer 1247 - repo, _, cleanup := CreateDeclarativeRepoWithOptions(t, env.User.User, DeclarativeRepoOptions{}) 1247 + repo, _, cleanup := tests.CreateDeclarativeRepoWithOptions(t, env.User.User, tests.DeclarativeRepoOptions{}) 1248 1248 defer cleanup() 1249 1249 1250 1250 // Initiate repo transfer
+1 -1
tests/integration/api_repo_activities_test.go
··· 21 21 defer tests.PrepareTestEnv(t)() 22 22 23 23 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 24 - repo, _, f := CreateDeclarativeRepoWithOptions(t, owner, DeclarativeRepoOptions{}) 24 + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, owner, tests.DeclarativeRepoOptions{}) 25 25 defer f() 26 26 27 27 feedURL := fmt.Sprintf("/api/v1/repos/%s/activities/feeds", repo.FullName())
+2 -2
tests/integration/api_wiki_test.go
··· 296 296 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) 297 297 298 298 // Create a new repository for testing purposes 299 - repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 299 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 300 300 unit_model.TypeCode, 301 301 unit_model.TypeWiki, 302 302 }, nil, nil) ··· 389 389 defer tests.PrepareTestEnv(t)() 390 390 391 391 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 392 - repo, _, f := CreateDeclarativeRepoWithOptions(t, user, DeclarativeRepoOptions{ 392 + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ 393 393 WikiBranch: optional.Some("main"), 394 394 }) 395 395 defer f()
+1 -1
tests/integration/codeowner_test.go
··· 30 30 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 31 31 32 32 // Create the repo. 33 - repo, _, f := CreateDeclarativeRepo(t, user2, "", 33 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "", 34 34 []unit_model.Type{unit_model.TypePullRequests}, nil, 35 35 []*files_service.ChangeRepoFile{ 36 36 {
+1 -1
tests/integration/compare_test.go
··· 224 224 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 225 225 226 226 // Create a new repository, with a file that has many lines 227 - repo, _, f := CreateDeclarativeRepoWithOptions(t, owner, DeclarativeRepoOptions{ 227 + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, owner, tests.DeclarativeRepoOptions{ 228 228 Files: optional.Some([]*files_service.ChangeRepoFile{ 229 229 { 230 230 Operation: "create",
-145
tests/integration/integration_test.go
··· 28 28 "code.gitea.io/gitea/cmd" 29 29 "code.gitea.io/gitea/models/auth" 30 30 "code.gitea.io/gitea/models/db" 31 - repo_model "code.gitea.io/gitea/models/repo" 32 - unit_model "code.gitea.io/gitea/models/unit" 33 31 "code.gitea.io/gitea/models/unittest" 34 32 user_model "code.gitea.io/gitea/models/user" 35 - "code.gitea.io/gitea/modules/git" 36 33 "code.gitea.io/gitea/modules/graceful" 37 34 "code.gitea.io/gitea/modules/json" 38 35 "code.gitea.io/gitea/modules/log" 39 - "code.gitea.io/gitea/modules/optional" 40 36 "code.gitea.io/gitea/modules/setting" 41 37 "code.gitea.io/gitea/modules/testlogger" 42 38 "code.gitea.io/gitea/modules/util" ··· 44 40 "code.gitea.io/gitea/routers" 45 41 "code.gitea.io/gitea/services/auth/source/remote" 46 42 gitea_context "code.gitea.io/gitea/services/context" 47 - repo_service "code.gitea.io/gitea/services/repository" 48 - files_service "code.gitea.io/gitea/services/repository/files" 49 43 user_service "code.gitea.io/gitea/services/user" 50 - wiki_service "code.gitea.io/gitea/services/wiki" 51 44 "code.gitea.io/gitea/tests" 52 45 53 46 "github.com/PuerkitoBio/goquery" 54 - gouuid "github.com/google/uuid" 55 47 "github.com/markbates/goth" 56 48 "github.com/markbates/goth/gothic" 57 49 goth_github "github.com/markbates/goth/providers/github" ··· 693 685 doc := NewHTMLParser(t, resp.Body) 694 686 return doc.Find("head title").Text() 695 687 } 696 - 697 - type DeclarativeRepoOptions struct { 698 - Name optional.Option[string] 699 - EnabledUnits optional.Option[[]unit_model.Type] 700 - DisabledUnits optional.Option[[]unit_model.Type] 701 - Files optional.Option[[]*files_service.ChangeRepoFile] 702 - WikiBranch optional.Option[string] 703 - AutoInit optional.Option[bool] 704 - IsTemplate optional.Option[bool] 705 - } 706 - 707 - func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) { 708 - t.Helper() 709 - 710 - // Not using opts.Name.ValueOrDefault() here to avoid unnecessarily 711 - // generating an UUID when a name is specified. 712 - var repoName string 713 - if opts.Name.Has() { 714 - repoName = opts.Name.Value() 715 - } else { 716 - repoName = gouuid.NewString() 717 - } 718 - 719 - var autoInit bool 720 - if opts.AutoInit.Has() { 721 - autoInit = opts.AutoInit.Value() 722 - } else { 723 - autoInit = true 724 - } 725 - 726 - // Create the repository 727 - repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{ 728 - Name: repoName, 729 - Description: "Temporary Repo", 730 - AutoInit: autoInit, 731 - Gitignores: "", 732 - License: "WTFPL", 733 - Readme: "Default", 734 - DefaultBranch: "main", 735 - IsTemplate: opts.IsTemplate.Value(), 736 - }) 737 - require.NoError(t, err) 738 - assert.NotEmpty(t, repo) 739 - 740 - // Populate `enabledUnits` if we have any enabled. 741 - var enabledUnits []repo_model.RepoUnit 742 - if opts.EnabledUnits.Has() { 743 - units := opts.EnabledUnits.Value() 744 - enabledUnits = make([]repo_model.RepoUnit, len(units)) 745 - 746 - for i, unitType := range units { 747 - enabledUnits[i] = repo_model.RepoUnit{ 748 - RepoID: repo.ID, 749 - Type: unitType, 750 - } 751 - } 752 - } 753 - 754 - // Adjust the repo units according to our parameters. 755 - if opts.EnabledUnits.Has() || opts.DisabledUnits.Has() { 756 - err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, opts.DisabledUnits.ValueOrDefault(nil)) 757 - require.NoError(t, err) 758 - } 759 - 760 - // Add files, if any. 761 - var sha string 762 - if opts.Files.Has() { 763 - assert.True(t, autoInit, "Files cannot be specified if AutoInit is disabled") 764 - files := opts.Files.Value() 765 - 766 - resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{ 767 - Files: files, 768 - Message: "add files", 769 - OldBranch: "main", 770 - NewBranch: "main", 771 - Author: &files_service.IdentityOptions{ 772 - Name: owner.Name, 773 - Email: owner.Email, 774 - }, 775 - Committer: &files_service.IdentityOptions{ 776 - Name: owner.Name, 777 - Email: owner.Email, 778 - }, 779 - Dates: &files_service.CommitDateOptions{ 780 - Author: time.Now(), 781 - Committer: time.Now(), 782 - }, 783 - }) 784 - require.NoError(t, err) 785 - assert.NotEmpty(t, resp) 786 - 787 - sha = resp.Commit.SHA 788 - } 789 - 790 - // If there's a Wiki branch specified, create a wiki, and a default wiki page. 791 - if opts.WikiBranch.Has() { 792 - // Set the wiki branch in the database first 793 - repo.WikiBranch = opts.WikiBranch.Value() 794 - err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "wiki_branch") 795 - require.NoError(t, err) 796 - 797 - // Initialize the wiki 798 - err = wiki_service.InitWiki(db.DefaultContext, repo) 799 - require.NoError(t, err) 800 - 801 - // Add a new wiki page 802 - err = wiki_service.AddWikiPage(db.DefaultContext, owner, repo, "Home", "Welcome to the wiki!", "Add a Home page") 803 - require.NoError(t, err) 804 - } 805 - 806 - // Return the repo, the top commit, and a defer-able function to delete the 807 - // repo. 808 - return repo, sha, func() { 809 - repo_service.DeleteRepository(db.DefaultContext, owner, repo, false) 810 - } 811 - } 812 - 813 - func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, enabledUnits, disabledUnits []unit_model.Type, files []*files_service.ChangeRepoFile) (*repo_model.Repository, string, func()) { 814 - t.Helper() 815 - 816 - var opts DeclarativeRepoOptions 817 - 818 - if name != "" { 819 - opts.Name = optional.Some(name) 820 - } 821 - if enabledUnits != nil { 822 - opts.EnabledUnits = optional.Some(enabledUnits) 823 - } 824 - if disabledUnits != nil { 825 - opts.DisabledUnits = optional.Some(disabledUnits) 826 - } 827 - if files != nil { 828 - opts.Files = optional.Some(files) 829 - } 830 - 831 - return CreateDeclarativeRepoWithOptions(t, owner, opts) 832 - }
+3 -3
tests/integration/issue_test.go
··· 338 338 session := loginUser(t, owner.Name) 339 339 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 340 340 341 - repo, _, f := CreateDeclarativeRepoWithOptions(t, owner, DeclarativeRepoOptions{}) 341 + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, owner, tests.DeclarativeRepoOptions{}) 342 342 defer f() 343 343 344 344 createIssue := func(t *testing.T, title string) api.Issue { ··· 1200 1200 onGiteaRun(t, func(t *testing.T, u *url.URL) { 1201 1201 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 1202 1202 session := loginUser(t, user2.Name) 1203 - repo, _, f := CreateDeclarativeRepo(t, user2, "", 1203 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "", 1204 1204 []unit_model.Type{unit_model.TypeCode, unit_model.TypeIssues}, nil, 1205 1205 []*files_service.ChangeRepoFile{ 1206 1206 { ··· 1250 1250 defer tests.PrepareTestEnv(t)() 1251 1251 1252 1252 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 1253 - repo, _, f := CreateDeclarativeRepoWithOptions(t, user, DeclarativeRepoOptions{ 1253 + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ 1254 1254 AutoInit: optional.Some(false), 1255 1255 }) 1256 1256 defer f()
+1 -1
tests/integration/linguist_test.go
··· 34 34 35 35 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 36 36 37 - repo, sha, f := CreateDeclarativeRepo(t, user2, "", nil, nil, 37 + repo, sha, f := tests.CreateDeclarativeRepo(t, user2, "", nil, nil, 38 38 []*files_service.ChangeRepoFile{ 39 39 { 40 40 Operation: "create",
+1 -1
tests/integration/mirror_push_test.go
··· 167 167 srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) 168 168 assert.False(t, srcRepo.HasWiki()) 169 169 sess := loginUser(t, user.Name) 170 - pushToRepo, _, f := CreateDeclarativeRepoWithOptions(t, user, DeclarativeRepoOptions{ 170 + pushToRepo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ 171 171 Name: optional.Some("push-mirror-test"), 172 172 AutoInit: optional.Some(false), 173 173 EnabledUnits: optional.Some([]unit.Type{unit.TypeCode}),
+1 -1
tests/integration/pull_create_test.go
··· 126 126 } 127 127 } 128 128 129 - repo, _, deferrer := CreateDeclarativeRepo(t, baseUser, "", nil, nil, changeOps) 129 + repo, _, deferrer := tests.CreateDeclarativeRepo(t, baseUser, "", nil, nil, changeOps) 130 130 131 131 return repo, deferrer 132 132 }
+1 -1
tests/integration/pull_icon_test.go
··· 32 32 func TestPullRequestIcons(t *testing.T) { 33 33 onGiteaRun(t, func(t *testing.T, u *url.URL) { 34 34 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 35 - repo, _, f := CreateDeclarativeRepo(t, user, "pr-icons", []unit_model.Type{unit_model.TypeCode, unit_model.TypePullRequests}, nil, nil) 35 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "pr-icons", []unit_model.Type{unit_model.TypeCode, unit_model.TypePullRequests}, nil, nil) 36 36 defer f() 37 37 38 38 session := loginUser(t, user.LoginName)
+2 -1
tests/integration/pull_merge_test.go
··· 42 42 commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus" 43 43 files_service "code.gitea.io/gitea/services/repository/files" 44 44 webhook_service "code.gitea.io/gitea/services/webhook" 45 + "code.gitea.io/gitea/tests" 45 46 46 47 "github.com/stretchr/testify/assert" 47 48 "github.com/stretchr/testify/require" ··· 496 497 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 497 498 498 499 // Create new clean repo to test conflict checking. 499 - baseRepo, _, f := CreateDeclarativeRepo(t, user, "conflict-checking", nil, nil, nil) 500 + baseRepo, _, f := tests.CreateDeclarativeRepo(t, user, "conflict-checking", nil, nil, nil) 500 501 defer f() 501 502 502 503 // create a commit on new branch.
+1 -1
tests/integration/pull_reopen_test.go
··· 37 37 org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26}) 38 38 39 39 // Create an base repository. 40 - baseRepo, _, f := CreateDeclarativeRepo(t, user2, "reopen-base", 40 + baseRepo, _, f := tests.CreateDeclarativeRepo(t, user2, "reopen-base", 41 41 []unit_model.Type{unit_model.TypePullRequests}, nil, nil, 42 42 ) 43 43 defer f()
+2 -1
tests/integration/pull_update_test.go
··· 19 19 pull_service "code.gitea.io/gitea/services/pull" 20 20 repo_service "code.gitea.io/gitea/services/repository" 21 21 files_service "code.gitea.io/gitea/services/repository/files" 22 + "code.gitea.io/gitea/tests" 22 23 23 24 "github.com/stretchr/testify/assert" 24 25 "github.com/stretchr/testify/require" ··· 83 84 } 84 85 85 86 func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *issues_model.PullRequest { 86 - baseRepo, _, _ := CreateDeclarativeRepo(t, actor, "repo-pr-update", nil, nil, nil) 87 + baseRepo, _, _ := tests.CreateDeclarativeRepo(t, actor, "repo-pr-update", nil, nil, nil) 87 88 88 89 headRepo, err := repo_service.ForkRepositoryAndUpdates(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{ 89 90 BaseRepo: baseRepo,
+2 -2
tests/integration/quota_use_test.go
··· 1018 1018 user.Session = loginUser(t, userName) 1019 1019 1020 1020 // Create a repository for the user 1021 - repo, _, _ := CreateDeclarativeRepoWithOptions(t, user.User, DeclarativeRepoOptions{}) 1021 + repo, _, _ := tests.CreateDeclarativeRepoWithOptions(t, user.User, tests.DeclarativeRepoOptions{}) 1022 1022 user.Repo = repo 1023 1023 1024 1024 // Create a quota group for them ··· 1058 1058 1059 1059 // Create a repository for the org 1060 1060 orgUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: org.Org.ID}) 1061 - repo, _, _ := CreateDeclarativeRepoWithOptions(t, orgUser, DeclarativeRepoOptions{}) 1061 + repo, _, _ := tests.CreateDeclarativeRepoWithOptions(t, orgUser, tests.DeclarativeRepoOptions{}) 1062 1062 org.Repo = repo 1063 1063 1064 1064 // Create a quota group for them
+1 -1
tests/integration/repo_activity_test.go
··· 167 167 unit_model.LoadUnitConfig() 168 168 169 169 // Create a repo, with only code unit enabled. 170 - repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{unit_model.TypeCode}, nil, nil) 170 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{unit_model.TypeCode}, nil, nil) 171 171 defer f() 172 172 173 173 req := NewRequest(t, "GET", fmt.Sprintf("%s/activity", repo.Link()))
+2 -1
tests/integration/repo_archive_text_test.go
··· 13 13 "code.gitea.io/gitea/models/unittest" 14 14 user_model "code.gitea.io/gitea/models/user" 15 15 "code.gitea.io/gitea/modules/translation" 16 + "code.gitea.io/gitea/tests" 16 17 17 18 "github.com/PuerkitoBio/goquery" 18 19 "github.com/stretchr/testify/assert" ··· 28 29 link := path.Join(testUser, testRepoName, "settings") 29 30 30 31 // Create test repo 31 - _, _, f := CreateDeclarativeRepo(t, user2, testRepoName, nil, nil, nil) 32 + _, _, f := tests.CreateDeclarativeRepo(t, user2, testRepoName, nil, nil, nil) 32 33 defer f() 33 34 34 35 // Test settings page
+1 -1
tests/integration/repo_badges_test.go
··· 35 35 prep := func(t *testing.T) (*repo_model.Repository, func()) { 36 36 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 37 37 38 - repo, _, f := CreateDeclarativeRepo(t, owner, "", 38 + repo, _, f := tests.CreateDeclarativeRepo(t, owner, "", 39 39 []unit_model.Type{unit_model.TypeActions}, 40 40 []unit_model.Type{unit_model.TypeIssues, unit_model.TypePullRequests, unit_model.TypeReleases}, 41 41 []*files_service.ChangeRepoFile{
+2 -2
tests/integration/repo_citation_test.go
··· 28 28 t.Run("No citation", func(t *testing.T) { 29 29 defer tests.PrintCurrentTest(t)() 30 30 31 - repo, _, f := CreateDeclarativeRepo(t, user, "citation-no-citation", []unit_model.Type{unit_model.TypeCode}, nil, nil) 31 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "citation-no-citation", []unit_model.Type{unit_model.TypeCode}, nil, nil) 32 32 defer f() 33 33 34 34 testCitationButtonExists(t, session, repo, "", false) ··· 70 70 } 71 71 72 72 func createRepoWithEmptyFile(t *testing.T, user *user_model.User, repoName, fileName string) (*repo_model.Repository, func()) { 73 - repo, _, f := CreateDeclarativeRepo(t, user, repoName, []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{ 73 + repo, _, f := tests.CreateDeclarativeRepo(t, user, repoName, []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{ 74 74 { 75 75 Operation: "create", 76 76 TreePath: fileName,
+2 -2
tests/integration/repo_settings_test.go
··· 51 51 unit_model.LoadUnitConfig() 52 52 53 53 // Create a known-good repo, with some units disabled. 54 - repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 54 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 55 55 unit_model.TypeCode, 56 56 unit_model.TypePullRequests, 57 57 unit_model.TypeProjects, ··· 142 142 unit_model.LoadUnitConfig() 143 143 144 144 // Create a known-good repo, with all units enabled. 145 - repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 145 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 146 146 unit_model.TypeCode, 147 147 unit_model.TypePullRequests, 148 148 unit_model.TypeProjects,
+1 -1
tests/integration/repo_signed_tag_test.go
··· 29 29 30 30 // Preparations 31 31 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 32 - repo, _, f := CreateDeclarativeRepo(t, user, "", nil, nil, nil) 32 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", nil, nil, nil) 33 33 defer f() 34 34 35 35 // Set up an SSH key for the tagger
+1 -1
tests/integration/repo_test.go
··· 834 834 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 835 835 836 836 // create the repo 837 - repo, _, f := CreateDeclarativeRepo(t, user2, "", 837 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "", 838 838 []unit_model.Type{unit_model.TypeCode}, nil, 839 839 []*files_service.ChangeRepoFile{ 840 840 {
+2 -2
tests/integration/repo_view_test.go
··· 47 47 } 48 48 49 49 // README.md is already added by auto init 50 - repo, _, f := CreateDeclarativeRepo(t, user, "readmetest", []unit_model.Type{unit_model.TypeCode}, nil, changeFiles) 50 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "readmetest", []unit_model.Type{unit_model.TypeCode}, nil, changeFiles) 51 51 52 52 ctx, _ := contexttest.MockContext(t, "user1/readmetest") 53 53 ctx.SetParams(":id", fmt.Sprint(repo.ID)) ··· 158 158 func TestRepoViewFileLines(t *testing.T) { 159 159 onGiteaRun(t, func(t *testing.T, _ *url.URL) { 160 160 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 161 - repo, _, f := CreateDeclarativeRepo(t, user, "file-lines", []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{ 161 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "file-lines", []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{ 162 162 { 163 163 Operation: "create", 164 164 TreePath: "test-1",
+2 -1
tests/integration/size_translations_test.go
··· 14 14 "code.gitea.io/gitea/models/unittest" 15 15 user_model "code.gitea.io/gitea/models/user" 16 16 files_service "code.gitea.io/gitea/services/repository/files" 17 + "code.gitea.io/gitea/tests" 17 18 18 19 "github.com/PuerkitoBio/goquery" 19 20 "github.com/stretchr/testify/assert" ··· 31 32 session := loginUser(t, testUser) 32 33 33 34 // Create test repo 34 - testRepo, _, f := CreateDeclarativeRepo(t, user2, testRepoName, nil, nil, 35 + testRepo, _, f := tests.CreateDeclarativeRepo(t, user2, testRepoName, nil, nil, 35 36 []*files_service.ChangeRepoFile{ 36 37 { 37 38 Operation: "create",
+1 -1
tests/integration/user_profile_test.go
··· 44 44 }) 45 45 } 46 46 47 - _, _, f := CreateDeclarativeRepo(t, user2, ".profile", nil, nil, ops) 47 + _, _, f := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, ops) 48 48 defer f() 49 49 50 50 // Perform the test
+1 -1
tests/integration/user_test.go
··· 323 323 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser) 324 324 325 325 // Create a known-good repo, with only one unit enabled 326 - repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 326 + repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{ 327 327 unit_model.TypeCode, 328 328 }, []unit_model.Type{ 329 329 unit_model.TypePullRequests,
+2 -2
tests/integration/view_test.go
··· 41 41 session := loginUser(t, user2.Name) 42 42 43 43 // Prepare the environments. File view, commit view (diff), wiki page. 44 - repo, commitID, f := CreateDeclarativeRepo(t, user2, "", 44 + repo, commitID, f := tests.CreateDeclarativeRepo(t, user2, "", 45 45 []unit_model.Type{unit_model.TypeCode, unit_model.TypeWiki}, nil, 46 46 []*files_service.ChangeRepoFile{ 47 47 { ··· 135 135 onGiteaRun(t, func(t *testing.T, u *url.URL) { 136 136 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 137 137 session := loginUser(t, user2.Name) 138 - repo, commitID, f := CreateDeclarativeRepo(t, user2, "", 138 + repo, commitID, f := tests.CreateDeclarativeRepo(t, user2, "", 139 139 []unit_model.Type{unit_model.TypeCode, unit_model.TypeWiki}, nil, 140 140 []*files_service.ChangeRepoFile{ 141 141 {
+146
tests/test_utils.go
··· 18 18 19 19 "code.gitea.io/gitea/models/db" 20 20 packages_model "code.gitea.io/gitea/models/packages" 21 + repo_model "code.gitea.io/gitea/models/repo" 22 + unit_model "code.gitea.io/gitea/models/unit" 21 23 "code.gitea.io/gitea/models/unittest" 24 + user_model "code.gitea.io/gitea/models/user" 22 25 "code.gitea.io/gitea/modules/base" 23 26 "code.gitea.io/gitea/modules/git" 24 27 "code.gitea.io/gitea/modules/graceful" 25 28 "code.gitea.io/gitea/modules/log" 29 + "code.gitea.io/gitea/modules/optional" 26 30 "code.gitea.io/gitea/modules/process" 27 31 repo_module "code.gitea.io/gitea/modules/repository" 28 32 "code.gitea.io/gitea/modules/setting" ··· 30 34 "code.gitea.io/gitea/modules/testlogger" 31 35 "code.gitea.io/gitea/modules/util" 32 36 "code.gitea.io/gitea/routers" 37 + repo_service "code.gitea.io/gitea/services/repository" 38 + files_service "code.gitea.io/gitea/services/repository/files" 39 + wiki_service "code.gitea.io/gitea/services/wiki" 33 40 41 + "github.com/google/uuid" 42 + "github.com/stretchr/testify/assert" 34 43 "github.com/stretchr/testify/require" 35 44 ) 36 45 ··· 304 313 }, 305 314 ) 306 315 } 316 + 317 + type DeclarativeRepoOptions struct { 318 + Name optional.Option[string] 319 + EnabledUnits optional.Option[[]unit_model.Type] 320 + DisabledUnits optional.Option[[]unit_model.Type] 321 + Files optional.Option[[]*files_service.ChangeRepoFile] 322 + WikiBranch optional.Option[string] 323 + AutoInit optional.Option[bool] 324 + IsTemplate optional.Option[bool] 325 + } 326 + 327 + func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) { 328 + t.Helper() 329 + 330 + // Not using opts.Name.ValueOrDefault() here to avoid unnecessarily 331 + // generating an UUID when a name is specified. 332 + var repoName string 333 + if opts.Name.Has() { 334 + repoName = opts.Name.Value() 335 + } else { 336 + repoName = uuid.NewString() 337 + } 338 + 339 + var autoInit bool 340 + if opts.AutoInit.Has() { 341 + autoInit = opts.AutoInit.Value() 342 + } else { 343 + autoInit = true 344 + } 345 + 346 + // Create the repository 347 + repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{ 348 + Name: repoName, 349 + Description: "Temporary Repo", 350 + AutoInit: autoInit, 351 + Gitignores: "", 352 + License: "WTFPL", 353 + Readme: "Default", 354 + DefaultBranch: "main", 355 + IsTemplate: opts.IsTemplate.Value(), 356 + }) 357 + require.NoError(t, err) 358 + assert.NotEmpty(t, repo) 359 + 360 + // Populate `enabledUnits` if we have any enabled. 361 + var enabledUnits []repo_model.RepoUnit 362 + if opts.EnabledUnits.Has() { 363 + units := opts.EnabledUnits.Value() 364 + enabledUnits = make([]repo_model.RepoUnit, len(units)) 365 + 366 + for i, unitType := range units { 367 + enabledUnits[i] = repo_model.RepoUnit{ 368 + RepoID: repo.ID, 369 + Type: unitType, 370 + } 371 + } 372 + } 373 + 374 + // Adjust the repo units according to our parameters. 375 + if opts.EnabledUnits.Has() || opts.DisabledUnits.Has() { 376 + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, opts.DisabledUnits.ValueOrDefault(nil)) 377 + require.NoError(t, err) 378 + } 379 + 380 + // Add files, if any. 381 + var sha string 382 + if opts.Files.Has() { 383 + assert.True(t, autoInit, "Files cannot be specified if AutoInit is disabled") 384 + files := opts.Files.Value() 385 + 386 + resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{ 387 + Files: files, 388 + Message: "add files", 389 + OldBranch: "main", 390 + NewBranch: "main", 391 + Author: &files_service.IdentityOptions{ 392 + Name: owner.Name, 393 + Email: owner.Email, 394 + }, 395 + Committer: &files_service.IdentityOptions{ 396 + Name: owner.Name, 397 + Email: owner.Email, 398 + }, 399 + Dates: &files_service.CommitDateOptions{ 400 + Author: time.Now(), 401 + Committer: time.Now(), 402 + }, 403 + }) 404 + require.NoError(t, err) 405 + assert.NotEmpty(t, resp) 406 + 407 + sha = resp.Commit.SHA 408 + } 409 + 410 + // If there's a Wiki branch specified, create a wiki, and a default wiki page. 411 + if opts.WikiBranch.Has() { 412 + // Set the wiki branch in the database first 413 + repo.WikiBranch = opts.WikiBranch.Value() 414 + err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "wiki_branch") 415 + require.NoError(t, err) 416 + 417 + // Initialize the wiki 418 + err = wiki_service.InitWiki(db.DefaultContext, repo) 419 + require.NoError(t, err) 420 + 421 + // Add a new wiki page 422 + err = wiki_service.AddWikiPage(db.DefaultContext, owner, repo, "Home", "Welcome to the wiki!", "Add a Home page") 423 + require.NoError(t, err) 424 + } 425 + 426 + // Return the repo, the top commit, and a defer-able function to delete the 427 + // repo. 428 + return repo, sha, func() { 429 + _ = repo_service.DeleteRepository(db.DefaultContext, owner, repo, false) 430 + } 431 + } 432 + 433 + func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, enabledUnits, disabledUnits []unit_model.Type, files []*files_service.ChangeRepoFile) (*repo_model.Repository, string, func()) { 434 + t.Helper() 435 + 436 + var opts DeclarativeRepoOptions 437 + 438 + if name != "" { 439 + opts.Name = optional.Some(name) 440 + } 441 + if enabledUnits != nil { 442 + opts.EnabledUnits = optional.Some(enabledUnits) 443 + } 444 + if disabledUnits != nil { 445 + opts.DisabledUnits = optional.Some(disabledUnits) 446 + } 447 + if files != nil { 448 + opts.Files = optional.Some(files) 449 + } 450 + 451 + return CreateDeclarativeRepoWithOptions(t, owner, opts) 452 + }