From 13892121e63e401b4af5807c8e836733d01ad6a8 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 24 Apr 2024 23:33:08 +0200 Subject: [PATCH] tests: Refactor CreateDeclarativeRepo Lets introduce a new helper function, `CreateDeclarativeRepoWithOptions`! This is almost the same as the existing `CreateDeclarativeRepo` helper, but instead of taking a list of random parameters the author thought of at the time of its introduction, it takes a `DeclarativeRepoOptions` struct, with optional members. This makes it easier to extend the function, as new members can be added without breaking or having to update existing callsites, as long as the newly added members default to compatible values. `CreateDeclarativeRepo` is then reimplemented on top of the new function. Callsites aren't updated yet, we can do that organically, whenever touching code that uses the older function. No new functionality is introduced just yet, this is merely a refactor. Signed-off-by: Gergely Nagy --- tests/integration/integration_test.go | 65 ++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 8fd55d7fa8..e50338d5fa 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -36,6 +36,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/testlogger" "code.gitea.io/gitea/modules/util" @@ -652,15 +653,26 @@ func GetHTMLTitle(t testing.TB, session *TestSession, urlStr string) string { return doc.Find("head title").Text() } -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()) { +type DeclarativeRepoOptions struct { + Name optional.Option[string] + EnabledUnits optional.Option[[]unit_model.Type] + DisabledUnits optional.Option[[]unit_model.Type] + Files optional.Option[[]*files_service.ChangeRepoFile] +} + +func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) { t.Helper() - repoName := name - if repoName == "" { + // Not using opts.Name.ValueOrDefault() here to avoid unnecessarily + // generating an UUID when a name is specified. + var repoName string + if opts.Name.Has() { + repoName = opts.Name.Value() + } else { repoName = gouuid.NewString() } - // Create a new repository + // Create the repository repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{ Name: repoName, Description: "Temporary Repo", @@ -673,21 +685,31 @@ func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, en assert.NoError(t, err) assert.NotEmpty(t, repo) - if enabledUnits != nil || disabledUnits != nil { - units := make([]repo_model.RepoUnit, len(enabledUnits)) - for i, unitType := range enabledUnits { - units[i] = repo_model.RepoUnit{ + // Populate `enabledUnits` if we have any enabled. + var enabledUnits []repo_model.RepoUnit + if opts.EnabledUnits.Has() { + units := opts.EnabledUnits.Value() + enabledUnits = make([]repo_model.RepoUnit, len(units)) + + for i, unitType := range units { + enabledUnits[i] = repo_model.RepoUnit{ RepoID: repo.ID, Type: unitType, } } + } - err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, units, disabledUnits) + // Adjust the repo units according to our parameters. + if opts.EnabledUnits.Has() || opts.DisabledUnits.Has() { + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, opts.DisabledUnits.ValueOrDefault(nil)) assert.NoError(t, err) } + // Add files, if any. var sha string - if len(files) > 0 { + if opts.Files.Has() { + files := opts.Files.Value() + resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{ Files: files, Message: "add files", @@ -712,7 +734,30 @@ func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, en sha = resp.Commit.SHA } + // Return the repo, the top commit, and a defer-able function to delete the + // repo. return repo, sha, func() { repo_service.DeleteRepository(db.DefaultContext, owner, repo, false) } } + +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()) { + t.Helper() + + var opts DeclarativeRepoOptions + + if name != "" { + opts.Name = optional.Some(name) + } + if enabledUnits != nil { + opts.EnabledUnits = optional.Some(enabledUnits) + } + if disabledUnits != nil { + opts.DisabledUnits = optional.Some(disabledUnits) + } + if files != nil { + opts.Files = optional.Some(files) + } + + return CreateDeclarativeRepoWithOptions(t, owner, opts) +}