mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-05 01:45:22 +02:00
[v11.0/forgejo] chore: replace github.com/go-testfixtures/testfixtures
(#7729)
**Backport:** #7715
- Replaces `github.com/go-testfixtures/testfixtures` with a homebrew solution that is fully compatible.
- The reason to replace this library is that it pulls in a lot of other libraries which is causing issues: (1) the test binary becomes bigger than necessary which really shows in incremental build times (this patch removes 27.6MiB of the integration test binary) (2) it pulls in libraries (mainly database drivers) that are not used and are not easy to upgrade in case of a security vulnerability, causing CI failures.
(cherry picked from commit 32e64ccd34
)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7729
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
fe07c90636
commit
6a9fb3dbbc
4 changed files with 220 additions and 1592 deletions
|
@ -6,7 +6,6 @@ package unittest
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
|
@ -14,12 +13,11 @@ import (
|
|||
"forgejo.org/modules/auth/password/hash"
|
||||
"forgejo.org/modules/setting"
|
||||
|
||||
"github.com/go-testfixtures/testfixtures/v3"
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/schemas"
|
||||
)
|
||||
|
||||
var fixturesLoader *testfixtures.Loader
|
||||
var fixturesLoader *loader
|
||||
|
||||
// GetXORMEngine gets the XORM engine
|
||||
func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
|
||||
|
@ -31,6 +29,7 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
|
|||
|
||||
func OverrideFixtures(dir string) func() {
|
||||
old := fixturesLoader
|
||||
|
||||
opts := FixturesOptions{
|
||||
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
||||
Base: setting.AppWorkPath,
|
||||
|
@ -39,6 +38,7 @@ func OverrideFixtures(dir string) func() {
|
|||
if err := InitFixtures(opts); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return func() {
|
||||
fixturesLoader = old
|
||||
}
|
||||
|
@ -47,19 +47,19 @@ func OverrideFixtures(dir string) func() {
|
|||
// InitFixtures initialize test fixtures for a test database
|
||||
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
||||
e := GetXORMEngine(engine...)
|
||||
var fixtureOptionFiles func(*testfixtures.Loader) error
|
||||
fixturePaths := []string{}
|
||||
if opts.Dir != "" {
|
||||
fixtureOptionFiles = testfixtures.Directory(opts.Dir)
|
||||
fixturePaths = append(fixturePaths, opts.Dir)
|
||||
} else {
|
||||
fixtureOptionFiles = testfixtures.Files(opts.Files...)
|
||||
fixturePaths = append(fixturePaths, 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)))
|
||||
fixturePaths = append(fixturePaths, filepath.Join(opts.Base, dir))
|
||||
}
|
||||
}
|
||||
dialect := "unknown"
|
||||
|
||||
var dialect string
|
||||
switch e.Dialect().URI().DBType {
|
||||
case schemas.POSTGRES:
|
||||
dialect = "postgres"
|
||||
|
@ -68,22 +68,10 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
|||
case schemas.SQLITE:
|
||||
dialect = "sqlite3"
|
||||
default:
|
||||
fmt.Println("Unsupported RDBMS for integration tests")
|
||||
os.Exit(1)
|
||||
}
|
||||
loaderOptions := []func(loader *testfixtures.Loader) error{
|
||||
testfixtures.Database(e.DB().DB),
|
||||
testfixtures.Dialect(dialect),
|
||||
testfixtures.DangerousSkipTestDatabaseCheck(),
|
||||
fixtureOptionFiles,
|
||||
}
|
||||
loaderOptions = append(loaderOptions, fixtureOptionDirs...)
|
||||
|
||||
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
||||
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
|
||||
panic("Unsupported RDBMS for test")
|
||||
}
|
||||
|
||||
fixturesLoader, err = testfixtures.New(loaderOptions...)
|
||||
fixturesLoader, err = newFixtureLoader(e.DB().DB, dialect, fixturePaths)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue