From 6c7292419c2cda5cfaef5dde8bdef024dd9841ab Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 1 Oct 2023 18:38:19 +0200 Subject: [PATCH] [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) --- models/unittest/fixtures.go | 18 ++++++++++++++++++ models/unittest/testdb.go | 2 ++ tests/test_utils.go | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/models/unittest/fixtures.go b/models/unittest/fixtures.go index c653ce1e38..9ce0909589 100644 --- a/models/unittest/fixtures.go +++ b/models/unittest/fixtures.go @@ -7,6 +7,7 @@ package unittest import ( "fmt" "os" + "path/filepath" "time" "code.gitea.io/gitea/models/db" @@ -28,6 +29,16 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) { return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) } +func OverrideFixtures(opts FixturesOptions, engine ...*xorm.Engine) func() { + old := fixturesLoader + if err := InitFixtures(opts, engine...); err != nil { + panic(err) + } + return func() { + fixturesLoader = old + } +} + // InitFixtures initialize test fixtures for a test database func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { e := GetXORMEngine(engine...) @@ -37,6 +48,12 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { } else { fixtureOptionFiles = testfixtures.Files(opts.Files...) } + var fixtureOptionDirs []func(*testfixtures.Loader) error + if opts.Dirs != nil { + for _, dir := range opts.Dirs { + fixtureOptionDirs = append(fixtureOptionDirs, testfixtures.Directory(filepath.Join(opts.Base, dir))) + } + } dialect := "unknown" switch e.Dialect().URI().DBType { case schemas.POSTGRES: @@ -57,6 +74,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { testfixtures.DangerousSkipTestDatabaseCheck(), fixtureOptionFiles, } + loaderOptions = append(loaderOptions, fixtureOptionDirs...) if e.Dialect().URI().DBType == schemas.POSTGRES { loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences()) diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index 7b40a3147b..0402234fd2 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -197,6 +197,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) { type FixturesOptions struct { Dir string Files []string + Dirs []string + Base string } // CreateTestEngine creates a memory database and loads the fixture data from fixturesDir diff --git a/tests/test_utils.go b/tests/test_utils.go index 50049e73f0..8e456783cf 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -267,3 +267,13 @@ func PrintCurrentTest(t testing.TB, skip ...int) func() { func Printf(format string, args ...any) { testlogger.Printf(format, args...) } + +func AddFixtures(dirs ...string) func() { + return unittest.OverrideFixtures( + unittest.FixturesOptions{ + Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"), + Base: filepath.Dir(setting.AppPath), + Dirs: dirs, + }, + ) +}