Skip to content

Commit

Permalink
Use a struct as test options (go-gitea#19393)
Browse files Browse the repository at this point in the history
* Use a struct as test options

* Fix name

* Fix test
  • Loading branch information
lunny authored and AbdulrhmnGhanem committed Aug 23, 2022
1 parent f3e1e05 commit 61b2f1e
Show file tree
Hide file tree
Showing 40 changed files with 210 additions and 100 deletions.
7 changes: 4 additions & 3 deletions models/admin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"notice.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{"notice.yml"},
})
}
19 changes: 11 additions & 8 deletions models/asymkey/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ func init() {
}

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"gpg_key.yml",
"public_key.yml",
"deploy_key.yml",
"gpg_key_import.yml",
"user.yml",
"email_address.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"gpg_key.yml",
"public_key.yml",
"deploy_key.yml",
"gpg_key_import.yml",
"user.yml",
"email_address.yml",
},
})
}
17 changes: 10 additions & 7 deletions models/auth/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"login_source.yml",
"oauth2_application.yml",
"oauth2_authorization_code.yml",
"oauth2_grant.yml",
"webauthn_credential.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"login_source.yml",
"oauth2_application.yml",
"oauth2_authorization_code.yml",
"oauth2_grant.yml",
"webauthn_credential.yml",
},
})
}
4 changes: 3 additions & 1 deletion models/db/paginator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
}
15 changes: 9 additions & 6 deletions models/issues/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ func init() {
}

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"reaction.yml",
"user.yml",
"repository.yml",
"milestone.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"reaction.yml",
"user.yml",
"repository.yml",
"milestone.yml",
},
})
}
4 changes: 3 additions & 1 deletion models/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ func TestFixturesAreConsistent(t *testing.T) {
}

func TestMain(m *testing.M) {
unittest.MainTest(m, "..")
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
})
}
21 changes: 12 additions & 9 deletions models/organization/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"user.yml",
"org_user.yml",
"team.yml",
"team_repo.yml",
"team_unit.yml",
"team_user.yml",
"repository.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"user.yml",
"org_user.yml",
"team.yml",
"team_repo.yml",
"team_unit.yml",
"team_user.yml",
"repository.yml",
},
})
}
15 changes: 9 additions & 6 deletions models/project/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"project.yml",
"project_board.yml",
"project_issue.yml",
"repository.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"project.yml",
"project_board.yml",
"project_issue.yml",
"repository.yml",
},
})
}
29 changes: 16 additions & 13 deletions models/repo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"attachment.yml",
"repo_archiver.yml",
"repository.yml",
"repo_unit.yml",
"repo_indexer_status.yml",
"repo_redirect.yml",
"watch.yml",
"star.yml",
"topic.yml",
"repo_topic.yml",
"user.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"attachment.yml",
"repo_archiver.yml",
"repository.yml",
"repo_unit.yml",
"repo_indexer_status.yml",
"repo_redirect.yml",
"watch.yml",
"star.yml",
"topic.yml",
"repo_topic.yml",
"user.yml",
},
})
}
37 changes: 29 additions & 8 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand Down
21 changes: 12 additions & 9 deletions models/user/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"email_address.yml",
"user_redirect.yml",
"follow.yml",
"user_open_id.yml",
"two_factor.yml",
"oauth2_application.yml",
"user.yml",
)
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"email_address.yml",
"user_redirect.yml",
"follow.yml",
"user_open_id.yml",
"two_factor.yml",
"oauth2_application.yml",
"user.yml",
},
})
}
8 changes: 7 additions & 1 deletion models/webhook/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), "webhook.yml", "hook_task.yml")
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{
"webhook.yml",
"hook_task.yml",
},
})
}
5 changes: 4 additions & 1 deletion modules/appstate/appstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), "")
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
FixtureFiles: []string{""}, // load nothing
})
}

type testItem1 struct {
Expand Down
4 changes: 3 additions & 1 deletion modules/convert/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
}
4 changes: 3 additions & 1 deletion modules/indexer/code/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
)

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

func testIndexer(name string, t *testing.T, indexer Indexer) {
Expand Down
4 changes: 3 additions & 1 deletion modules/indexer/issues/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

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

func TestBleveSearchIssues(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/indexer/stats/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

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

func TestRepoStatsIndex(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/notification/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
)

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

func TestRenameRepoAction(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/repository/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
}
4 changes: 3 additions & 1 deletion routers/api/v1/repo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", "..", ".."),
})
}
4 changes: 3 additions & 1 deletion routers/web/admin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
}
4 changes: 3 additions & 1 deletion routers/web/auth/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
}
4 changes: 3 additions & 1 deletion routers/web/repo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
)

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

0 comments on commit 61b2f1e

Please sign in to comment.