-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
188 lines (150 loc) · 4.84 KB
/
Copy pathenv_test.go
File metadata and controls
188 lines (150 loc) · 4.84 KB
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
package env_test
import (
"context"
"fmt"
"log"
"os"
"sync"
"syscall"
"testing"
"time"
"github.com/zxdev/env"
)
func TestEnv(t *testing.T) {
type Action struct {
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
Secret string `env:"hidden" help:"the shared secret"`
Show bool `default:"on" help:"show the processing values"`
Seg []string `env:"-"` // args segments
Path *env.Path `env:"-"` // path params
}
var a Action
a.Path = env.NewEnv(&a)
}
func TestHelp(t *testing.T) {
type Action struct {
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
Secret string `env:"hidden" help:"the shared secret"`
Show bool `default:"on" help:"show the processing values"`
Seg []string `env:"-"` // args segments
Path *env.Path `env:"-"` // path params
}
// spoof help request
os.Args = []string{"test", "help"}
// we have to set opt.NoExit so this test will operate
var a Action
a.Path = env.NewEnv(&env.Options{NoExit: true}, &a)
}
func TestVersion(t *testing.T) {
type Action struct {
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
Secret string `env:"hidden" help:"the shared secret"`
Show bool `default:"on" help:"show the processing values"`
Seg []string `env:"-"` // args segments
Path *env.Path `env:"-"` // path params
}
// spoof version request
os.Args = []string{"test", "version"}
env.Version = "test.0.0.0"
env.Build = "abc"
// we have to set opt.NoExit so this test will operate
var a Action
a.Path = env.NewEnv(&env.Options{NoExit: true}, &a)
}
// TestNakedBool exercises the naked-bool flag form: a "-flag" with no value
// sets the bool true (at eof, before another flag, or before a bare token it
// must not swallow), the alias works the same, and -flag:off / -flag=false
// still disable it.
func TestNakedBool(t *testing.T) {
type cfg struct {
TLS bool `env:"t" help:"enable TLS"`
Since string `env:"s" help:"a value flag"`
}
cases := []struct {
name string
args []string
tls bool
}{
{"naked at eof", []string{"prog", "-tls"}, true},
{"naked before bare token", []string{"prog", "-tls", "foo"}, true},
{"alias naked", []string{"prog", "-t"}, true},
{"explicit colon off", []string{"prog", "-tls:off"}, false},
{"explicit equals false", []string{"prog", "-tls=false"}, false},
{"absent keeps default", []string{"prog"}, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
os.Args = tc.args
var c cfg
env.NewEnv(&env.Options{NoExit: true, Silent: true}, &c)
if c.TLS != tc.tls {
t.Fatalf("TLS = %v, want %v", c.TLS, tc.tls)
}
})
}
// a naked bool must not swallow the following value flag: -tls leaves
// -since to parse normally rather than consuming it as tls's value
os.Args = []string{"prog", "-tls", "-since", "48h"}
var c cfg
env.NewEnv(&env.Options{NoExit: true, Silent: true}, &c)
if !c.TLS || c.Since != "48h" {
t.Fatalf("TLS=%v Since=%q, want true and %q", c.TLS, c.Since, "48h")
}
}
// TestNakedBoolDefaultOff guards the default:"on" + -flag:off path: a naked
// presence-only flag never resets a defaulted-true bool.
func TestNakedBoolDefaultOff(t *testing.T) {
type cfg struct {
Quiet bool `default:"on" help:"suppress output"`
}
os.Args = []string{"prog"} // no flag -> default holds
var c cfg
env.NewEnv(&env.Options{NoExit: true, Silent: true}, &c)
if !c.Quiet {
t.Fatalf("Quiet = %v, want true (default:on)", c.Quiet)
}
os.Args = []string{"prog", "-quiet:off"}
c = cfg{}
env.NewEnv(&env.Options{NoExit: true, Silent: true}, &c)
if c.Quiet {
t.Fatalf("Quiet = %v, want false (-quiet:off)", c.Quiet)
}
}
type Action struct{}
func (a *Action) Start(ctx context.Context) {
log.Println("action: start entry")
defer log.Println("action: start exit")
<-ctx.Done()
}
func (a *Action) Init00() {
defer log.Println("action: init00")
}
func (a *Action) Init01(ctx context.Context, init *sync.WaitGroup) {
log.Println("action: init01 entry")
defer log.Println("action: init01 exit")
init.Done()
<-ctx.Done()
}
func (a *Action) Init02(ctx context.Context) {
log.Println("action: init02 start")
defer log.Println("action: init02 stop")
time.Sleep(time.Second * 5)
<-ctx.Done()
}
func TestGraceInit(t *testing.T) {
var a Action
grace := env.NewGraceful().Init(a.Init00, a.Init01)
defer grace.Shutdown()
grace.Register(func() { fmt.Println("bye-bye") })
// generate a SIGTERM after 5s so the graceful controller exercises its
// signal-driven shutdown path and the deferred Shutdown() unblocks rather
// than waiting on a termination signal that never arrives
go func() {
time.Sleep(5 * time.Second)
if p, err := os.FindProcess(os.Getpid()); err == nil {
p.Signal(syscall.SIGTERM)
}
}()
t.Log("grace.Done()")
grace.Wait()
}