-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
109 lines (81 loc) · 2.45 KB
/
env_test.go
File metadata and controls
109 lines (81 loc) · 2.45 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
package env_test
import (
"context"
"fmt"
"log"
"os"
"sync"
"testing"
"time"
"github.com/zxdev/env/v2"
)
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)
}
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") })
t.Log("grace.Done()")
grace.Wait()
}