-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
102 lines (90 loc) · 3.05 KB
/
Copy pathcommand_test.go
File metadata and controls
102 lines (90 loc) · 3.05 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
package env_test
import (
"os"
"testing"
"github.com/zxdev/env"
)
type pullCfg struct {
Path *env.Path `env:"-"`
Since string `env:"s" default:"24h" help:"lookback window"`
Limit int `default:"100" help:"max records"`
}
type exportCfg struct {
Path *env.Path `env:"-"`
Format string `env:"f" default:"json" help:"output format"`
}
func commands(pull *pullCfg, export *exportCfg) (*env.Path, string) {
return env.Commands(&env.Options{NoExit: true, Silent: true},
env.Command{Name: "pull", Help: "retrieve and stage records", Cfg: pull},
env.Command{Name: "export", Help: "emit processed records", Cfg: export},
)
}
// TestCommandsMenu spoofs a bare invocation; the top-level menu enumerates
// every subcommand and no command is selected.
func TestCommandsMenu(t *testing.T) {
os.Args = []string{"prog"}
var pull pullCfg
var export exportCfg
if _, name := commands(&pull, &export); name != "" {
t.Fatalf("menu: expected no command selected, got %q", name)
}
}
// TestCommandsHelp spoofs `prog help pull`; the drill-in selects pull and
// renders its field table without parsing/running it.
func TestCommandsHelp(t *testing.T) {
os.Args = []string{"prog", "help", "pull"}
var pull pullCfg
var export exportCfg
if _, name := commands(&pull, &export); name != "pull" {
t.Fatalf("help pull: expected %q, got %q", "pull", name)
}
}
// TestCommandsDispatch spoofs `prog pull -since 48h`; the subcommand word is
// consumed and Configure parses only pull's flags, leaving the default intact
// for unset fields.
func TestCommandsDispatch(t *testing.T) {
os.Args = []string{"prog", "pull", "-since", "48h"}
var pull pullCfg
var export exportCfg
_, name := commands(&pull, &export)
if name != "pull" {
t.Fatalf("dispatch: expected %q, got %q", "pull", name)
}
if pull.Since != "48h" {
t.Fatalf("dispatch: Since = %q, want %q", pull.Since, "48h")
}
if pull.Limit != 100 {
t.Fatalf("dispatch: Limit = %d, want default 100", pull.Limit)
}
}
// TestCommandsUnknown spoofs an unknown subcommand; nothing is selected and the
// menu is shown (NoExit prevents the os.Exit(2)).
func TestCommandsUnknown(t *testing.T) {
os.Args = []string{"prog", "nope"}
var pull pullCfg
var export exportCfg
if _, name := commands(&pull, &export); name != "" {
t.Fatalf("unknown: expected no command selected, got %q", name)
}
}
// TestCommandsRun spoofs `prog pull -since 12h`; the matched command's Run
// handler fires after Cfg is populated and receives the environment Path.
func TestCommandsRun(t *testing.T) {
os.Args = []string{"prog", "pull", "-since", "12h"}
var pull pullCfg
var ran bool
var gotPath *env.Path
env.Commands(&env.Options{NoExit: true, Silent: true},
env.Command{Name: "pull", Help: "retrieve and stage records", Cfg: &pull,
Run: func(path *env.Path) { ran, gotPath = true, path }},
)
if !ran {
t.Fatal("run: Run handler did not fire")
}
if gotPath == nil {
t.Fatal("run: Run handler received nil Path")
}
if pull.Since != "12h" {
t.Fatalf("run: Since = %q, want %q (Cfg must be populated before Run)", pull.Since, "12h")
}
}