-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecedence_test.go
63 lines (49 loc) · 1.37 KB
/
precedence_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package rconfig
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrecedence(t *testing.T) {
type testcfg struct {
A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"`
}
var (
cfg testcfg
args []string
vardefaults map[string]string
)
exec := func(desc string, fn func() interface{}, exp interface{}) {
cfg = testcfg{}
SetVariableDefaults(vardefaults)
assert.NoError(t, parse(&cfg, args), desc)
assert.Equal(t, exp, fn())
}
// Provided: Flag, Env, Default, VarDefault
args = []string{"-a", "5"}
t.Setenv("a", "8")
vardefaults = map[string]string{
"a": "3",
}
exec("Provided: Flag, Env, Default, VarDefault", func() interface{} { return cfg.A }, 5)
// Provided: Env, Default, VarDefault
args = []string{}
t.Setenv("a", "8")
vardefaults = map[string]string{
"a": "3",
}
exec("Provided: Env, Default, VarDefault", func() interface{} { return cfg.A }, 8)
// Provided: Default, VarDefault
args = []string{}
require.NoError(t, os.Unsetenv("a"))
vardefaults = map[string]string{
"a": "3",
}
exec("Provided: Default, VarDefault", func() interface{} { return cfg.A }, 3)
// Provided: Default
args = []string{}
require.NoError(t, os.Unsetenv("a"))
vardefaults = map[string]string{}
exec("Provided: Default", func() interface{} { return cfg.A }, 1)
}