Skip to content

Commit eaa9a1a

Browse files
committed
add tests
1 parent 4484c69 commit eaa9a1a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

routers/web/repo/wiki_test.go

Lines changed: 29 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,31 @@ func TestWikiRaw(t *testing.T) {
221222
}
222223
}
223224
}
225+
226+
func TestDefaultWikiBranch(t *testing.T) {
227+
unittest.PrepareTestEnv(t)
228+
229+
ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki")
230+
ctx.SetParams("*", "Home")
231+
contexttest.LoadRepo(t, ctx, 1)
232+
// by default the test fixture doesn't have "default wiki branch"
233+
assert.Equal(t, "", ctx.Repo.Repository.DefaultWikiBranch)
234+
Wiki(ctx) // after the visiting, the out-of-sync database record will update the branch name to "master"
235+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
236+
assert.Equal(t, "master", ctx.Repo.Repository.DefaultWikiBranch)
237+
238+
// invalid branch name should fail
239+
assert.Error(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "the bad name"))
240+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
241+
assert.Equal(t, "master", repo.DefaultWikiBranch)
242+
243+
// the same branch name, should succeed (actually a no-op)
244+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "master"))
245+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
246+
assert.Equal(t, "master", repo.DefaultWikiBranch)
247+
248+
// change to another name
249+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "main"))
250+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
251+
assert.Equal(t, "main", repo.DefaultWikiBranch)
252+
}

services/wiki/wiki.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/gitrepo"
2121
"code.gitea.io/gitea/modules/log"
2222
repo_module "code.gitea.io/gitea/modules/repository"
23+
"code.gitea.io/gitea/modules/setting"
2324
"code.gitea.io/gitea/modules/sync"
2425
"code.gitea.io/gitea/modules/util"
2526
asymkey_service "code.gitea.io/gitea/services/asymkey"
@@ -38,11 +39,15 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
3839
return nil
3940
}
4041

42+
defaultBranchName := repo.DefaultWikiBranch
43+
if defaultBranchName == "" { // usually this shouldn't happen, but for testing and for legacy data, it's safe to do the fallback
44+
defaultBranchName = setting.Repository.DefaultBranch
45+
}
4146
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
4247
return fmt.Errorf("InitRepository: %w", err)
4348
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
4449
return fmt.Errorf("createDelegateHooks: %w", err)
45-
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix + repo.DefaultWikiBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
50+
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix + defaultBranchName).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
4651
return fmt.Errorf("unable to set default wiki branch to master: %w", err)
4752
}
4853
return nil

0 commit comments

Comments
 (0)