forked from namsral/flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extras_test.go
203 lines (177 loc) · 6.1 KB
/
extras_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flag_test
import (
"os"
"syscall"
"testing"
"time"
. "github.com/namsral/flag"
)
// Test parsing a environment variables
func TestParseEnv(t *testing.T) {
syscall.Setenv("BOOL", "")
syscall.Setenv("BOOL2", "true")
syscall.Setenv("INT", "22")
syscall.Setenv("INT64", "0x23")
syscall.Setenv("UINT", "24")
syscall.Setenv("UINT64", "25")
syscall.Setenv("STRING", "hello")
syscall.Setenv("FLOAT64", "2718e28")
syscall.Setenv("DURATION", "2m")
f := NewFlagSet(os.Args[0], ContinueOnError)
boolFlag := f.Bool("bool", false, "bool value")
bool2Flag := f.Bool("bool2", false, "bool2 value")
intFlag := f.Int("int", 0, "int value")
int64Flag := f.Int64("int64", 0, "int64 value")
uintFlag := f.Uint("uint", 0, "uint value")
uint64Flag := f.Uint64("uint64", 0, "uint64 value")
stringFlag := f.String("string", "0", "string value")
float64Flag := f.Float64("float64", 0, "float64 value")
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
err := f.ParseEnv(os.Environ())
if err != nil {
t.Fatal("expected no error; got ", err)
}
if *boolFlag != true {
t.Error("bool flag should be true, is ", *boolFlag)
}
if *bool2Flag != true {
t.Error("bool2 flag should be true, is ", *bool2Flag)
}
if *intFlag != 22 {
t.Error("int flag should be 22, is ", *intFlag)
}
if *int64Flag != 0x23 {
t.Error("int64 flag should be 0x23, is ", *int64Flag)
}
if *uintFlag != 24 {
t.Error("uint flag should be 24, is ", *uintFlag)
}
if *uint64Flag != 25 {
t.Error("uint64 flag should be 25, is ", *uint64Flag)
}
if *stringFlag != "hello" {
t.Error("string flag should be `hello`, is ", *stringFlag)
}
if *float64Flag != 2718e28 {
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
}
if *durationFlag != 2*time.Minute {
t.Error("duration flag should be 2m, is ", *durationFlag)
}
}
// Test parsing a configuration file
func TestParseFile(t *testing.T) {
f := NewFlagSet(os.Args[0], ContinueOnError)
boolFlag := f.Bool("bool", false, "bool value")
bool2Flag := f.Bool("bool2", false, "bool2 value")
intFlag := f.Int("int", 0, "int value")
int64Flag := f.Int64("int64", 0, "int64 value")
uintFlag := f.Uint("uint", 0, "uint value")
uint64Flag := f.Uint64("uint64", 0, "uint64 value")
stringFlag := f.String("string", "0", "string value")
float64Flag := f.Float64("float64", 0, "float64 value")
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
err := f.ParseFile("./testdata/test.conf")
if err != nil {
t.Fatal("expected no error; got ", err)
}
if *boolFlag != true {
t.Error("bool flag should be true, is ", *boolFlag)
}
if *bool2Flag != true {
t.Error("bool2 flag should be true, is ", *bool2Flag)
}
if *intFlag != 22 {
t.Error("int flag should be 22, is ", *intFlag)
}
if *int64Flag != 0x23 {
t.Error("int64 flag should be 0x23, is ", *int64Flag)
}
if *uintFlag != 24 {
t.Error("uint flag should be 24, is ", *uintFlag)
}
if *uint64Flag != 25 {
t.Error("uint64 flag should be 25, is ", *uint64Flag)
}
if *stringFlag != "hello" {
t.Error("string flag should be `hello`, is ", *stringFlag)
}
if *float64Flag != 2718e28 {
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
}
if *durationFlag != 2*time.Minute {
t.Error("duration flag should be 2m, is ", *durationFlag)
}
}
func TestParseFileUnknownFlag(t *testing.T) {
f := NewFlagSet("test", ContinueOnError)
if err := f.ParseFile("./testdata/bad_test.conf"); err == nil {
t.Error("parse did not fail for unknown flag; ", err)
}
}
func TestDefaultConfigFlagname(t *testing.T) {
f := NewFlagSet("test", ContinueOnError)
f.Bool("bool", false, "bool value")
f.Bool("bool2", false, "bool2 value")
f.Int("int", 0, "int value")
f.Int64("int64", 0, "int64 value")
f.Uint("uint", 0, "uint value")
f.Uint64("uint64", 0, "uint64 value")
stringFlag := f.String("string", "0", "string value")
f.Float64("float64", 0, "float64 value")
f.Duration("duration", 5*time.Second, "time.Duration value")
f.String(DefaultConfigFlagname, "./testdata/test.conf", "config path")
if err := os.Unsetenv("STRING"); err != nil {
t.Error(err)
}
if err := f.Parse([]string{}); err != nil {
t.Error("parse failed; ", err)
}
if *stringFlag != "hello" {
t.Error("string flag should be `hello`, is", *stringFlag)
}
}
func TestDefaultConfigFlagnameMissingFile(t *testing.T) {
f := NewFlagSet("test", ContinueOnError)
f.String(DefaultConfigFlagname, "./testdata/missing", "config path")
if err := os.Unsetenv("STRING"); err != nil {
t.Error(err)
}
if err := f.Parse([]string{}); err == nil {
t.Error("expected error of missing config file, got nil")
}
}
func TestFlagSetParseErrors(t *testing.T) {
fs := NewFlagSet("test", ContinueOnError)
fs.Int("int", 0, "int value")
args := []string{"-int", "bad"}
expected := `invalid value "bad" for flag -int: strconv.ParseInt: parsing "bad": invalid syntax`
if err := fs.Parse(args); err == nil || err.Error() != expected {
t.Errorf("expected error %q parsing from args, got: %v", expected, err)
}
if err := os.Setenv("INT", "bad"); err != nil {
t.Fatalf("error setting env: %s", err.Error())
}
expected = `invalid value "bad" for environment variable int: strconv.ParseInt: parsing "bad": invalid syntax`
if err := fs.Parse([]string{}); err == nil || err.Error() != expected {
t.Errorf("expected error %q parsing from env, got: %v", expected, err)
}
if err := os.Unsetenv("INT"); err != nil {
t.Fatalf("error unsetting env: %s", err.Error())
}
fs.String("config", "", "config filename")
args = []string{"-config", "testdata/bad_test.conf"}
expected = `invalid value "bad" for configuration variable int: strconv.ParseInt: parsing "bad": invalid syntax`
if err := fs.Parse(args); err == nil || err.Error() != expected {
t.Errorf("expected error %q parsing from config, got: %v", expected, err)
}
}
func TestTestingPackageFlags(t *testing.T) {
f := NewFlagSet("test", ContinueOnError)
if err := f.Parse([]string{"-test.v", "-test.count", "1"}); err != nil {
t.Error(err)
}
}