Skip to content

Commit de6474c

Browse files
committed
[TESTS] tests.AddFixtures helper loads additional per-test fixtures
(cherry picked from commit 93a844dd13904c0ba1b7fd4a0a233002194a504b) (cherry picked from commit 6d6d1a121ce3fc5cf7cd92ad1a38be3bdcbf7088) (cherry picked from commit 34646f9886bb6b06dc1bd06b44c49c28ddb29aad) (cherry picked from commit 83801a64cff076618fe14134c90f38b36a60675c) (cherry picked from commit c9a1e55a80495effb8bd2b45b833943ba19f31d3) (cherry picked from commit 6c7292419c2cda5cfaef5dde8bdef024dd9841ab) (cherry picked from commit 5439218f1a3543fe6941a4dfbbe59784940d092c)
1 parent 12ef8a7 commit de6474c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

models/unittest/fixtures.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package unittest
77
import (
88
"fmt"
99
"os"
10+
"path/filepath"
1011
"time"
1112

1213
"code.gitea.io/gitea/models/db"
@@ -28,6 +29,16 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
2829
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
2930
}
3031

32+
func OverrideFixtures(opts FixturesOptions, engine ...*xorm.Engine) func() {
33+
old := fixturesLoader
34+
if err := InitFixtures(opts, engine...); err != nil {
35+
panic(err)
36+
}
37+
return func() {
38+
fixturesLoader = old
39+
}
40+
}
41+
3142
// InitFixtures initialize test fixtures for a test database
3243
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
3344
e := GetXORMEngine(engine...)
@@ -37,6 +48,12 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
3748
} else {
3849
fixtureOptionFiles = testfixtures.Files(opts.Files...)
3950
}
51+
var fixtureOptionDirs []func(*testfixtures.Loader) error
52+
if opts.Dirs != nil {
53+
for _, dir := range opts.Dirs {
54+
fixtureOptionDirs = append(fixtureOptionDirs, testfixtures.Directory(filepath.Join(opts.Base, dir)))
55+
}
56+
}
4057
dialect := "unknown"
4158
switch e.Dialect().URI().DBType {
4259
case schemas.POSTGRES:
@@ -57,6 +74,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
5774
testfixtures.DangerousSkipTestDatabaseCheck(),
5875
fixtureOptionFiles,
5976
}
77+
loaderOptions = append(loaderOptions, fixtureOptionDirs...)
6078

6179
if e.Dialect().URI().DBType == schemas.POSTGRES {
6280
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())

models/unittest/testdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
197197
type FixturesOptions struct {
198198
Dir string
199199
Files []string
200+
Dirs []string
201+
Base string
200202
}
201203

202204
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir

tests/test_utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,13 @@ func PrintCurrentTest(t testing.TB, skip ...int) func() {
267267
func Printf(format string, args ...any) {
268268
testlogger.Printf(format, args...)
269269
}
270+
271+
func AddFixtures(dirs ...string) func() {
272+
return unittest.OverrideFixtures(
273+
unittest.FixturesOptions{
274+
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
275+
Base: filepath.Dir(setting.AppPath),
276+
Dirs: dirs,
277+
},
278+
)
279+
}

0 commit comments

Comments
 (0)