diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2017-11-30 07:52:15 -0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-11-30 17:52:15 +0200 |
commit | 91f3d77ceb66bedc7bb4d792306beb547f104dce (patch) | |
tree | b081210cb7b6b26415e0e28ceb3a51477407a97e /models/unit_tests.go | |
parent | 82e8486f13253e5a2b1a06c286b1e2b2b6049473 (diff) | |
download | gitea-91f3d77ceb66bedc7bb4d792306beb547f104dce.tar.gz gitea-91f3d77ceb66bedc7bb4d792306beb547f104dce.zip |
Unit tests for wiki routers (#3022)
Diffstat (limited to 'models/unit_tests.go')
-rw-r--r-- | models/unit_tests.go | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/models/unit_tests.go b/models/unit_tests.go index 7f8e86fb8f..cf7c3e4f92 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -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 { |