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"
|
2015-08-02 00:37:27 +00:00
|
|
|
"io/ioutil"
|
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"
|
2014-09-27 21:00:51 +00:00
|
|
|
"sort"
|
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"
|
2018-06-28 09:55:33 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var yamlExample = []byte(`Hacker: true
|
|
|
|
name: steve
|
|
|
|
hobbies:
|
|
|
|
- skateboarding
|
|
|
|
- snowboarding
|
|
|
|
- go
|
2014-05-29 20:48:24 +00:00
|
|
|
clothing:
|
|
|
|
jacket: leather
|
|
|
|
trousers: denim
|
2015-05-27 15:15:02 +00:00
|
|
|
pants:
|
|
|
|
size: large
|
2014-08-05 11:35:21 +00:00
|
|
|
age: 35
|
2014-10-09 20:39:24 +00:00
|
|
|
eyes : brown
|
2014-08-05 11:35:21 +00:00
|
|
|
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" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
2015-12-11 22:51:11 +00:00
|
|
|
var hclExample = []byte(`
|
|
|
|
id = "0001"
|
|
|
|
type = "donut"
|
|
|
|
name = "Cake"
|
|
|
|
ppu = 0.55
|
2015-12-13 20:37:32 +00:00
|
|
|
foos {
|
|
|
|
foo {
|
|
|
|
key = 1
|
|
|
|
}
|
|
|
|
foo {
|
|
|
|
key = 2
|
|
|
|
}
|
|
|
|
foo {
|
|
|
|
key = 3
|
|
|
|
}
|
|
|
|
foo {
|
|
|
|
key = 4
|
|
|
|
}
|
|
|
|
}`)
|
2015-12-11 22:51:11 +00:00
|
|
|
|
2015-04-14 18:15:02 +00:00
|
|
|
var propertiesExample = []byte(`
|
|
|
|
p_id: 0001
|
|
|
|
p_type: donut
|
|
|
|
p_name: Cake
|
|
|
|
p_ppu: 0.55
|
|
|
|
p_batters.batter.type: Regular
|
|
|
|
`)
|
|
|
|
|
2014-10-27 19:32:46 +00:00
|
|
|
var remoteExample = []byte(`{
|
|
|
|
"id":"0002",
|
|
|
|
"type":"cronut",
|
|
|
|
"newkey":"remote"
|
|
|
|
}`)
|
|
|
|
|
2019-12-03 15:46:17 +00:00
|
|
|
var iniExample = []byte(`; Package name
|
|
|
|
NAME = ini
|
|
|
|
; Package version
|
|
|
|
VERSION = v1
|
|
|
|
; Package import path
|
|
|
|
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
|
|
|
|
|
|
|
|
# Information about package author
|
|
|
|
# Bio can be written in multiple lines.
|
|
|
|
[author]
|
2019-12-06 11:48:55 +00:00
|
|
|
NAME = Unknown ; Succeeding comment
|
2019-12-03 15:46:17 +00:00
|
|
|
E-MAIL = fake@localhost
|
|
|
|
GITHUB = https://github.com/%(NAME)s
|
|
|
|
BIO = """Gopher.
|
|
|
|
Coding addict.
|
|
|
|
Good man.
|
|
|
|
""" # Succeeding comment`)
|
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
func initConfigs() {
|
|
|
|
Reset()
|
2016-10-08 08:00:18 +00:00
|
|
|
var r io.Reader
|
2014-12-22 23:31:11 +00:00
|
|
|
SetConfigType("yaml")
|
2016-10-08 08:00:18 +00:00
|
|
|
r = bytes.NewReader(yamlExample)
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
|
|
|
SetConfigType("json")
|
|
|
|
r = bytes.NewReader(jsonExample)
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2015-12-11 23:27:07 +00:00
|
|
|
SetConfigType("hcl")
|
|
|
|
r = bytes.NewReader(hclExample)
|
|
|
|
unmarshalReader(r, v.config)
|
|
|
|
|
2015-04-14 18:15:02 +00:00
|
|
|
SetConfigType("properties")
|
|
|
|
r = bytes.NewReader(propertiesExample)
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2015-04-14 18:15:02 +00:00
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
SetConfigType("toml")
|
|
|
|
r = bytes.NewReader(tomlExample)
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2019-06-11 20:47:43 +00:00
|
|
|
SetConfigType("env")
|
|
|
|
r = bytes.NewReader(dotenvExample)
|
|
|
|
unmarshalReader(r, v.config)
|
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
SetConfigType("json")
|
|
|
|
remote := bytes.NewReader(remoteExample)
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(remote, v.kvstore)
|
2019-12-03 15:46:17 +00:00
|
|
|
|
|
|
|
SetConfigType("ini")
|
|
|
|
r = bytes.NewReader(iniExample)
|
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:40:38 +00:00
|
|
|
func initConfig(typ, config string) {
|
2014-12-22 23:31:11 +00:00
|
|
|
Reset()
|
2016-10-10 11:40:38 +00:00
|
|
|
SetConfigType(typ)
|
|
|
|
r := strings.NewReader(config)
|
2014-12-22 23:31:11 +00:00
|
|
|
|
2016-10-10 11:40:38 +00:00
|
|
|
if err := unmarshalReader(r, v.config); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initYAML() {
|
|
|
|
initConfig("yaml", string(yamlExample))
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initJSON() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("json")
|
|
|
|
r := bytes.NewReader(jsonExample)
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:15:02 +00:00
|
|
|
func initProperties() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("properties")
|
|
|
|
r := bytes.NewReader(propertiesExample)
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2015-04-14 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
func initTOML() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("toml")
|
|
|
|
r := bytes.NewReader(tomlExample)
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-12-22 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:47:43 +00:00
|
|
|
func initDotEnv() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("env")
|
|
|
|
r := bytes.NewReader(dotenvExample)
|
|
|
|
|
|
|
|
unmarshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2015-12-11 22:51:11 +00:00
|
|
|
func initHcl() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("hcl")
|
|
|
|
r := bytes.NewReader(hclExample)
|
|
|
|
|
|
|
|
unmarshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:46:17 +00:00
|
|
|
func initIni() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("ini")
|
|
|
|
r := bytes.NewReader(iniExample)
|
|
|
|
|
|
|
|
unmarshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2015-08-02 00:37:27 +00:00
|
|
|
// make directories for testing
|
|
|
|
func initDirs(t *testing.T) (string, string, func()) {
|
|
|
|
var (
|
2019-03-16 11:10:32 +00:00
|
|
|
testDirs = []string{`a a`, `b`, `C_`}
|
2015-08-02 00:37:27 +00:00
|
|
|
config = `improbable`
|
|
|
|
)
|
|
|
|
|
2019-03-16 11:10:32 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
testDirs = append(testDirs, `d\d`)
|
|
|
|
}
|
|
|
|
|
2015-08-02 00:37:27 +00:00
|
|
|
root, err := ioutil.TempDir("", "")
|
2019-03-16 11:10:32 +00:00
|
|
|
require.NoError(t, err, "Failed to create temporary directory")
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
cleanup := true
|
|
|
|
defer func() {
|
|
|
|
if cleanup {
|
|
|
|
os.Chdir("..")
|
|
|
|
os.RemoveAll(root)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = os.Chdir(root)
|
2019-03-16 11:10:32 +00:00
|
|
|
require.Nil(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
|
|
|
|
for _, dir := range testDirs {
|
|
|
|
err = os.Mkdir(dir, 0750)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
path.Join(dir, config+".toml"),
|
|
|
|
[]byte("key = \"value is "+dir+"\"\n"),
|
|
|
|
0640)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = false
|
|
|
|
return root, config, func() {
|
|
|
|
os.Chdir("..")
|
|
|
|
os.RemoveAll(root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:09:18 +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
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestBasics(t *testing.T) {
|
|
|
|
SetConfigFile("/tmp/config.yaml")
|
2016-10-13 11:33:30 +00:00
|
|
|
filename, err := v.getConfigFile()
|
|
|
|
assert.Equal(t, "/tmp/config.yaml", filename)
|
|
|
|
assert.NoError(t, err)
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 18:23:50 +00:00
|
|
|
func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) {
|
|
|
|
filename := ".dotfilenoext"
|
|
|
|
path := "/tmp"
|
|
|
|
file := filepath.Join(path, filename)
|
|
|
|
SetConfigName(filename)
|
|
|
|
AddConfigPath(path)
|
|
|
|
_, createErr := v.fs.Create(file)
|
|
|
|
defer func() {
|
|
|
|
_ = v.fs.Remove(file)
|
|
|
|
}()
|
|
|
|
assert.NoError(t, createErr)
|
|
|
|
_, err := v.getConfigFile()
|
|
|
|
// unless config type is set, files without extension
|
|
|
|
// are not considered
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:54:13 +00:00
|
|
|
func TestSearchInPath(t *testing.T) {
|
|
|
|
filename := ".dotfilenoext"
|
|
|
|
path := "/tmp"
|
|
|
|
file := filepath.Join(path, filename)
|
|
|
|
SetConfigName(filename)
|
2020-01-16 18:23:50 +00:00
|
|
|
SetConfigType("yaml")
|
2019-11-06 13:54:13 +00:00
|
|
|
AddConfigPath(path)
|
|
|
|
_, createErr := v.fs.Create(file)
|
|
|
|
defer func() {
|
|
|
|
_ = v.fs.Remove(file)
|
|
|
|
}()
|
|
|
|
assert.NoError(t, createErr)
|
|
|
|
filename, err := v.getConfigFile()
|
|
|
|
assert.Equal(t, file, filename)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:23:59 +00:00
|
|
|
func TestSearchInPath_FilesOnly(t *testing.T) {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
|
|
|
|
err := fs.Mkdir("/tmp/config", 0777)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = fs.Create("/tmp/config/config.yaml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
|
|
|
|
v.SetFs(fs)
|
|
|
|
v.AddConfigPath("/tmp")
|
|
|
|
v.AddConfigPath("/tmp/config")
|
|
|
|
|
|
|
|
filename, err := v.getConfigFile()
|
|
|
|
assert.Equal(t, "/tmp/config/config.yaml", filename)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
SetDefault("age", 45)
|
|
|
|
assert.Equal(t, 45, Get("age"))
|
2015-10-13 22:31:32 +00:00
|
|
|
|
|
|
|
SetDefault("clothing.jacket", "slacks")
|
|
|
|
assert.Equal(t, "slacks", Get("clothing.jacket"))
|
|
|
|
|
|
|
|
SetConfigType("yaml")
|
|
|
|
err := ReadConfig(bytes.NewBuffer(yamlExample))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "leather", Get("clothing.jacket"))
|
2014-04-04 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:26:31 +00:00
|
|
|
func TestUnmarshaling(t *testing.T) {
|
2014-04-04 21:21:59 +00:00
|
|
|
SetConfigType("yaml")
|
|
|
|
r := bytes.NewReader(yamlExample)
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(r, v.config)
|
2014-04-04 21:21:59 +00:00
|
|
|
assert.True(t, InConfig("name"))
|
|
|
|
assert.False(t, InConfig("state"))
|
|
|
|
assert.Equal(t, "steve", Get("name"))
|
|
|
|
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies"))
|
2016-10-14 09:24:45 +00:00
|
|
|
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, Get("clothing"))
|
2014-04-04 21:21:59 +00:00
|
|
|
assert.Equal(t, 35, Get("age"))
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:11:39 +00:00
|
|
|
func TestUnmarshalExact(t *testing.T) {
|
|
|
|
vip := New()
|
|
|
|
target := &testUnmarshalExtra{}
|
|
|
|
vip.SetConfigType("yaml")
|
|
|
|
r := bytes.NewReader(yamlExampleWithExtras)
|
|
|
|
vip.ReadConfig(r)
|
|
|
|
err := vip.UnmarshalExact(target)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("UnmarshalExact should error when populating a struct from a conf that contains unused fields")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestOverrides(t *testing.T) {
|
|
|
|
Set("age", 40)
|
|
|
|
assert.Equal(t, 40, Get("age"))
|
|
|
|
}
|
|
|
|
|
2021-02-21 20:00:11 +00:00
|
|
|
func TestUnset(t *testing.T) {
|
|
|
|
SetDefault("unset", 20)
|
|
|
|
Set("unset", 10)
|
|
|
|
RegisterAlias("unset_alias", "unset")
|
|
|
|
|
|
|
|
Unset("unset")
|
|
|
|
|
|
|
|
assert.Equal(t, nil, Get("unset"))
|
|
|
|
assert.Equal(t, nil, Get("unset_alias"))
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestDefaultPost(t *testing.T) {
|
|
|
|
assert.NotEqual(t, "NYC", Get("state"))
|
|
|
|
SetDefault("state", "NYC")
|
|
|
|
assert.Equal(t, "NYC", Get("state"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliases(t *testing.T) {
|
|
|
|
RegisterAlias("years", "age")
|
|
|
|
assert.Equal(t, 40, Get("years"))
|
|
|
|
Set("years", 45)
|
|
|
|
assert.Equal(t, 45, Get("age"))
|
|
|
|
}
|
|
|
|
|
2014-08-05 11:35:21 +00:00
|
|
|
func TestAliasInConfigFile(t *testing.T) {
|
|
|
|
// the config file specifies "beard". If we make this an alias for
|
|
|
|
// "hasbeard", we still want the old config file to work with beard.
|
|
|
|
RegisterAlias("beard", "hasbeard")
|
|
|
|
assert.Equal(t, true, Get("hasbeard"))
|
|
|
|
Set("hasbeard", false)
|
|
|
|
assert.Equal(t, false, Get("beard"))
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:40:14 +00:00
|
|
|
func TestYML(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initYAML()
|
2014-05-08 14:40:14 +00:00
|
|
|
assert.Equal(t, "steve", Get("name"))
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestJSON(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initJSON()
|
2014-04-04 21:21:59 +00:00
|
|
|
assert.Equal(t, "0001", Get("id"))
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:15:02 +00:00
|
|
|
func TestProperties(t *testing.T) {
|
|
|
|
initProperties()
|
|
|
|
assert.Equal(t, "0001", Get("p_id"))
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:21:59 +00:00
|
|
|
func TestTOML(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initTOML()
|
2014-04-04 21:21:59 +00:00
|
|
|
assert.Equal(t, "TOML Example", Get("title"))
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:47:43 +00:00
|
|
|
func TestDotEnv(t *testing.T) {
|
|
|
|
initDotEnv()
|
|
|
|
assert.Equal(t, "DotEnv Example", Get("title_dotenv"))
|
|
|
|
}
|
|
|
|
|
2015-12-11 22:51:11 +00:00
|
|
|
func TestHCL(t *testing.T) {
|
|
|
|
initHcl()
|
|
|
|
assert.Equal(t, "0001", Get("id"))
|
|
|
|
assert.Equal(t, 0.55, Get("ppu"))
|
|
|
|
assert.Equal(t, "donut", Get("type"))
|
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
Set("id", "0002")
|
|
|
|
assert.Equal(t, "0002", Get("id"))
|
|
|
|
assert.NotEqual(t, "cronut", Get("type"))
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:46:17 +00:00
|
|
|
func TestIni(t *testing.T) {
|
|
|
|
initIni()
|
|
|
|
assert.Equal(t, "ini", Get("default.name"))
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:32:46 +00:00
|
|
|
func TestRemotePrecedence(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initJSON()
|
|
|
|
|
2014-10-27 19:32:46 +00:00
|
|
|
remote := bytes.NewReader(remoteExample)
|
|
|
|
assert.Equal(t, "0001", Get("id"))
|
2015-08-24 03:40:56 +00:00
|
|
|
unmarshalReader(remote, v.kvstore)
|
2014-10-27 19:32:46 +00:00
|
|
|
assert.Equal(t, "0001", Get("id"))
|
|
|
|
assert.NotEqual(t, "cronut", Get("type"))
|
|
|
|
assert.Equal(t, "remote", Get("newkey"))
|
|
|
|
Set("newkey", "newvalue")
|
|
|
|
assert.NotEqual(t, "remote", Get("newkey"))
|
|
|
|
assert.Equal(t, "newvalue", Get("newkey"))
|
|
|
|
Set("newkey", "remote")
|
|
|
|
}
|
|
|
|
|
2014-09-27 21:03:00 +00:00
|
|
|
func TestEnv(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initJSON()
|
|
|
|
|
2014-09-27 21:03:00 +00:00
|
|
|
BindEnv("id")
|
2020-09-10 10:08:26 +00:00
|
|
|
BindEnv("f", "FOOD", "OLD_FOOD")
|
2014-09-27 21:03:00 +00:00
|
|
|
|
2020-09-30 11:02:07 +00:00
|
|
|
testutil.Setenv(t, "ID", "13")
|
|
|
|
testutil.Setenv(t, "FOOD", "apple")
|
|
|
|
testutil.Setenv(t, "OLD_FOOD", "banana")
|
|
|
|
testutil.Setenv(t, "NAME", "crunk")
|
2014-09-27 21:03:00 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "13", Get("id"))
|
|
|
|
assert.Equal(t, "apple", Get("f"))
|
2014-09-27 21:01:11 +00:00
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
|
|
|
|
assert.Equal(t, "crunk", Get("name"))
|
2014-09-27 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 10:08:26 +00:00
|
|
|
func TestMultipleEnv(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
|
|
|
|
BindEnv("f", "FOOD", "OLD_FOOD")
|
|
|
|
|
2020-09-30 11:02:07 +00:00
|
|
|
testutil.Setenv(t, "OLD_FOOD", "banana")
|
2020-09-10 10:08:26 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "banana", Get("f"))
|
|
|
|
}
|
|
|
|
|
2018-11-06 21:53:21 +00:00
|
|
|
func TestEmptyEnv(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
|
|
|
|
BindEnv("type") // Empty environment variable
|
|
|
|
BindEnv("name") // Bound, but not set environment variable
|
|
|
|
|
2020-09-30 11:02:07 +00:00
|
|
|
testutil.Setenv(t, "TYPE", "")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "donut", Get("type"))
|
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyEnv_Allowed(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
|
|
|
|
AllowEmptyEnv(true)
|
|
|
|
|
|
|
|
BindEnv("type") // Empty environment variable
|
|
|
|
BindEnv("name") // Bound, but not set environment variable
|
|
|
|
|
2020-09-30 11:02:07 +00:00
|
|
|
testutil.Setenv(t, "TYPE", "")
|
2018-11-06 21:53:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "", Get("type"))
|
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
}
|
|
|
|
|
2014-12-22 23:31:11 +00:00
|
|
|
func TestEnvPrefix(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
|
|
|
|
SetEnvPrefix("foo") // will be uppercased automatically
|
|
|
|
BindEnv("id")
|
|
|
|
BindEnv("f", "FOOD") // not using prefix
|
|
|
|
|
2020-09-30 11:02:07 +00:00
|
|
|
testutil.Setenv(t, "FOO_ID", "13")
|
|
|
|
testutil.Setenv(t, "FOOD", "apple")
|
|
|
|
testutil.Setenv(t, "FOO_NAME", "crunk")
|
2014-12-22 23:31:11 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "13", Get("id"))
|
|
|
|
assert.Equal(t, "apple", Get("f"))
|
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
|
|
|
|
assert.Equal(t, "crunk", Get("name"))
|
|
|
|
}
|
|
|
|
|
2015-02-17 04:32:10 +00:00
|
|
|
func TestAutoEnv(t *testing.T) {
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
testutil.Setenv(t, "FOO_BAR", "13")
|
|
|
|
|
2015-02-17 04:32:10 +00:00
|
|
|
assert.Equal(t, "13", Get("foo_bar"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoEnvWithPrefix(t *testing.T) {
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
SetEnvPrefix("Baz")
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
testutil.Setenv(t, "BAZ_BAR", "13")
|
|
|
|
|
2015-02-17 04:32:10 +00:00
|
|
|
assert.Equal(t, "13", Get("bar"))
|
|
|
|
}
|
|
|
|
|
2017-10-20 10:22:28 +00:00
|
|
|
func TestSetEnvKeyReplacer(t *testing.T) {
|
2015-03-06 19:21:17 +00:00
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
testutil.Setenv(t, "REFRESH_INTERVAL", "30s")
|
2015-03-06 19:21:17 +00:00
|
|
|
|
|
|
|
replacer := strings.NewReplacer("-", "_")
|
|
|
|
SetEnvKeyReplacer(replacer)
|
|
|
|
|
|
|
|
assert.Equal(t, "30s", Get("refresh-interval"))
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:11:31 +00:00
|
|
|
func TestEnvKeyReplacer(t *testing.T) {
|
|
|
|
v := NewWithOptions(EnvKeyReplacer(strings.NewReplacer("-", "_")))
|
|
|
|
|
|
|
|
v.AutomaticEnv()
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
testutil.Setenv(t, "REFRESH_INTERVAL", "30s")
|
2019-12-06 13:11:31 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
|
|
|
}
|
|
|
|
|
2014-09-27 21:00:51 +00:00
|
|
|
func TestAllKeys(t *testing.T) {
|
2014-12-22 23:31:11 +00:00
|
|
|
initConfigs()
|
|
|
|
|
2019-03-16 11:10:32 +00:00
|
|
|
ks := sort.StringSlice{
|
|
|
|
"title",
|
2019-12-03 15:46:17 +00:00
|
|
|
"author.bio",
|
|
|
|
"author.e-mail",
|
|
|
|
"author.github",
|
|
|
|
"author.name",
|
2019-03-16 11:10:32 +00:00
|
|
|
"newkey",
|
|
|
|
"owner.organization",
|
|
|
|
"owner.dob",
|
|
|
|
"owner.bio",
|
|
|
|
"name",
|
|
|
|
"beard",
|
|
|
|
"ppu",
|
|
|
|
"batters.batter",
|
|
|
|
"hobbies",
|
|
|
|
"clothing.jacket",
|
|
|
|
"clothing.trousers",
|
2019-12-03 15:46:17 +00:00
|
|
|
"default.import_path",
|
|
|
|
"default.name",
|
|
|
|
"default.version",
|
2019-03-16 11:10:32 +00:00
|
|
|
"clothing.pants.size",
|
|
|
|
"age",
|
|
|
|
"hacker",
|
|
|
|
"id",
|
|
|
|
"type",
|
|
|
|
"eyes",
|
|
|
|
"p_id",
|
|
|
|
"p_ppu",
|
|
|
|
"p_batters.batter.type",
|
|
|
|
"p_type",
|
|
|
|
"p_name",
|
|
|
|
"foos",
|
|
|
|
"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")
|
2019-03-16 11:10:32 +00:00
|
|
|
all := map[string]interface{}{
|
|
|
|
"owner": map[string]interface{}{
|
|
|
|
"organization": "MongoDB",
|
|
|
|
"bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
|
|
|
"dob": dob,
|
|
|
|
},
|
|
|
|
"title": "TOML Example",
|
2019-12-03 15:46:17 +00:00
|
|
|
"author": map[string]interface{}{
|
|
|
|
"e-mail": "fake@localhost",
|
2019-12-06 11:48:55 +00:00
|
|
|
"github": "https://github.com/Unknown",
|
|
|
|
"name": "Unknown",
|
2019-12-03 15:46:17 +00:00
|
|
|
"bio": "Gopher.\nCoding addict.\nGood man.\n",
|
|
|
|
},
|
|
|
|
"ppu": 0.55,
|
|
|
|
"eyes": "brown",
|
2019-03-16 11:10:32 +00:00
|
|
|
"clothing": map[string]interface{}{
|
|
|
|
"trousers": "denim",
|
|
|
|
"jacket": "leather",
|
|
|
|
"pants": map[string]interface{}{"size": "large"},
|
|
|
|
},
|
2019-12-03 15:46:17 +00:00
|
|
|
"default": map[string]interface{}{
|
|
|
|
"import_path": "gopkg.in/ini.v1",
|
|
|
|
"name": "ini",
|
|
|
|
"version": "v1",
|
|
|
|
},
|
2019-03-16 11:10:32 +00:00
|
|
|
"id": "0001",
|
|
|
|
"batters": map[string]interface{}{
|
|
|
|
"batter": []interface{}{
|
|
|
|
map[string]interface{}{"type": "Regular"},
|
|
|
|
map[string]interface{}{"type": "Chocolate"},
|
|
|
|
map[string]interface{}{"type": "Blueberry"},
|
|
|
|
map[string]interface{}{"type": "Devil's Food"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"hacker": true,
|
|
|
|
"beard": true,
|
|
|
|
"hobbies": []interface{}{
|
|
|
|
"skateboarding",
|
|
|
|
"snowboarding",
|
|
|
|
"go",
|
|
|
|
},
|
|
|
|
"age": 35,
|
|
|
|
"type": "donut",
|
|
|
|
"newkey": "remote",
|
|
|
|
"name": "Cake",
|
|
|
|
"p_id": "0001",
|
|
|
|
"p_ppu": "0.55",
|
|
|
|
"p_name": "Cake",
|
|
|
|
"p_batters": map[string]interface{}{
|
|
|
|
"batter": map[string]interface{}{"type": "Regular"},
|
|
|
|
},
|
|
|
|
"p_type": "donut",
|
|
|
|
"foos": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"foo": []map[string]interface{}{
|
|
|
|
{"key": 1},
|
|
|
|
{"key": 2},
|
|
|
|
{"key": 3},
|
2020-09-11 15:48:38 +00:00
|
|
|
{"key": 4},
|
|
|
|
},
|
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
|
|
|
|
2019-07-19 22:07:23 +00:00
|
|
|
allkeys := sort.StringSlice(AllKeys())
|
2014-09-27 21:00:51 +00:00
|
|
|
allkeys.Sort()
|
|
|
|
ks.Sort()
|
|
|
|
|
|
|
|
assert.Equal(t, ks, allkeys)
|
|
|
|
assert.Equal(t, all, AllSettings())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
testutil.Setenv(t, "ID", "13")
|
|
|
|
testutil.Setenv(t, "FOO_BAR", "baz")
|
2016-10-23 21:04:21 +00:00
|
|
|
|
|
|
|
expectedKeys := sort.StringSlice{"id", "foo.bar"}
|
|
|
|
expectedKeys.Sort()
|
|
|
|
keys := sort.StringSlice(v.AllKeys())
|
|
|
|
keys.Sort()
|
|
|
|
assert.Equal(t, expectedKeys, keys)
|
|
|
|
}
|
|
|
|
|
2014-04-05 05:19:39 +00:00
|
|
|
func TestAliasesOfAliases(t *testing.T) {
|
2016-10-10 11:40:38 +00:00
|
|
|
Set("Title", "Checking Case")
|
2014-04-05 05:19:39 +00:00
|
|
|
RegisterAlias("Foo", "Bar")
|
|
|
|
RegisterAlias("Bar", "Title")
|
|
|
|
assert.Equal(t, "Checking Case", Get("FOO"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecursiveAliases(t *testing.T) {
|
|
|
|
RegisterAlias("Baz", "Roo")
|
|
|
|
RegisterAlias("Roo", "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) {
|
2014-06-26 21:58:55 +00:00
|
|
|
SetDefault("port", 1313)
|
|
|
|
Set("name", "Steve")
|
2016-09-23 23:20:44 +00:00
|
|
|
Set("duration", "1s1ms")
|
2019-06-11 20:51:57 +00:00
|
|
|
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
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
err := Unmarshal(&C)
|
2014-06-26 21:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
Set("port", 1234)
|
2015-08-24 03:40:56 +00:00
|
|
|
err = Unmarshal(&C)
|
2014-06-26 21:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
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
|
|
|
|
2018-06-28 09:55:33 +00:00
|
|
|
func TestUnmarshalWithDecoderOptions(t *testing.T) {
|
|
|
|
Set("credentials", "{\"foo\":\"bar\"}")
|
|
|
|
|
|
|
|
opt := DecodeHook(mapstructure.ComposeDecodeHookFunc(
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
|
|
// Custom Decode Hook Function
|
|
|
|
func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) {
|
|
|
|
if rf != reflect.String || rt != reflect.Map {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
m := map[string]string{}
|
|
|
|
raw := data.(string)
|
|
|
|
if raw == "" {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return m, json.Unmarshal([]byte(raw), &m)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Credentials map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
|
|
|
err := Unmarshal(&C, opt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, &config{
|
|
|
|
Credentials: map[string]string{"foo": "bar"},
|
|
|
|
}, &C)
|
|
|
|
}
|
|
|
|
|
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)
|
2015-04-02 01:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:20:00 +00:00
|
|
|
// nolint: dupl
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestStr struct {
|
|
|
|
StringSlice []string
|
|
|
|
}
|
|
|
|
val := &TestStr{}
|
|
|
|
if err := v.Unmarshal(val); err != nil {
|
|
|
|
t.Fatalf("%+#v cannot unmarshal: %s", testValue.Value, err)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:20:00 +00:00
|
|
|
// nolint: dupl
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestInt struct {
|
|
|
|
IntSlice []int
|
|
|
|
}
|
|
|
|
val := &TestInt{}
|
|
|
|
if err := v.Unmarshal(val); err != nil {
|
|
|
|
t.Fatalf("%+#v cannot unmarshal: %s", testValue.Value, err)
|
|
|
|
}
|
|
|
|
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) {
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
BindPFlag("testvalue", flag)
|
|
|
|
|
|
|
|
assert.Equal(t, testString, Get("testvalue"))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
|
|
|
}
|
2014-10-09 20:39:24 +00:00
|
|
|
|
2020-08-11 23:26:27 +00:00
|
|
|
func TestBindPFlagDetectNilFlag(t *testing.T) {
|
|
|
|
result := BindPFlag("testvalue", nil)
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestMap struct {
|
|
|
|
StringToString map[string]string
|
|
|
|
}
|
|
|
|
val := &TestMap{}
|
|
|
|
if err := v.Unmarshal(val); err != nil {
|
|
|
|
t.Fatalf("%+#v cannot unmarshal: %s", testValue.Value, err)
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
assert.Equal(t, testValue.Expected, val.StringToString)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, defaultVal, val.StringToString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 20:39:24 +00:00
|
|
|
func TestBoundCaseSensitivity(t *testing.T) {
|
|
|
|
assert.Equal(t, "brown", Get("eyes"))
|
|
|
|
|
|
|
|
BindEnv("eYEs", "TURTLE_EYES")
|
2020-09-30 11:02:07 +00:00
|
|
|
|
|
|
|
testutil.Setenv(t, "TURTLE_EYES", "blue")
|
2014-10-09 20:39:24 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "blue", Get("eyes"))
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
BindPFlag("eYEs", flag)
|
|
|
|
assert.Equal(t, "green", Get("eyes"))
|
|
|
|
}
|
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) {
|
|
|
|
initConfigs()
|
|
|
|
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
|
|
|
|
|
|
|
Set("super", map[string]interface{}{
|
|
|
|
"deep": map[string]interface{}{
|
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"super": map[string]interface{}{
|
|
|
|
"deep": map[string]interface{}{
|
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"super.deep": map[string]interface{}{
|
|
|
|
"nested": "value",
|
|
|
|
},
|
|
|
|
"super.deep.nested": "value",
|
|
|
|
"owner.organization": "MongoDB",
|
|
|
|
"batters.batter": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Regular",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Chocolate",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Blueberry",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Devil's Food",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"hobbies": []interface{}{
|
|
|
|
"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",
|
2015-05-01 19:15:24 +00:00
|
|
|
"batters": map[string]interface{}{
|
|
|
|
"batter": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Regular",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "Chocolate",
|
2020-09-11 15:48:38 +00:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Blueberry",
|
2020-09-11 15:48:38 +00:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2015-05-01 19:15:24 +00:00
|
|
|
"type": "Devil's Food",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"eyes": "brown",
|
|
|
|
"age": 35,
|
|
|
|
"owner": map[string]interface{}{
|
|
|
|
"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,
|
2016-10-08 08:00:18 +00:00
|
|
|
"clothing": map[string]interface{}{
|
2015-05-01 19:15:24 +00:00
|
|
|
"jacket": "leather",
|
|
|
|
"trousers": "denim",
|
2016-10-14 09:24:45 +00:00
|
|
|
"pants": map[string]interface{}{
|
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-12-13 20:37:32 +00:00
|
|
|
"foos": []map[string]interface{}{
|
2019-12-06 11:37:45 +00:00
|
|
|
{
|
2015-12-13 20:37:32 +00:00
|
|
|
"foo": []map[string]interface{}{
|
2019-12-06 11:37:45 +00:00
|
|
|
{
|
2015-12-13 20:37:32 +00:00
|
|
|
"key": 1,
|
|
|
|
},
|
2019-12-06 11:37:45 +00:00
|
|
|
{
|
2015-12-13 20:37:32 +00:00
|
|
|
"key": 2,
|
|
|
|
},
|
2019-12-06 11:37:45 +00:00
|
|
|
{
|
2015-12-13 20:37:32 +00:00
|
|
|
"key": 3,
|
|
|
|
},
|
2019-12-06 11:37:45 +00:00
|
|
|
{
|
2015-12-13 20:37:32 +00:00
|
|
|
"key": 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
|
|
|
|
func TestReadBufConfig(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yaml")
|
2015-05-14 09:40:59 +00:00
|
|
|
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
2015-05-08 09:13:33 +00:00
|
|
|
t.Log(v.AllKeys())
|
|
|
|
|
|
|
|
assert.True(t, v.InConfig("name"))
|
|
|
|
assert.False(t, v.InConfig("state"))
|
|
|
|
assert.Equal(t, "steve", v.Get("name"))
|
|
|
|
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
|
2016-10-14 09:24:45 +00:00
|
|
|
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, v.Get("clothing"))
|
2015-05-08 09:13:33 +00:00
|
|
|
assert.Equal(t, 35, v.Get("age"))
|
|
|
|
}
|
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
|
|
|
|
|
|
|
testutil.Setenv(t, "FOO", "bar")
|
|
|
|
testutil.Setenv(t, "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) {
|
|
|
|
root, config, cleanup := initDirs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.SetConfigName(config)
|
|
|
|
v.SetDefault(`key`, `default`)
|
|
|
|
|
|
|
|
entries, err := ioutil.ReadDir(root)
|
2019-07-19 22:10:42 +00:00
|
|
|
assert.Nil(t, err)
|
2015-08-02 00:37:27 +00:00
|
|
|
for _, e := range entries {
|
|
|
|
if e.IsDir() {
|
|
|
|
v.AddConfigPath(e.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
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) {
|
|
|
|
_, config, cleanup := initDirs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
v := New()
|
|
|
|
v.SetConfigName(config)
|
|
|
|
v.SetDefault(`key`, `default`)
|
|
|
|
|
|
|
|
v.AddConfigPath(`whattayoutalkingbout`)
|
|
|
|
v.AddConfigPath(`thispathaintthere`)
|
|
|
|
|
|
|
|
err := v.ReadInConfig()
|
2016-10-13 11:33:30 +00:00
|
|
|
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
|
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) {
|
|
|
|
_, config, cleanup := initDirs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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`))
|
|
|
|
}
|
|
|
|
|
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)
|
2015-12-24 11:44:44 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:26:31 +00:00
|
|
|
var hclWriteExpected = []byte(`"foos" = {
|
|
|
|
"foo" = {
|
|
|
|
"key" = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
"foo" = {
|
|
|
|
"key" = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
"foo" = {
|
|
|
|
"key" = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
"foo" = {
|
|
|
|
"key" = 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"id" = "0001"
|
|
|
|
|
|
|
|
"name" = "Cake"
|
|
|
|
|
|
|
|
"ppu" = 0.55
|
|
|
|
|
|
|
|
"type" = "donut"`)
|
|
|
|
|
|
|
|
var jsonWriteExpected = []byte(`{
|
|
|
|
"batters": {
|
|
|
|
"batter": [
|
|
|
|
{
|
|
|
|
"type": "Regular"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Chocolate"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Blueberry"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Devil's Food"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"id": "0001",
|
|
|
|
"name": "Cake",
|
|
|
|
"ppu": 0.55,
|
|
|
|
"type": "donut"
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var propertiesWriteExpected = []byte(`p_id = 0001
|
|
|
|
p_type = donut
|
|
|
|
p_name = Cake
|
|
|
|
p_ppu = 0.55
|
|
|
|
p_batters.batter.type = Regular
|
|
|
|
`)
|
|
|
|
|
|
|
|
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
|
|
|
|
`)
|
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
"hcl with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "hcl",
|
|
|
|
outConfigType: "hcl",
|
|
|
|
fileName: "c.hcl",
|
|
|
|
input: hclExample,
|
|
|
|
expectedContent: hclWriteExpected,
|
|
|
|
},
|
|
|
|
"hcl without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "hcl",
|
|
|
|
outConfigType: "hcl",
|
|
|
|
fileName: "c",
|
|
|
|
input: hclExample,
|
|
|
|
expectedContent: hclWriteExpected,
|
|
|
|
},
|
|
|
|
"hcl with file extension and mismatch type": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "hcl",
|
|
|
|
outConfigType: "json",
|
|
|
|
fileName: "c.hcl",
|
|
|
|
input: hclExample,
|
|
|
|
expectedContent: hclWriteExpected,
|
|
|
|
},
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
"properties with file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "properties",
|
|
|
|
outConfigType: "properties",
|
|
|
|
fileName: "c.properties",
|
|
|
|
input: propertiesExample,
|
|
|
|
expectedContent: propertiesWriteExpected,
|
|
|
|
},
|
|
|
|
"properties without file extension": {
|
|
|
|
configName: "c",
|
|
|
|
inConfigType: "properties",
|
|
|
|
outConfigType: "properties",
|
|
|
|
fileName: "c",
|
|
|
|
input: propertiesExample,
|
|
|
|
expectedContent: propertiesWriteExpected,
|
|
|
|
},
|
|
|
|
"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))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
v.SetConfigType(tc.outConfigType)
|
|
|
|
if err := v.WriteConfigAs(tc.fileName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
read, err := afero.ReadFile(fs, tc.fileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := v.WriteConfigAs(tc.fileName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := v.WriteConfigAs(tc.fileName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2019-09-13 14:29:19 +00:00
|
|
|
read, err := afero.ReadFile(fs, "/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()
|
|
|
|
fs.Create("/test/c.yaml")
|
|
|
|
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)
|
|
|
|
err := v.ReadConfig(bytes.NewBuffer(yamlExample))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-04 01:14:08 +00:00
|
|
|
require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml"))
|
2019-09-13 14:29:19 +00:00
|
|
|
if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
`)
|
|
|
|
|
|
|
|
func TestMergeConfig(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yml")
|
|
|
|
if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetInt("hello.pop"); pop != 37890 {
|
|
|
|
t.Fatalf("pop != 37890, = %d", pop)
|
|
|
|
}
|
|
|
|
|
2018-05-06 01:38:06 +00:00
|
|
|
if pop := v.GetInt32("hello.pop"); pop != int32(37890) {
|
|
|
|
t.Fatalf("pop != 37890, = %d", pop)
|
|
|
|
}
|
|
|
|
|
2019-05-24 13:40:12 +00:00
|
|
|
if pop := v.GetInt64("hello.largenum"); pop != int64(765432101234567) {
|
|
|
|
t.Fatalf("int64 largenum != 765432101234567, = %d", pop)
|
2016-08-05 07:16:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 14:06:45 +00:00
|
|
|
if pop := v.GetUint("hello.pop"); pop != 37890 {
|
|
|
|
t.Fatalf("uint pop != 37890, = %d", pop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetUint32("hello.pop"); pop != 37890 {
|
|
|
|
t.Fatalf("uint32 pop != 37890, = %d", pop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetUint64("hello.num2pow63"); pop != 9223372036854775808 {
|
|
|
|
t.Fatalf("uint64 num2pow63 != 9223372036854775808, = %d", pop)
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
|
|
|
t.Fatalf("len(world) != 4, = %d", len(world))
|
|
|
|
}
|
|
|
|
|
|
|
|
if fu := v.GetString("fu"); fu != "" {
|
|
|
|
t.Fatalf("fu != \"\", = %s", fu)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetInt("hello.pop"); pop != 45000 {
|
|
|
|
t.Fatalf("pop != 45000, = %d", pop)
|
|
|
|
}
|
|
|
|
|
2018-05-06 01:38:06 +00:00
|
|
|
if pop := v.GetInt32("hello.pop"); pop != int32(45000) {
|
|
|
|
t.Fatalf("pop != 45000, = %d", pop)
|
|
|
|
}
|
|
|
|
|
2019-05-24 13:40:12 +00:00
|
|
|
if pop := v.GetInt64("hello.largenum"); pop != int64(7654321001234567) {
|
|
|
|
t.Fatalf("int64 largenum != 7654321001234567, = %d", pop)
|
2016-08-05 07:16:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
|
|
|
t.Fatalf("len(world) != 4, = %d", len(world))
|
|
|
|
}
|
|
|
|
|
|
|
|
if universe := v.GetStringSlice("hello.universe"); len(universe) != 2 {
|
|
|
|
t.Fatalf("len(universe) != 2, = %d", len(universe))
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:05:58 +00:00
|
|
|
if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 {
|
|
|
|
t.Fatalf("len(ints) != 2, = %d", len(ints))
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
if fu := v.GetString("fu"); fu != "bar" {
|
|
|
|
t.Fatalf("fu != \"bar\", = %s", fu)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeConfigNoMerge(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
v.SetConfigType("yml")
|
|
|
|
if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetInt("hello.pop"); pop != 37890 {
|
|
|
|
t.Fatalf("pop != 37890, = %d", pop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
|
|
|
t.Fatalf("len(world) != 4, = %d", len(world))
|
|
|
|
}
|
|
|
|
|
|
|
|
if fu := v.GetString("fu"); fu != "" {
|
|
|
|
t.Fatalf("fu != \"\", = %s", fu)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop := v.GetInt("hello.pop"); pop != 45000 {
|
|
|
|
t.Fatalf("pop != 45000, = %d", pop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if world := v.GetStringSlice("hello.world"); len(world) != 0 {
|
|
|
|
t.Fatalf("len(world) != 0, = %d", len(world))
|
|
|
|
}
|
|
|
|
|
|
|
|
if universe := v.GetStringSlice("hello.universe"); len(universe) != 2 {
|
|
|
|
t.Fatalf("len(universe) != 2, = %d", len(universe))
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:05:58 +00:00
|
|
|
if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 {
|
|
|
|
t.Fatalf("len(ints) != 2, = %d", len(ints))
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:20:40 +00:00
|
|
|
if fu := v.GetString("fu"); fu != "bar" {
|
|
|
|
t.Fatalf("fu != \"bar\", = %s", fu)
|
|
|
|
}
|
|
|
|
}
|
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")
|
|
|
|
if err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleTgt)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert := 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")
|
|
|
|
if large != 765432101234567 {
|
|
|
|
t.Fatal("Got large num:", large)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pop != i {
|
|
|
|
t.Fatal("Got pop:", pop)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(37890)
|
|
|
|
|
|
|
|
update := map[string]interface{}{
|
2018-12-07 10:02:11 +00:00
|
|
|
"Hello": map[string]interface{}{
|
|
|
|
"Pop": 1234,
|
|
|
|
},
|
|
|
|
"World": map[interface{}]interface{}{
|
|
|
|
"Rock": 345,
|
2018-12-05 14:19:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.MergeConfigMap(update); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-07 10:02:11 +00:00
|
|
|
if rock := v.GetInt("world.rock"); rock != 345 {
|
|
|
|
t.Fatal("Got rock:", rock)
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:19:25 +00:00
|
|
|
assert(1234)
|
|
|
|
}
|
|
|
|
|
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)
|
2016-01-20 21:15:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
SetConfigFile("/tmp/config.yaml")
|
|
|
|
SetConfigName("default")
|
2016-10-13 11:33:30 +00:00
|
|
|
f, err := v.getConfigFile()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("config file cache should have been cleared")
|
|
|
|
}
|
|
|
|
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) {
|
2016-10-10 11:40:38 +00:00
|
|
|
config := `name: steve
|
|
|
|
clothing:
|
|
|
|
jacket: leather
|
|
|
|
trousers: denim
|
|
|
|
pants:
|
|
|
|
size: large
|
|
|
|
`
|
|
|
|
initConfig("yaml", config)
|
|
|
|
|
|
|
|
assert.Equal(t, "steve", GetString("name"))
|
|
|
|
|
2016-09-19 17:37:56 +00:00
|
|
|
polyester := "polyester"
|
|
|
|
SetDefault("clothing.shirt", polyester)
|
2016-10-08 08:00:18 +00:00
|
|
|
SetDefault("clothing.jacket.price", 100)
|
|
|
|
|
|
|
|
assert.Equal(t, "leather", GetString("clothing.jacket"))
|
|
|
|
assert.Nil(t, Get("clothing.jacket.price"))
|
|
|
|
assert.Equal(t, polyester, GetString("clothing.shirt"))
|
|
|
|
|
|
|
|
clothingSettings := AllSettings()["clothing"].(map[string]interface{})
|
|
|
|
assert.Equal(t, "leather", clothingSettings["jacket"])
|
|
|
|
assert.Equal(t, polyester, clothingSettings["shirt"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDotParameter(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
// shoud take precedence over batters defined in jsonExample
|
|
|
|
r := bytes.NewReader([]byte(`{ "batters.batter": [ { "type": "Small" } ] }`))
|
|
|
|
unmarshalReader(r, v.config)
|
2016-09-19 17:37:56 +00:00
|
|
|
|
2016-10-08 08:00:18 +00:00
|
|
|
actual := Get("batters.batter")
|
|
|
|
expected := []interface{}{map[string]interface{}{"type": "Small"}}
|
|
|
|
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) {
|
|
|
|
Reset()
|
|
|
|
m1 := map[string]interface{}{
|
|
|
|
"Foo": 32,
|
|
|
|
"Bar": map[interface{}]interface {
|
|
|
|
}{
|
|
|
|
"ABc": "A",
|
2020-09-11 15:48:38 +00:00
|
|
|
"cDE": "B",
|
|
|
|
},
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m2 := map[string]interface{}{
|
|
|
|
"Foo": 52,
|
|
|
|
"Bar": map[interface{}]interface {
|
|
|
|
}{
|
|
|
|
"bCd": "A",
|
2020-09-11 15:48:38 +00:00
|
|
|
"eFG": "B",
|
|
|
|
},
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Set("Given1", m1)
|
|
|
|
Set("Number1", 42)
|
|
|
|
|
|
|
|
SetDefault("Given2", m2)
|
|
|
|
SetDefault("Number2", 52)
|
|
|
|
|
|
|
|
// Verify SetDefault
|
|
|
|
if v := Get("number2"); v != 52 {
|
|
|
|
t.Fatalf("Expected 52 got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := Get("given2.foo"); v != 52 {
|
|
|
|
t.Fatalf("Expected 52 got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := Get("given2.bar.bcd"); v != "A" {
|
|
|
|
t.Fatalf("Expected A got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m2["Foo"]; !ok {
|
|
|
|
t.Fatal("Input map changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify Set
|
|
|
|
if v := Get("number1"); v != 42 {
|
|
|
|
t.Fatalf("Expected 42 got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := Get("given1.foo"); v != 32 {
|
|
|
|
t.Fatalf("Expected 32 got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := Get("given1.bar.abc"); v != "A" {
|
|
|
|
t.Fatalf("Expected A got %q", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m1["Foo"]; !ok {
|
|
|
|
t.Fatal("Input map changed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:26:50 +00:00
|
|
|
func TestParseNested(t *testing.T) {
|
|
|
|
type duration struct {
|
|
|
|
Delay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type item struct {
|
|
|
|
Name string
|
|
|
|
Delay time.Duration
|
|
|
|
Nested duration
|
|
|
|
}
|
|
|
|
|
|
|
|
config := `[[parent]]
|
|
|
|
delay="100ms"
|
|
|
|
[parent.nested]
|
|
|
|
delay="200ms"
|
|
|
|
`
|
|
|
|
initConfig("toml", config)
|
|
|
|
|
|
|
|
var items []item
|
|
|
|
err := v.UnmarshalKey("parent", &items)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(items))
|
|
|
|
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) {
|
2016-10-10 11:40:38 +00:00
|
|
|
initConfig(typ, config)
|
|
|
|
Set("RfD", true)
|
|
|
|
assert.Equal(t, true, Get("rfd"))
|
|
|
|
assert.Equal(t, true, Get("rFD"))
|
|
|
|
assert.Equal(t, 1, cast.ToInt(Get("abcd")))
|
|
|
|
assert.Equal(t, 1, cast.ToInt(Get("Abcd")))
|
|
|
|
assert.Equal(t, 2, cast.ToInt(Get("ef.gh")))
|
|
|
|
assert.Equal(t, 3, cast.ToInt(Get("ef.ijk")))
|
|
|
|
assert.Equal(t, 4, cast.ToInt(Get("ef.lm.no")))
|
|
|
|
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
|
|
|
|
}
|
|
|
|
|
2018-01-03 09:37:18 +00:00
|
|
|
func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) {
|
|
|
|
watchDir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
configFile := path.Join(watchDir, "config.yaml")
|
|
|
|
err = ioutil.WriteFile(configFile, []byte("foo: bar\n"), 0640)
|
|
|
|
require.Nil(t, err)
|
|
|
|
cleanup := func() {
|
|
|
|
os.RemoveAll(watchDir)
|
|
|
|
}
|
|
|
|
v := New()
|
|
|
|
v.SetConfigFile(configFile)
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "bar", v.Get("foo"))
|
|
|
|
return v, configFile, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func()) {
|
|
|
|
watchDir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
dataDir1 := path.Join(watchDir, "data1")
|
|
|
|
err = os.Mkdir(dataDir1, 0777)
|
|
|
|
require.Nil(t, err)
|
|
|
|
realConfigFile := path.Join(dataDir1, "config.yaml")
|
|
|
|
t.Logf("Real config file location: %s\n", realConfigFile)
|
|
|
|
err = ioutil.WriteFile(realConfigFile, []byte("foo: bar\n"), 0640)
|
|
|
|
require.Nil(t, err)
|
|
|
|
cleanup := func() {
|
|
|
|
os.RemoveAll(watchDir)
|
|
|
|
}
|
|
|
|
// 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`
|
|
|
|
configFile := path.Join(watchDir, "config.yaml")
|
|
|
|
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
|
|
|
|
v := New()
|
|
|
|
v.SetConfigFile(configFile)
|
|
|
|
err = v.ReadInConfig()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "bar", v.Get("foo"))
|
|
|
|
return v, watchDir, configFile, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
v, configFile, cleanup := newViperWithConfigFile(t)
|
|
|
|
defer cleanup()
|
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)
|
2018-01-03 09:37:18 +00:00
|
|
|
v.OnConfigChange(func(in fsnotify.Event) {
|
|
|
|
t.Logf("config file changed")
|
|
|
|
wg.Done()
|
|
|
|
})
|
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
|
2018-08-06 07:29:13 +00:00
|
|
|
err = ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0640)
|
2018-01-03 09:37:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
// then the config value should have changed
|
|
|
|
require.Nil(t, err)
|
|
|
|
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...")
|
|
|
|
}
|
|
|
|
v, watchDir, _, _ := newViperWithSymlinkedConfigFile(t)
|
|
|
|
// defer cleanup()
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
v.WatchConfig()
|
|
|
|
v.OnConfigChange(func(in fsnotify.Event) {
|
|
|
|
t.Logf("config file changed")
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
wg.Add(1)
|
|
|
|
// when link to another `config.yaml` file
|
|
|
|
dataDir2 := path.Join(watchDir, "data2")
|
|
|
|
err := os.Mkdir(dataDir2, 0777)
|
|
|
|
require.Nil(t, err)
|
|
|
|
configFile2 := path.Join(dataDir2, "config.yaml")
|
|
|
|
err = ioutil.WriteFile(configFile2, []byte("foo: baz\n"), 0640)
|
|
|
|
require.Nil(t, err)
|
|
|
|
// change the symlink using the `ln -sfn` command
|
|
|
|
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
|
|
|
|
require.Nil(t, err)
|
|
|
|
wg.Wait()
|
|
|
|
// then
|
|
|
|
require.Nil(t, err)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-06 11:40:41 +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-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)
|
|
|
|
|
|
|
|
values := map[string]interface{}{
|
|
|
|
"image": map[string]interface{}{
|
|
|
|
"repository": "someImage",
|
|
|
|
"tag": "1.0.0",
|
|
|
|
},
|
|
|
|
"ingress": map[string]interface{}{
|
|
|
|
"annotations": map[string]interface{}{
|
|
|
|
"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 {
|
|
|
|
Values map[string]interface{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := config{
|
|
|
|
Charts: struct {
|
|
|
|
Values map[string]interface{}
|
|
|
|
}{
|
|
|
|
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:
|
|
|
|
- title: "The expanse"
|
|
|
|
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) {
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
r := strings.NewReader(string(yamlDeepNestedSlices))
|
|
|
|
|
|
|
|
err := v.unmarshalReader(r, v.config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "The expanse", v.GetString("tv.0.title"))
|
|
|
|
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"))
|
|
|
|
|
|
|
|
// 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"))
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|