mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Move login related structs and functions to models/login (#17093)
* Move login related structs and functions to models/login * Fix test * Fix lint * Fix lint * Fix lint of windows * Fix lint * Fix test * Fix test * Only load necessary fixtures when preparing unit tests envs * Fix lint * Fix test * Fix test * Fix error log * Fix error log * Fix error log * remove unnecessary change * fix error log * merge main branch
This commit is contained in:
@@ -44,11 +44,21 @@ func fatalTestError(fmtStr string, args ...interface{}) {
|
||||
|
||||
// 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) {
|
||||
func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
|
||||
var err error
|
||||
giteaRoot = pathToGiteaRoot
|
||||
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
|
||||
if err = CreateTestEngine(fixturesDir); err != nil {
|
||||
|
||||
var opts FixturesOptions
|
||||
if len(fixtureFiles) == 0 {
|
||||
opts.Dir = fixturesDir
|
||||
} else {
|
||||
for _, f := range fixtureFiles {
|
||||
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
|
||||
}
|
||||
}
|
||||
|
||||
if err = CreateTestEngine(opts); err != nil {
|
||||
fatalTestError("Error creating test engine: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -102,8 +112,14 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
|
||||
os.Exit(exitStatus)
|
||||
}
|
||||
|
||||
// FixturesOptions fixtures needs to be loaded options
|
||||
type FixturesOptions struct {
|
||||
Dir string
|
||||
Files []string
|
||||
}
|
||||
|
||||
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
|
||||
func CreateTestEngine(fixturesDir string) error {
|
||||
func CreateTestEngine(opts FixturesOptions) error {
|
||||
var err error
|
||||
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
|
||||
if err != nil {
|
||||
@@ -123,7 +139,7 @@ func CreateTestEngine(fixturesDir string) error {
|
||||
e: x,
|
||||
}
|
||||
|
||||
return InitFixtures(fixturesDir)
|
||||
return InitFixtures(opts)
|
||||
}
|
||||
|
||||
// PrepareTestDatabase load test fixtures into test database
|
||||
|
Reference in New Issue
Block a user