mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add TestPrepareWikiFileName (#16487)
* Add TestPrepareWikiFileName * use LsTree as LsFiles is index only * ajust other tests Co-authored-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -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)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user