2014-04-04 21:21:59 +00:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-06-28 09:55:33 +00:00
|
|
|
"encoding/json"
|
2016-10-08 08:00:18 +00:00
|
|
|
"io"
|
2014-09-27 21:03:00 +00:00
|
|
|
"os"
|
2018-01-03 09:37:18 +00:00
|
|
|
"os/exec"
|
2015-08-02 00:37:27 +00:00
|
|
|
"path"
|
2019-03-16 11:10:32 +00:00
|
|
|
"path/filepath"
|
2015-08-02 00:37:27 +00:00
|
|
|
"reflect"
|
2018-01-03 09:37:18 +00:00
|
|
|
"runtime"
|
2015-03-06 19:21:17 +00:00
|
|
|
"strings"
|
2018-01-03 09:37:18 +00:00
|
|
|
"sync"
|
2014-04-04 21:21:59 +00:00
|
|
|
"testing"
|
2014-09-27 21:00:51 +00:00
|
|
|
"time"
|
2014-04-04 21:21:59 +00:00
|
|
|
|
2018-01-03 09:37:18 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2023-12-19 21:15:13 +00:00
|
|
|
"github.com/go-viper/mapstructure/v2"
|
2024-06-04 11:51:18 +00:00
|
|
|
"github.com/sagikazarmark/locafero"
|
2017-12-07 04:26:31 +00:00
|
|
|
"github.com/spf13/afero"
|
2016-10-10 11:40:38 +00:00
|
|
|
"github.com/spf13/cast"
|
2014-10-09 19:48:55 +00:00
|
|
|
"github.com/spf13/pflag"
|
2014-04-04 21:21:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-03 09:37:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper/internal/testutil"
|
2014-04-04 21:21:59 +00:00
|
|
|
)
|
|
|
|
|
2022-01-01 16:02:57 +00:00
|
|
|
// var yamlExample = []byte(`Hacker: true
|
|
|
|
// name: steve
|
|
|
|
// hobbies:
|
|
|
|
// - skateboarding
|
|
|
|
// - snowboarding
|
|
|
|
// - go
|
|
|
|
// clothing:
|
|
|
|
// jacket: leather
|
|
|
|
// trousers: denim
|
|
|
|
// pants:
|
|
|
|
// size: large
|
|
|
|
// age: 35
|
|
|
|
// eyes : brown
|
|
|
|
// beard: true
|
|
|
|
// `)
|
2014-04-04 21:21:59 +00:00
|
|
|
|
2015-12-30 06:11:39 +00:00
|
|
|
var yamlExampleWithExtras = []byte(`Existing: true
|
|
|
|
Bogus: true
|
|
|
|
`)
|
|
|
|
|
|
|
|
type testUnmarshalExtra struct {
|
|
|
|
Existing bool
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
var tomlExample = []byte(`
|
|
|
|
title = "TOML Example"
|
|
|
|
|
|
|
|
[owner]
|
|
|
|
organization = "MongoDB"
|
2014-04-05 05:19:39 +00:00
|
|
|
Bio = "MongoDB Chief Developer Advocate & Hacker at Large"
|
2014-04-04 21:21:59 +00:00
|
|
|
dob = 1979-05-27T07:32:00Z # First class dates? Why not?`)
|
|
|
|
|
2019-06-11 20:47:43 +00:00
|
|
|
var dotenvExample = []byte(`
|
|
|
|
TITLE_DOTENV="DotEnv Example"
|
|
|
|
TYPE_DOTENV=donut
|
|
|
|
NAME_DOTENV=Cake`)
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
var jsonExample = []byte(`{
|
|
|
|
"id": "0001",
|
|
|
|
"type": "donut",
|
|
|
|
"name": "Cake",
|
|
|
|
"ppu": 0.55,
|
|
|
|
"batters": {
|
|
|
|
"batter": [
|
|
|
|
{ "type": "Regular" },
|
|
|
|
{ "type": "Chocolate" },
|
|
|
|
{ "type": "Blueberry" },
|
|
|
|
{ "type": "Devil's Food" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
2014-10-27 19:32:46 +00:00
|
|
|
var remoteExample = []byte(`{
|
|
|
|
"id":"0002",
|
|
|
|
"type":"cronut",
|
|
|
|
"newkey":"remote"
|
|
|
|
}`)
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
func initConfigs(v *Viper) {
|
2016-10-08 08:00:18 +00:00
|
|
|
var r io.Reader
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("yaml")
|
2016-10-08 08:00:18 +00:00
|
|
|
r = bytes.NewReader(yamlExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("json")
|
2014-12-22 23:31:11 +00:00
|
|
|
r = bytes.NewReader(jsonExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("toml")
|
2014-12-22 23:31:11 +00:00
|
|
|
r = bytes.NewReader(tomlExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("env")
|
2019-06-11 20:47:43 +00:00
|
|
|
r = bytes.NewReader(dotenvExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
2019-06-11 20:47:43 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("json")
|
2014-12-22 23:31:11 +00:00
|
|
|
remote := bytes.NewReader(remoteExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(remote, v.kvstore)
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
func initConfig(typ, config string, v *Viper) {
|
|
|
|
v.SetConfigType(typ)
|
2016-10-10 11:40:38 +00:00
|
|
|
r := strings.NewReader(config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
if err := v.unmarshalReader(r, v.config); err != nil {
|
2016-10-10 11:40:38 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 14:52:53 +00:00
|
|
|
// initDirs makes directories for testing.
|
2023-12-01 19:51:58 +00:00
|
|
|
func initDirs(t *testing.T) (string, string) {
|
|
|
|
var (
|
|
|
|
testDirs = []string{`a a`, `b`, `C_`}
|
|
|
|
config = `improbable`
|
|
|
|
)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
2019-03-16 11:10:32 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
testDirs = append(testDirs, `d\d`)
|
|
|
|
}
|
|
|
|
|
2023-12-01 19:51:58 +00:00
|
|
|
root := t.TempDir()
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
for _, dir := range testDirs {
|
2023-09-14 10:49:54 +00:00
|
|
|
innerDir := filepath.Join(root, dir)
|
|
|
|
err := os.Mkdir(innerDir, 0o750)
|
|
|
|
require.NoError(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
2023-09-13 14:56:49 +00:00
|
|
|
err = os.WriteFile(
|
2023-09-14 10:49:54 +00:00
|
|
|
filepath.Join(innerDir, config+".toml"),
|
|
|
|
[]byte(`key = "value is `+dir+`"`+"\n"),
|
2021-09-15 19:12:02 +00:00
|
|
|
0o640)
|
2023-09-14 10:49:54 +00:00
|
|
|
require.NoError(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:56:49 +00:00
|
|
|
return root, config
|
2015-08-02 00:37:27 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 14:52:53 +00:00
|
|
|
// stubs for PFlag Values.
|
2014-10-09 19:48:55 +00:00
|
|
|
type stringValue string
|
|
|
|
|
|
|
|
func newStringValue(val string, p *string) *stringValue {
|
|
|
|
*p = val
|
|
|
|
return (*stringValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Set(val string) error {
|
|
|
|
*s = stringValue(val)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Type() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) String() string {
|
2019-07-19 22:07:23 +00:00
|
|
|
return string(*s)
|
2014-10-09 19:48:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:04:49 +00:00
|
|
|
func TestGetConfigFile(t *testing.T) {
|
|
|
|
t.Run("config file set", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
2014-04-04 21:21:59 +00:00
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-16 18:23:50 +00:00
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
2021-09-22 15:13:54 +00:00
|
|
|
v.SetConfigFile(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2021-09-22 15:13:54 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename)
|
2021-09-21 09:04:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("find file", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2021-09-22 15:13:54 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename)
|
2021-09-21 09:04:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("find files only", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/config"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/config/config.yaml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc")
|
|
|
|
v.AddConfigPath("/etc/config")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2021-09-22 15:13:54 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/config/config.yaml"), filename)
|
2021-09-21 09:04:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("precedence", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/home/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/home/viper/config.zml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err = fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.bml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err = fs.Mkdir(testutil.AbsFilePath(t, "/var/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/var/viper/config.yaml"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/home/viper")
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
v.AddConfigPath("/var/viper")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2021-09-22 15:13:54 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/var/viper/config.yaml"), filename)
|
2021-09-21 09:04:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("without extension", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
v.SetConfigName(".dotfilenoext")
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2021-09-22 15:13:54 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext"), filename)
|
2021-09-21 09:04:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2019-11-06 13:54:13 +00:00
|
|
|
|
2021-09-21 09:04:49 +00:00
|
|
|
t.Run("without extension and config type", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
2019-12-06 16:23:59 +00:00
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-06 16:23:59 +00:00
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/.dotfilenoext"))
|
2021-09-21 09:04:49 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-06 16:23:59 +00:00
|
|
|
|
2021-09-21 09:04:49 +00:00
|
|
|
v := New()
|
2019-12-06 16:23:59 +00:00
|
|
|
|
2021-09-21 09:04:49 +00:00
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
v.SetConfigName(".dotfilenoext")
|
2019-12-06 16:23:59 +00:00
|
|
|
|
2021-09-21 09:04:49 +00:00
|
|
|
_, err = v.getConfigFile()
|
|
|
|
// unless config type is set, files without extension
|
|
|
|
// are not considered
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2024-06-04 13:44:19 +00:00
|
|
|
|
2024-06-04 18:56:30 +00:00
|
|
|
t.Run("experimental finder", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := NewWithOptions(ExperimentalFinder())
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), testutil.AbsFilePath(t, filename))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("finder", func(t *testing.T) {
|
2024-06-04 13:44:19 +00:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2024-06-04 18:25:38 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2024-06-04 13:44:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-06-04 18:25:38 +00:00
|
|
|
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2024-06-04 13:44:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
finder := locafero.Finder{
|
2024-06-04 18:25:38 +00:00
|
|
|
Paths: []string{testutil.AbsFilePath(t, "/etc/viper")},
|
2024-06-04 13:44:19 +00:00
|
|
|
Names: locafero.NameWithExtensions("config", SupportedExts...),
|
|
|
|
Type: locafero.FileTypeFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
v := NewWithOptions(WithFinder(finder))
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
|
|
|
|
// These should be ineffective
|
|
|
|
v.AddConfigPath("/etc/something_else")
|
|
|
|
v.SetConfigName("not-config")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
2024-06-04 18:25:38 +00:00
|
|
|
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), testutil.AbsFilePath(t, filename))
|
2024-06-04 13:44:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2019-12-06 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:46:32 +00:00
|
|
|
func TestReadInConfig(t *testing.T) {
|
|
|
|
t.Run("config file set", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-09-15 19:12:02 +00:00
|
|
|
err := fs.Mkdir("/etc/viper", 0o777)
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-30 15:45:20 +00:00
|
|
|
_, err = file.WriteString(`key: value`)
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
2021-09-22 15:13:54 +00:00
|
|
|
v.SetConfigFile(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:46:32 +00:00
|
|
|
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value", v.Get("key"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("find file", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
2021-11-21 13:57:28 +00:00
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 15:13:54 +00:00
|
|
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-30 15:45:20 +00:00
|
|
|
_, err = file.WriteString(`key: value`)
|
2021-09-21 09:46:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value", v.Get("key"))
|
|
|
|
})
|
2024-06-04 11:51:18 +00:00
|
|
|
|
2024-06-04 18:56:30 +00:00
|
|
|
t.Run("find file with experimental finder", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = file.WriteString(`key: value`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
v := NewWithOptions(ExperimentalFinder())
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/etc/viper")
|
|
|
|
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value", v.Get("key"))
|
|
|
|
})
|
|
|
|
|
2024-06-04 11:51:18 +00:00
|
|
|
t.Run("find file using a finder", func(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
|
|
|
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-06-04 13:44:19 +00:00
|
|
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = file.WriteString(`key: value`)
|
2024-06-04 11:51:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-06-04 13:44:19 +00:00
|
|
|
file.Close()
|
|
|
|
|
2024-06-04 11:51:18 +00:00
|
|
|
finder := locafero.Finder{
|
2024-06-04 18:25:38 +00:00
|
|
|
Paths: []string{testutil.AbsFilePath(t, "/etc/viper")},
|
2024-06-04 11:51:18 +00:00
|
|
|
Names: locafero.NameWithExtensions("config", SupportedExts...),
|
|
|
|
Type: locafero.FileTypeFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
v := NewWithOptions(WithFinder(finder))
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
|
|
|
|
// These should be ineffective
|
|
|
|
v.AddConfigPath("/etc/something_else")
|
|
|
|
v.SetConfigName("not-config")
|
|
|
|
|
2024-06-04 13:44:19 +00:00
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value", v.Get("key"))
|
2024-06-04 11:51:18 +00:00
|
|
|
})
|
2021-09-21 09:46:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestDefault(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetDefault("age", 45)
|
|
|
|
assert.Equal(t, 45, v.Get("age"))
|
2015-10-13 22:31:32 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetDefault("clothing.jacket", "slacks")
|
|
|
|
assert.Equal(t, "slacks", v.Get("clothing.jacket"))
|
2015-10-13 22:31:32 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
2015-10-13 22:31:32 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "leather", v.Get("clothing.jacket"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:26:31 +00:00
|
|
|
func TestUnmarshaling(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
2014-04-04 21:21:59 +00:00
|
|
|
r := bytes.NewReader(yamlExample)
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
|
|
|
assert.True(t, v.InConfig("name"))
|
|
|
|
assert.True(t, v.InConfig("clothing.jacket"))
|
|
|
|
assert.False(t, v.InConfig("state"))
|
|
|
|
assert.False(t, v.InConfig("clothing.hat"))
|
|
|
|
assert.Equal(t, "steve", v.Get("name"))
|
|
|
|
assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
|
|
|
|
assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing"))
|
|
|
|
assert.Equal(t, 35, v.Get("age"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:11:39 +00:00
|
|
|
func TestUnmarshalExact(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2015-12-30 06:11:39 +00:00
|
|
|
target := &testUnmarshalExtra{}
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetConfigType("yaml")
|
2015-12-30 06:11:39 +00:00
|
|
|
r := bytes.NewReader(yamlExampleWithExtras)
|
2024-04-07 11:09:02 +00:00
|
|
|
v.ReadConfig(r)
|
|
|
|
err := v.UnmarshalExact(target)
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Error(t, err, "UnmarshalExact should error when populating a struct from a conf that contains unused fields")
|
2015-12-30 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestOverrides(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.Set("age", 40)
|
|
|
|
assert.Equal(t, 40, v.Get("age"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultPost(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
assert.NotEqual(t, "NYC", v.Get("state"))
|
|
|
|
v.SetDefault("state", "NYC")
|
|
|
|
assert.Equal(t, "NYC", v.Get("state"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliases(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.Set("age", 40)
|
|
|
|
v.RegisterAlias("years", "age")
|
|
|
|
assert.Equal(t, 40, v.Get("years"))
|
|
|
|
v.Set("years", 45)
|
|
|
|
assert.Equal(t, 45, v.Get("age"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 11:35:21 +00:00
|
|
|
func TestAliasInConfigFile(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
|
|
|
|
// Read the YAML data into Viper configuration
|
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading YAML data")
|
|
|
|
|
|
|
|
v.RegisterAlias("beard", "hasbeard")
|
|
|
|
assert.Equal(t, true, v.Get("hasbeard"))
|
|
|
|
|
|
|
|
v.Set("hasbeard", false)
|
|
|
|
assert.Equal(t, false, v.Get("beard"))
|
2014-08-05 11:35:21 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 14:40:14 +00:00
|
|
|
func TestYML(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
|
|
|
|
// Read the YAML data into Viper configuration
|
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading YAML data")
|
|
|
|
|
|
|
|
assert.Equal(t, "steve", v.Get("name"))
|
2014-05-08 14:40:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestJSON(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetConfigType("json")
|
|
|
|
// Read the JSON data into Viper configuration
|
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading JSON data")
|
|
|
|
|
|
|
|
assert.Equal(t, "0001", v.Get("id"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTOML(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("toml")
|
|
|
|
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the TOML data into Viper configuration
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(tomlExample)), "Error reading toml data")
|
|
|
|
|
|
|
|
assert.Equal(t, "TOML Example", v.Get("title"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:47:43 +00:00
|
|
|
func TestDotEnv(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("env")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the dotenv data into Viper configuration
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(dotenvExample)), "Error reading env data")
|
|
|
|
|
|
|
|
assert.Equal(t, "DotEnv Example", v.Get("title_dotenv"))
|
2019-06-11 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 19:32:46 +00:00
|
|
|
func TestRemotePrecedence(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the remote data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
|
|
|
|
|
|
|
assert.Equal(t, "0001", v.Get("id"))
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
// update the kvstore with the remoteExample which should overite the key in v.config
|
2014-10-27 19:32:46 +00:00
|
|
|
remote := bytes.NewReader(remoteExample)
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.unmarshalReader(remote, v.kvstore), "Error reading json data in to kvstore")
|
|
|
|
|
|
|
|
assert.Equal(t, "0001", v.Get("id"))
|
|
|
|
assert.NotEqual(t, "cronut", v.Get("type"))
|
|
|
|
assert.Equal(t, "remote", v.Get("newkey"))
|
|
|
|
v.Set("newkey", "newvalue")
|
|
|
|
assert.NotEqual(t, "remote", v.Get("newkey"))
|
|
|
|
assert.Equal(t, "newvalue", v.Get("newkey"))
|
2014-10-27 19:32:46 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 21:03:00 +00:00
|
|
|
func TestEnv(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the JSON data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindEnv("id")
|
|
|
|
v.BindEnv("f", "FOOD", "OLD_FOOD")
|
2014-09-27 21:03:00 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("ID", "13")
|
|
|
|
t.Setenv("FOOD", "apple")
|
|
|
|
t.Setenv("OLD_FOOD", "banana")
|
|
|
|
t.Setenv("NAME", "crunk")
|
2014-09-27 21:03:00 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "13", v.Get("id"))
|
|
|
|
assert.Equal(t, "apple", v.Get("f"))
|
|
|
|
assert.Equal(t, "Cake", v.Get("name"))
|
2014-09-27 21:01:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.AutomaticEnv()
|
2014-09-27 21:01:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "crunk", v.Get("name"))
|
2014-09-27 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 10:08:26 +00:00
|
|
|
func TestMultipleEnv(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the JSON data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
2020-09-10 10:08:26 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindEnv("f", "FOOD", "OLD_FOOD")
|
2020-09-10 10:08:26 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("OLD_FOOD", "banana")
|
2020-09-10 10:08:26 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "banana", v.Get("f"))
|
2020-09-10 10:08:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 21:53:21 +00:00
|
|
|
func TestEmptyEnv(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the JSON data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindEnv("type") // Empty environment variable
|
|
|
|
v.BindEnv("name") // Bound, but not set environment variable
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("TYPE", "")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "donut", v.Get("type"))
|
|
|
|
assert.Equal(t, "Cake", v.Get("name"))
|
2018-11-06 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyEnv_Allowed(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the JSON data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.AllowEmptyEnv(true)
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindEnv("type") // Empty environment variable
|
|
|
|
v.BindEnv("name") // Bound, but not set environment variable
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("TYPE", "")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "", v.Get("type"))
|
|
|
|
assert.Equal(t, "Cake", v.Get("name"))
|
2018-11-06 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
func TestEnvPrefix(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the JSON data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetEnvPrefix("foo") // will be uppercased automatically
|
|
|
|
v.BindEnv("id")
|
|
|
|
v.BindEnv("f", "FOOD") // not using prefix
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("FOO_ID", "13")
|
|
|
|
t.Setenv("FOOD", "apple")
|
|
|
|
t.Setenv("FOO_NAME", "crunk")
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "13", v.Get("id"))
|
|
|
|
assert.Equal(t, "apple", v.Get("f"))
|
|
|
|
assert.Equal(t, "Cake", v.Get("name"))
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.AutomaticEnv()
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "crunk", v.Get("name"))
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 04:32:10 +00:00
|
|
|
func TestAutoEnv(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2015-02-17 04:32:10 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.AutomaticEnv()
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("FOO_BAR", "13")
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "13", v.Get("foo_bar"))
|
2015-02-17 04:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoEnvWithPrefix(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetEnvPrefix("Baz")
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("BAZ_BAR", "13")
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "13", v.Get("bar"))
|
2015-02-17 04:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 10:22:28 +00:00
|
|
|
func TestSetEnvKeyReplacer(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.AutomaticEnv()
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("REFRESH_INTERVAL", "30s")
|
2015-03-06 19:21:17 +00:00
|
|
|
|
|
|
|
replacer := strings.NewReplacer("-", "_")
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetEnvKeyReplacer(replacer)
|
2015-03-06 19:21:17 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
2015-03-06 19:21:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:11:31 +00:00
|
|
|
func TestEnvKeyReplacer(t *testing.T) {
|
|
|
|
v := NewWithOptions(EnvKeyReplacer(strings.NewReplacer("-", "_")))
|
|
|
|
v.AutomaticEnv()
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("REFRESH_INTERVAL", "30s")
|
2019-12-06 13:11:31 +00:00
|
|
|
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
|
|
|
}
|
|
|
|
|
2020-12-24 06:04:37 +00:00
|
|
|
func TestEnvSubConfig(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
2024-06-24 13:10:56 +00:00
|
|
|
// Read the YAML data into Viper configuration v.config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading json data")
|
2020-12-24 06:04:37 +00:00
|
|
|
v.AutomaticEnv()
|
2021-04-04 20:16:36 +00:00
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-12-24 06:04:37 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("CLOTHING_PANTS_SIZE", "small")
|
2020-12-24 06:04:37 +00:00
|
|
|
subv := v.Sub("clothing").Sub("pants")
|
|
|
|
assert.Equal(t, "small", subv.Get("size"))
|
2021-04-04 20:16:36 +00:00
|
|
|
|
|
|
|
// again with EnvPrefix
|
|
|
|
v.SetEnvPrefix("foo") // will be uppercased automatically
|
|
|
|
subWithPrefix := v.Sub("clothing").Sub("pants")
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("FOO_CLOTHING_PANTS_SIZE", "large")
|
2021-04-04 20:16:36 +00:00
|
|
|
assert.Equal(t, "large", subWithPrefix.Get("size"))
|
2020-12-24 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 21:00:51 +00:00
|
|
|
func TestAllKeys(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
initConfigs(v)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2023-09-25 12:43:09 +00:00
|
|
|
ks := []string{
|
2019-03-16 11:10:32 +00:00
|
|
|
"title",
|
|
|
|
"newkey",
|
|
|
|
"owner.organization",
|
|
|
|
"owner.dob",
|
|
|
|
"owner.bio",
|
|
|
|
"name",
|
|
|
|
"beard",
|
|
|
|
"ppu",
|
|
|
|
"batters.batter",
|
|
|
|
"hobbies",
|
|
|
|
"clothing.jacket",
|
|
|
|
"clothing.trousers",
|
|
|
|
"clothing.pants.size",
|
|
|
|
"age",
|
|
|
|
"hacker",
|
|
|
|
"id",
|
|
|
|
"type",
|
|
|
|
"eyes",
|
|
|
|
"title_dotenv",
|
|
|
|
"type_dotenv",
|
|
|
|
"name_dotenv",
|
2019-06-11 20:47:43 +00:00
|
|
|
}
|
2014-09-27 21:00:51 +00:00
|
|
|
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
2023-09-26 14:59:38 +00:00
|
|
|
all := map[string]any{
|
|
|
|
"owner": map[string]any{
|
2019-03-16 11:10:32 +00:00
|
|
|
"organization": "MongoDB",
|
|
|
|
"bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
|
|
|
"dob": dob,
|
|
|
|
},
|
|
|
|
"title": "TOML Example",
|
2024-06-24 13:10:56 +00:00
|
|
|
"ppu": 0.55,
|
|
|
|
"eyes": "brown",
|
2023-09-26 14:59:38 +00:00
|
|
|
"clothing": map[string]any{
|
2019-03-16 11:10:32 +00:00
|
|
|
"trousers": "denim",
|
|
|
|
"jacket": "leather",
|
2023-09-26 14:59:38 +00:00
|
|
|
"pants": map[string]any{"size": "large"},
|
2019-03-16 11:10:32 +00:00
|
|
|
},
|
|
|
|
"id": "0001",
|
2023-09-26 14:59:38 +00:00
|
|
|
"batters": map[string]any{
|
|
|
|
"batter": []any{
|
|
|
|
map[string]any{"type": "Regular"},
|
|
|
|
map[string]any{"type": "Chocolate"},
|
|
|
|
map[string]any{"type": "Blueberry"},
|
|
|
|
map[string]any{"type": "Devil's Food"},
|
2019-03-16 11:10:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"hacker": true,
|
|
|
|
"beard": true,
|
2023-09-26 14:59:38 +00:00
|
|
|
"hobbies": []any{
|
2019-03-16 11:10:32 +00:00
|
|
|
"skateboarding",
|
|
|
|
"snowboarding",
|
|
|
|
"go",
|
|
|
|
},
|
2024-06-24 13:10:56 +00:00
|
|
|
"age": 35,
|
|
|
|
"type": "donut",
|
|
|
|
"newkey": "remote",
|
|
|
|
"name": "Cake",
|
2019-03-16 11:10:32 +00:00
|
|
|
"title_dotenv": "DotEnv Example",
|
|
|
|
"type_dotenv": "donut",
|
|
|
|
"name_dotenv": "Cake",
|
|
|
|
}
|
2014-09-27 21:00:51 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.ElementsMatch(t, ks, v.AllKeys())
|
|
|
|
assert.Equal(t, all, v.AllSettings())
|
2014-09-27 21:00:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 21:04:21 +00:00
|
|
|
func TestAllKeysWithEnv(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
// bind and define environment variables (including a nested one)
|
|
|
|
v.BindEnv("id")
|
|
|
|
v.BindEnv("foo.bar")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("ID", "13")
|
|
|
|
t.Setenv("FOO_BAR", "baz")
|
2016-10-23 21:04:21 +00:00
|
|
|
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.ElementsMatch(t, []string{"id", "foo.bar"}, v.AllKeys())
|
2016-10-23 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 05:19:39 +00:00
|
|
|
func TestAliasesOfAliases(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.Set("Title", "Checking Case")
|
|
|
|
v.RegisterAlias("Foo", "Bar")
|
|
|
|
v.RegisterAlias("Bar", "Title")
|
|
|
|
assert.Equal(t, "Checking Case", v.Get("FOO"))
|
2014-04-05 05:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecursiveAliases(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.Set("baz", "bat")
|
|
|
|
v.RegisterAlias("Baz", "Roo")
|
|
|
|
v.RegisterAlias("Roo", "baz")
|
|
|
|
assert.Equal(t, "bat", v.Get("Baz"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
2014-06-26 21:58:55 +00:00
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
func TestUnmarshal(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetDefault("port", 1313)
|
|
|
|
v.Set("name", "Steve")
|
|
|
|
v.Set("duration", "1s1ms")
|
|
|
|
v.Set("modes", []int{1, 2, 3})
|
2014-06-26 21:58:55 +00:00
|
|
|
|
|
|
|
type config struct {
|
2016-09-23 23:20:44 +00:00
|
|
|
Port int
|
|
|
|
Name string
|
|
|
|
Duration time.Duration
|
2019-06-11 20:51:57 +00:00
|
|
|
Modes []int
|
2014-06-26 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
|
2014-06-26 21:58:55 +00:00
|
|
|
|
2019-06-11 20:51:57 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
&config{
|
|
|
|
Name: "Steve",
|
|
|
|
Port: 1313,
|
|
|
|
Duration: time.Second + time.Millisecond,
|
|
|
|
Modes: []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
&C,
|
|
|
|
)
|
2014-06-26 21:58:55 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.Set("port", 1234)
|
|
|
|
require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
|
2019-06-11 20:51:57 +00:00
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
&config{
|
|
|
|
Name: "Steve",
|
|
|
|
Port: 1234,
|
|
|
|
Duration: time.Second + time.Millisecond,
|
|
|
|
Modes: []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
&C,
|
|
|
|
)
|
2014-09-27 21:03:00 +00:00
|
|
|
}
|
2014-10-09 19:48:55 +00:00
|
|
|
|
2024-06-24 16:20:56 +00:00
|
|
|
func TestUnmarshalWithDefaultDecodeHook(t *testing.T) {
|
|
|
|
opt := mapstructure.ComposeDecodeHookFunc(
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
|
|
// Custom Decode Hook Function
|
|
|
|
func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
|
|
|
|
if rf != reflect.String || rt != reflect.Map {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
m := map[string]string{}
|
|
|
|
raw := data.(string)
|
|
|
|
if raw == "" {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(raw), &m)
|
|
|
|
return m, err
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
v := NewWithOptions(WithDecodeHook(opt))
|
|
|
|
v.Set("credentials", "{\"foo\":\"bar\"}")
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Credentials map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
|
|
|
require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
|
|
|
|
|
|
|
|
assert.Equal(t, &config{
|
|
|
|
Credentials: map[string]string{"foo": "bar"},
|
|
|
|
}, &C)
|
|
|
|
}
|
|
|
|
|
2018-06-28 09:55:33 +00:00
|
|
|
func TestUnmarshalWithDecoderOptions(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.Set("credentials", "{\"foo\":\"bar\"}")
|
2018-06-28 09:55:33 +00:00
|
|
|
|
|
|
|
opt := DecodeHook(mapstructure.ComposeDecodeHookFunc(
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
|
|
// Custom Decode Hook Function
|
2023-09-26 14:59:38 +00:00
|
|
|
func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
|
2018-06-28 09:55:33 +00:00
|
|
|
if rf != reflect.String || rt != reflect.Map {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
m := map[string]string{}
|
|
|
|
raw := data.(string)
|
|
|
|
if raw == "" {
|
|
|
|
return m, nil
|
|
|
|
}
|
2023-11-30 15:45:20 +00:00
|
|
|
err := json.Unmarshal([]byte(raw), &m)
|
|
|
|
return m, err
|
2018-06-28 09:55:33 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Credentials map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
require.NoError(t, v.Unmarshal(&C, opt), "unable to decode into struct")
|
2018-06-28 09:55:33 +00:00
|
|
|
|
|
|
|
assert.Equal(t, &config{
|
|
|
|
Credentials: map[string]string{"foo": "bar"},
|
|
|
|
}, &C)
|
|
|
|
}
|
|
|
|
|
2023-12-05 14:40:27 +00:00
|
|
|
func TestUnmarshalWithAutomaticEnv(t *testing.T) {
|
|
|
|
t.Setenv("PORT", "1313")
|
|
|
|
t.Setenv("NAME", "Steve")
|
|
|
|
t.Setenv("DURATION", "1s1ms")
|
|
|
|
t.Setenv("MODES", "1,2,3")
|
|
|
|
t.Setenv("SECRET", "42")
|
|
|
|
t.Setenv("FILESYSTEM_SIZE", "4096")
|
|
|
|
|
|
|
|
type AuthConfig struct {
|
|
|
|
Secret string `mapstructure:"secret"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageConfig struct {
|
|
|
|
Size int `mapstructure:"size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Configuration struct {
|
|
|
|
Port int `mapstructure:"port"`
|
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Duration time.Duration `mapstructure:"duration"`
|
|
|
|
|
|
|
|
// Infer name from struct
|
|
|
|
Modes []int
|
|
|
|
|
|
|
|
// Squash nested struct (omit prefix)
|
|
|
|
Authentication AuthConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
// Different key
|
|
|
|
Storage StorageConfig `mapstructure:"filesystem"`
|
|
|
|
|
|
|
|
// Omitted field
|
|
|
|
Flag bool `mapstructure:"flag"`
|
|
|
|
}
|
|
|
|
|
2024-06-04 19:06:34 +00:00
|
|
|
v := NewWithOptions(ExperimentalBindStruct())
|
2023-12-05 14:40:27 +00:00
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
v.AutomaticEnv()
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
var config Configuration
|
|
|
|
if err := v.Unmarshal(&config); err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
Configuration{
|
|
|
|
Name: "Steve",
|
|
|
|
Port: 1313,
|
|
|
|
Duration: time.Second + time.Millisecond,
|
|
|
|
Modes: []int{1, 2, 3},
|
|
|
|
Authentication: AuthConfig{
|
|
|
|
Secret: "42",
|
|
|
|
},
|
|
|
|
Storage: StorageConfig{
|
|
|
|
Size: 4096,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Precedence", func(t *testing.T) {
|
|
|
|
var config Configuration
|
|
|
|
|
|
|
|
v.Set("port", 1234)
|
|
|
|
if err := v.Unmarshal(&config); err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
Configuration{
|
|
|
|
Name: "Steve",
|
|
|
|
Port: 1234,
|
|
|
|
Duration: time.Second + time.Millisecond,
|
|
|
|
Modes: []int{1, 2, 3},
|
|
|
|
Authentication: AuthConfig{
|
|
|
|
Secret: "42",
|
|
|
|
},
|
|
|
|
Storage: StorageConfig{
|
|
|
|
Size: 4096,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Unset", func(t *testing.T) {
|
|
|
|
var config Configuration
|
|
|
|
|
|
|
|
err := v.Unmarshal(&config, func(config *mapstructure.DecoderConfig) {
|
|
|
|
config.ErrorUnset = true
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Error(t, err, "expected viper.Unmarshal to return error due to unset field 'FLAG'")
|
|
|
|
})
|
2023-12-06 21:20:47 +00:00
|
|
|
|
|
|
|
t.Run("Exact", func(t *testing.T) {
|
|
|
|
var config Configuration
|
|
|
|
|
|
|
|
v.Set("port", 1234)
|
|
|
|
if err := v.UnmarshalExact(&config); err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
Configuration{
|
|
|
|
Name: "Steve",
|
|
|
|
Port: 1234,
|
|
|
|
Duration: time.Second + time.Millisecond,
|
|
|
|
Modes: []int{1, 2, 3},
|
|
|
|
Authentication: AuthConfig{
|
|
|
|
Secret: "42",
|
|
|
|
},
|
|
|
|
Storage: StorageConfig{
|
|
|
|
Size: 4096,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
})
|
2023-12-05 14:40:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 01:38:54 +00:00
|
|
|
func TestBindPFlags(t *testing.T) {
|
2016-10-08 08:00:18 +00:00
|
|
|
v := New() // create independent Viper object
|
2015-04-02 01:38:54 +00:00
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
|
2020-09-11 15:48:38 +00:00
|
|
|
testValues := map[string]*string{
|
2015-04-02 01:38:54 +00:00
|
|
|
"host": nil,
|
|
|
|
"port": nil,
|
|
|
|
"endpoint": nil,
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:48:38 +00:00
|
|
|
mutatedTestValues := map[string]string{
|
2015-04-02 01:38:54 +00:00
|
|
|
"host": "localhost",
|
|
|
|
"port": "6060",
|
|
|
|
"endpoint": "/public",
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
for name := range testValues {
|
2015-04-02 01:38:54 +00:00
|
|
|
testValues[name] = flagSet.String(name, "", "test")
|
|
|
|
}
|
|
|
|
|
2016-10-08 08:00:18 +00:00
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2015-04-02 01:38:54 +00:00
|
|
|
|
|
|
|
flagSet.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Value.Set(mutatedTestValues[flag.Name])
|
|
|
|
flag.Changed = true
|
|
|
|
})
|
|
|
|
|
|
|
|
for name, expected := range mutatedTestValues {
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, expected, v.Get(name))
|
2015-04-02 01:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 08:08:15 +00:00
|
|
|
func TestBindPFlagsStringSlice(t *testing.T) {
|
2017-04-17 16:52:44 +00:00
|
|
|
tests := []struct {
|
2017-04-17 08:08:15 +00:00
|
|
|
Expected []string
|
|
|
|
Value string
|
|
|
|
}{
|
2020-09-30 12:20:00 +00:00
|
|
|
{[]string{}, ""},
|
2017-04-17 08:08:15 +00:00
|
|
|
{[]string{"jeden"}, "jeden"},
|
|
|
|
{[]string{"dwa", "trzy"}, "dwa,trzy"},
|
2017-04-17 16:52:44 +00:00
|
|
|
{[]string{"cztery", "piec , szesc"}, "cztery,\"piec , szesc\""},
|
|
|
|
}
|
2017-04-17 08:08:15 +00:00
|
|
|
|
2017-04-17 16:52:44 +00:00
|
|
|
v := New() // create independent Viper object
|
|
|
|
defaultVal := []string{"default"}
|
|
|
|
v.SetDefault("stringslice", defaultVal)
|
2017-04-17 16:33:30 +00:00
|
|
|
|
2017-04-17 16:52:44 +00:00
|
|
|
for _, testValue := range tests {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.StringSlice("stringslice", testValue.Expected, "test")
|
|
|
|
|
|
|
|
for _, changed := range []bool{true, false} {
|
2017-04-17 16:52:25 +00:00
|
|
|
flagSet.VisitAll(func(f *pflag.Flag) {
|
|
|
|
f.Value.Set(testValue.Value)
|
|
|
|
f.Changed = changed
|
2017-04-17 08:08:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2017-04-17 08:08:15 +00:00
|
|
|
|
|
|
|
type TestStr struct {
|
|
|
|
StringSlice []string
|
|
|
|
}
|
|
|
|
val := &TestStr{}
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.Unmarshal(val)
|
|
|
|
require.NoError(t, err, "cannot unmarshal")
|
2017-04-17 16:33:30 +00:00
|
|
|
if changed {
|
2018-12-05 15:27:02 +00:00
|
|
|
assert.Equal(t, testValue.Expected, val.StringSlice)
|
2020-09-30 12:20:00 +00:00
|
|
|
assert.Equal(t, testValue.Expected, v.Get("stringslice"))
|
2017-04-17 16:33:30 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.StringSlice)
|
|
|
|
}
|
2017-04-17 08:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:24:02 +00:00
|
|
|
func TestBindPFlagsStringArray(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Expected []string
|
|
|
|
Value string
|
|
|
|
}{
|
|
|
|
{[]string{}, ""},
|
|
|
|
{[]string{"jeden"}, "jeden"},
|
|
|
|
{[]string{"dwa,trzy"}, "dwa,trzy"},
|
|
|
|
{[]string{"cztery,\"piec , szesc\""}, "cztery,\"piec , szesc\""},
|
|
|
|
}
|
|
|
|
|
|
|
|
v := New() // create independent Viper object
|
|
|
|
defaultVal := []string{"default"}
|
|
|
|
v.SetDefault("stringarray", defaultVal)
|
|
|
|
|
|
|
|
for _, testValue := range tests {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.StringArray("stringarray", testValue.Expected, "test")
|
|
|
|
|
|
|
|
for _, changed := range []bool{true, false} {
|
|
|
|
flagSet.VisitAll(func(f *pflag.Flag) {
|
|
|
|
f.Value.Set(testValue.Value)
|
|
|
|
f.Changed = changed
|
|
|
|
})
|
|
|
|
|
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2021-06-23 11:24:02 +00:00
|
|
|
|
|
|
|
type TestStr struct {
|
|
|
|
StringArray []string
|
|
|
|
}
|
|
|
|
val := &TestStr{}
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.Unmarshal(val)
|
|
|
|
require.NoError(t, err, "cannot unmarshal")
|
2021-06-23 11:24:02 +00:00
|
|
|
if changed {
|
|
|
|
assert.Equal(t, testValue.Expected, val.StringArray)
|
|
|
|
assert.Equal(t, testValue.Expected, v.Get("stringarray"))
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.StringArray)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:30:05 +00:00
|
|
|
func TestSliceFlagsReturnCorrectType(t *testing.T) {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.IntSlice("int", []int{1, 2}, "")
|
|
|
|
flagSet.StringSlice("str", []string{"3", "4"}, "")
|
|
|
|
flagSet.DurationSlice("duration", []time.Duration{5 * time.Second}, "")
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.BindPFlags(flagSet)
|
|
|
|
|
|
|
|
all := v.AllSettings()
|
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.IsType(t, []int{}, all["int"])
|
|
|
|
assert.IsType(t, []string{}, all["str"])
|
|
|
|
assert.IsType(t, []time.Duration{}, all["duration"])
|
2023-02-05 17:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:51:57 +00:00
|
|
|
func TestBindPFlagsIntSlice(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Expected []int
|
|
|
|
Value string
|
|
|
|
}{
|
2020-09-30 12:20:00 +00:00
|
|
|
{[]int{}, ""},
|
2019-06-11 20:51:57 +00:00
|
|
|
{[]int{1}, "1"},
|
|
|
|
{[]int{2, 3}, "2,3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
v := New() // create independent Viper object
|
|
|
|
defaultVal := []int{0}
|
|
|
|
v.SetDefault("intslice", defaultVal)
|
|
|
|
|
|
|
|
for _, testValue := range tests {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.IntSlice("intslice", testValue.Expected, "test")
|
|
|
|
|
|
|
|
for _, changed := range []bool{true, false} {
|
|
|
|
flagSet.VisitAll(func(f *pflag.Flag) {
|
|
|
|
f.Value.Set(testValue.Value)
|
|
|
|
f.Changed = changed
|
|
|
|
})
|
|
|
|
|
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2019-06-11 20:51:57 +00:00
|
|
|
|
|
|
|
type TestInt struct {
|
|
|
|
IntSlice []int
|
|
|
|
}
|
|
|
|
val := &TestInt{}
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.Unmarshal(val)
|
|
|
|
require.NoError(t, err, "cannot unmarshal")
|
2019-06-11 20:51:57 +00:00
|
|
|
if changed {
|
|
|
|
assert.Equal(t, testValue.Expected, val.IntSlice)
|
2020-09-30 12:20:00 +00:00
|
|
|
assert.Equal(t, testValue.Expected, v.Get("intslice"))
|
2019-06-11 20:51:57 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.IntSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 19:48:55 +00:00
|
|
|
func TestBindPFlag(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2020-09-11 15:48:38 +00:00
|
|
|
testString := "testing"
|
|
|
|
testValue := newStringValue(testString, &testString)
|
2014-10-09 19:48:55 +00:00
|
|
|
|
|
|
|
flag := &pflag.Flag{
|
|
|
|
Name: "testflag",
|
|
|
|
Value: testValue,
|
|
|
|
Changed: false,
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindPFlag("testvalue", flag)
|
2014-10-09 19:48:55 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, testString, v.Get("testvalue"))
|
2014-10-09 19:48:55 +00:00
|
|
|
|
|
|
|
flag.Value.Set("testing_mutate")
|
2019-09-27 15:09:18 +00:00
|
|
|
flag.Changed = true // hack for pflag usage
|
2014-10-09 19:48:55 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "testing_mutate", v.Get("testvalue"))
|
2014-10-09 19:48:55 +00:00
|
|
|
}
|
2014-10-09 20:39:24 +00:00
|
|
|
|
2020-08-11 23:26:27 +00:00
|
|
|
func TestBindPFlagDetectNilFlag(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
result := v.BindPFlag("testvalue", nil)
|
2020-08-11 23:26:27 +00:00
|
|
|
assert.Error(t, result)
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:38:39 +00:00
|
|
|
func TestBindPFlagStringToString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Expected map[string]string
|
|
|
|
Value string
|
|
|
|
}{
|
|
|
|
{map[string]string{}, ""},
|
|
|
|
{map[string]string{"yo": "hi"}, "yo=hi"},
|
|
|
|
{map[string]string{"yo": "hi", "oh": "hi=there"}, "yo=hi,oh=hi=there"},
|
|
|
|
{map[string]string{"yo": ""}, "yo="},
|
|
|
|
{map[string]string{"yo": "", "oh": "hi=there"}, "yo=,oh=hi=there"},
|
|
|
|
}
|
|
|
|
|
|
|
|
v := New() // create independent Viper object
|
|
|
|
defaultVal := map[string]string{}
|
|
|
|
v.SetDefault("stringtostring", defaultVal)
|
|
|
|
|
|
|
|
for _, testValue := range tests {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.StringToString("stringtostring", testValue.Expected, "test")
|
|
|
|
|
|
|
|
for _, changed := range []bool{true, false} {
|
|
|
|
flagSet.VisitAll(func(f *pflag.Flag) {
|
|
|
|
f.Value.Set(testValue.Value)
|
|
|
|
f.Changed = changed
|
|
|
|
})
|
|
|
|
|
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2020-05-09 09:38:39 +00:00
|
|
|
|
|
|
|
type TestMap struct {
|
|
|
|
StringToString map[string]string
|
|
|
|
}
|
|
|
|
val := &TestMap{}
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.Unmarshal(val)
|
|
|
|
require.NoError(t, err, "cannot unmarshal")
|
2020-05-09 09:38:39 +00:00
|
|
|
if changed {
|
|
|
|
assert.Equal(t, testValue.Expected, val.StringToString)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.StringToString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:10:59 +00:00
|
|
|
func TestBindPFlagStringToInt(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Expected map[string]int
|
|
|
|
Value string
|
|
|
|
}{
|
|
|
|
{map[string]int{"yo": 1, "oh": 21}, "yo=1,oh=21"},
|
|
|
|
{map[string]int{"yo": 100000000, "oh": 0}, "yo=100000000,oh=0"},
|
|
|
|
{map[string]int{}, "yo=2,oh=21.0"},
|
|
|
|
{map[string]int{}, "yo=,oh=20.99"},
|
|
|
|
{map[string]int{}, "yo=,oh="},
|
|
|
|
}
|
|
|
|
|
|
|
|
v := New() // create independent Viper object
|
|
|
|
defaultVal := map[string]int{}
|
|
|
|
v.SetDefault("stringtoint", defaultVal)
|
|
|
|
|
|
|
|
for _, testValue := range tests {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flagSet.StringToInt("stringtoint", testValue.Expected, "test")
|
|
|
|
|
|
|
|
for _, changed := range []bool{true, false} {
|
|
|
|
flagSet.VisitAll(func(f *pflag.Flag) {
|
|
|
|
f.Value.Set(testValue.Value)
|
|
|
|
f.Changed = changed
|
|
|
|
})
|
|
|
|
|
|
|
|
err := v.BindPFlags(flagSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2023-02-07 11:10:59 +00:00
|
|
|
|
|
|
|
type TestMap struct {
|
|
|
|
StringToInt map[string]int
|
|
|
|
}
|
|
|
|
val := &TestMap{}
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.Unmarshal(val)
|
|
|
|
require.NoError(t, err, "cannot unmarshal")
|
2023-02-07 11:10:59 +00:00
|
|
|
if changed {
|
|
|
|
assert.Equal(t, testValue.Expected, val.StringToInt)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.StringToInt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 20:39:24 +00:00
|
|
|
func TestBoundCaseSensitivity(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
initConfigs(v)
|
|
|
|
assert.Equal(t, "brown", v.Get("eyes"))
|
2014-10-09 20:39:24 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindEnv("eYEs", "TURTLE_EYES")
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("TURTLE_EYES", "blue")
|
2014-10-09 20:39:24 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "blue", v.Get("eyes"))
|
2014-10-09 20:39:24 +00:00
|
|
|
|
2020-09-11 15:48:38 +00:00
|
|
|
testString := "green"
|
|
|
|
testValue := newStringValue(testString, &testString)
|
2014-10-09 20:39:24 +00:00
|
|
|
|
|
|
|
flag := &pflag.Flag{
|
|
|
|
Name: "eyeballs",
|
|
|
|
Value: testValue,
|
|
|
|
Changed: true,
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.BindPFlag("eYEs", flag)
|
|
|
|
assert.Equal(t, "green", v.Get("eyes"))
|
2014-10-09 20:39:24 +00:00
|
|
|
}
|
2015-02-28 21:03:22 +00:00
|
|
|
|
|
|
|
func TestSizeInBytes(t *testing.T) {
|
|
|
|
input := map[string]uint{
|
|
|
|
"": 0,
|
|
|
|
"b": 0,
|
|
|
|
"12 bytes": 0,
|
|
|
|
"200000000000gb": 0,
|
|
|
|
"12 b": 12,
|
|
|
|
"43 MB": 43 * (1 << 20),
|
|
|
|
"10mb": 10 * (1 << 20),
|
|
|
|
"1gb": 1 << 30,
|
|
|
|
}
|
|
|
|
|
|
|
|
for str, expected := range input {
|
|
|
|
assert.Equal(t, expected, parseSizeInBytes(str), str)
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 19:15:24 +00:00
|
|
|
|
|
|
|
func TestFindsNestedKeys(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
initConfigs(v)
|
2015-05-01 19:15:24 +00:00
|
|
|
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.Set("super", map[string]any{
|
2023-09-26 14:59:38 +00:00
|
|
|
"deep": map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
expected := map[string]any{
|
|
|
|
"super": map[string]any{
|
|
|
|
"deep": map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
"super.deep": map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
"super.deep.nested": "value",
|
|
|
|
"owner.organization": "MongoDB",
|
2023-09-26 14:59:38 +00:00
|
|
|
"batters.batter": []any{
|
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Regular",
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Chocolate",
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Blueberry",
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Devil's Food",
|
|
|
|
},
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
"hobbies": []any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"skateboarding", "snowboarding", "go",
|
|
|
|
},
|
2019-06-11 20:47:43 +00:00
|
|
|
"TITLE_DOTENV": "DotEnv Example",
|
|
|
|
"TYPE_DOTENV": "donut",
|
|
|
|
"NAME_DOTENV": "Cake",
|
|
|
|
"title": "TOML Example",
|
|
|
|
"newkey": "remote",
|
2023-09-26 14:59:38 +00:00
|
|
|
"batters": map[string]any{
|
|
|
|
"batter": []any{
|
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Regular",
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Chocolate",
|
2020-09-11 15:48:38 +00:00
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Blueberry",
|
2020-09-11 15:48:38 +00:00
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Devil's Food",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"eyes": "brown",
|
|
|
|
"age": 35,
|
2023-09-26 14:59:38 +00:00
|
|
|
"owner": map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"organization": "MongoDB",
|
2016-10-14 09:24:45 +00:00
|
|
|
"bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
2015-05-01 19:15:24 +00:00
|
|
|
"dob": dob,
|
|
|
|
},
|
2016-10-14 09:24:45 +00:00
|
|
|
"owner.bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "donut",
|
|
|
|
"id": "0001",
|
|
|
|
"name": "Cake",
|
|
|
|
"hacker": true,
|
|
|
|
"ppu": 0.55,
|
2023-09-26 14:59:38 +00:00
|
|
|
"clothing": map[string]any{
|
2015-05-01 19:15:24 +00:00
|
|
|
"jacket": "leather",
|
|
|
|
"trousers": "denim",
|
2023-09-26 14:59:38 +00:00
|
|
|
"pants": map[string]any{
|
2015-05-27 15:15:02 +00:00
|
|
|
"size": "large",
|
|
|
|
},
|
2015-05-01 19:15:24 +00:00
|
|
|
},
|
2015-05-27 15:15:02 +00:00
|
|
|
"clothing.jacket": "leather",
|
|
|
|
"clothing.pants.size": "large",
|
|
|
|
"clothing.trousers": "denim",
|
|
|
|
"owner.dob": dob,
|
|
|
|
"beard": true,
|
2015-05-01 19:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, expectedValue := range expected {
|
|
|
|
assert.Equal(t, expectedValue, v.Get(key))
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 09:13:33 +00:00
|
|
|
|
2024-06-05 08:28:54 +00:00
|
|
|
func TestReadConfig(t *testing.T) {
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Log(v.AllKeys())
|
|
|
|
|
|
|
|
assert.True(t, v.InConfig("name"))
|
|
|
|
assert.True(t, v.InConfig("clothing.jacket"))
|
|
|
|
assert.False(t, v.InConfig("state"))
|
|
|
|
assert.False(t, v.InConfig("clothing.hat"))
|
|
|
|
assert.Equal(t, "steve", v.Get("name"))
|
|
|
|
assert.Equal(t, []any{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
|
|
|
|
assert.Equal(t, map[string]any{"jacket": "leather", "trousers": "denim", "pants": map[string]any{"size": "large"}}, v.Get("clothing"))
|
|
|
|
assert.Equal(t, 35, v.Get("age"))
|
|
|
|
})
|
2024-06-05 08:29:25 +00:00
|
|
|
|
|
|
|
t.Run("missing config type", func(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2015-05-08 09:13:33 +00:00
|
|
|
}
|
2015-08-02 00:37:27 +00:00
|
|
|
|
2015-11-29 23:16:21 +00:00
|
|
|
func TestIsSet(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
2017-04-11 15:49:10 +00:00
|
|
|
|
|
|
|
/* config and defaults */
|
2015-11-29 23:16:21 +00:00
|
|
|
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
2017-04-11 15:49:10 +00:00
|
|
|
v.SetDefault("clothing.shoes", "sneakers")
|
|
|
|
|
|
|
|
assert.True(t, v.IsSet("clothing"))
|
2015-11-29 23:16:21 +00:00
|
|
|
assert.True(t, v.IsSet("clothing.jacket"))
|
|
|
|
assert.False(t, v.IsSet("clothing.jackets"))
|
2017-04-11 15:49:10 +00:00
|
|
|
assert.True(t, v.IsSet("clothing.shoes"))
|
|
|
|
|
|
|
|
/* state change */
|
2015-11-29 23:16:21 +00:00
|
|
|
assert.False(t, v.IsSet("helloworld"))
|
|
|
|
v.Set("helloworld", "fubar")
|
|
|
|
assert.True(t, v.IsSet("helloworld"))
|
2017-04-11 15:49:10 +00:00
|
|
|
|
|
|
|
/* env */
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
v.BindEnv("eyes")
|
|
|
|
v.BindEnv("foo")
|
|
|
|
v.BindEnv("clothing.hat")
|
|
|
|
v.BindEnv("clothing.hats")
|
2020-09-30 11:02:07 +00:00
|
|
|
|
2023-08-18 14:16:47 +00:00
|
|
|
t.Setenv("FOO", "bar")
|
|
|
|
t.Setenv("CLOTHING_HAT", "bowler")
|
2017-04-11 15:49:10 +00:00
|
|
|
|
|
|
|
assert.True(t, v.IsSet("eyes")) // in the config file
|
|
|
|
assert.True(t, v.IsSet("foo")) // in the environment
|
|
|
|
assert.True(t, v.IsSet("clothing.hat")) // in the environment
|
|
|
|
assert.False(t, v.IsSet("clothing.hats")) // not defined
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
flagset := pflag.NewFlagSet("testisset", pflag.ContinueOnError)
|
|
|
|
flagset.Bool("foobaz", false, "foobaz")
|
|
|
|
flagset.Bool("barbaz", false, "barbaz")
|
|
|
|
foobaz, barbaz := flagset.Lookup("foobaz"), flagset.Lookup("barbaz")
|
|
|
|
v.BindPFlag("foobaz", foobaz)
|
|
|
|
v.BindPFlag("barbaz", barbaz)
|
|
|
|
barbaz.Value.Set("true")
|
|
|
|
barbaz.Changed = true // hack for pflag usage
|
|
|
|
|
|
|
|
assert.False(t, v.IsSet("foobaz"))
|
|
|
|
assert.True(t, v.IsSet("barbaz"))
|
2015-11-29 23:16:21 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 00:37:27 +00:00
|
|
|
func TestDirsSearch(t *testing.T) {
|
2023-09-13 14:56:49 +00:00
|
|
|
root, config := initDirs(t)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.SetConfigName(config)
|
|
|
|
v.SetDefault(`key`, `default`)
|
|
|
|
|
2023-09-13 14:56:49 +00:00
|
|
|
entries, err := os.ReadDir(root)
|
2023-09-14 10:49:54 +00:00
|
|
|
require.NoError(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
for _, e := range entries {
|
|
|
|
if e.IsDir() {
|
2023-09-14 10:49:54 +00:00
|
|
|
v.AddConfigPath(filepath.Join(root, e.Name()))
|
2015-08-02 00:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v.ReadInConfig()
|
2023-09-14 10:49:54 +00:00
|
|
|
require.NoError(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
2019-03-16 11:10:32 +00:00
|
|
|
assert.Equal(t, `value is `+filepath.Base(v.configPaths[0]), v.GetString(`key`))
|
2015-08-02 00:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongDirsSearchNotFound(t *testing.T) {
|
2023-09-13 14:56:49 +00:00
|
|
|
_, config := initDirs(t)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.SetConfigName(config)
|
|
|
|
v.SetDefault(`key`, `default`)
|
|
|
|
|
|
|
|
v.AddConfigPath(`whattayoutalkingbout`)
|
|
|
|
v.AddConfigPath(`thispathaintthere`)
|
|
|
|
|
|
|
|
err := v.ReadInConfig()
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.IsType(t, err, ConfigFileNotFoundError{"", ""})
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
// Even though config did not load and the error might have
|
|
|
|
// been ignored by the client, the default still loads
|
|
|
|
assert.Equal(t, `default`, v.GetString(`key`))
|
|
|
|
}
|
2015-12-24 11:44:44 +00:00
|
|
|
|
2016-12-13 09:38:49 +00:00
|
|
|
func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
|
2023-09-13 14:56:49 +00:00
|
|
|
_, config := initDirs(t)
|
2016-12-13 09:38:49 +00:00
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.SetConfigName(config)
|
|
|
|
v.SetDefault(`key`, `default`)
|
|
|
|
|
|
|
|
v.AddConfigPath(`whattayoutalkingbout`)
|
|
|
|
v.AddConfigPath(`thispathaintthere`)
|
|
|
|
|
|
|
|
err := v.MergeInConfig()
|
|
|
|
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
|
|
|
|
|
|
|
|
// Even though config did not load and the error might have
|
|
|
|
// been ignored by the client, the default still loads
|
|
|
|
assert.Equal(t, `default`, v.GetString(`key`))
|
|
|
|
}
|
|
|
|
|
2023-05-30 10:07:27 +00:00
|
|
|
var yamlInvalid = []byte(`hash: map
|
|
|
|
- foo
|
|
|
|
- bar
|
|
|
|
`)
|
|
|
|
|
|
|
|
func TestUnwrapParseErrors(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
assert.ErrorAs(t, v.ReadConfig(bytes.NewBuffer(yamlInvalid)), &ConfigParseError{})
|
2023-05-30 10:07:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 11:44:44 +00:00
|
|
|
func TestSub(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
|
|
|
|
2015-12-25 04:29:33 +00:00
|
|
|
subv := v.Sub("clothing")
|
|
|
|
assert.Equal(t, v.Get("clothing.pants.size"), subv.Get("pants.size"))
|
|
|
|
|
|
|
|
subv = v.Sub("clothing.pants")
|
2015-12-24 11:44:44 +00:00
|
|
|
assert.Equal(t, v.Get("clothing.pants.size"), subv.Get("size"))
|
|
|
|
|
|
|
|
subv = v.Sub("clothing.pants.size")
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, (*Viper)(nil), subv)
|
2016-10-05 23:22:39 +00:00
|
|
|
|
|
|
|
subv = v.Sub("missing.key")
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, (*Viper)(nil), subv)
|
2020-12-24 06:04:37 +00:00
|
|
|
|
|
|
|
subv = v.Sub("clothing")
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.Equal(t, []string{"clothing"}, subv.parents)
|
2020-12-24 06:04:37 +00:00
|
|
|
|
|
|
|
subv = v.Sub("clothing").Sub("pants")
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.Equal(t, []string{"clothing", "pants"}, subv.parents)
|
2015-12-24 11:44:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 20:14:28 +00:00
|
|
|
func TestSubWithKeyDelimiter(t *testing.T) {
|
|
|
|
v := NewWithOptions(KeyDelimiter("::"))
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
r := strings.NewReader(string(yamlExampleWithDot))
|
|
|
|
err := v.unmarshalReader(r, v.config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
subv := v.Sub("emails")
|
|
|
|
assert.Equal(t, "01/02/03", subv.Get("steve@hacker.com::created"))
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:26:31 +00:00
|
|
|
var jsonWriteExpected = []byte(`{
|
|
|
|
"batters": {
|
|
|
|
"batter": [
|
|
|
|
{
|
|
|
|
"type": "Regular"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Chocolate"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Blueberry"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Devil's Food"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"id": "0001",
|
|
|
|
"name": "Cake",
|
|
|
|
"ppu": 0.55,
|
|
|
|
"type": "donut"
|
|
|
|
}`)
|
|
|
|
|
2022-01-01 16:02:57 +00:00
|
|
|
// var yamlWriteExpected = []byte(`age: 35
|
|
|
|
// beard: true
|
|
|
|
// clothing:
|
|
|
|
// jacket: leather
|
|
|
|
// pants:
|
|
|
|
// size: large
|
|
|
|
// trousers: denim
|
|
|
|
// eyes: brown
|
|
|
|
// hacker: true
|
|
|
|
// hobbies:
|
|
|
|
// - skateboarding
|
|
|
|
// - snowboarding
|
|
|
|
// - go
|
|
|
|
// name: steve
|
|
|
|
// `)
|
2017-12-07 04:26:31 +00:00
|
|
|
|
2020-02-19 23:41:04 +00:00
|
|
|
func TestWriteConfig(t *testing.T) {
|
2017-12-07 04:26:31 +00:00
|
|
|
fs := afero.NewMemMapFs()
|
2020-02-19 23:41:04 +00:00
|
|
|
testCases := map[string]struct {
|
|
|
|
configName string
|
|
|
|
inConfigType string
|
|
|
|
outConfigType string
|
|
|
|
fileName string
|
|
|
|
input []byte
|
|
|
|
expectedContent []byte
|
|
|
|
}{
|
|
|
|
"json with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "json",
|
|
|
|
outConfigType: "json",
|
|
|
|
fileName: "c.json",
|
|
|
|
input: jsonExample,
|
|
|
|
expectedContent: jsonWriteExpected,
|
|
|
|
},
|
|
|
|
"json without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "json",
|
|
|
|
outConfigType: "json",
|
|
|
|
fileName: "c",
|
|
|
|
input: jsonExample,
|
|
|
|
expectedContent: jsonWriteExpected,
|
|
|
|
},
|
|
|
|
"json with file extension and mismatch type": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "json",
|
|
|
|
outConfigType: "hcl",
|
|
|
|
fileName: "c.json",
|
|
|
|
input: jsonExample,
|
|
|
|
expectedContent: jsonWriteExpected,
|
|
|
|
},
|
|
|
|
"yaml with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "yaml",
|
|
|
|
outConfigType: "yaml",
|
|
|
|
fileName: "c.yaml",
|
|
|
|
input: yamlExample,
|
|
|
|
expectedContent: yamlWriteExpected,
|
|
|
|
},
|
|
|
|
"yaml without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "yaml",
|
|
|
|
outConfigType: "yaml",
|
|
|
|
fileName: "c",
|
|
|
|
input: yamlExample,
|
|
|
|
expectedContent: yamlWriteExpected,
|
|
|
|
},
|
|
|
|
"yaml with file extension and mismatch type": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "yaml",
|
|
|
|
outConfigType: "json",
|
|
|
|
fileName: "c.yaml",
|
|
|
|
input: yamlExample,
|
|
|
|
expectedContent: yamlWriteExpected,
|
|
|
|
},
|
2017-12-07 04:26:31 +00:00
|
|
|
}
|
2020-02-19 23:41:04 +00:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.SetConfigName(tc.fileName)
|
|
|
|
v.SetConfigType(tc.inConfigType)
|
|
|
|
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(tc.input))
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
v.SetConfigType(tc.outConfigType)
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.WriteConfigAs(tc.fileName)
|
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
read, err := afero.ReadFile(fs, tc.fileName)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
assert.Equal(t, tc.expectedContent, read)
|
|
|
|
})
|
2017-12-07 04:26:31 +00:00
|
|
|
}
|
2020-02-19 23:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteConfigTOML(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
configName string
|
|
|
|
configType string
|
|
|
|
fileName string
|
|
|
|
input []byte
|
|
|
|
}{
|
|
|
|
"with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
configType: "toml",
|
|
|
|
fileName: "c.toml",
|
|
|
|
input: tomlExample,
|
|
|
|
},
|
|
|
|
"without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
configType: "toml",
|
|
|
|
fileName: "c",
|
|
|
|
input: tomlExample,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.SetConfigName(tc.configName)
|
|
|
|
v.SetConfigType(tc.configType)
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(tc.input))
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = v.WriteConfigAs(tc.fileName)
|
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
|
|
|
|
// The TOML String method does not order the contents.
|
|
|
|
// Therefore, we must read the generated file and compare the data.
|
|
|
|
v2 := New()
|
|
|
|
v2.SetFs(fs)
|
|
|
|
v2.SetConfigName(tc.configName)
|
|
|
|
v2.SetConfigType(tc.configType)
|
|
|
|
v2.SetConfigFile(tc.fileName)
|
|
|
|
err = v2.ReadInConfig()
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
|
|
|
|
assert.Equal(t, v.GetString("title"), v2.GetString("title"))
|
|
|
|
assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio"))
|
|
|
|
assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob"))
|
|
|
|
assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteConfigDotEnv(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
testCases := map[string]struct {
|
|
|
|
configName string
|
|
|
|
configType string
|
|
|
|
fileName string
|
|
|
|
input []byte
|
|
|
|
}{
|
|
|
|
"with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
configType: "env",
|
|
|
|
fileName: "c.env",
|
|
|
|
input: dotenvExample,
|
|
|
|
},
|
|
|
|
"without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
configType: "env",
|
|
|
|
fileName: "c",
|
|
|
|
input: dotenvExample,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.SetConfigName(tc.configName)
|
|
|
|
v.SetConfigType(tc.configType)
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(tc.input))
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = v.WriteConfigAs(tc.fileName)
|
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
|
|
|
|
// The TOML String method does not order the contents.
|
|
|
|
// Therefore, we must read the generated file and compare the data.
|
|
|
|
v2 := New()
|
|
|
|
v2.SetFs(fs)
|
|
|
|
v2.SetConfigName(tc.configName)
|
|
|
|
v2.SetConfigType(tc.configType)
|
|
|
|
v2.SetConfigFile(tc.fileName)
|
|
|
|
err = v2.ReadInConfig()
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-19 23:41:04 +00:00
|
|
|
|
|
|
|
assert.Equal(t, v.GetString("title_dotenv"), v2.GetString("title_dotenv"))
|
|
|
|
assert.Equal(t, v.GetString("type_dotenv"), v2.GetString("type_dotenv"))
|
|
|
|
assert.Equal(t, v.GetString("kind_dotenv"), v2.GetString("kind_dotenv"))
|
|
|
|
})
|
2017-12-07 04:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:29:19 +00:00
|
|
|
func TestSafeWriteConfig(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/test")
|
|
|
|
v.SetConfigName("c")
|
|
|
|
v.SetConfigType("yaml")
|
2019-12-04 01:14:08 +00:00
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)))
|
|
|
|
require.NoError(t, v.SafeWriteConfig())
|
2021-09-22 18:17:20 +00:00
|
|
|
read, err := afero.ReadFile(fs, testutil.AbsFilePath(t, "/test/c.yaml"))
|
2019-12-04 01:14:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-13 14:29:19 +00:00
|
|
|
assert.Equal(t, yamlWriteExpected, read)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.SetConfigName("c")
|
|
|
|
v.SetConfigType("yaml")
|
2019-12-06 11:46:23 +00:00
|
|
|
require.EqualError(t, v.SafeWriteConfig(), "missing configuration for 'configPath'")
|
2019-09-13 14:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteConfigWithExistingFile(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
2021-09-22 18:17:20 +00:00
|
|
|
fs.Create(testutil.AbsFilePath(t, "/test/c.yaml"))
|
2019-09-13 14:29:19 +00:00
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/test")
|
|
|
|
v.SetConfigName("c")
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
err := v.SafeWriteConfig()
|
2019-12-04 01:14:08 +00:00
|
|
|
require.Error(t, err)
|
2019-09-13 14:29:19 +00:00
|
|
|
_, ok := err.(ConfigFileAlreadyExistsError)
|
|
|
|
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteAsConfig(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
v.SetFs(fs)
|
2024-06-05 08:29:25 +00:00
|
|
|
v.SetConfigType("yaml")
|
2019-09-13 14:29:19 +00:00
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-04 01:14:08 +00:00
|
|
|
require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml"))
|
2023-10-07 19:28:36 +00:00
|
|
|
_, err = afero.ReadFile(fs, "/test/c.yaml")
|
|
|
|
require.NoError(t, err)
|
2019-09-13 14:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteConfigAsWithExistingFile(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fs.Create("/test/c.yaml")
|
|
|
|
v.SetFs(fs)
|
|
|
|
err := v.SafeWriteConfigAs("/test/c.yaml")
|
2019-12-04 01:14:08 +00:00
|
|
|
require.Error(t, err)
|
2019-09-13 14:29:19 +00:00
|
|
|
_, ok := err.(ConfigFileAlreadyExistsError)
|
|
|
|
assert.True(t, ok, "Expected ConfigFileAlreadyExistsError")
|
|
|
|
}
|
|
|
|
|
2021-01-15 13:18:17 +00:00
|
|
|
func TestWriteHiddenFile(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
fs := afero.NewMemMapFs()
|
2021-09-22 18:17:20 +00:00
|
|
|
fs.Create(testutil.AbsFilePath(t, "/test/.config"))
|
2021-01-15 13:18:17 +00:00
|
|
|
v.SetFs(fs)
|
|
|
|
|
|
|
|
v.SetConfigName(".config")
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.AddConfigPath("/test")
|
|
|
|
|
|
|
|
err := v.ReadInConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = v.WriteConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
var yamlMergeExampleTgt = []byte(`
|
|
|
|
hello:
|
|
|
|
pop: 37890
|
2019-05-24 13:40:12 +00:00
|
|
|
largenum: 765432101234567
|
2019-04-08 14:06:45 +00:00
|
|
|
num2pow63: 9223372036854775808
|
2021-05-12 22:09:19 +00:00
|
|
|
universe: null
|
2015-11-12 20:20:40 +00:00
|
|
|
world:
|
|
|
|
- us
|
|
|
|
- uk
|
|
|
|
- fr
|
|
|
|
- de
|
|
|
|
`)
|
|
|
|
|
|
|
|
var yamlMergeExampleSrc = []byte(`
|
|
|
|
hello:
|
|
|
|
pop: 45000
|
2019-05-24 13:40:12 +00:00
|
|
|
largenum: 7654321001234567
|
2015-11-12 20:20:40 +00:00
|
|
|
universe:
|
|
|
|
- mw
|
|
|
|
- ad
|
2019-06-19 14:05:58 +00:00
|
|
|
ints:
|
|
|
|
- 1
|
|
|
|
- 2
|
2015-11-12 20:20:40 +00:00
|
|
|
fu: bar
|
|
|
|
`)
|
|
|
|
|
2020-12-23 13:28:47 +00:00
|
|
|
var jsonMergeExampleTgt = []byte(`
|
|
|
|
{
|
|
|
|
"hello": {
|
2021-10-14 13:29:39 +00:00
|
|
|
"foo": null,
|
2020-12-23 13:28:47 +00:00
|
|
|
"pop": 123456
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
var jsonMergeExampleSrc = []byte(`
|
|
|
|
{
|
|
|
|
"hello": {
|
2021-10-14 13:29:39 +00:00
|
|
|
"foo": "foo str",
|
2020-12-23 13:28:47 +00:00
|
|
|
"pop": "pop str"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
func TestMergeConfig(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yml")
|
2023-10-07 19:28:36 +00:00
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt))
|
|
|
|
require.NoError(t, err)
|
2015-11-12 20:20:40 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, 37890, v.GetInt("hello.pop"))
|
|
|
|
assert.Equal(t, int32(37890), v.GetInt32("hello.pop"))
|
|
|
|
assert.Equal(t, int64(765432101234567), v.GetInt64("hello.largenum"))
|
|
|
|
assert.Equal(t, uint(37890), v.GetUint("hello.pop"))
|
|
|
|
assert.Equal(t, uint16(37890), v.GetUint16("hello.pop"))
|
|
|
|
assert.Equal(t, uint32(37890), v.GetUint32("hello.pop"))
|
|
|
|
assert.Equal(t, uint64(9223372036854775808), v.GetUint64("hello.num2pow63"))
|
|
|
|
assert.Len(t, v.GetStringSlice("hello.world"), 4)
|
|
|
|
assert.Empty(t, v.GetString("fu"))
|
|
|
|
|
|
|
|
err = v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
|
|
|
require.NoError(t, err)
|
2019-06-19 14:05:58 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
|
|
|
assert.Equal(t, int32(45000), v.GetInt32("hello.pop"))
|
|
|
|
assert.Equal(t, int64(7654321001234567), v.GetInt64("hello.largenum"))
|
|
|
|
assert.Len(t, v.GetStringSlice("hello.world"), 4)
|
|
|
|
assert.Len(t, v.GetStringSlice("hello.universe"), 2)
|
|
|
|
assert.Len(t, v.GetIntSlice("hello.ints"), 2)
|
|
|
|
assert.Equal(t, "bar", v.GetString("fu"))
|
2015-11-12 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 13:28:47 +00:00
|
|
|
func TestMergeConfigOverrideType(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("json")
|
2023-10-07 19:28:36 +00:00
|
|
|
err := v.ReadConfig(bytes.NewBuffer(jsonMergeExampleTgt))
|
|
|
|
require.NoError(t, err)
|
2020-12-23 13:28:47 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.MergeConfig(bytes.NewBuffer(jsonMergeExampleSrc))
|
|
|
|
require.NoError(t, err)
|
2021-10-14 13:29:39 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, "pop str", v.GetString("hello.pop"))
|
|
|
|
assert.Equal(t, "foo str", v.GetString("hello.foo"))
|
2020-12-23 13:28:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
func TestMergeConfigNoMerge(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yml")
|
2023-10-07 19:28:36 +00:00
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt))
|
|
|
|
require.NoError(t, err)
|
2015-11-12 20:20:40 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, 37890, v.GetInt("hello.pop"))
|
|
|
|
assert.Len(t, v.GetStringSlice("hello.world"), 4)
|
|
|
|
assert.Empty(t, v.GetString("fu"))
|
2015-11-12 20:20:40 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
|
|
|
require.NoError(t, err)
|
2019-06-19 14:05:58 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
|
|
|
assert.Empty(t, v.GetStringSlice("hello.world"))
|
|
|
|
assert.Len(t, v.GetStringSlice("hello.universe"), 2)
|
|
|
|
assert.Len(t, v.GetIntSlice("hello.ints"), 2)
|
|
|
|
assert.Equal(t, "bar", v.GetString("fu"))
|
2015-11-12 20:20:40 +00:00
|
|
|
}
|
2016-01-20 21:15:43 +00:00
|
|
|
|
2018-12-05 14:19:25 +00:00
|
|
|
func TestMergeConfigMap(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yml")
|
2023-10-07 19:28:36 +00:00
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt))
|
|
|
|
require.NoError(t, err)
|
2018-12-05 14:19:25 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assertFn := func(i int) {
|
2019-05-24 17:53:49 +00:00
|
|
|
large := v.GetInt64("hello.largenum")
|
2018-12-05 14:19:25 +00:00
|
|
|
pop := v.GetInt("hello.pop")
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, int64(765432101234567), large)
|
|
|
|
assert.Equal(t, i, pop)
|
2018-12-05 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assertFn(37890)
|
2018-12-05 14:19:25 +00:00
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
update := map[string]any{
|
|
|
|
"Hello": map[string]any{
|
2018-12-07 10:02:11 +00:00
|
|
|
"Pop": 1234,
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
"World": map[any]any{
|
2018-12-07 10:02:11 +00:00
|
|
|
"Rock": 345,
|
2018-12-05 14:19:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
err = v.MergeConfigMap(update)
|
|
|
|
require.NoError(t, err)
|
2018-12-05 14:19:25 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assert.Equal(t, 345, v.GetInt("world.rock"))
|
2018-12-07 10:02:11 +00:00
|
|
|
|
2023-10-07 19:28:36 +00:00
|
|
|
assertFn(1234)
|
2018-12-05 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 21:15:43 +00:00
|
|
|
func TestUnmarshalingWithAliases(t *testing.T) {
|
2016-10-08 08:00:18 +00:00
|
|
|
v := New()
|
|
|
|
v.SetDefault("ID", 1)
|
|
|
|
v.Set("name", "Steve")
|
|
|
|
v.Set("lastname", "Owen")
|
2016-01-20 21:15:43 +00:00
|
|
|
|
2016-10-08 08:00:18 +00:00
|
|
|
v.RegisterAlias("UserID", "ID")
|
|
|
|
v.RegisterAlias("Firstname", "name")
|
|
|
|
v.RegisterAlias("Surname", "lastname")
|
2016-01-20 21:15:43 +00:00
|
|
|
|
|
|
|
type config struct {
|
2016-10-08 08:00:18 +00:00
|
|
|
ID int
|
2016-01-20 21:15:43 +00:00
|
|
|
FirstName string
|
2015-12-30 06:11:39 +00:00
|
|
|
Surname string
|
2016-01-20 21:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
2016-10-08 08:00:18 +00:00
|
|
|
err := v.Unmarshal(&C)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "unable to decode into struct")
|
2016-01-20 21:15:43 +00:00
|
|
|
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, &config{ID: 1, FirstName: "Steve", Surname: "Owen"}, &C)
|
2015-12-30 06:11:39 +00:00
|
|
|
}
|
2016-08-05 07:18:19 +00:00
|
|
|
|
|
|
|
func TestSetConfigNameClearsFileCache(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
v.SetConfigFile("/tmp/config.yaml")
|
|
|
|
v.SetConfigName("default")
|
2016-10-13 11:33:30 +00:00
|
|
|
f, err := v.getConfigFile()
|
2023-10-07 19:28:36 +00:00
|
|
|
require.Error(t, err, "config file cache should have been cleared")
|
2016-10-13 11:33:30 +00:00
|
|
|
assert.Empty(t, f)
|
2016-08-05 07:18:19 +00:00
|
|
|
}
|
2016-09-19 17:37:56 +00:00
|
|
|
|
|
|
|
func TestShadowedNestedValue(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2016-10-10 11:40:38 +00:00
|
|
|
config := `name: steve
|
|
|
|
clothing:
|
|
|
|
jacket: leather
|
|
|
|
trousers: denim
|
|
|
|
pants:
|
|
|
|
size: large
|
|
|
|
`
|
2024-04-07 11:09:02 +00:00
|
|
|
initConfig("yaml", config, v)
|
2016-10-10 11:40:38 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "steve", v.GetString("name"))
|
2016-10-10 11:40:38 +00:00
|
|
|
|
2016-09-19 17:37:56 +00:00
|
|
|
polyester := "polyester"
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetDefault("clothing.shirt", polyester)
|
|
|
|
v.SetDefault("clothing.jacket.price", 100)
|
2016-10-08 08:00:18 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, "leather", v.GetString("clothing.jacket"))
|
|
|
|
assert.Nil(t, v.Get("clothing.jacket.price"))
|
|
|
|
assert.Equal(t, polyester, v.GetString("clothing.shirt"))
|
2016-10-08 08:00:18 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
clothingSettings := v.AllSettings()["clothing"].(map[string]any)
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, "leather", clothingSettings["jacket"])
|
|
|
|
assert.Equal(t, polyester, clothingSettings["shirt"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDotParameter(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetConfigType("json")
|
|
|
|
|
|
|
|
// Read the YAML data into Viper configuration
|
|
|
|
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading YAML data")
|
|
|
|
|
2023-08-18 13:29:46 +00:00
|
|
|
// should take precedence over batters defined in jsonExample
|
2016-10-08 08:00:18 +00:00
|
|
|
r := bytes.NewReader([]byte(`{ "batters.batter": [ { "type": "Small" } ] }`))
|
2024-04-07 11:09:02 +00:00
|
|
|
v.unmarshalReader(r, v.config)
|
2016-09-19 17:37:56 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
actual := v.Get("batters.batter")
|
2023-09-26 14:59:38 +00:00
|
|
|
expected := []any{map[string]any{"type": "Small"}}
|
2016-10-08 08:00:18 +00:00
|
|
|
assert.Equal(t, expected, actual)
|
2016-09-19 17:37:56 +00:00
|
|
|
}
|
2016-09-26 08:02:50 +00:00
|
|
|
|
2016-10-24 15:49:00 +00:00
|
|
|
func TestCaseInsensitive(t *testing.T) {
|
2016-10-10 11:40:38 +00:00
|
|
|
for _, config := range []struct {
|
|
|
|
typ string
|
|
|
|
content string
|
|
|
|
}{
|
|
|
|
{"yaml", `
|
|
|
|
aBcD: 1
|
|
|
|
eF:
|
|
|
|
gH: 2
|
|
|
|
iJk: 3
|
|
|
|
Lm:
|
|
|
|
nO: 4
|
|
|
|
P:
|
|
|
|
Q: 5
|
|
|
|
R: 6
|
|
|
|
`},
|
|
|
|
{"json", `{
|
|
|
|
"aBcD": 1,
|
|
|
|
"eF": {
|
|
|
|
"iJk": 3,
|
|
|
|
"Lm": {
|
|
|
|
"P": {
|
|
|
|
"Q": 5,
|
|
|
|
"R": 6
|
|
|
|
},
|
|
|
|
"nO": 4
|
|
|
|
},
|
|
|
|
"gH": 2
|
|
|
|
}
|
|
|
|
}`},
|
|
|
|
{"toml", `aBcD = 1
|
|
|
|
[eF]
|
|
|
|
gH = 2
|
|
|
|
iJk = 3
|
|
|
|
[eF.Lm]
|
|
|
|
nO = 4
|
|
|
|
[eF.Lm.P]
|
|
|
|
Q = 5
|
|
|
|
R = 6
|
|
|
|
`},
|
|
|
|
} {
|
2016-10-24 15:49:00 +00:00
|
|
|
doTestCaseInsensitive(t, config.typ, config.content)
|
2016-09-26 15:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:49:00 +00:00
|
|
|
func TestCaseInsensitiveSet(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2023-09-26 14:59:38 +00:00
|
|
|
m1 := map[string]any{
|
2016-10-24 15:49:00 +00:00
|
|
|
"Foo": 32,
|
2023-09-26 14:59:38 +00:00
|
|
|
"Bar": map[any]any{
|
2016-10-24 15:49:00 +00:00
|
|
|
"ABc": "A",
|
2020-09-11 15:48:38 +00:00
|
|
|
"cDE": "B",
|
|
|
|
},
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
m2 := map[string]any{
|
2016-10-24 15:49:00 +00:00
|
|
|
"Foo": 52,
|
2023-09-26 14:59:38 +00:00
|
|
|
"Bar": map[any]any{
|
2016-10-24 15:49:00 +00:00
|
|
|
"bCd": "A",
|
2020-09-11 15:48:38 +00:00
|
|
|
"eFG": "B",
|
|
|
|
},
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.Set("Given1", m1)
|
|
|
|
v.Set("Number1", 42)
|
2016-10-24 15:49:00 +00:00
|
|
|
|
2024-04-07 11:09:02 +00:00
|
|
|
v.SetDefault("Given2", m2)
|
|
|
|
v.SetDefault("Number2", 52)
|
2016-10-24 15:49:00 +00:00
|
|
|
|
|
|
|
// Verify SetDefault
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, 52, v.Get("number2"))
|
|
|
|
assert.Equal(t, 52, v.Get("given2.foo"))
|
|
|
|
assert.Equal(t, "A", v.Get("given2.bar.bcd"))
|
2023-10-07 19:28:36 +00:00
|
|
|
_, ok := m2["Foo"]
|
|
|
|
assert.True(t, ok)
|
2016-10-24 15:49:00 +00:00
|
|
|
|
|
|
|
// Verify Set
|
2024-04-07 11:09:02 +00:00
|
|
|
assert.Equal(t, 42, v.Get("number1"))
|
|
|
|
assert.Equal(t, 32, v.Get("given1.foo"))
|
|
|
|
assert.Equal(t, "A", v.Get("given1.bar.abc"))
|
2023-10-07 19:28:36 +00:00
|
|
|
_, ok = m1["Foo"]
|
|
|
|
assert.True(t, ok)
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 09:26:50 +00:00
|
|
|
func TestParseNested(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2017-04-10 09:26:50 +00:00
|
|
|
type duration struct {
|
|
|
|
Delay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type item struct {
|
|
|
|
Name string
|
|
|
|
Delay time.Duration
|
|
|
|
Nested duration
|
|
|
|
}
|
|
|
|
|
|
|
|
config := `[[parent]]
|
|
|
|
delay="100ms"
|
|
|
|
[parent.nested]
|
|
|
|
delay="200ms"
|
|
|
|
`
|
2024-04-07 11:09:02 +00:00
|
|
|
initConfig("toml", config, v)
|
2017-04-10 09:26:50 +00:00
|
|
|
|
|
|
|
var items []item
|
|
|
|
err := v.UnmarshalKey("parent", &items)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "unable to decode into struct")
|
2017-04-10 09:26:50 +00:00
|
|
|
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.Len(t, items, 1)
|
2017-04-10 09:26:50 +00:00
|
|
|
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
|
|
|
|
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:49:00 +00:00
|
|
|
func doTestCaseInsensitive(t *testing.T, typ, config string) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
|
|
|
initConfig(typ, config, v)
|
|
|
|
v.Set("RfD", true)
|
|
|
|
assert.Equal(t, true, v.Get("rfd"))
|
|
|
|
assert.Equal(t, true, v.Get("rFD"))
|
|
|
|
assert.Equal(t, 1, cast.ToInt(v.Get("abcd")))
|
|
|
|
assert.Equal(t, 1, cast.ToInt(v.Get("Abcd")))
|
|
|
|
assert.Equal(t, 2, cast.ToInt(v.Get("ef.gh")))
|
|
|
|
assert.Equal(t, 3, cast.ToInt(v.Get("ef.ijk")))
|
|
|
|
assert.Equal(t, 4, cast.ToInt(v.Get("ef.lm.no")))
|
|
|
|
assert.Equal(t, 5, cast.ToInt(v.Get("ef.lm.p.q")))
|
2016-10-10 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:51:58 +00:00
|
|
|
func newViperWithConfigFile(t *testing.T) (*Viper, string) {
|
2023-09-13 14:56:49 +00:00
|
|
|
watchDir := t.TempDir()
|
2023-12-01 19:51:58 +00:00
|
|
|
configFile := path.Join(watchDir, "config.yaml")
|
2023-09-13 14:56:49 +00:00
|
|
|
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-01 19:51:58 +00:00
|
|
|
v := New()
|
2018-01-03 09:37:18 +00:00
|
|
|
v.SetConfigFile(configFile)
|
|
|
|
err = v.ReadInConfig()
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
require.Equal(t, "bar", v.Get("foo"))
|
2023-09-13 14:56:49 +00:00
|
|
|
return v, configFile
|
2018-01-03 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:51:58 +00:00
|
|
|
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
|
|
|
|
watchDir := t.TempDir()
|
2018-01-03 09:37:18 +00:00
|
|
|
dataDir1 := path.Join(watchDir, "data1")
|
2023-09-13 14:56:49 +00:00
|
|
|
err := os.Mkdir(dataDir1, 0o777)
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
realConfigFile := path.Join(dataDir1, "config.yaml")
|
|
|
|
t.Logf("Real config file location: %s\n", realConfigFile)
|
2023-09-13 14:56:49 +00:00
|
|
|
err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
// now, symlink the tm `data1` dir to `data` in the baseDir
|
|
|
|
os.Symlink(dataDir1, path.Join(watchDir, "data"))
|
|
|
|
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
|
2023-12-01 19:51:58 +00:00
|
|
|
configFile := path.Join(watchDir, "config.yaml")
|
2018-01-03 09:37:18 +00:00
|
|
|
os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
|
2018-09-28 08:04:19 +00:00
|
|
|
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))
|
2018-01-03 09:37:18 +00:00
|
|
|
// init Viper
|
2023-12-01 19:51:58 +00:00
|
|
|
v := New()
|
2018-01-03 09:37:18 +00:00
|
|
|
v.SetConfigFile(configFile)
|
|
|
|
err = v.ReadInConfig()
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
require.Equal(t, "bar", v.Get("foo"))
|
2023-09-13 14:56:49 +00:00
|
|
|
return v, watchDir, configFile
|
2018-01-03 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchFile(t *testing.T) {
|
2018-12-05 15:27:02 +00:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
// TODO(bep) FIX ME
|
|
|
|
t.Skip("Skip test on Linux ...")
|
|
|
|
}
|
2018-05-24 08:09:29 +00:00
|
|
|
|
2018-01-03 09:37:18 +00:00
|
|
|
t.Run("file content changed", func(t *testing.T) {
|
|
|
|
// given a `config.yaml` file being watched
|
2023-09-13 14:56:49 +00:00
|
|
|
v, configFile := newViperWithConfigFile(t)
|
2018-08-06 07:29:13 +00:00
|
|
|
_, err := os.Stat(configFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("test config file: %s\n", configFile)
|
2018-01-03 09:37:18 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2018-07-13 08:30:23 +00:00
|
|
|
wg.Add(1)
|
2021-09-22 08:39:19 +00:00
|
|
|
var wgDoneOnce sync.Once // OnConfigChange is called twice on Windows
|
2024-02-15 11:07:55 +00:00
|
|
|
v.OnConfigChange(func(_ fsnotify.Event) {
|
2018-01-03 09:37:18 +00:00
|
|
|
t.Logf("config file changed")
|
2021-09-22 08:39:19 +00:00
|
|
|
wgDoneOnce.Do(func() {
|
|
|
|
wg.Done()
|
|
|
|
})
|
2018-01-03 09:37:18 +00:00
|
|
|
})
|
2018-08-06 07:29:13 +00:00
|
|
|
v.WatchConfig()
|
2018-01-03 09:37:18 +00:00
|
|
|
// when overwriting the file and waiting for the custom change notification handler to be triggered
|
2023-09-13 14:56:49 +00:00
|
|
|
err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
|
2018-01-03 09:37:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
// then the config value should have changed
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
assert.Equal(t, "baz", v.Get("foo"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("link to real file changed (à la Kubernetes)", func(t *testing.T) {
|
|
|
|
// skip if not executed on Linux
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...")
|
|
|
|
}
|
2023-09-13 14:56:49 +00:00
|
|
|
v, watchDir, _ := newViperWithSymlinkedConfigFile(t)
|
2018-01-03 09:37:18 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
v.WatchConfig()
|
2024-02-15 11:07:55 +00:00
|
|
|
v.OnConfigChange(func(_ fsnotify.Event) {
|
2018-01-03 09:37:18 +00:00
|
|
|
t.Logf("config file changed")
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
wg.Add(1)
|
|
|
|
// when link to another `config.yaml` file
|
|
|
|
dataDir2 := path.Join(watchDir, "data2")
|
2021-09-15 19:12:02 +00:00
|
|
|
err := os.Mkdir(dataDir2, 0o777)
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
configFile2 := path.Join(dataDir2, "config.yaml")
|
2023-09-13 14:56:49 +00:00
|
|
|
err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
// change the symlink using the `ln -sfn` command
|
|
|
|
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
// then
|
2023-09-25 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-03 09:37:18 +00:00
|
|
|
assert.Equal(t, "baz", v.Get("foo"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:09:18 +00:00
|
|
|
func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) {
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("foo.bar", "cobra_flag", "")
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
assert.NoError(t, v.BindPFlags(flags))
|
|
|
|
|
|
|
|
config := &struct {
|
|
|
|
Foo struct {
|
|
|
|
Bar string
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
|
|
|
|
assert.NoError(t, v.Unmarshal(config))
|
|
|
|
assert.Equal(t, "cobra_flag", config.Foo.Bar)
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:02:57 +00:00
|
|
|
// var yamlExampleWithDot = []byte(`Hacker: true
|
|
|
|
// name: steve
|
|
|
|
// hobbies:
|
|
|
|
// - skateboarding
|
|
|
|
// - snowboarding
|
|
|
|
// - go
|
|
|
|
// clothing:
|
|
|
|
// jacket: leather
|
|
|
|
// trousers: denim
|
|
|
|
// pants:
|
|
|
|
// size: large
|
|
|
|
// age: 35
|
|
|
|
// eyes : brown
|
|
|
|
// beard: true
|
|
|
|
// emails:
|
|
|
|
// steve@hacker.com:
|
|
|
|
// created: 01/02/03
|
|
|
|
// active: true
|
|
|
|
// `)
|
2019-11-06 11:40:41 +00:00
|
|
|
|
2019-11-08 13:23:20 +00:00
|
|
|
func TestKeyDelimiter(t *testing.T) {
|
|
|
|
v := NewWithOptions(KeyDelimiter("::"))
|
2019-11-06 11:40:41 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
r := strings.NewReader(string(yamlExampleWithDot))
|
|
|
|
|
|
|
|
err := v.unmarshalReader(r, v.config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
values := map[string]any{
|
|
|
|
"image": map[string]any{
|
2019-11-06 11:40:41 +00:00
|
|
|
"repository": "someImage",
|
|
|
|
"tag": "1.0.0",
|
|
|
|
},
|
2023-09-26 14:59:38 +00:00
|
|
|
"ingress": map[string]any{
|
|
|
|
"annotations": map[string]any{
|
2019-11-06 11:40:41 +00:00
|
|
|
"traefik.frontend.rule.type": "PathPrefix",
|
|
|
|
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetDefault("charts::values", values)
|
|
|
|
|
|
|
|
assert.Equal(t, "leather", v.GetString("clothing::jacket"))
|
|
|
|
assert.Equal(t, "01/02/03", v.GetString("emails::steve@hacker.com::created"))
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Charts struct {
|
2023-09-26 14:59:38 +00:00
|
|
|
Values map[string]any
|
2019-11-06 11:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := config{
|
|
|
|
Charts: struct {
|
2023-09-26 14:59:38 +00:00
|
|
|
Values map[string]any
|
2019-11-06 11:40:41 +00:00
|
|
|
}{
|
|
|
|
Values: values,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual config
|
|
|
|
|
|
|
|
assert.NoError(t, v.Unmarshal(&actual))
|
|
|
|
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:07:34 +00:00
|
|
|
var yamlDeepNestedSlices = []byte(`TV:
|
2022-06-23 21:34:55 +00:00
|
|
|
- title: "The Expanse"
|
|
|
|
title_i18n:
|
|
|
|
USA: "The Expanse"
|
|
|
|
Japan: "エクスパンス -巨獣めざめる-"
|
2020-10-04 18:07:34 +00:00
|
|
|
seasons:
|
|
|
|
- first_released: "December 14, 2015"
|
|
|
|
episodes:
|
|
|
|
- title: "Dulcinea"
|
|
|
|
air_date: "December 14, 2015"
|
|
|
|
- title: "The Big Empty"
|
|
|
|
air_date: "December 15, 2015"
|
|
|
|
- title: "Remember the Cant"
|
|
|
|
air_date: "December 22, 2015"
|
|
|
|
- first_released: "February 1, 2017"
|
|
|
|
episodes:
|
|
|
|
- title: "Safe"
|
|
|
|
air_date: "February 1, 2017"
|
|
|
|
- title: "Doors & Corners"
|
|
|
|
air_date: "February 1, 2017"
|
|
|
|
- title: "Static"
|
|
|
|
air_date: "February 8, 2017"
|
|
|
|
episodes:
|
|
|
|
- ["Dulcinea", "The Big Empty", "Remember the Cant"]
|
|
|
|
- ["Safe", "Doors & Corners", "Static"]
|
|
|
|
`)
|
|
|
|
|
|
|
|
func TestSliceIndexAccess(t *testing.T) {
|
2024-04-07 11:09:02 +00:00
|
|
|
v := New()
|
2020-10-04 18:07:34 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
r := strings.NewReader(string(yamlDeepNestedSlices))
|
|
|
|
|
|
|
|
err := v.unmarshalReader(r, v.config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-23 21:34:55 +00:00
|
|
|
assert.Equal(t, "The Expanse", v.GetString("tv.0.title"))
|
2020-10-04 18:07:34 +00:00
|
|
|
assert.Equal(t, "February 1, 2017", v.GetString("tv.0.seasons.1.first_released"))
|
|
|
|
assert.Equal(t, "Static", v.GetString("tv.0.seasons.1.episodes.2.title"))
|
|
|
|
assert.Equal(t, "December 15, 2015", v.GetString("tv.0.seasons.0.episodes.1.air_date"))
|
|
|
|
|
2022-06-23 21:34:55 +00:00
|
|
|
// Test nested keys with capital letters
|
|
|
|
assert.Equal(t, "The Expanse", v.GetString("tv.0.title_i18n.USA"))
|
|
|
|
assert.Equal(t, "エクスパンス -巨獣めざめる-", v.GetString("tv.0.title_i18n.Japan"))
|
|
|
|
|
2020-10-04 18:07:34 +00:00
|
|
|
// Test for index out of bounds
|
|
|
|
assert.Equal(t, "", v.GetString("tv.0.seasons.2.first_released"))
|
|
|
|
|
|
|
|
// Accessing multidimensional arrays
|
|
|
|
assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2"))
|
|
|
|
}
|
|
|
|
|
2023-12-06 13:41:36 +00:00
|
|
|
func TestIsPathShadowedInFlatMap(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
stringMap := map[string]string{
|
|
|
|
"foo": "value",
|
|
|
|
}
|
|
|
|
|
|
|
|
flagMap := map[string]FlagValue{
|
|
|
|
"foo": pflagValue{},
|
|
|
|
}
|
|
|
|
|
|
|
|
path1 := []string{"foo", "bar"}
|
|
|
|
expected1 := "foo"
|
|
|
|
|
|
|
|
// "foo.bar" should shadowed by "foo"
|
|
|
|
assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, stringMap))
|
|
|
|
assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, flagMap))
|
|
|
|
|
|
|
|
path2 := []string{"bar", "foo"}
|
|
|
|
expected2 := ""
|
|
|
|
|
|
|
|
// "bar.foo" should not shadowed by "foo"
|
|
|
|
assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, stringMap))
|
|
|
|
assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, flagMap))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagShadow(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetDefault("foo.bar1.bar2", "default")
|
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("foo.bar1", "shadowed", "")
|
|
|
|
flags.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Changed = true
|
|
|
|
})
|
|
|
|
|
|
|
|
v.BindPFlags(flags)
|
|
|
|
|
|
|
|
assert.Equal(t, "shadowed", v.GetString("foo.bar1"))
|
|
|
|
// the default "foo.bar1.bar2" value should shadowed by flag "foo.bar1" value
|
|
|
|
// and should return an empty string
|
|
|
|
assert.Equal(t, "", v.GetString("foo.bar1.bar2"))
|
|
|
|
}
|
|
|
|
|
2016-09-26 08:02:50 +00:00
|
|
|
func BenchmarkGetBool(b *testing.B) {
|
|
|
|
key := "BenchmarkGetBool"
|
|
|
|
v = New()
|
|
|
|
v.Set(key, true)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !v.GetBool(key) {
|
|
|
|
b.Fatal("GetBool returned false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 09:47:23 +00:00
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
key := "BenchmarkGet"
|
|
|
|
v = New()
|
|
|
|
v.Set(key, true)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !v.Get(key).(bool) {
|
|
|
|
b.Fatal("Get returned false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:48:45 +00:00
|
|
|
// BenchmarkGetBoolFromMap is the "perfect result" for the above.
|
2016-09-26 08:02:50 +00:00
|
|
|
func BenchmarkGetBoolFromMap(b *testing.B) {
|
|
|
|
m := make(map[string]bool)
|
|
|
|
key := "BenchmarkGetBool"
|
|
|
|
m[key] = true
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !m[key] {
|
|
|
|
b.Fatal("Map value was false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 09:06:45 +00:00
|
|
|
|
|
|
|
// Skip some tests on Windows that kept failing when Windows was added to the CI as a target.
|
|
|
|
func skipWindows(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Skip test on Windows")
|
|
|
|
}
|
|
|
|
}
|