Skip to content

Commit c31988e

Browse files
committed
add tests
1 parent 4484c69 commit c31988e

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

models/repo/repo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ func (repo *Repository) AfterLoad() {
286286
repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
287287
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
288288
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns
289+
if repo.DefaultWikiBranch == "" {
290+
repo.DefaultWikiBranch = setting.Repository.DefaultBranch
291+
}
289292
}
290293

291294
// LoadAttributes loads attributes of the repository.

routers/web/repo/wiki_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/models/db"
1213
repo_model "code.gitea.io/gitea/models/repo"
1314
"code.gitea.io/gitea/models/unittest"
1415
"code.gitea.io/gitea/modules/git"
@@ -221,3 +222,32 @@ func TestWikiRaw(t *testing.T) {
221222
}
222223
}
223224
}
225+
226+
func TestDefaultWikiBranch(t *testing.T) {
227+
unittest.PrepareTestEnv(t)
228+
229+
assert.NoError(t, repo_model.UpdateRepositoryCols(db.DefaultContext, &repo_model.Repository{ID: 1, DefaultWikiBranch: "wrong-branch"}))
230+
231+
ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki")
232+
ctx.SetParams("*", "Home")
233+
contexttest.LoadRepo(t, ctx, 1)
234+
assert.Equal(t, "wrong-branch", ctx.Repo.Repository.DefaultWikiBranch)
235+
Wiki(ctx) // after the visiting, the out-of-sync database record will update the branch name to "master"
236+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
237+
assert.Equal(t, "master", ctx.Repo.Repository.DefaultWikiBranch)
238+
239+
// invalid branch name should fail
240+
assert.Error(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "the bad name"))
241+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
242+
assert.Equal(t, "master", repo.DefaultWikiBranch)
243+
244+
// the same branch name, should succeed (actually a no-op)
245+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "master"))
246+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
247+
assert.Equal(t, "master", repo.DefaultWikiBranch)
248+
249+
// change to another name
250+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "main"))
251+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
252+
assert.Equal(t, "main", repo.DefaultWikiBranch)
253+
}

services/repository/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOp
6767
defaultBranch, err := wikiRepo.GetDefaultBranch()
6868
if err != nil {
6969
cleanIncompleteWikiPath()
70-
return "", fmt.Errorf("failed to get wiki repo defaul branch for %q, err: %w", wikiPath, err)
70+
return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err)
7171
}
7272

7373
return defaultBranch, nil

0 commit comments

Comments
 (0)