@ -62,7 +62,6 @@ func InitSettings(extraConfigs ...string) {
// TestOptions represents test options
// TestOptions represents test options
type TestOptions struct {
type TestOptions struct {
GiteaRootPath string
FixtureFiles [ ] string
FixtureFiles [ ] string
SetUp func ( ) error // SetUp will be executed before all tests in this package
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
TearDown func ( ) error // TearDown will be executed after all tests in this package
@ -70,27 +69,39 @@ type TestOptions struct {
// 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 , testOpts * TestOptions ) {
func MainTest ( m * testing . M , testOpts ... * TestOptions ) {
setting . CustomPath = filepath . Join ( testOpts . GiteaRootPath , "custom" )
searchDir , _ := os . Getwd ( )
InitSettings ( )
for searchDir != "" {
if _ , err := os . Stat ( filepath . Join ( searchDir , "go.mod" ) ) ; err == nil {
var err error
break // The "go.mod" should be the one for Gitea repository
}
if dir := filepath . Dir ( searchDir ) ; dir == searchDir {
searchDir = "" // reaches the root of filesystem
} else {
searchDir = dir
}
}
if searchDir == "" {
panic ( "The tests should run in a Gitea repository, there should be a 'go.mod' in the root" )
}
giteaRoot = testOpts . GiteaRootPath
giteaRoot = searchDir
fixturesDir = filepath . Join ( testOpts . GiteaRootPath , "models" , "fixtures" )
setting . CustomPath = filepath . Join ( giteaRoot , "custom" )
InitSettings ( )
fixturesDir = filepath . Join ( giteaRoot , "models" , "fixtures" )
var opts FixturesOptions
var opts FixturesOptions
if len ( testOpts . FixtureFiles ) == 0 {
if len ( testOpts ) == 0 || len ( testOpts [ 0 ] . FixtureFiles ) == 0 {
opts . Dir = fixturesDir
opts . Dir = fixturesDir
} else {
} else {
for _ , f := range testOpts . FixtureFiles {
for _ , f := range testOpts [ 0 ] . 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 ) )
}
}
}
}
}
}
if err = CreateTestEngine ( opts ) ; err != nil {
if err : = CreateTestEngine ( opts ) ; err != nil {
fatalTestError ( "Error creating test engine: %v\n" , err )
fatalTestError ( "Error creating test engine: %v\n" , err )
}
}
@ -112,8 +123,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
fatalTestError ( "TempDir: %v\n" , err )
fatalTestError ( "TempDir: %v\n" , err )
}
}
setting . AppDataPath = appDataPath
setting . AppDataPath = appDataPath
setting . AppWorkPath = testOpts . GiteaRootPath
setting . AppWorkPath = giteaRoot
setting . StaticRootPath = testOpts . GiteaRootPath
setting . StaticRootPath = giteaRoot
setting . GravatarSource = "https://secure.gravatar.com/avatar/"
setting . GravatarSource = "https://secure.gravatar.com/avatar/"
setting . Attachment . Storage . Path = filepath . Join ( setting . AppDataPath , "attachments" )
setting . Attachment . Storage . Path = filepath . Join ( setting . AppDataPath , "attachments" )
@ -144,7 +155,7 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
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 ( 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 )
fatalTestError ( "util.CopyDir: %v\n" , err )
}
}
@ -171,16 +182,16 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
}
}
}
}
if testOpts . SetUp != nil {
if len ( testOpts ) > 0 && testOpts [ 0 ] . SetUp != nil {
if err := testOpts . SetUp ( ) ; err != nil {
if err := testOpts [ 0 ] . SetUp ( ) ; err != nil {
fatalTestError ( "set up failed: %v\n" , err )
fatalTestError ( "set up failed: %v\n" , err )
}
}
}
}
exitStatus := m . Run ( )
exitStatus := m . Run ( )
if testOpts . TearDown != nil {
if len ( testOpts ) > 0 && testOpts [ 0 ] . TearDown != nil {
if err := testOpts . TearDown ( ) ; err != nil {
if err := testOpts [ 0 ] . TearDown ( ) ; err != nil {
fatalTestError ( "tear down failed: %v\n" , err )
fatalTestError ( "tear down failed: %v\n" , err )
}
}
}
}