chore: replace github.com/go-testfixtures/testfixtures (#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.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7715
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-04-29 19:28:56 +00:00 committed by Earl Warren
parent a16350d9f4
commit 32e64ccd34
4 changed files with 212 additions and 1583 deletions

View file

@ -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, err error) {
@ -31,6 +29,7 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine, err error) {
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
}
@ -50,19 +50,20 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
if err != nil {
return err
}
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"
@ -71,22 +72,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
}