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 <forgejo@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2024-04-24 23:33:08 +02:00
parent 1bce2dc5c5
commit e7fcf3f189
No known key found for this signature in database

View file

@ -36,6 +36,7 @@ import (
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/testlogger" "code.gitea.io/gitea/modules/testlogger"
"code.gitea.io/gitea/modules/util" "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() 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() t.Helper()
repoName := name // Not using opts.Name.ValueOrDefault() here to avoid unnecessarily
if repoName == "" { // generating an UUID when a name is specified.
var repoName string
if opts.Name.Has() {
repoName = opts.Name.Value()
} else {
repoName = gouuid.NewString() repoName = gouuid.NewString()
} }
// Create a new repository // Create the repository
repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{ repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{
Name: repoName, Name: repoName,
Description: "Temporary Repo", Description: "Temporary Repo",
@ -673,21 +685,31 @@ func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, en
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, repo) assert.NotEmpty(t, repo)
if enabledUnits != nil || disabledUnits != nil { // Populate `enabledUnits` if we have any enabled.
units := make([]repo_model.RepoUnit, len(enabledUnits)) var enabledUnits []repo_model.RepoUnit
for i, unitType := range enabledUnits { if opts.EnabledUnits.Has() {
units[i] = repo_model.RepoUnit{ units := opts.EnabledUnits.Value()
enabledUnits = make([]repo_model.RepoUnit, len(units))
for i, unitType := range units {
enabledUnits[i] = repo_model.RepoUnit{
RepoID: repo.ID, RepoID: repo.ID,
Type: unitType, 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) assert.NoError(t, err)
} }
// Add files, if any.
var sha string 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{ resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{
Files: files, Files: files,
Message: "add files", Message: "add files",
@ -712,7 +734,30 @@ func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, en
sha = resp.Commit.SHA sha = resp.Commit.SHA
} }
// Return the repo, the top commit, and a defer-able function to delete the
// repo.
return repo, sha, func() { return repo, sha, func() {
repo_service.DeleteRepository(db.DefaultContext, owner, repo, false) 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)
}