aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/bolttest/db.go10
-rw-r--r--internal/bolttest/env.go19
-rw-r--r--internal/configloader/configloader_test.go42
-rw-r--r--internal/configloader/configloader_unix_test.go31
-rw-r--r--internal/configloader/configloader_windows_test.go34
-rw-r--r--internal/services/migration_test.go6
-rw-r--r--internal/storage/db_test.go3
7 files changed, 70 insertions, 75 deletions
diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go
index bde519b..6ebbabf 100644
--- a/internal/bolttest/db.go
+++ b/internal/bolttest/db.go
@@ -12,16 +12,16 @@ import (
)
func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig {
- port, err := strconv.Atoi(os.Getenv("DB_PORT"))
+ port, err := strconv.Atoi(os.Getenv("BOLT_DB_CONN_PORT"))
assert.Nil(t, err)
return &configloader.ConnectionConfig{
Driver: driver,
- DBName: os.Getenv("DB_NAME"),
- Host: os.Getenv("DB_HOST"),
+ DBName: os.Getenv("BOLT_DB_CONN_DBNAME"),
+ Host: os.Getenv("BOLT_DB_CONN_HOST"),
Port: port,
- User: os.Getenv("DB_USERNAME"),
- Password: os.Getenv("DB_PASSWORD"),
+ User: os.Getenv("BOLT_DB_CONN_USER"),
+ Password: os.Getenv("BOLT_DB_CONN_PASSWORD"),
}
}
diff --git a/internal/bolttest/env.go b/internal/bolttest/env.go
new file mode 100644
index 0000000..8f0ad63
--- /dev/null
+++ b/internal/bolttest/env.go
@@ -0,0 +1,19 @@
+package bolttest
+
+import (
+ "os"
+ "testing"
+
+ "github.com/eugenetriguba/checkmate/assert"
+)
+
+func UnsetEnv(t *testing.T, key string) {
+ originalVal, isSet := os.LookupEnv(key)
+ if isSet {
+ t.Cleanup(func() {
+ err := os.Setenv(key, originalVal)
+ assert.Nil(t, err)
+ })
+ os.Unsetenv(key)
+ }
+}
diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go
index 0ad6930..6aa3b45 100644
--- a/internal/configloader/configloader_test.go
+++ b/internal/configloader/configloader_test.go
@@ -3,6 +3,7 @@ package configloader_test
import (
"fmt"
"os"
+ "path/filepath"
"testing"
"github.com/eugenetriguba/bolt/internal/bolttest"
@@ -35,6 +36,14 @@ func TestNewConfigWithInvalidVersionStyle(t *testing.T) {
}
func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_HOST")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_PORT")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_USER")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_PASSWORD")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_DBNAME")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_DRIVER")
+ bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH")
+ bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE")
expectedCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "myfancymigrations",
@@ -49,9 +58,12 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
Driver: "postgres",
},
}
- bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml")
+ tmpdir := t.TempDir()
+ bolttest.ChangeCwd(t, tmpdir)
+ bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml"))
cfg, err := configloader.NewConfig()
+
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, expectedCfg)
}
@@ -100,3 +112,31 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, envCfg)
}
+
+func TestNewConfigSearchesParentDirectories(t *testing.T) {
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_HOST")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_PORT")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_USER")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_PASSWORD")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_DBNAME")
+ bolttest.UnsetEnv(t, "BOLT_DB_CONN_DRIVER")
+ bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH")
+ bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE")
+ expectedCfg := configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ DirectoryPath: "differentmigrationsdir",
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ }
+ tmpdir := t.TempDir()
+ bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml"))
+ nestedTmpDir := filepath.Join(tmpdir, "nested-dir", "nested-x2-dir")
+ err := os.MkdirAll(nestedTmpDir, 0755)
+ assert.Nil(t, err)
+ bolttest.ChangeCwd(t, nestedTmpDir)
+
+ cfg, err := configloader.NewConfig()
+ assert.Nil(t, err)
+
+ assert.DeepEqual(t, *cfg, expectedCfg)
+}
diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go
deleted file mode 100644
index 8043bdb..0000000
--- a/internal/configloader/configloader_unix_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-//go:build unix
-
-package configloader_test
-
-import (
- "os"
- "testing"
-
- "github.com/eugenetriguba/bolt/internal/bolttest"
- "github.com/eugenetriguba/bolt/internal/configloader"
- "github.com/eugenetriguba/checkmate/assert"
-)
-
-func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) {
- expectedCfg := configloader.Config{
- Migrations: configloader.MigrationsConfig{
- DirectoryPath: "differentmigrationsdir",
- VersionStyle: configloader.VersionStyleSequential,
- },
- }
- bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml")
-
- homedir, err := os.UserHomeDir()
- assert.Nil(t, err)
- bolttest.ChangeCwd(t, homedir)
-
- cfg, err := configloader.NewConfig()
- assert.Nil(t, err)
-
- assert.DeepEqual(t, *cfg, expectedCfg)
-}
diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go
deleted file mode 100644
index 1ba6c51..0000000
--- a/internal/configloader/configloader_windows_test.go
+++ /dev/null
@@ -1,34 +0,0 @@
-//go:build windows
-
-package configloader_test
-
-import (
- "os"
- "path/filepath"
- "testing"
-
- "github.com/eugenetriguba/bolt/internal/bolttest"
- "github.com/eugenetriguba/bolt/internal/configloader"
- "github.com/eugenetriguba/checkmate/assert"
-)
-
-func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) {
- expectedCfg := configloader.Config{
- Migrations: configloader.MigrationsConfig{
- DirectoryPath: "differentmigrationsdir",
- VersionStyle: configloader.VersionStyleSequential,
- },
- }
- configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml")
- bolttest.CreateConfigFile(t, &expectedCfg, configPath)
- assert.Nil(t, err)
-
- homedir, err := os.UserHomeDir()
- assert.Nil(t, err)
- bolttest.ChangeCwd(t, homedir)
-
- cfg, err := configloader.NewConfig()
- assert.Nil(t, err)
-
- assert.DeepEqual(t, *cfg, expectedCfg)
-}
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go
index 0c30c76..b2b480b 100644
--- a/internal/services/migration_test.go
+++ b/internal/services/migration_test.go
@@ -373,12 +373,12 @@ func TestCreateMigration_VersionStyleTimestamp(t *testing.T) {
bolttest.NullOutputter{},
)
- beforeCreateTs := time.Now().Add(-time.Second)
+ beforeCreateTs := time.Now().Add(30 * -time.Second)
migration, err := svc.CreateMigration("new migration")
- afterCreateTs := time.Now().Add(time.Second)
+ afterCreateTs := time.Now().Add(30 * time.Second)
assert.Nil(t, err)
- version, err := time.Parse("20060102150405", migration.Version)
+ version, err := time.ParseInLocation("20060102150405", migration.Version, time.Local)
assert.Nil(t, err)
check.True(t, beforeCreateTs.Before(version))
check.True(t, afterCreateTs.After(version))
diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go
index eaf9dfe..9f87d4c 100644
--- a/internal/storage/db_test.go
+++ b/internal/storage/db_test.go
@@ -12,8 +12,9 @@ import (
func TestDBConnect_Success(t *testing.T) {
cfg := bolttest.NewTestConnectionConfig(t, "postgres")
+ connParams := storage.DBConnectionString(cfg)
- conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg))
+ conn, err := storage.DBConnect(cfg.Driver, connParams)
assert.Nil(t, err)
defer conn.Close()