From 6a4e04533c68dc806f007d50fd211b7dd2516a5a Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 6 Dec 2024 21:38:57 +0200 Subject: [PATCH] added tests.NewTestAppWithConfig helper --- tests/app.go | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/app.go b/tests/app.go index 90940062..d951753c 100644 --- a/tests/app.go +++ b/tests/app.go @@ -70,27 +70,43 @@ func (t *TestApp) registerEventCall(name string) { // NewTestApp creates and initializes a test application instance. // -// It is the caller's responsibility to call `app.Cleanup()` -// when the app is no longer needed. +// It is the caller's responsibility to call app.Cleanup() when the app is no longer needed. func NewTestApp(optTestDataDir ...string) (*TestApp, error) { var testDataDir string - if len(optTestDataDir) == 0 || optTestDataDir[0] == "" { - // fallback to the default test data directory - _, currentFile, _, _ := runtime.Caller(0) - testDataDir = filepath.Join(path.Dir(currentFile), "data") - } else { + if len(optTestDataDir) > 0 { testDataDir = optTestDataDir[0] } - tempDir, err := TempDirClone(testDataDir) + return NewTestAppWithConfig(core.BaseAppConfig{ + DataDir: testDataDir, + EncryptionEnv: "pb_test_env", + }) +} + +// NewTestAppWithConfig creates and initializes a test application instance +// from the provided config. +// +// If config.DataDir is not set it fallbacks to the default internal test data directory. +// +// config.DataDir is cloned for each new test application instance. +// +// It is the caller's responsibility to call app.Cleanup() when the app is no longer needed. +func NewTestAppWithConfig(config core.BaseAppConfig) (*TestApp, error) { + if config.DataDir == "" { + // fallback to the default test data directory + _, currentFile, _, _ := runtime.Caller(0) + config.DataDir = filepath.Join(path.Dir(currentFile), "data") + } + + tempDir, err := TempDirClone(config.DataDir) if err != nil { return nil, err } - app := core.NewBaseApp(core.BaseAppConfig{ - DataDir: tempDir, - EncryptionEnv: "pb_test_env", - }) + // replace with the clone + config.DataDir = tempDir + + app := core.NewBaseApp(config) // load data dir and db connections if err := app.Bootstrap(); err != nil {