mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-03-20 06:21:06 +02:00
added tests.NewTestAppWithConfig helper
This commit is contained in:
parent
e8f49c31e4
commit
6a4e04533c
40
tests/app.go
40
tests/app.go
@ -70,27 +70,43 @@ func (t *TestApp) registerEventCall(name string) {
|
|||||||
|
|
||||||
// NewTestApp creates and initializes a test application instance.
|
// NewTestApp creates and initializes a test application instance.
|
||||||
//
|
//
|
||||||
// It is the caller's responsibility to call `app.Cleanup()`
|
// It is the caller's responsibility to call app.Cleanup() when the app is no longer needed.
|
||||||
// when the app is no longer needed.
|
|
||||||
func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
|
func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
|
||||||
var testDataDir string
|
var testDataDir string
|
||||||
if len(optTestDataDir) == 0 || optTestDataDir[0] == "" {
|
if len(optTestDataDir) > 0 {
|
||||||
// fallback to the default test data directory
|
|
||||||
_, currentFile, _, _ := runtime.Caller(0)
|
|
||||||
testDataDir = filepath.Join(path.Dir(currentFile), "data")
|
|
||||||
} else {
|
|
||||||
testDataDir = 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
app := core.NewBaseApp(core.BaseAppConfig{
|
// replace with the clone
|
||||||
DataDir: tempDir,
|
config.DataDir = tempDir
|
||||||
EncryptionEnv: "pb_test_env",
|
|
||||||
})
|
app := core.NewBaseApp(config)
|
||||||
|
|
||||||
// load data dir and db connections
|
// load data dir and db connections
|
||||||
if err := app.Bootstrap(); err != nil {
|
if err := app.Bootstrap(); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user