Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make writing main test easier #27270

Merged
merged 10 commits into from
Sep 28, 2023
4 changes: 1 addition & 3 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
})
unittest.MainTest(m)
}

func makePathOutput(workPath, customPath, customConf string) string {
Expand Down
5 changes: 1 addition & 4 deletions models/activities/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package activities_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -14,7 +13,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
2 changes: 0 additions & 2 deletions models/asymkey/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
package asymkey

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"gpg_key.yml",
"public_key.yml",
Expand Down
5 changes: 1 addition & 4 deletions models/auth/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package auth_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -17,7 +16,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/avatars/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package avatars_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -15,7 +14,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/db/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package db_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -14,7 +13,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/db/paginator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
package paginator

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/dbfs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
package dbfs

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/git/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package git_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -15,7 +14,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/issues/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package issues_test

import (
"path/filepath"
"testing"

issues_model "code.gitea.io/gitea/models/issues"
Expand All @@ -30,7 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) {
}

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
4 changes: 1 addition & 3 deletions models/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) {
}

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/organization/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package organization_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -18,7 +17,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package packages_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/db"
Expand All @@ -20,9 +19,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}

func TestHasOwnerPackages(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions models/perm/access/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package access_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -17,7 +16,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
unittest.MainTest(m)
}
2 changes: 0 additions & 2 deletions models/project/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package project

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -14,7 +13,6 @@ import (

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"project.yml",
"project_board.yml",
Expand Down
5 changes: 1 addition & 4 deletions models/repo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package repo_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -18,7 +17,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
5 changes: 1 addition & 4 deletions models/system/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package system_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -16,7 +15,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
53 changes: 33 additions & 20 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,41 @@ func InitSettings(extraConfigs ...string) {

// 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
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, testOpts *TestOptions) {
setting.CustomPath = filepath.Join(testOpts.GiteaRootPath, "custom")
InitSettings()

var err error
func MainTest(m *testing.M, testOpts ...*TestOptions) {
file, err := os.Getwd()
if err != nil {
panic(err)
}
var found bool
for {
file = filepath.Dir(file)
exist, _ := util.IsFile(filepath.Join(file, "go.mod")) // Gitea workspace should be only one golang project
if exist {
found = true
break
}
}
if !found {
panic("You cannot run the tests out of a golang repository")
}

giteaRoot = testOpts.GiteaRootPath
fixturesDir = filepath.Join(testOpts.GiteaRootPath, "models", "fixtures")
giteaRoot = file
setting.CustomPath = filepath.Join(giteaRoot, "custom")
InitSettings()

fixturesDir = filepath.Join(giteaRoot, "models", "fixtures")
var opts FixturesOptions
if len(testOpts.FixtureFiles) == 0 {
if len(testOpts) == 0 || len(testOpts[0].FixtureFiles) == 0 {
opts.Dir = fixturesDir
} else {
for _, f := range testOpts.FixtureFiles {
for _, f := range testOpts[0].FixtureFiles {
if len(f) != 0 {
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
}
Expand Down Expand Up @@ -112,8 +125,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
fatalTestError("TempDir: %v\n", err)
}
setting.AppDataPath = appDataPath
setting.AppWorkPath = testOpts.GiteaRootPath
setting.StaticRootPath = testOpts.GiteaRootPath
setting.AppWorkPath = giteaRoot
setting.StaticRootPath = giteaRoot
setting.GravatarSource = "https://secure.gravatar.com/avatar/"

setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
Expand Down Expand Up @@ -144,7 +157,7 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err)
}
if err = CopyDir(filepath.Join(testOpts.GiteaRootPath, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
if err = CopyDir(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
fatalTestError("util.CopyDir: %v\n", err)
}

Expand All @@ -171,16 +184,16 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
}
}

if testOpts.SetUp != nil {
if err := testOpts.SetUp(); err != nil {
if len(testOpts) > 0 && testOpts[0].SetUp != nil {
if err := testOpts[0].SetUp(); err != nil {
fatalTestError("set up failed: %v\n", err)
}
}

exitStatus := m.Run()

if testOpts.TearDown != nil {
if err := testOpts.TearDown(); err != nil {
if len(testOpts) > 0 && testOpts[0].TearDown != nil {
if err := testOpts[0].TearDown(); err != nil {
fatalTestError("tear down failed: %v\n", err)
}
}
Expand Down
5 changes: 1 addition & 4 deletions models/user/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package user_test

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
Expand All @@ -16,7 +15,5 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
unittest.MainTest(m)
}
Loading