diff options
Diffstat (limited to 'models/unittest/testdb.go')
-rw-r--r-- | models/unittest/testdb.go | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index 4f1bd63f29..117614a7a4 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -39,19 +39,27 @@ func fatalTestError(fmtStr string, args ...interface{}) { os.Exit(1) } +// TestOptions represents test options +type TestOptions struct { + GiteaRootPath string + FixtureFiles []string + SetUp func() error // SetUp will be executed before all tests in this package + TearDown func() error // TearDown will be executed after all tests in this package +} + // 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, fixtureFiles ...string) { +func MainTest(m *testing.M, testOpts *TestOptions) { var err error - giteaRoot = pathToGiteaRoot - fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") + giteaRoot = testOpts.GiteaRootPath + fixturesDir = filepath.Join(testOpts.GiteaRootPath, "models", "fixtures") var opts FixturesOptions - if len(fixtureFiles) == 0 { + if len(testOpts.FixtureFiles) == 0 { opts.Dir = fixturesDir } else { - for _, f := range fixtureFiles { + for _, f := range testOpts.FixtureFiles { if len(f) != 0 { opts.Files = append(opts.Files, filepath.Join(fixturesDir, f)) } @@ -80,8 +88,8 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { fatalTestError("TempDir: %v\n", err) } setting.AppDataPath = appDataPath - setting.AppWorkPath = pathToGiteaRoot - setting.StaticRootPath = pathToGiteaRoot + setting.AppWorkPath = testOpts.GiteaRootPath + setting.StaticRootPath = testOpts.GiteaRootPath setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") if err != nil { fatalTestError("url.Parse: %v\n", err) @@ -105,7 +113,7 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { if err = util.RemoveAll(repoRootPath); err != nil { fatalTestError("util.RemoveAll: %v\n", err) } - if err = CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { + if err = CopyDir(filepath.Join(testOpts.GiteaRootPath, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { fatalTestError("util.CopyDir: %v\n", err) } @@ -129,7 +137,20 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { } } + if testOpts.SetUp != nil { + if err := testOpts.SetUp(); err != nil { + fatalTestError("set up failed: %v\n", err) + } + } + exitStatus := m.Run() + + if testOpts.TearDown != nil { + if err := testOpts.TearDown(); err != nil { + fatalTestError("tear down failed: %v\n", err) + } + } + if err = util.RemoveAll(repoRootPath); err != nil { fatalTestError("util.RemoveAll: %v\n", err) } |