-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclop_env_test.go
160 lines (142 loc) · 3.71 KB
/
clop_env_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
package clop
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type testEnvHelp struct {
Number int `clop:"long;env=NUMBER" usage:"number all output lines"`
ShowEnds bool `clop:"-E;long;env=SHOW_ENDS" usage:"display $ at end of each line"`
}
type testEnv2Help struct {
Number int `clop:"long;env=NUMBER" usage:"number all output lines"`
ShowEnds bool `clop:"-E;long;env=SHOW_ENDS" usage:"display $ at end of each line"`
}
// 常规env help测试
func Test_Env_usage(t *testing.T) {
te := testEnvHelp{}
var out bytes.Buffer
c := New([]string{"--help"}).SetExit(false).SetOutput(&out)
c.Bind(&te)
assert.NotEqual(t, -1, bytes.Index(out.Bytes(), []byte("Environment Variable:")))
}
// 简写env help测试
func Test_Env2_usage(t *testing.T) {
te := testEnv2Help{}
var out bytes.Buffer
c := New([]string{"--help"}).SetExit(false).SetOutput(&out)
c.Bind(&te)
assert.NotEqual(t, -1, bytes.Index(out.Bytes(), []byte("Environment Variable:")))
}
// 测试常规用法环境变量
func Test_API_env(t *testing.T) {
type env struct {
Url []string `clop:"-u; --url; env=CLOP-TEST-URL" usage:"URL to work with"`
Debug bool `clop:"-d; --debug; env=CLOP-DEBUG" usage:"debug"`
MaxLine int `clop:"env=CLOP-MAXLINE" usage:"test int"`
}
for _, test := range []testAPI{
{
func() env {
e := env{}
defer func() {
os.Unsetenv("CLOP-TEST-URL")
os.Unsetenv("CLOP-DEBUG")
}()
os.Setenv("CLOP-TEST-URL", "godoc.org")
err := os.Setenv("CLOP-DEBUG", "")
assert.NoError(t, err)
p := New([]string{"-u", "qq.com", "-u", "baidu.com"}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{Url: []string{"qq.com", "baidu.com", "godoc.org"}, Debug: true},
},
{
func() env {
defer func() {
os.Unsetenv("CLOP-MAXLINE")
}()
err := os.Setenv("CLOP-MAXLINE", "3")
assert.NoError(t, err)
e := env{}
p := New([]string{}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{MaxLine: 3},
},
{
func() env {
defer func() {
os.Unsetenv("CLOP-DEBUG")
}()
err := os.Setenv("CLOP-DEBUG", "false")
assert.NoError(t, err)
e := env{}
p := New([]string{}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{},
},
} {
assert.Equal(t, test.need, test.got)
}
}
func Test_API_env2(t *testing.T) {
type env struct {
ClopTestUrl []string `clop:"-u; --url; env" usage:"URL to work with"`
ClopDebug bool `clop:"-d; --debug; env" usage:"debug"`
ClopMaxline int `clop:"env" usage:"test int"`
}
for _, test := range []testAPI{
{
func() env {
e := env{}
defer func() {
os.Unsetenv("CLOP_TEST_URL")
os.Unsetenv("CLOP_DEBUG")
}()
os.Setenv("CLOP_TEST_URL", "godoc.org")
err := os.Setenv("CLOP_DEBUG", "")
assert.NoError(t, err)
p := New([]string{"-u", "qq.com", "-u", "baidu.com"}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{ClopTestUrl: []string{"qq.com", "baidu.com", "godoc.org"}, ClopDebug: true},
},
{
func() env {
defer func() {
os.Unsetenv("CLOP_MAXLINE")
}()
err := os.Setenv("CLOP_MAXLINE", "3")
assert.NoError(t, err)
e := env{}
p := New([]string{}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{ClopMaxline: 3},
},
{
func() env {
defer func() {
os.Unsetenv("CLOP_DEBUG")
}()
err := os.Setenv("CLOP_DEBUG", "false")
assert.NoError(t, err)
e := env{}
p := New([]string{}).SetExit(false)
err = p.Bind(&e)
assert.NoError(t, err)
return e
}(), env{},
},
} {
assert.Equal(t, test.need, test.got)
}
}