mirror of
https://github.com/navidrome/navidrome.git
synced 2025-12-23 23:18:05 -05:00
test: prevent environment variables from overriding config file values in tests
Added a loadEnvVars parameter to InitConfig to control whether environment variables should be loaded via viper.AutomaticEnv(). In tests, environment variables (like ND_MUSICFOLDER) were overriding values from config test files, causing tests to fail when these variables were set in the developer's environment. Now tests can pass loadEnvVars=false to isolate from the environment while production code continues to use loadEnvVars=true. Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -346,7 +346,7 @@ func startPluginManager(ctx context.Context) func() error {
|
|||||||
// TODO: Implement some struct tags to map flags to viper
|
// TODO: Implement some struct tags to map flags to viper
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(func() {
|
cobra.OnInitialize(func() {
|
||||||
conf.InitConfig(cfgFile)
|
conf.InitConfig(cfgFile, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "configfile", "c", "", `config file (default "./navidrome.toml")`)
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "configfile", "c", "", `config file (default "./navidrome.toml")`)
|
||||||
|
|||||||
@@ -617,7 +617,7 @@ func init() {
|
|||||||
setViperDefaults()
|
setViperDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(cfgFile string) {
|
func InitConfig(cfgFile string, loadEnvVars bool) {
|
||||||
codecRegistry := viper.NewCodecRegistry()
|
codecRegistry := viper.NewCodecRegistry()
|
||||||
_ = codecRegistry.RegisterCodec("ini", ini.Codec{
|
_ = codecRegistry.RegisterCodec("ini", ini.Codec{
|
||||||
LoadOptions: ini.LoadOptions{
|
LoadOptions: ini.LoadOptions{
|
||||||
@@ -638,10 +638,12 @@ func InitConfig(cfgFile string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = viper.BindEnv("port")
|
_ = viper.BindEnv("port")
|
||||||
viper.SetEnvPrefix("ND")
|
if loadEnvVars {
|
||||||
replacer := strings.NewReplacer(".", "_")
|
viper.SetEnvPrefix("ND")
|
||||||
viper.SetEnvKeyReplacer(replacer)
|
replacer := strings.NewReplacer(".", "_")
|
||||||
viper.AutomaticEnv()
|
viper.SetEnvKeyReplacer(replacer)
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
}
|
||||||
|
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if viper.ConfigFileUsed() != "" && err != nil {
|
if viper.ConfigFileUsed() != "" && err != nil {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ var _ = Describe("Configuration", func() {
|
|||||||
filename := filepath.Join("testdata", "cfg."+format)
|
filename := filepath.Join("testdata", "cfg."+format)
|
||||||
|
|
||||||
// Initialize config with the test file
|
// Initialize config with the test file
|
||||||
conf.InitConfig(filename)
|
conf.InitConfig(filename, false)
|
||||||
// Load the configuration (with noConfigDump=true)
|
// Load the configuration (with noConfigDump=true)
|
||||||
conf.Load(true)
|
conf.Load(true)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user