All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// ---- ConfigDir ----
|
|
|
|
func TestConfigDirDefault(t *testing.T) {
|
|
os.Unsetenv("CONFIG_PATH")
|
|
assert.Equal(t, ".", ConfigDir())
|
|
}
|
|
|
|
func TestConfigDirFromEnv(t *testing.T) {
|
|
os.Setenv("CONFIG_PATH", "/etc/app/config")
|
|
defer os.Unsetenv("CONFIG_PATH")
|
|
assert.Equal(t, "/etc/app/config", ConfigDir())
|
|
}
|
|
|
|
// ---- TZ ----
|
|
|
|
func TestTZDefault(t *testing.T) {
|
|
os.Unsetenv("TZ")
|
|
assert.Equal(t, "Asia/Shanghai", TZ())
|
|
}
|
|
|
|
func TestTZFromEnv(t *testing.T) {
|
|
os.Setenv("TZ", "UTC")
|
|
defer os.Unsetenv("TZ")
|
|
assert.Equal(t, "UTC", TZ())
|
|
}
|
|
|
|
// ---- Init ----
|
|
|
|
func TestInitNoConfigFile(t *testing.T) {
|
|
viper.Reset()
|
|
defer viper.Reset()
|
|
|
|
// Point to a directory that has no config.yaml — Init should log a
|
|
// warning (ConfigFileNotFoundError) but must not call log.Fatalf.
|
|
os.Setenv("CONFIG_PATH", t.TempDir())
|
|
defer os.Unsetenv("CONFIG_PATH")
|
|
|
|
assert.NotPanics(t, func() { Init() })
|
|
}
|
|
|
|
func TestInitWithConfigFile(t *testing.T) {
|
|
viper.Reset()
|
|
defer viper.Reset()
|
|
|
|
dir := t.TempDir()
|
|
// Write a minimal valid config file
|
|
yaml := []byte("server:\n application: test-app\n address: :9090\n")
|
|
if err := os.WriteFile(dir+"/config.yaml", yaml, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
os.Setenv("CONFIG_PATH", dir)
|
|
defer os.Unsetenv("CONFIG_PATH")
|
|
|
|
assert.NotPanics(t, func() { Init() })
|
|
assert.Equal(t, "test-app", viper.GetString("server.application"))
|
|
assert.Equal(t, ":9090", viper.GetString("server.address"))
|
|
}
|