Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestPrepareWikiFileName #16487

Merged
merged 8 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
423313fbd38093bb10d0c8387db9105409c6f196
0dca5bd9b5d7ef937710e056f575e86c0184ba85
21 changes: 21 additions & 0 deletions modules/git/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package git

import (
"bytes"
"strings"
)

Expand Down Expand Up @@ -45,3 +46,23 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) {
}
return g, nil
}

// LsTree checks if the given filenames are in the tree
func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
cmd := NewCommand("ls-tree", "-z", "--name-only", "--", ref)
for _, arg := range filenames {
if arg != "" {
cmd.AddArguments(arg)
}
}
res, err := cmd.RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
filelist := make([]string, 0, len(filenames))
for _, line := range bytes.Split(res, []byte{'\000'}) {
filelist = append(filelist, string(line))
}

return filelist, err
}
4 changes: 2 additions & 2 deletions routers/web/repo/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestWiki(t *testing.T) {
Wiki(ctx)
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
assert.EqualValues(t, "Home", ctx.Data["Title"])
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name", "Unescaped File"}, ctx.Data["Pages"])
}

func TestWikiPages(t *testing.T) {
Expand All @@ -91,7 +91,7 @@ func TestWikiPages(t *testing.T) {
test.LoadRepo(t, ctx, 1)
WikiPages(ctx)
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name", "Unescaped File"}, ctx.Data["Pages"])
}

func TestNewWiki(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string
escaped := NameToFilename(wikiName)

// Look for both files
filesInIndex, err := gitRepo.LsFiles(unescaped, escaped)
filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped)
if err != nil {
log.Error("%v", err)
return false, escaped, err
Expand Down
51 changes: 51 additions & 0 deletions services/wiki/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,54 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
_, err = masterTree.GetTreeEntryByPath(wikiPath)
assert.Error(t, err)
}

func TestPrepareWikiFileName(t *testing.T) {
models.PrepareTestEnv(t)
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
gitRepo, err := git.OpenRepository(repo.WikiPath())
defer gitRepo.Close()
assert.NoError(t, err)

tests := []struct {
name string
arg string
existence bool
wikiPath string
wantErr bool
}{{
name: "add suffix",
arg: "Home",
existence: true,
wikiPath: "Home.md",
wantErr: false,
}, {
name: "test special chars",
arg: "home of and & or wiki page!",
existence: false,
wikiPath: "home-of-and-%26-or-wiki-page%21.md",
wantErr: false,
}, {
name: "fount unescaped cases",
arg: "Unescaped File",
existence: true,
wikiPath: "Unescaped File.md",
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
existence, newWikiPath, err := prepareWikiFileName(gitRepo, tt.arg)
if (err != nil) != tt.wantErr {
assert.NoError(t, err)
return
}
if existence != tt.existence {
if existence {
t.Errorf("expect to find no escaped file but we detect one")
} else {
t.Errorf("expect to find an escaped file but we could not detect one")
}
}
assert.Equal(t, tt.wikiPath, newWikiPath)
})
}
}