c572fcfe04
Rename the Go module path from code.gitea.io/gitea to git.mokoconsulting.tech/MokoConsulting/MokoGitea across the entire codebase. Scope: - go.mod module declaration - 2,235 Go source files (import paths) - Dockerfile WORKDIR and COPY paths - Swagger API templates - golangci.yml linter config External dependencies (code.gitea.io/gitea-vet, code.gitea.io/sdk/gitea, gitea.com/gitea/act, etc.) are intentionally NOT renamed — they are separate upstream modules. Closes #132 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package migrationtest
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/models/db"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/models/unittest"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/git"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/setting"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/testlogger"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"xorm.io/xorm"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
// PrepareTestEnv prepares the test environment and reset the database. The skip parameter should usually be 0.
|
|
// Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from.
|
|
//
|
|
// fixtures in `models/migrations/fixtures/<TestName>` will be loaded automatically
|
|
func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, func()) {
|
|
t.Helper()
|
|
ourSkip := 2
|
|
ourSkip += skip
|
|
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
|
|
giteaRoot := setting.GetGiteaTestSourceRoot()
|
|
require.NoError(t, unittest.SyncDirs(filepath.Join(giteaRoot, "tests/gitea-repositories-meta"), setting.RepoRootPath))
|
|
|
|
cleanup, err := unittest.ResetTestDatabase()
|
|
if err != nil {
|
|
t.Fatalf("unable to reset database: %v", err)
|
|
return nil, deferFn
|
|
}
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
cleanup()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
err = db.InitEngine(t.Context())
|
|
if !assert.NoError(t, err) {
|
|
return nil, deferFn
|
|
}
|
|
x := unittest.GetXORMEngine()
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
_ = x.Close()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
if len(syncModels) > 0 {
|
|
if err := x.Sync(syncModels...); err != nil {
|
|
t.Errorf("error during sync: %v", err)
|
|
return x, deferFn
|
|
}
|
|
}
|
|
|
|
fixturesDir := filepath.Join(giteaRoot, "models", "migrations", "fixtures", t.Name())
|
|
|
|
if _, err := os.Stat(fixturesDir); err == nil {
|
|
t.Logf("initializing fixtures from: %s", fixturesDir)
|
|
if err := unittest.InitFixtures(
|
|
unittest.FixturesOptions{
|
|
Dir: fixturesDir,
|
|
}, x); err != nil {
|
|
t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
if err := unittest.LoadFixtures(); err != nil {
|
|
t.Errorf("error whilst loading fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
} else if !os.IsNotExist(err) {
|
|
t.Errorf("unexpected error whilst checking for existence of fixtures: %v", err)
|
|
} else {
|
|
t.Logf("no fixtures found in: %s", fixturesDir)
|
|
}
|
|
|
|
return x, deferFn
|
|
}
|
|
|
|
func LoadTableSchemasMap(t *testing.T, x *xorm.Engine) map[string]*schemas.Table {
|
|
tables, err := x.DBMetas()
|
|
require.NoError(t, err)
|
|
tableMap := make(map[string]*schemas.Table)
|
|
for _, table := range tables {
|
|
tableMap[table.Name] = table
|
|
}
|
|
return tableMap
|
|
}
|
|
|
|
func mainTest(m *testing.M) int {
|
|
testlogger.Init()
|
|
err := setting.PrepareIntegrationTestConfig()
|
|
if err != nil {
|
|
return testlogger.MainErrorf("Unable to prepare integration test config: %v", err)
|
|
}
|
|
setting.SetupGiteaTestEnv()
|
|
|
|
if err = git.InitFull(); err != nil {
|
|
return testlogger.MainErrorf("Unable to InitFull: %v", err)
|
|
}
|
|
setting.LoadDBSetting()
|
|
setting.InitLoggersForTest()
|
|
return m.Run()
|
|
}
|
|
|
|
func MainTest(m *testing.M) {
|
|
os.Exit(mainTest(m))
|
|
}
|