package configloader_test import ( "os" "path/filepath" "testing" "git.eugenetriguba.com/bolt/cli/internal/bolttest" "git.eugenetriguba.com/bolt/cli/internal/configloader" "git.eugenetriguba.com/checkmate/assert" "git.eugenetriguba.com/checkmate/check" ) func TestNewConfigDefaults(t *testing.T) { bolttest.ChangeCwd(t, os.TempDir()) cfg, err := configloader.NewConfig() assert.Nil(t, err) check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") check.Equal(t, cfg.Connection.MigrationsTable, "bolt_migrations") } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { bolttest.UnsetEnv(t, "BOLT_DB_DSN") bolttest.UnsetEnv(t, "BOLT_DB_DRIVER") bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE") bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") expectedCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "myfancymigrations", }, Connection: configloader.ConnectionConfig{ DSN: "postgresql://testuser:testpassword@testhost:1234/testdb", Driver: "postgresql", MigrationsTable: "test_table", }, } 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) } func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { fileCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "cfgmigrations", }, Connection: configloader.ConnectionConfig{ DSN: "mysql://testuser:testpassword@testhost:1234/testdb", Driver: "mysql", MigrationsTable: "test_table", }, } bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml") envCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "envmigrations", }, Connection: configloader.ConnectionConfig{ DSN: "postgresql://envtestuser:envtestpassword@envtesthost:4321/envtestdb", Driver: "postgresql", MigrationsTable: "different_table", }, } t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath) t.Setenv("BOLT_DB_DSN", envCfg.Connection.DSN) t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver) t.Setenv("BOLT_DB_MIGRATIONS_TABLE", envCfg.Connection.MigrationsTable) cfg, err := configloader.NewConfig() assert.Nil(t, err) assert.DeepEqual(t, *cfg, envCfg) } func TestNewConfigSearchesParentDirectories(t *testing.T) { bolttest.UnsetEnv(t, "BOLT_DB_DSN") bolttest.UnsetEnv(t, "BOLT_DB_DRIVER") bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE") bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") expectedCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "differentmigrationsdir", }, Connection: configloader.ConnectionConfig{ MigrationsTable: "migration_table", }, } 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) }