forked from forgejo/forgejo
Unit tests for wiki routers (#3022)
This commit is contained in:
parent
82e8486f13
commit
91f3d77ceb
9 changed files with 285 additions and 69 deletions
|
@ -297,7 +297,7 @@ func TestCommitRepoAction(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, s := range samples {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
|
||||
|
|
|
@ -1,14 +1,8 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3" // for the test engine
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -19,17 +13,5 @@ func TestFixturesAreConsistent(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if err := CreateTestEngine("fixtures/"); err != nil {
|
||||
fmt.Printf("Error creating test engine: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
setting.AppURL = "https://try.gitea.io/"
|
||||
setting.RunUser = "runuser"
|
||||
setting.SSH.Port = 3000
|
||||
setting.SSH.Domain = "try.gitea.io"
|
||||
setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
|
||||
setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
|
||||
|
||||
os.Exit(m.Run())
|
||||
MainTest(m, "..")
|
||||
}
|
||||
|
|
|
@ -5,7 +5,9 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -13,6 +15,7 @@ import (
|
|||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/core"
|
||||
"github.com/go-xorm/xorm"
|
||||
_ "github.com/mattn/go-sqlite3" // for the test engine
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/testfixtures.v2"
|
||||
)
|
||||
|
@ -20,9 +23,30 @@ import (
|
|||
// NonexistentID an ID that will never exist
|
||||
const NonexistentID = 9223372036854775807
|
||||
|
||||
// CreateTestEngine create in-memory sqlite database for unit tests
|
||||
// Any package that calls this must import github.com/mattn/go-sqlite3
|
||||
func CreateTestEngine(fixturesDir string) error {
|
||||
// giteaRoot a path to the gitea root
|
||||
var giteaRoot string
|
||||
|
||||
// MainTest a reusable TestMain(..) function for unit tests that need to use a
|
||||
// test database. Creates the test database, and sets necessary settings.
|
||||
func MainTest(m *testing.M, pathToGiteaRoot string) {
|
||||
giteaRoot = pathToGiteaRoot
|
||||
fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures")
|
||||
if err := createTestEngine(fixturesDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
setting.AppURL = "https://try.gitea.io/"
|
||||
setting.RunUser = "runuser"
|
||||
setting.SSH.Port = 3000
|
||||
setting.SSH.Domain = "try.gitea.io"
|
||||
setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
|
||||
setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func createTestEngine(fixturesDir string) error {
|
||||
var err error
|
||||
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
|
||||
if err != nil {
|
||||
|
@ -45,10 +69,13 @@ func PrepareTestDatabase() error {
|
|||
return LoadFixtures()
|
||||
}
|
||||
|
||||
func prepareTestEnv(t testing.TB) {
|
||||
// PrepareTestEnv prepares the environment for unit tests. Can only be called
|
||||
// by tests that use the above MainTest(..) function.
|
||||
func PrepareTestEnv(t testing.TB) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
|
||||
assert.NoError(t, com.CopyDir("../integrations/gitea-repositories-meta", setting.RepoRootPath))
|
||||
metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
|
||||
assert.NoError(t, com.CopyDir(metaPath, setting.RepoRootPath))
|
||||
}
|
||||
|
||||
type testCond struct {
|
||||
|
|
|
@ -74,6 +74,14 @@ func TestWikiFilenameToName(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.Expected, name)
|
||||
}
|
||||
for _, badFilename := range []string{
|
||||
"nofileextension",
|
||||
"wrongfileextension.txt",
|
||||
"badescaping%%.md",
|
||||
} {
|
||||
_, err := WikiFilenameToName(badFilename)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWikiNameToFilenameToName(t *testing.T) {
|
||||
|
@ -115,7 +123,7 @@ func TestRepository_WikiPath(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRepository_HasWiki(t *testing.T) {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
assert.True(t, repo1.HasWiki())
|
||||
repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
||||
|
@ -123,7 +131,7 @@ func TestRepository_HasWiki(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRepository_InitWiki(t *testing.T) {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
// repo1 already has a wiki
|
||||
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
assert.NoError(t, repo1.InitWiki())
|
||||
|
@ -135,7 +143,7 @@ func TestRepository_InitWiki(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRepository_LocalWikiPath(t *testing.T) {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
expected := filepath.Join(setting.AppDataPath, "tmp/local-wiki/1")
|
||||
assert.Equal(t, expected, repo.LocalWikiPath())
|
||||
|
@ -150,11 +158,23 @@ func TestRepository_AddWikiPage(t *testing.T) {
|
|||
"Another page",
|
||||
"Here's a <tag> and a/slash",
|
||||
} {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
assert.NoError(t, repo.AddWikiPage(doer, wikiName, wikiContent, commitMsg))
|
||||
expectedPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(wikiName))
|
||||
assert.True(t, com.IsExist(expectedPath))
|
||||
}
|
||||
|
||||
// test for already-existing wiki name
|
||||
PrepareTestEnv(t)
|
||||
err := repo.AddWikiPage(doer, "Home", wikiContent, commitMsg)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrWikiAlreadyExist(err))
|
||||
|
||||
// test for reserved wiki name
|
||||
PrepareTestEnv(t)
|
||||
err = repo.AddWikiPage(doer, "_edit", wikiContent, commitMsg)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrWikiReservedName(err))
|
||||
}
|
||||
|
||||
func TestRepository_EditWikiPage(t *testing.T) {
|
||||
|
@ -163,20 +183,23 @@ func TestRepository_EditWikiPage(t *testing.T) {
|
|||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
for _, newWikiName := range []string{
|
||||
"Home", // same name as before
|
||||
"New home",
|
||||
"New/name/with/slashes",
|
||||
} {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
assert.NoError(t, repo.EditWikiPage(doer, "Home", newWikiName, newWikiContent, commitMsg))
|
||||
newPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(newWikiName))
|
||||
assert.True(t, com.IsExist(newPath))
|
||||
oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
|
||||
assert.False(t, com.IsExist(oldPath))
|
||||
if newWikiName != "Home" {
|
||||
oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
|
||||
assert.False(t, com.IsExist(oldPath))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_DeleteWikiPage(t *testing.T) {
|
||||
prepareTestEnv(t)
|
||||
PrepareTestEnv(t)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
assert.NoError(t, repo.DeleteWikiPage(doer, "Home"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue