refactor: remove environment variable requirements for go test

This commit is contained in:
John Starich 2026-02-08 19:31:20 -06:00 committed by kolaente
parent 65ef54f623
commit 591a646f84
12 changed files with 19 additions and 28 deletions

View File

@ -65,8 +65,6 @@ func CreateTestEngine() (engine *xorm.Engine, err error) {
func InitTestFixtures(tablenames ...string) (err error) {
// Create all fixtures
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
// Sync fixtures
err = InitFixtures(tablenames...)

View File

@ -17,6 +17,7 @@
package db
import (
"embed"
"fmt"
"path/filepath"
"testing"
@ -28,13 +29,16 @@ import (
"xorm.io/xorm/schemas"
)
var fixtures *testfixtures.Loader
var (
fixtures *testfixtures.Loader
//go:embed fixtures
fixturesFS embed.FS
)
// InitFixtures initialize test fixtures for a test database
func InitFixtures(tablenames ...string) (err error) {
var testfiles func(loader *testfixtures.Loader) error
dir := filepath.Join(config.ServiceRootpath.GetString(), "pkg", "db", "fixtures")
dir := "fixtures"
// If fixture table names are specified, load them
// Otherwise, load all fixtures
@ -48,6 +52,7 @@ func InitFixtures(tablenames ...string) (err error) {
}
loaderOptions := []func(loader *testfixtures.Loader) error{
testfixtures.FS(fixturesFS),
testfixtures.Database(x.DB().DB),
testfixtures.Dialect(GetDialect()),
testfixtures.DangerousSkipTestDatabaseCheck(),

View File

@ -60,8 +60,6 @@ func TestMain(m *testing.M) {
// Set default config
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
i18n.Init()

View File

@ -36,8 +36,6 @@ func TestMain(m *testing.M) {
// Set default config
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()

View File

@ -20,6 +20,7 @@ import (
"os"
"testing"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
)
@ -27,6 +28,8 @@ import (
func TestMain(m *testing.M) {
// Initialize logger for tests
log.InitLogger()
config.ServicePublicURL.Set("https://localhost")
config.InitConfig()
os.Exit(m.Run())
}

View File

@ -31,9 +31,6 @@ import (
)
func TestConvertTodoistToVikunja(t *testing.T) {
config.InitConfig()
time1, err := time.Parse(time.RFC3339Nano, "2014-09-26T08:25:05Z")
require.NoError(t, err)
time1 = time1.In(config.GetTimeZone())
@ -48,7 +45,7 @@ func TestConvertTodoistToVikunja(t *testing.T) {
dueTimeWithTime = dueTimeWithTime.In(config.GetTimeZone())
nilTime, err := time.Parse(time.RFC3339Nano, "0001-01-01T00:00:00Z")
require.NoError(t, err)
exampleFile, err := os.ReadFile(config.ServiceRootpath.GetString() + "/pkg/modules/migration/testimage.jpg")
exampleFile, err := os.ReadFile("../testimage.jpg")
require.NoError(t, err)
makeTestItem := func(id, projectId string, hasDueDate, hasLabels, done bool) *item {

View File

@ -20,6 +20,7 @@ import (
"os"
"testing"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
)
@ -27,6 +28,8 @@ import (
func TestMain(m *testing.M) {
// Initialize logger for tests
log.InitLogger()
config.ServicePublicURL.Set("https://localhost")
config.InitConfig()
os.Exit(m.Run())
}

View File

@ -22,7 +22,6 @@ import (
"testing"
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/models"
@ -33,9 +32,6 @@ import (
)
func getTestBoard(t *testing.T) ([]*trello.Board, time.Time) {
config.InitConfig()
time1, err := time.Parse(time.RFC3339Nano, "2014-09-26T08:25:05Z")
require.NoError(t, err)
@ -240,7 +236,7 @@ func getTestBoard(t *testing.T) ([]*trello.Board, time.Time) {
func TestConvertTrelloToVikunja(t *testing.T) {
trelloData, time1 := getTestBoard(t)
exampleFile, err := os.ReadFile(config.ServiceRootpath.GetString() + "/pkg/modules/migration/testimage.jpg")
exampleFile, err := os.ReadFile("../testimage.jpg")
require.NoError(t, err)
expectedHierarchyOrg := map[string][]*models.ProjectWithTasksAndBuckets{

View File

@ -36,8 +36,6 @@ func TestMain(m *testing.M) {
// Set default config
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()

View File

@ -20,7 +20,6 @@ import (
"os"
"testing"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
@ -34,7 +33,7 @@ func TestVikunjaFileMigrator_Migrate(t *testing.T) {
m := &FileMigrator{}
u := &user.User{ID: 1}
f, err := os.Open(config.ServiceRootpath.GetString() + "/pkg/modules/migration/vikunja-file/export.zip")
f, err := os.Open("export.zip")
if err != nil {
t.Fatalf("Could not open file: %s", err)
}
@ -81,7 +80,7 @@ func TestVikunjaFileMigrator_Migrate(t *testing.T) {
m := &FileMigrator{}
u := &user.User{ID: 1}
f, err := os.Open(config.ServiceRootpath.GetString() + "/pkg/modules/migration/vikunja-file/export_pre_0.21.0.zip")
f, err := os.Open("export_pre_0.21.0.zip")
if err != nil {
t.Fatalf("Could not open file: %s", err)
}

View File

@ -50,8 +50,6 @@ func TestMain(m *testing.M) {
// Set default config
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
SetupTests()

View File

@ -21,7 +21,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
@ -65,8 +64,7 @@ var (
func setupTestEnv() (e *echo.Echo, err error) {
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
config.ServicePublicURL.Set("https://localhost")
// Initialize logger for tests
log.InitLogger()