diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-04-20 01:50:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-19 13:50:10 -0400 |
commit | b39a5bbbd610ba30651218658caaec1c86d6bca1 (patch) | |
tree | 392d7ca47badb62190b286cb2a82ef1e9042956a /services | |
parent | 738f2af5274e8c82ff5bff87d80b10c7ab391667 (diff) | |
download | gitea-b39a5bbbd610ba30651218658caaec1c86d6bca1.tar.gz gitea-b39a5bbbd610ba30651218658caaec1c86d6bca1.zip |
Make wiki title supports dashes and improve wiki name related features (#24143)
Close #7570
1. Clearly define the wiki path behaviors, see
`services/wiki/wiki_path.go` and tests
2. Keep compatibility with old contents
3. Allow to use dashes in titles, eg: "2000-01-02 Meeting record"
4. Add a "Pages" link in the dropdown, otherwise users can't go to the
Pages page easily.
5. Add a "View original git file" link in the Pages list, even if some
file names are broken, users still have a chance to edit or remove it,
without cloning the wiki repo to local.
6. Fix 500 error when the name contains prefix spaces.
This PR also introduces the ability to support sub-directories, but it
can't be done at the moment due to there are a lot of legacy wiki data,
which use "%2F" in file names.
![image](https://user-images.githubusercontent.com/2114189/232239004-3359d7b9-7bf3-4ff3-8446-bfb0e79645dd.png)
![image](https://user-images.githubusercontent.com/2114189/232239020-74b92c72-bf73-4377-a319-1c85609f82b1.png)
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'services')
-rw-r--r-- | services/convert/wiki.go | 9 | ||||
-rw-r--r-- | services/wiki/wiki.go | 97 | ||||
-rw-r--r-- | services/wiki/wiki_path.go | 153 | ||||
-rw-r--r-- | services/wiki/wiki_test.go | 149 |
4 files changed, 268 insertions, 140 deletions
diff --git a/services/convert/wiki.go b/services/convert/wiki.go index 20d76162c7..1f04843483 100644 --- a/services/convert/wiki.go +++ b/services/convert/wiki.go @@ -48,12 +48,13 @@ func ToWikiCommitList(commits []*git.Commit, total int64) *api.WikiCommitList { } // ToWikiPageMetaData converts meta information to a WikiPageMetaData -func ToWikiPageMetaData(title string, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData { - suburl := wiki_service.NameToSubURL(title) +func ToWikiPageMetaData(wikiName wiki_service.WebPath, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData { + subURL := string(wikiName) + _, title := wiki_service.WebPathToUserTitle(wikiName) return &api.WikiPageMetaData{ Title: title, - HTMLURL: util.URLJoin(repo.HTMLURL(), "wiki", suburl), - SubURL: suburl, + HTMLURL: util.URLJoin(repo.HTMLURL(), "wiki", subURL), + SubURL: subURL, LastCommit: ToWikiCommit(lastCommit), } } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index e5cb2db02b..9ceb8e5817 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -7,7 +7,6 @@ package wiki import ( "context" "fmt" - "net/url" "os" "strings" @@ -19,61 +18,17 @@ import ( "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/sync" - "code.gitea.io/gitea/modules/util" asymkey_service "code.gitea.io/gitea/services/asymkey" ) -var ( - reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"} - // TODO: use clustered lock (unique queue? or *abuse* cache) - wikiWorkingPool = sync.NewExclusivePool() -) +// TODO: use clustered lock (unique queue? or *abuse* cache) +var wikiWorkingPool = sync.NewExclusivePool() const ( DefaultRemote = "origin" DefaultBranch = "master" ) -func nameAllowed(name string) error { - if util.SliceContainsString(reservedWikiNames, name) { - return repo_model.ErrWikiReservedName{ - Title: name, - } - } - return nil -} - -// NameToSubURL converts a wiki name to its corresponding sub-URL. -func NameToSubURL(name string) string { - return url.PathEscape(strings.ReplaceAll(name, " ", "-")) -} - -// NormalizeWikiName normalizes a wiki name -func NormalizeWikiName(name string) string { - return strings.ReplaceAll(name, "-", " ") -} - -// NameToFilename converts a wiki name to its corresponding filename. -func NameToFilename(name string) string { - name = strings.ReplaceAll(name, " ", "-") - return url.QueryEscape(name) + ".md" -} - -// FilenameToName converts a wiki filename to its corresponding page name. -func FilenameToName(filename string) (string, error) { - if !strings.HasSuffix(filename, ".md") { - return "", repo_model.ErrWikiInvalidFileName{ - FileName: filename, - } - } - basename := filename[:len(filename)-3] - unescaped, err := url.QueryUnescape(basename) - if err != nil { - return "", err - } - return NormalizeWikiName(unescaped), nil -} - // InitWiki initializes a wiki for repository, // it does nothing when repository already has wiki. func InitWiki(ctx context.Context, repo *repo_model.Repository) error { @@ -91,20 +46,20 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { return nil } -// prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name. +// prepareGitPath try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error -func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) { - unescaped := wikiName + ".md" - escaped := NameToFilename(wikiName) +func prepareGitPath(gitRepo *git.Repository, wikiPath WebPath) (bool, string, error) { + unescaped := string(wikiPath) + ".md" + gitPath := WebPathToGitPath(wikiPath) // Look for both files - filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, escaped) + filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, gitPath) if err != nil { if strings.Contains(err.Error(), "Not a valid object name master") { - return false, escaped, nil + return false, gitPath, nil } log.Error("%v", err) - return false, escaped, err + return false, gitPath, err } foundEscaped := false @@ -113,18 +68,18 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string case unescaped: // if we find the unescaped file return it return true, unescaped, nil - case escaped: + case gitPath: foundEscaped = true } } // If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility. - return foundEscaped, escaped, nil + return foundEscaped, gitPath, nil } // updateWikiPage adds a new page or edits an existing page in repository wiki. -func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { - if err = nameAllowed(newWikiName); err != nil { +func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName WebPath, content, message string, isNew bool) (err error) { + if err = validateWebPath(newWikiName); err != nil { return err } wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID)) @@ -157,24 +112,24 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) - return fmt.Errorf("Failed to clone repository: %s (%w)", repo.FullName(), err) + return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err) } gitRepo, err := git.OpenRepository(ctx, basePath) if err != nil { log.Error("Unable to open temporary repository: %s (%v)", basePath, err) - return fmt.Errorf("Failed to open new temporary repository in: %s %w", basePath, err) + return fmt.Errorf("failed to open new temporary repository in: %s %w", basePath, err) } defer gitRepo.Close() if hasMasterBranch { if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil { log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err) - return fmt.Errorf("Unable to read HEAD tree to index in: %s %w", basePath, err) + return fmt.Errorf("fnable to read HEAD tree to index in: %s %w", basePath, err) } } - isWikiExist, newWikiPath, err := prepareWikiFileName(gitRepo, newWikiName) + isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, newWikiName) if err != nil { return err } @@ -190,7 +145,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model isOldWikiExist := true oldWikiPath := newWikiPath if oldWikiName != newWikiName { - isOldWikiExist, oldWikiPath, err = prepareWikiFileName(gitRepo, oldWikiName) + isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, oldWikiName) if err != nil { return err } @@ -271,18 +226,18 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } // AddWikiPage adds a new wiki page with a given wikiPath. -func AddWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName, content, message string) error { +func AddWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName WebPath, content, message string) error { return updateWikiPage(ctx, doer, repo, "", wikiName, content, message, true) } // EditWikiPage updates a wiki page identified by its wikiPath, // optionally also changing wikiPath. -func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string) error { +func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName WebPath, content, message string) error { return updateWikiPage(ctx, doer, repo, oldWikiName, newWikiName, content, message, false) } // DeleteWikiPage deletes a wiki page identified by its path. -func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName string) (err error) { +func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName WebPath) (err error) { wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID)) defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID)) @@ -306,22 +261,22 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model Branch: DefaultBranch, }); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) - return fmt.Errorf("Failed to clone repository: %s (%w)", repo.FullName(), err) + return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err) } gitRepo, err := git.OpenRepository(ctx, basePath) if err != nil { log.Error("Unable to open temporary repository: %s (%v)", basePath, err) - return fmt.Errorf("Failed to open new temporary repository in: %s %w", basePath, err) + return fmt.Errorf("failed to open new temporary repository in: %s %w", basePath, err) } defer gitRepo.Close() if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil { log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err) - return fmt.Errorf("Unable to read HEAD tree to index in: %s %w", basePath, err) + return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err) } - found, wikiPath, err := prepareWikiFileName(gitRepo, wikiName) + found, wikiPath, err := prepareGitPath(gitRepo, wikiName) if err != nil { return err } @@ -340,7 +295,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model if err != nil { return err } - message := "Delete page '" + wikiName + "'" + message := fmt.Sprintf("Delete page %q", wikiName) commitTreeOpts := git.CommitTreeOpts{ Message: message, Parents: []string{"HEAD"}, diff --git a/services/wiki/wiki_path.go b/services/wiki/wiki_path.go new file mode 100644 index 0000000000..45c6a5a84e --- /dev/null +++ b/services/wiki/wiki_path.go @@ -0,0 +1,153 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package wiki + +import ( + "net/url" + "path" + "strings" + + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/util" +) + +// To define the wiki related concepts: +// * Display Segment: the text what user see for a wiki page (aka, the title): +// - "Home Page" +// - "100% Free" +// - "2000-01-02 meeting" +// * Web Path: +// - "/wiki/Home-Page" +// - "/wiki/100%25+Free" +// - "/wiki/2000-01-02+meeting.-" +// - If a segment has a suffix "DashMarker(.-)", it means that there is no dash-space conversion for this segment. +// - If a WebPath is a "*.md" pattern, then use it directly as GitPath, to make users can access the raw file. +// * Git Path (only space doesn't need to be escaped): +// - "/.wiki.git/Home-Page.md" +// - "/.wiki.git/100%25 Free.md" +// - "/.wiki.git/2000-01-02 meeting.-.md" +// TODO: support subdirectory in the future +// +// Although this package now has the ablity to support subdirectory, but the route package doesn't: +// * Double-escaping problem: the URL "/wiki/abc%2Fdef" becomes "/wiki/abc/def" by ctx.Params, which is incorrect +// * The old wiki code's behavior is always using %2F, instead of subdirectory, so there are a lot of legacy "%2F" files in user wikis. + +type WebPath string + +var reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"} + +func validateWebPath(name WebPath) error { + for _, s := range WebPathSegments(name) { + if util.SliceContainsString(reservedWikiNames, s) { + return repo_model.ErrWikiReservedName{Title: s} + } + } + return nil +} + +func hasDashMarker(s string) bool { + return strings.HasSuffix(s, ".-") +} + +func removeDashMarker(s string) string { + return strings.TrimSuffix(s, ".-") +} + +func addDashMarker(s string) string { + return s + ".-" +} + +func unescapeSegment(s string) (string, error) { + if hasDashMarker(s) { + s = removeDashMarker(s) + } else { + s = strings.ReplaceAll(s, "-", " ") + } + unescaped, err := url.QueryUnescape(s) + if err != nil { + return s, err // un-escaping failed, but it's still safe to return the original string, because it is only a title for end users + } + return unescaped, nil +} + +func escapeSegToWeb(s string, hadDashMarker bool) string { + if hadDashMarker || strings.Contains(s, "-") { + s = addDashMarker(s) + } else { + s = strings.ReplaceAll(s, " ", "-") + } + s = url.QueryEscape(s) + return s +} + +func WebPathSegments(s WebPath) []string { + a := strings.Split(string(s), "/") + for i := range a { + a[i], _ = unescapeSegment(a[i]) + } + return a +} + +func WebPathToGitPath(s WebPath) string { + if strings.HasSuffix(string(s), ".md") { + return string(s) + } + + a := strings.Split(string(s), "/") + for i := range a { + shouldAddDashMarker := hasDashMarker(a[i]) + a[i], _ = unescapeSegment(a[i]) + a[i] = escapeSegToWeb(a[i], shouldAddDashMarker) + a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path + a[i] = strings.ReplaceAll(a[i], "+", " ") + } + return strings.Join(a, "/") + ".md" +} + +func GitPathToWebPath(s string) (wp WebPath, err error) { + if !strings.HasSuffix(s, ".md") { + return "", repo_model.ErrWikiInvalidFileName{FileName: s} + } + s = strings.TrimSuffix(s, ".md") + a := strings.Split(s, "/") + for i := range a { + shouldAddDashMarker := hasDashMarker(a[i]) + if a[i], err = unescapeSegment(a[i]); err != nil { + return "", err + } + a[i] = escapeSegToWeb(a[i], shouldAddDashMarker) + } + return WebPath(strings.Join(a, "/")), nil +} + +func WebPathToUserTitle(s WebPath) (dir, display string) { + dir = path.Dir(string(s)) + display = path.Base(string(s)) + display = strings.TrimSuffix(display, ".md") + display, _ = unescapeSegment(display) + return dir, display +} + +func WebPathToURLPath(s WebPath) string { + return string(s) +} + +func WebPathFromRequest(s string) WebPath { + s = util.PathJoinRelX(s) + // The old wiki code's behavior is always using %2F, instead of subdirectory. + s = strings.ReplaceAll(s, "/", "%2F") + return WebPath(s) +} + +func UserTitleToWebPath(base, title string) WebPath { + // TODO: ctx.Params does un-escaping, so the URL "/wiki/abc%2Fdef" becomes "wiki path = `abc/def`", which is incorrect. + // And the old wiki code's behavior is always using %2F, instead of subdirectory. + // So we do not add the support for writing slashes in title at the moment. + title = strings.TrimSpace(title) + title = util.PathJoinRelX(base, escapeSegToWeb(title, false)) + if title == "" || title == "." { + title = "unnamed" + } + return WebPath(title) +} diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 268d8848c5..716ea6104a 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -4,7 +4,9 @@ package wiki import ( + "math/rand" "path/filepath" + "strings" "testing" repo_model "code.gitea.io/gitea/models/repo" @@ -21,91 +23,113 @@ func TestMain(m *testing.M) { }) } -func TestWikiNameToSubURL(t *testing.T) { +func TestWebPathSegments(t *testing.T) { + a := WebPathSegments("a%2Fa/b+c/d-e/f-g.-") + assert.EqualValues(t, []string{"a/a", "b c", "d e", "f-g"}, a) +} + +func TestUserTitleToWebPath(t *testing.T) { type test struct { - Expected string - WikiName string + Expected string + UserTitle string } for _, test := range []test{ {"wiki-name", "wiki name"}, - {"wiki-name", "wiki-name"}, - {"name-with%2Fslash", "name with/slash"}, - {"name-with%25percent", "name with%percent"}, + {"wiki-name.-", "wiki-name"}, + {"the+wiki-name.-", "the wiki-name"}, + {"a%2Fb", "a/b"}, + {"a%25b", "a%b"}, } { - assert.Equal(t, test.Expected, NameToSubURL(test.WikiName)) + assert.EqualValues(t, test.Expected, UserTitleToWebPath("", test.UserTitle)) } } -func TestNormalizeWikiName(t *testing.T) { +func TestWebPathToDisplayName(t *testing.T) { type test struct { Expected string - WikiName string + WebPath WebPath } for _, test := range []test{ - {"wiki name", "wiki name"}, {"wiki name", "wiki-name"}, - {"name with/slash", "name with/slash"}, - {"name with%percent", "name-with%percent"}, - {"%2F", "%2F"}, + {"wiki-name", "wiki-name.-"}, + {"name with / slash", "name-with %2F slash"}, + {"name with % percent", "name-with %25 percent"}, + {"2000-01-02 meeting", "2000-01-02+meeting.-.md"}, } { - assert.Equal(t, test.Expected, NormalizeWikiName(test.WikiName)) + _, displayName := WebPathToUserTitle(test.WebPath) + assert.EqualValues(t, test.Expected, displayName) } } -func TestWikiNameToFilename(t *testing.T) { +func TestWebPathToGitPath(t *testing.T) { type test struct { Expected string - WikiName string + WikiName WebPath } for _, test := range []test{ - {"wiki-name.md", "wiki name"}, - {"wiki-name.md", "wiki-name"}, - {"name-with%2Fslash.md", "name with/slash"}, - {"name-with%25percent.md", "name with%percent"}, + {"wiki-name.md", "wiki%20name"}, + {"wiki-name.md", "wiki+name"}, + {"wiki%20name.md", "wiki%20name.md"}, + {"2000-01-02-meeting.md", "2000-01-02+meeting"}, + {"2000-01-02 meeting.-.md", "2000-01-02%20meeting.-"}, } { - assert.Equal(t, test.Expected, NameToFilename(test.WikiName)) + assert.EqualValues(t, test.Expected, WebPathToGitPath(test.WikiName)) } } -func TestWikiFilenameToName(t *testing.T) { +func TestGitPathToWebPath(t *testing.T) { type test struct { Expected string Filename string } for _, test := range []test{ - {"hello world", "hello-world.md"}, - {"symbols/?*", "symbols%2F%3F%2A.md"}, + {"hello-world", "hello-world.md"}, // this shouldn't happen, because it should always have a ".-" suffix + {"hello-world", "hello world.md"}, + {"hello-world.-", "hello-world.-.md"}, + {"hello+world.-", "hello world.-.md"}, + {"symbols-%2F", "symbols %2F.md"}, } { - name, err := FilenameToName(test.Filename) + name, err := GitPathToWebPath(test.Filename) assert.NoError(t, err) - assert.Equal(t, test.Expected, name) + assert.EqualValues(t, test.Expected, name) } for _, badFilename := range []string{ "nofileextension", "wrongfileextension.txt", } { - _, err := FilenameToName(badFilename) + _, err := GitPathToWebPath(badFilename) assert.Error(t, err) assert.True(t, repo_model.IsErrWikiInvalidFileName(err)) } - _, err := FilenameToName("badescaping%%.md") + _, err := GitPathToWebPath("badescaping%%.md") assert.Error(t, err) assert.False(t, repo_model.IsErrWikiInvalidFileName(err)) } -func TestWikiNameToFilenameToName(t *testing.T) { - // converting from wiki name to filename, then back to wiki name should - // return the original (normalized) name - for _, name := range []string{ - "wiki-name", - "wiki name", - "wiki name with/slash", - "$$$%%%^^&&!@#$(),.<>", - } { - filename := NameToFilename(name) - resultName, err := FilenameToName(filename) - assert.NoError(t, err) - assert.Equal(t, NormalizeWikiName(name), resultName) +func TestUserWebGitPathConsistency(t *testing.T) { + maxLen := 20 + b := make([]byte, maxLen) + for i := 0; i < 1000; i++ { + l := rand.Intn(maxLen) + for j := 0; j < l; j++ { + r := rand.Intn(0x80-0x20) + 0x20 + b[j] = byte(r) + } + + userTitle := strings.TrimSpace(string(b[:l])) + if userTitle == "" || userTitle == "." { + continue + } + webPath := UserTitleToWebPath("", userTitle) + gitPath := WebPathToGitPath(webPath) + + webPath1, _ := GitPathToWebPath(gitPath) + _, userTitle1 := WebPathToUserTitle(webPath1) + gitPath1 := WebPathToGitPath(webPath1) + + assert.EqualValues(t, userTitle, userTitle1, "UserTitle for userTitle: %q", userTitle) + assert.EqualValues(t, webPath, webPath1, "WebPath for userTitle: %q", userTitle) + assert.EqualValues(t, gitPath, gitPath1, "GitPath for userTitle: %q", userTitle) } } @@ -127,24 +151,23 @@ func TestRepository_AddWikiPage(t *testing.T) { const commitMsg = "Commit message" repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - for _, wikiName := range []string{ + for _, userTitle := range []string{ "Another page", "Here's a <tag> and a/slash", } { - wikiName := wikiName - t.Run("test wiki exist: "+wikiName, func(t *testing.T) { - t.Parallel() - assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, wikiName, wikiContent, commitMsg)) + t.Run("test wiki exist: "+userTitle, func(t *testing.T) { + webPath := UserTitleToWebPath("", userTitle) + assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, webPath, wikiContent, commitMsg)) // Now need to show that the page has been added: gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) defer gitRepo.Close() masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) - wikiPath := NameToFilename(wikiName) - entry, err := masterTree.GetTreeEntryByPath(wikiPath) + gitPath := WebPathToGitPath(webPath) + entry, err := masterTree.GetTreeEntryByPath(gitPath) assert.NoError(t, err) - assert.Equal(t, wikiPath, entry.Name(), "%s not added correctly", wikiName) + assert.EqualValues(t, gitPath, entry.Name(), "%s not added correctly", userTitle) }) } @@ -177,18 +200,19 @@ func TestRepository_EditWikiPage(t *testing.T) { "New home", "New/name/with/slashes", } { + webPath := UserTitleToWebPath("", newWikiName) unittest.PrepareTestEnv(t) - assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", newWikiName, newWikiContent, commitMsg)) + assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", webPath, newWikiContent, commitMsg)) // Now need to show that the page has been added: gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) - wikiPath := NameToFilename(newWikiName) - entry, err := masterTree.GetTreeEntryByPath(wikiPath) + gitPath := WebPathToGitPath(webPath) + entry, err := masterTree.GetTreeEntryByPath(gitPath) assert.NoError(t, err) - assert.Equal(t, wikiPath, entry.Name(), "%s not edited correctly", newWikiName) + assert.EqualValues(t, gitPath, entry.Name(), "%s not edited correctly", newWikiName) if newWikiName != "Home" { _, err := masterTree.GetTreeEntryByPath("Home.md") @@ -210,8 +234,8 @@ func TestRepository_DeleteWikiPage(t *testing.T) { defer gitRepo.Close() masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) - wikiPath := NameToFilename("Home") - _, err = masterTree.GetTreeEntryByPath(wikiPath) + gitPath := WebPathToGitPath("Home") + _, err = masterTree.GetTreeEntryByPath(gitPath) assert.Error(t, err) } @@ -240,16 +264,11 @@ func TestPrepareWikiFileName(t *testing.T) { existence: false, wikiPath: "home-of-and-%26-or-wiki-page%21.md", wantErr: false, - }, { - name: "found 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) + webPath := UserTitleToWebPath("", tt.arg) + existence, newWikiPath, err := prepareGitPath(gitRepo, webPath) if (err != nil) != tt.wantErr { assert.NoError(t, err) return @@ -261,7 +280,7 @@ func TestPrepareWikiFileName(t *testing.T) { t.Errorf("expect to find an escaped file but we could not detect one") } } - assert.Equal(t, tt.wikiPath, newWikiPath) + assert.EqualValues(t, tt.wikiPath, newWikiPath) }) } } @@ -279,8 +298,8 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) { defer gitRepo.Close() assert.NoError(t, err) - existence, newWikiPath, err := prepareWikiFileName(gitRepo, "Home") + existence, newWikiPath, err := prepareGitPath(gitRepo, "Home") assert.False(t, existence) assert.NoError(t, err) - assert.Equal(t, "Home.md", newWikiPath) + assert.EqualValues(t, "Home.md", newWikiPath) } |