Use a struct as test options (#19393)

* Use a struct as test options

* Fix name

* Fix test
tokarchuk/v1.17
Lunny Xiao 3 years ago committed by GitHub
parent 4dabc212c7
commit b8911fb456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      models/admin/main_test.go
  2. 19
      models/asymkey/main_test.go
  3. 17
      models/auth/main_test.go
  4. 4
      models/db/paginator/main_test.go
  5. 15
      models/issues/main_test.go
  6. 4
      models/main_test.go
  7. 21
      models/organization/main_test.go
  8. 15
      models/project/main_test.go
  9. 29
      models/repo/main_test.go
  10. 37
      models/unittest/testdb.go
  11. 21
      models/user/main_test.go
  12. 8
      models/webhook/main_test.go
  13. 5
      modules/appstate/appstate_test.go
  14. 4
      modules/convert/main_test.go
  15. 4
      modules/indexer/code/indexer_test.go
  16. 4
      modules/indexer/issues/indexer_test.go
  17. 4
      modules/indexer/stats/indexer_test.go
  18. 4
      modules/notification/action/action_test.go
  19. 4
      modules/repository/main_test.go
  20. 4
      routers/api/v1/repo/main_test.go
  21. 4
      routers/web/admin/main_test.go
  22. 4
      routers/web/auth/main_test.go
  23. 4
      routers/web/repo/main_test.go
  24. 4
      routers/web/user/main_test.go
  25. 4
      routers/web/user/setting/main_test.go
  26. 4
      services/asymkey/main_test.go
  27. 4
      services/attachment/attachment_test.go
  28. 4
      services/gitdiff/main_test.go
  29. 4
      services/issue/main_test.go
  30. 4
      services/mailer/main_test.go
  31. 4
      services/migrations/main_test.go
  32. 4
      services/org/org_test.go
  33. 4
      services/pull/main_test.go
  34. 4
      services/release/release_test.go
  35. 4
      services/repository/archiver/archiver_test.go
  36. 4
      services/repository/files/content_test.go
  37. 4
      services/repository/main_test.go
  38. 4
      services/user/user_test.go
  39. 4
      services/webhook/main_test.go
  40. 4
      services/wiki/wiki_test.go

@ -12,7 +12,8 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"notice.yml", GiteaRootPath: filepath.Join("..", ".."),
) FixtureFiles: []string{"notice.yml"},
})
} }

@ -18,12 +18,15 @@ func init() {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"gpg_key.yml", GiteaRootPath: filepath.Join("..", ".."),
"public_key.yml", FixtureFiles: []string{
"deploy_key.yml", "gpg_key.yml",
"gpg_key_import.yml", "public_key.yml",
"user.yml", "deploy_key.yml",
"email_address.yml", "gpg_key_import.yml",
) "user.yml",
"email_address.yml",
},
})
} }

@ -12,11 +12,14 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"login_source.yml", GiteaRootPath: filepath.Join("..", ".."),
"oauth2_application.yml", FixtureFiles: []string{
"oauth2_authorization_code.yml", "login_source.yml",
"oauth2_grant.yml", "oauth2_application.yml",
"webauthn_credential.yml", "oauth2_authorization_code.yml",
) "oauth2_grant.yml",
"webauthn_credential.yml",
},
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }

@ -18,10 +18,13 @@ func init() {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"reaction.yml", GiteaRootPath: filepath.Join("..", ".."),
"user.yml", FixtureFiles: []string{
"repository.yml", "reaction.yml",
"milestone.yml", "user.yml",
) "repository.yml",
"milestone.yml",
},
})
} }

@ -37,5 +37,7 @@ func TestFixturesAreConsistent(t *testing.T) {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, "..") unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
})
} }

@ -12,13 +12,16 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"user.yml", GiteaRootPath: filepath.Join("..", ".."),
"org_user.yml", FixtureFiles: []string{
"team.yml", "user.yml",
"team_repo.yml", "org_user.yml",
"team_unit.yml", "team.yml",
"team_user.yml", "team_repo.yml",
"repository.yml", "team_unit.yml",
) "team_user.yml",
"repository.yml",
},
})
} }

@ -14,10 +14,13 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"project.yml", GiteaRootPath: filepath.Join("..", ".."),
"project_board.yml", FixtureFiles: []string{
"project_issue.yml", "project.yml",
"repository.yml", "project_board.yml",
) "project_issue.yml",
"repository.yml",
},
})
} }

@ -12,17 +12,20 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"attachment.yml", GiteaRootPath: filepath.Join("..", ".."),
"repo_archiver.yml", FixtureFiles: []string{
"repository.yml", "attachment.yml",
"repo_unit.yml", "repo_archiver.yml",
"repo_indexer_status.yml", "repository.yml",
"repo_redirect.yml", "repo_unit.yml",
"watch.yml", "repo_indexer_status.yml",
"star.yml", "repo_redirect.yml",
"topic.yml", "watch.yml",
"repo_topic.yml", "star.yml",
"user.yml", "topic.yml",
) "repo_topic.yml",
"user.yml",
},
})
} }

@ -39,19 +39,27 @@ func fatalTestError(fmtStr string, args ...interface{}) {
os.Exit(1) 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 // MainTest a reusable TestMain(..) function for unit tests that need to use a
// test database. Creates the test database, and sets necessary settings. // 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 var err error
giteaRoot = pathToGiteaRoot giteaRoot = testOpts.GiteaRootPath
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") fixturesDir = filepath.Join(testOpts.GiteaRootPath, "models", "fixtures")
var opts FixturesOptions var opts FixturesOptions
if len(fixtureFiles) == 0 { if len(testOpts.FixtureFiles) == 0 {
opts.Dir = fixturesDir opts.Dir = fixturesDir
} else { } else {
for _, f := range fixtureFiles { for _, f := range testOpts.FixtureFiles {
if len(f) != 0 { if len(f) != 0 {
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f)) 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) fatalTestError("TempDir: %v\n", err)
} }
setting.AppDataPath = appDataPath setting.AppDataPath = appDataPath
setting.AppWorkPath = pathToGiteaRoot setting.AppWorkPath = testOpts.GiteaRootPath
setting.StaticRootPath = pathToGiteaRoot setting.StaticRootPath = testOpts.GiteaRootPath
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
if err != nil { if err != nil {
fatalTestError("url.Parse: %v\n", err) 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 { if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err) 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) 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() 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 { if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err) fatalTestError("util.RemoveAll: %v\n", err)
} }

@ -12,13 +12,16 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), unittest.MainTest(m, &unittest.TestOptions{
"email_address.yml", GiteaRootPath: filepath.Join("..", ".."),
"user_redirect.yml", FixtureFiles: []string{
"follow.yml", "email_address.yml",
"user_open_id.yml", "user_redirect.yml",
"two_factor.yml", "follow.yml",
"oauth2_application.yml", "user_open_id.yml",
"user.yml", "two_factor.yml",
) "oauth2_application.yml",
"user.yml",
},
})
} }

@ -12,5 +12,11 @@ import (
) )
func TestMain(m *testing.M) { 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",
},
})
} }

@ -14,7 +14,10 @@ import (
) )
func TestMain(m *testing.M) { 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 { type testItem1 struct {

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -18,7 +18,9 @@ import (
) )
func TestMain(m *testing.M) { 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) { func testIndexer(name string, t *testing.T, indexer Indexer) {

@ -23,7 +23,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }
func TestBleveSearchIssues(t *testing.T) { func TestBleveSearchIssues(t *testing.T) {

@ -23,7 +23,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }
func TestRepoStatsIndex(t *testing.T) { func TestRepoStatsIndex(t *testing.T) {

@ -18,7 +18,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }
func TestRenameRepoAction(t *testing.T) { func TestRenameRepoAction(t *testing.T) {

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", "..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -17,7 +17,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func TestUploadAttachment(t *testing.T) { func TestUploadAttachment(t *testing.T) {

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -17,7 +17,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func timePtr(t time.Time) *time.Time { func timePtr(t time.Time) *time.Time {

@ -17,7 +17,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func TestDeleteOrganization(t *testing.T) { func TestDeleteOrganization(t *testing.T) {

@ -13,5 +13,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -21,7 +21,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func TestRelease_Create(t *testing.T) { func TestRelease_Create(t *testing.T) {

@ -17,7 +17,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }
func TestArchive_Basic(t *testing.T) { func TestArchive_Basic(t *testing.T) {

@ -16,7 +16,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
})
} }
func getExpectedReadmeContentsResponse() *api.ContentsResponse { func getExpectedReadmeContentsResponse() *api.ContentsResponse {

@ -12,5 +12,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -20,7 +20,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func TestDeleteUser(t *testing.T) { func TestDeleteUser(t *testing.T) {

@ -14,5 +14,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }

@ -20,7 +20,9 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", "..")) unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
} }
func TestWikiNameToSubURL(t *testing.T) { func TestWikiNameToSubURL(t *testing.T) {

Loading…
Cancel
Save