-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags_config_test.go
119 lines (109 loc) · 3.81 KB
/
flags_config_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
114
115
116
117
118
119
package upstart
import (
"flag"
"testing"
"github.com/gigawattio/go-commons/pkg/testlib"
)
// TestFlagsConfigValidationBindTo ensures that passing nil to `Validate()'
// produces the expected IllegalBindToError.
func TestFlagsConfigValidationBindTo(t *testing.T) {
config := NewFlagsConfig(BaseFlagSet(testlib.CurrentRunningTest()), []string{})
if expected, actual := IllegalBindToError, config.Validate(nil); actual != expected {
t.Fatalf("Expected Validate(nil)=%s but actual=%s", expected, actual)
}
}
func TestFlagsConfigScenarios(t *testing.T) {
type (
Validatable interface {
Validate(bindTo interface{}) error
}
ConfigExtra struct {
*FlagsConfig
Extra bool `flag:"extra"`
}
)
testCases := []struct {
name string
args []string
expectedError error
configFunc func(flagSet *flag.FlagSet, args []string) Validatable // Custom config struct instance provider. If nil, *FlagsConfig will be used.
callbackFunc func(config interface{}) // Callback which is invoked with the config struct instance.
}{
{
name: "expect-InvalidFlagsInstallUninstallError",
args: []string{"-install", "-uninstall"},
expectedError: InvalidFlagsInstallUninstallError,
},
{
name: "expect-InvalidFlagsMissingServiceUserError",
args: []string{"-install"},
expectedError: InvalidFlagsMissingServiceUserError,
},
{
name: "basic-test",
args: []string{"-install", "-user", "jay"},
expectedError: nil,
callbackFunc: func(configIface interface{}) {
config := configIface.(*FlagsConfig)
if expected, actual := false, config.Uninstall; actual != expected {
t.Logf("config=%+v", *config)
t.Errorf("Expected config.Uninstall=%v but actual=%v", expected, actual)
}
if expected, actual := true, config.Install; actual != expected {
t.Logf("config=%+v", config)
t.Errorf("Expected config.Install=%v but actual=%v", expected, actual)
}
if expected, actual := "jay", config.ServiceUser; actual != expected {
t.Logf("config=%+v", config)
t.Errorf("Expected config.ServiceUser=%v but actual=%v", expected, actual)
}
},
},
{
name: "extra-flag-test",
args: []string{"-extra=true", "-install", "-user=jay"},
expectedError: nil,
configFunc: func(flagSet *flag.FlagSet, args []string) Validatable {
flagSet.Bool("extra", false, "usage")
config := &ConfigExtra{
FlagsConfig: NewFlagsConfig(flagSet, args),
}
return config
},
callbackFunc: func(configIface interface{}) {
config := configIface.(*ConfigExtra)
if expected, actual := true, config.Extra; actual != expected {
t.Logf("config=%+v", *config)
t.Errorf("Expected config.Extra=%v but actual=%v", expected, actual)
}
if expected, actual := true, config.Install; actual != expected {
t.Logf("config=%+v", config)
t.Errorf("Expected config.Install=%v but actual=%v", expected, actual)
}
if expected, actual := "jay", config.ServiceUser; actual != expected {
t.Logf("config=%+v", config)
t.Errorf("Expected config.ServiceUser=%v but actual=%v", expected, actual)
}
},
},
}
for i, testCase := range testCases {
if testCase.configFunc == nil {
// Default config provider.
testCase.configFunc = func(flagSet *flag.FlagSet, args []string) Validatable {
return NewFlagsConfig(flagSet, args)
}
}
var (
flagSet = BaseFlagSet(testCase.name)
config = testCase.configFunc(flagSet, testCase.args)
)
if actual, expected := config.Validate(config), testCase.expectedError; actual != expected {
t.Logf("[i=%v] testCase=%+v", i, testCase)
t.Fatalf("[i=%v] Expected error=%s but actual=%s", i, expected, actual)
}
if testCase.callbackFunc != nil {
testCase.callbackFunc(config)
}
}
}