printLoadedConf modified to only print explicitly set config values (via env or config file)
This commit is contained in:
1 parent
d6688a6754
commit
803a8929e6
3 files changed
+122
-7
No files matched your search
@@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// initDir makes a temp working directory holding a .env file (if env is not
|
||||
@@ -167,3 +169,89 @@ func TestInitEnvOverride(t *testing.T) {
|
||||
t.Errorf("FTSMock.Interval = %v; want 45s from the environment", conf.Sources.FTSMock.Interval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvVarName(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"logging.level": "JOBSEARCH_LOGGING_LEVEL",
|
||||
"sources.fts_mock.interval": "JOBSEARCH_SOURCES_FTS_MOCK_INTERVAL",
|
||||
"server.websocket.connection_limit.enabled": "JOBSEARCH_SERVER_WEBSOCKET_CONNECTION_LIMIT_ENABLED",
|
||||
}
|
||||
for key, want := range tests {
|
||||
if got := envVarName(key); got != want {
|
||||
t.Errorf("envVarName(%q) = %q; want %q", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// explicitConfigYAML sets a top-level and a nested key in a config file.
|
||||
const explicitConfigYAML = `logging:
|
||||
level: info
|
||||
server:
|
||||
websocket:
|
||||
connection_limit:
|
||||
enabled: true
|
||||
`
|
||||
|
||||
// loadExplicit sets up Viper as Init does, with the defaults, the config
|
||||
// file explicitConfigYAML and the JOBSEARCH_* environment variables.
|
||||
func loadExplicit(t *testing.T) {
|
||||
t.Helper()
|
||||
resetViper(t)
|
||||
LoadDefaultConfig()
|
||||
viper.SetEnvPrefix(EnvPrefix)
|
||||
viper.SetEnvKeyReplacer(envKeyReplacer)
|
||||
viper.AutomaticEnv()
|
||||
viper.SetConfigType("yaml")
|
||||
if err := viper.ReadConfig(strings.NewReader(explicitConfigYAML)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// A key is explicitly set by the config file or a non-empty environment
|
||||
// variable, not by its default or viper.Set.
|
||||
func TestIsExplicitlySet(t *testing.T) {
|
||||
t.Setenv("JOBSEARCH_DATABASE_FILE", "/tmp/js.db")
|
||||
t.Setenv("JOBSEARCH_LOGGING_FILE", "")
|
||||
loadExplicit(t)
|
||||
viper.Set("sources.sleeper.enabled", true)
|
||||
|
||||
tests := map[string]bool{
|
||||
"logging.level": true, // config file
|
||||
"server.websocket.connection_limit.enabled": true, // nested, config file
|
||||
"database.file": true, // environment
|
||||
"logging.file": false, // empty environment variable
|
||||
"sources.sleeper.interval": false, // default only
|
||||
"sources.sleeper.enabled": false, // viper.Set
|
||||
"server.websocket.connection_limit.burst": false, // default, beside a set key
|
||||
}
|
||||
for key, want := range tests {
|
||||
if got := isExplicitlySet(key); got != want {
|
||||
t.Errorf("isExplicitlySet(%q) = %v; want %v", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// printLoadedConfig logs only the keys set by the config file or the
|
||||
// environment.
|
||||
func TestPrintLoadedConfig(t *testing.T) {
|
||||
t.Setenv("JOBSEARCH_DATABASE_FILE", "/tmp/js.db")
|
||||
loadExplicit(t)
|
||||
logs := captureLogs(t, slog.LevelDebug)
|
||||
printLoadedConfig()
|
||||
|
||||
out := logs.String()
|
||||
for _, want := range []string{
|
||||
`logging.level = \"info\" <string>`,
|
||||
`server.websocket.connection_limit.enabled = true <bool>`,
|
||||
`database.file = \"/tmp/js.db\" <string>`,
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("missing %q in:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
for _, unwanted := range []string{"sources.", "logging.file", "connection_limit.burst"} {
|
||||
if strings.Contains(out, unwanted) {
|
||||
t.Errorf("logged default key %q:\n%s", unwanted, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user