-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathflag_test.go
113 lines (89 loc) · 2.23 KB
/
flag_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package multiconfig
import (
"strings"
"testing"
"github.com/fatih/structs"
)
func TestFlag(t *testing.T) {
m := &FlagLoader{}
s := &Server{}
structName := structs.Name(s)
// get flags
args := getFlags(t, structName, "")
m.Args = args[1:]
if err := m.Load(s); err != nil {
t.Error(err)
}
testStruct(t, s, getDefaultServer())
}
func TestFlagWithPrefix(t *testing.T) {
const prefix = "Prefix"
m := FlagLoader{Prefix: prefix}
s := &Server{}
structName := structs.Name(s)
// get flags
args := getFlags(t, structName, prefix)
m.Args = args[1:]
if err := m.Load(s); err != nil {
t.Error(err)
}
testStruct(t, s, getDefaultServer())
}
func TestFlattenFlags(t *testing.T) {
m := FlagLoader{
Flatten: true,
}
s := &FlattenedServer{}
structName := structs.Name(s)
// get flags
args := getFlags(t, structName, "")
m.Args = args[1:]
if err := m.Load(s); err != nil {
t.Error(err)
}
testFlattenedStruct(t, s, getDefaultServer())
}
// getFlags returns a slice of arguments that can be passed to flag.Parse()
func getFlags(t *testing.T, structName, prefix string) []string {
if structName == "" {
t.Fatal("struct name can not be empty")
}
var flags map[string]string
switch structName {
case "Server":
flags = map[string]string{
"-name": "koding",
"-port": "6060",
"-enabled": "",
"-users": "ankara,istanbul",
"-postgres-enabled": "",
"-postgres-port": "5432",
"-postgres-hosts": "192.168.2.1,192.168.2.2,192.168.2.3",
"-postgres-dbname": "configdb",
"-postgres-availabilityratio": "8.23",
}
case "FlattenedServer":
flags = map[string]string{
"--enabled": "",
"--port": "5432",
"--fixme-port": "5432",
"--hosts": "192.168.2.1,192.168.2.2,192.168.2.3",
"--dbname": "configdb",
"--availabilityratio": "8.23",
}
}
prefix = strings.ToLower(prefix)
args := []string{"multiconfig-test"}
for key, val := range flags {
flag := key
if prefix != "" {
flag = "-" + prefix + key
}
if val == "" {
args = append(args, flag)
} else {
args = append(args, flag, val)
}
}
return args
}