|
| 1 | +package env_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/zxdev/env" |
| 11 | +) |
| 12 | + |
| 13 | +func TestEnv(t *testing.T) { |
| 14 | + |
| 15 | + type Action struct { |
| 16 | + Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"` |
| 17 | + Secret string `env:"hidden" help:"the shared secret"` |
| 18 | + Show bool `default:"on" help:"show the processing values"` |
| 19 | + |
| 20 | + Seg []string `env:"-"` // args segments |
| 21 | + Path *env.Path `env:"-"` // path params |
| 22 | + } |
| 23 | + |
| 24 | + var a Action |
| 25 | + a.Path = env.NewEnv(&a) |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +func TestHelp(t *testing.T) { |
| 30 | + |
| 31 | + type Action struct { |
| 32 | + Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"` |
| 33 | + Secret string `env:"hidden" help:"the shared secret"` |
| 34 | + Show bool `default:"on" help:"show the processing values"` |
| 35 | + |
| 36 | + Seg []string `env:"-"` // args segments |
| 37 | + Path *env.Path `env:"-"` // path params |
| 38 | + } |
| 39 | + |
| 40 | + // spoof help request |
| 41 | + os.Args = []string{"test", "help"} |
| 42 | + |
| 43 | + // we have to set opt.NoExit so this test will operate |
| 44 | + var a Action |
| 45 | + a.Path = env.NewEnv(&env.Options{NoExit: true}, &a) |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +func TestVersion(t *testing.T) { |
| 50 | + |
| 51 | + type Action struct { |
| 52 | + Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"` |
| 53 | + Secret string `env:"hidden" help:"the shared secret"` |
| 54 | + Show bool `default:"on" help:"show the processing values"` |
| 55 | + |
| 56 | + Seg []string `env:"-"` // args segments |
| 57 | + Path *env.Path `env:"-"` // path params |
| 58 | + } |
| 59 | + |
| 60 | + // spoof version request |
| 61 | + os.Args = []string{"test", "version"} |
| 62 | + env.Version = "test.0.0.0" |
| 63 | + env.Build = "abc" |
| 64 | + |
| 65 | + // we have to set opt.NoExit so this test will operate |
| 66 | + var a Action |
| 67 | + a.Path = env.NewEnv(&env.Options{NoExit: true}, &a) |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +type Action struct{} |
| 72 | + |
| 73 | +func (a *Action) Start(ctx context.Context) { |
| 74 | + log.Println("action: start entry") |
| 75 | + defer log.Println("action: start exit") |
| 76 | + <-ctx.Done() |
| 77 | +} |
| 78 | + |
| 79 | +func (a *Action) Init00() { |
| 80 | + log.Println("action: init00 entry") |
| 81 | +} |
| 82 | + |
| 83 | +func (a *Action) Init01(ctx context.Context, init *sync.WaitGroup) { |
| 84 | + log.Println("action: init01 entry") |
| 85 | + defer log.Println("action: init01 exit") |
| 86 | + defer init.Done() |
| 87 | + <-ctx.Done() |
| 88 | +} |
| 89 | + |
| 90 | +func (a *Action) Init02(ctx context.Context) func() { |
| 91 | + log.Println("action: init02 entry") |
| 92 | + return func() { |
| 93 | + defer log.Println("action: init92 exit") |
| 94 | + <-ctx.Done() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (a *Action) Init03(ctx context.Context) func() { |
| 99 | + log.Println("action: init03 entry") |
| 100 | + return func() { |
| 101 | + go func() { |
| 102 | + defer log.Println("action: init03 exit") |
| 103 | + <-ctx.Done() |
| 104 | + }() |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestGraceInit(t *testing.T) { |
| 109 | + |
| 110 | + var a Action |
| 111 | + grace := env.GraceInit(nil, a.Init00) |
| 112 | + defer grace.Wait() |
| 113 | + |
| 114 | + grace.Done() |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +func TestGraceInitContext(t *testing.T) { |
| 119 | + |
| 120 | + var a Action |
| 121 | + grace := env.GraceInitContext(nil, a.Init01) |
| 122 | + defer grace.SetExit(-1).Wait() |
| 123 | + |
| 124 | + grace.Done() |
| 125 | + |
| 126 | +} |
0 commit comments