]> source.dussan.org Git - gitea.git/commitdiff
Add TestPrepareWikiFileName (#16487) (#16498)
author6543 <6543@obermui.de>
Wed, 21 Jul 2021 02:37:00 +0000 (04:37 +0200)
committerGitHub <noreply@github.com>
Wed, 21 Jul 2021 02:37:00 +0000 (03:37 +0100)
* Add TestPrepareWikiFileName

* use LsTree as LsFiles is index only

* ajust other tests

Co-authored-by: Andrew Thornton <art27@cantab.net>
integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0d/ca5bd9b5d7ef937710e056f575e86c0184ba85 [new file with mode: 0644]
integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/89/43a1d5f93c00439d5ffc0f8e36f5d60abae46c [new file with mode: 0644]
integrations/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master
modules/git/tree.go
routers/web/repo/wiki_test.go
services/wiki/wiki.go
services/wiki/wiki_test.go

diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0d/ca5bd9b5d7ef937710e056f575e86c0184ba85 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0d/ca5bd9b5d7ef937710e056f575e86c0184ba85
new file mode 100644 (file)
index 0000000..a46c192
Binary files /dev/null and b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0d/ca5bd9b5d7ef937710e056f575e86c0184ba85 differ
diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/89/43a1d5f93c00439d5ffc0f8e36f5d60abae46c b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/89/43a1d5f93c00439d5ffc0f8e36f5d60abae46c
new file mode 100644 (file)
index 0000000..062641b
Binary files /dev/null and b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/89/43a1d5f93c00439d5ffc0f8e36f5d60abae46c differ
index 1b1d96a1f00be83e3cbfc1d4f769dd5cc750e05a..38984b12b7868eaa27c4bd19b3002f297e205bc4 100644 (file)
@@ -1 +1 @@
-423313fbd38093bb10d0c8387db9105409c6f196
+0dca5bd9b5d7ef937710e056f575e86c0184ba85
index 059f0a8287d2ab9b728d42c01b81ab26c9935ff7..3671f421e97f4a2f6142e1e65c7a6cc6b395bec0 100644 (file)
@@ -6,6 +6,7 @@
 package git
 
 import (
+       "bytes"
        "strings"
 )
 
@@ -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
+}
index 8934a6619f0fd8d3b302c0017b0c0ed9ffc5e0d0..bcdb8023acff38ae8ed1a87d339c0773ae89679f 100644 (file)
@@ -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) {
@@ -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) {
index 94f36ea2f250496ac336be525754dd7f23e1af55..f1632c6bf7aacfeeecd1fb3ccea28c7d87699d6b 100644 (file)
@@ -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
index b35b86d655be0e138f5e78670b0fa5ca72f69a0a..a1614b509c2bc08c95a5d42cbf8f934c5fd38a47 100644 (file)
@@ -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)
+               })
+       }
+}