2022-07-06 23:19:05 +02:00
|
|
|
package pocketbase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-12-15 23:20:23 +02:00
|
|
|
"path/filepath"
|
2022-07-06 23:19:05 +02:00
|
|
|
"testing"
|
2022-12-15 23:20:23 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-07-06 23:19:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2022-09-10 21:53:17 +02:00
|
|
|
// copy os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
originalArgs := make([]string, len(os.Args))
|
2022-09-10 21:53:17 +02:00
|
|
|
copy(originalArgs, os.Args)
|
|
|
|
defer func() {
|
|
|
|
// restore os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
os.Args = originalArgs
|
2022-09-10 21:53:17 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
// change os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
os.Args = os.Args[:1]
|
2022-07-06 23:19:05 +02:00
|
|
|
os.Args = append(
|
|
|
|
os.Args,
|
2022-09-10 21:53:17 +02:00
|
|
|
"--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")
|
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
func TestNewWithConfig(t *testing.T) {
|
2023-06-08 16:59:08 +02:00
|
|
|
app := NewWithConfig(Config{
|
2022-09-10 21:53:17 +02:00
|
|
|
DefaultDataDir: "test_dir",
|
|
|
|
DefaultEncryptionEnv: "test_encryption_env",
|
|
|
|
HideStartBanner: true,
|
|
|
|
})
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
if app == nil {
|
|
|
|
t.Fatal("Expected initialized PocketBase instance, got nil")
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
if app.RootCmd == nil {
|
|
|
|
t.Fatal("Expected RootCmd to be initialized, got nil")
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +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
|
|
|
|
2022-09-10 21:53:17 +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-09-10 21:53:17 +02:00
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
func TestNewWithConfigAndFlags(t *testing.T) {
|
|
|
|
// copy os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
originalArgs := make([]string, len(os.Args))
|
2022-09-10 21:53:17 +02:00
|
|
|
copy(originalArgs, os.Args)
|
|
|
|
defer func() {
|
|
|
|
// restore os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
os.Args = originalArgs
|
2022-09-10 21:53:17 +02:00
|
|
|
}()
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
// change os.Args
|
2022-12-15 23:20:23 +02:00
|
|
|
os.Args = os.Args[:1]
|
2022-09-10 21:53:17 +02:00
|
|
|
os.Args = append(
|
|
|
|
os.Args,
|
|
|
|
"--dir=test_dir_flag",
|
|
|
|
"--encryptionEnv=test_encryption_env_flag",
|
|
|
|
"--debug=false",
|
|
|
|
)
|
|
|
|
|
2023-06-08 16:59:08 +02:00
|
|
|
app := NewWithConfig(Config{
|
2022-09-10 21:53:17 +02:00
|
|
|
DefaultDataDir: "test_dir",
|
|
|
|
DefaultEncryptionEnv: "test_encryption_env",
|
|
|
|
HideStartBanner: true,
|
|
|
|
})
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-09-10 21:53:17 +02:00
|
|
|
if app == nil {
|
|
|
|
t.Fatal("Expected initialized PocketBase instance, got nil")
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 21:53:17 +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
|
|
|
|
2022-09-10 21:53:17 +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
|
|
|
}
|
|
|
|
}
|
2022-12-15 23:20:23 +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
|
2023-06-08 16:59:08 +02:00
|
|
|
app0 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
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")
|
2023-06-08 16:59:08 +02:00
|
|
|
app1 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
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)
|
2023-06-08 16:59:08 +02:00
|
|
|
app1 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
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)
|
2023-06-08 16:59:08 +02:00
|
|
|
app2 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
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)
|
2023-06-08 16:59:08 +02:00
|
|
|
app3 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
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)
|
2023-06-08 16:59:08 +02:00
|
|
|
app4 := NewWithConfig(Config{DefaultDataDir: tempDir})
|
2022-12-15 23:20:23 +02:00
|
|
|
app4.RootCmd.AddCommand(customCmd)
|
|
|
|
if v := app4.skipBootstrap(); v {
|
|
|
|
t.Fatalf("[-%s custom] Expected false, got true", s.short)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|