1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-21 13:35:49 +02:00
pocketbase/pocketbase_test.go

211 lines
5.0 KiB
Go
Raw Permalink Normal View History

2022-07-06 23:19:05 +02:00
package pocketbase
import (
"os"
"path/filepath"
2022-07-06 23:19:05 +02:00
"testing"
"github.com/spf13/cobra"
2022-07-06 23:19:05 +02:00
)
func TestNew(t *testing.T) {
// copy os.Args
originalArgs := make([]string, len(os.Args))
copy(originalArgs, os.Args)
defer func() {
// restore os.Args
os.Args = originalArgs
}()
// change os.Args
os.Args = os.Args[:1]
2022-07-06 23:19:05 +02:00
os.Args = append(
os.Args,
"--dir=test_dir",
2022-07-06 23:19:05 +02:00
"--encryptionEnv=test_encryption_env",
"--debug=true",
)
app := New()
if app == nil {
t.Fatal("Expected initialized PocketBase instance, got nil")
}
if app.RootCmd == nil {
t.Fatal("Expected RootCmd to be initialized, got nil")
}
if app.appWrapper == nil {
t.Fatal("Expected appWrapper to be initialized, got nil")
}
if app.DataDir() != "test_dir" {
t.Fatalf("Expected app.DataDir() %q, got %q", "test_dir", app.DataDir())
2022-07-06 23:19:05 +02:00
}
if app.EncryptionEnv() != "test_encryption_env" {
2023-12-04 18:09:29 +02:00
t.Fatalf("Expected app.EncryptionEnv() test_encryption_env, got %q", app.EncryptionEnv())
2022-07-06 23:19:05 +02:00
}
}
func TestNewWithConfig(t *testing.T) {
app := NewWithConfig(Config{
DefaultDataDir: "test_dir",
DefaultEncryptionEnv: "test_encryption_env",
HideStartBanner: true,
})
2022-07-06 23:19:05 +02:00
if app == nil {
t.Fatal("Expected initialized PocketBase instance, got nil")
2022-07-06 23:19:05 +02:00
}
if app.RootCmd == nil {
t.Fatal("Expected RootCmd to be initialized, got nil")
2022-07-06 23:19:05 +02:00
}
if app.appWrapper == nil {
t.Fatal("Expected appWrapper to be initialized, got nil")
}
if app.hideStartBanner != true {
t.Fatal("Expected app.hideStartBanner to be true, got false")
}
if app.DataDir() != "test_dir" {
t.Fatalf("Expected app.DataDir() %q, got %q", "test_dir", app.DataDir())
}
2022-07-06 23:19:05 +02:00
if app.EncryptionEnv() != "test_encryption_env" {
2023-12-04 18:09:29 +02:00
t.Fatalf("Expected app.EncryptionEnv() %q, got %q", "test_encryption_env", app.EncryptionEnv())
}
2022-07-06 23:19:05 +02:00
}
func TestNewWithConfigAndFlags(t *testing.T) {
// copy os.Args
originalArgs := make([]string, len(os.Args))
copy(originalArgs, os.Args)
defer func() {
// restore os.Args
os.Args = originalArgs
}()
2022-07-06 23:19:05 +02:00
// change os.Args
os.Args = os.Args[:1]
os.Args = append(
os.Args,
"--dir=test_dir_flag",
"--encryptionEnv=test_encryption_env_flag",
"--debug=false",
)
app := NewWithConfig(Config{
DefaultDataDir: "test_dir",
DefaultEncryptionEnv: "test_encryption_env",
HideStartBanner: true,
})
2022-07-06 23:19:05 +02:00
if app == nil {
t.Fatal("Expected initialized PocketBase instance, got nil")
2022-07-06 23:19:05 +02:00
}
if app.RootCmd == nil {
t.Fatal("Expected RootCmd to be initialized, got nil")
}
if app.appWrapper == nil {
t.Fatal("Expected appWrapper to be initialized, got nil")
}
if app.hideStartBanner != true {
t.Fatal("Expected app.hideStartBanner to be true, got false")
}
if app.DataDir() != "test_dir_flag" {
t.Fatalf("Expected app.DataDir() %q, got %q", "test_dir_flag", app.DataDir())
}
2022-07-06 23:19:05 +02:00
if app.EncryptionEnv() != "test_encryption_env_flag" {
2023-12-04 18:09:29 +02:00
t.Fatalf("Expected app.EncryptionEnv() %q, got %q", "test_encryption_env_flag", app.EncryptionEnv())
2022-07-06 23:19:05 +02:00
}
}
func TestSkipBootstrap(t *testing.T) {
// copy os.Args
originalArgs := make([]string, len(os.Args))
copy(originalArgs, os.Args)
defer func() {
// restore os.Args
os.Args = originalArgs
}()
tempDir := filepath.Join(os.TempDir(), "temp_pb_data")
defer os.RemoveAll(tempDir)
// already bootstrapped
app0 := NewWithConfig(Config{DefaultDataDir: tempDir})
app0.Bootstrap()
if v := app0.skipBootstrap(); !v {
t.Fatal("[bootstrapped] Expected true, got false")
}
// unknown command
os.Args = os.Args[:1]
os.Args = append(os.Args, "demo")
app1 := NewWithConfig(Config{DefaultDataDir: tempDir})
app1.RootCmd.AddCommand(&cobra.Command{Use: "test"})
if v := app1.skipBootstrap(); !v {
t.Fatal("[unknown] Expected true, got false")
}
// default flags
flagScenarios := []struct {
name string
short string
}{
{"help", "h"},
{"version", "v"},
}
for _, s := range flagScenarios {
// base flag
os.Args = os.Args[:1]
os.Args = append(os.Args, "--"+s.name)
app1 := NewWithConfig(Config{DefaultDataDir: tempDir})
if v := app1.skipBootstrap(); !v {
t.Fatalf("[--%s] Expected true, got false", s.name)
}
// short flag
os.Args = os.Args[:1]
os.Args = append(os.Args, "-"+s.short)
app2 := NewWithConfig(Config{DefaultDataDir: tempDir})
if v := app2.skipBootstrap(); !v {
t.Fatalf("[-%s] Expected true, got false", s.short)
}
customCmd := &cobra.Command{Use: "custom"}
customCmd.PersistentFlags().BoolP(s.name, s.short, false, "")
// base flag in custom command
os.Args = os.Args[:1]
os.Args = append(os.Args, "custom")
os.Args = append(os.Args, "--"+s.name)
app3 := NewWithConfig(Config{DefaultDataDir: tempDir})
app3.RootCmd.AddCommand(customCmd)
if v := app3.skipBootstrap(); v {
t.Fatalf("[--%s custom] Expected false, got true", s.name)
}
// short flag in custom command
os.Args = os.Args[:1]
os.Args = append(os.Args, "custom")
os.Args = append(os.Args, "-"+s.short)
app4 := NewWithConfig(Config{DefaultDataDir: tempDir})
app4.RootCmd.AddCommand(customCmd)
if v := app4.skipBootstrap(); v {
t.Fatalf("[-%s custom] Expected false, got true", s.short)
}
}
}