-
Notifications
You must be signed in to change notification settings - Fork 10
/
help_tmpl_test.go
125 lines (109 loc) · 3.37 KB
/
help_tmpl_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
package clop
import (
"bytes"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func checkUsage(b *bytes.Buffer) bool {
buf := b.Bytes()
lines := bytes.Split(buf, []byte("\n"))
haveUsage := false
for _, l := range lines {
l = bytes.TrimSpace(l)
if bytes.HasPrefix(l, []byte("Usage:")) {
haveUsage = true
continue
}
if haveUsage {
if bytes.Index(l, []byte("<Subcommand>")) == -1 {
return false
}
return true
}
}
return false
}
func haveDefaultInfo(b []byte) bool {
return bytes.Index(b, []byte("default")) != -1
}
func Test_Usage_tmpl_CloseDefault(t *testing.T) {
ShowUsageDefault = false
defer func() { ShowUsageDefault = true }()
help := Help{
ProcessName: "test",
Version: "clop v0.0.1",
About: "guonaihong development",
//Usage: "--output <output> [--] [FILE]...",
Flags: []showOption{
{Opt: "-d, --debug", Usage: "Activate debug mode", Env: "DEBUG=", Default: "true"},
{"-h, --help", "Prints help information", "", ""},
{"-V, --version", "Prints version information", "", ""},
{"-v, --verbose", "Verbose mode (-v, -vv, -vvv, etc.)", "", ""},
},
Options: []showOption{
{Opt: "-l, --level <level>...", Usage: "admin_level to consider", Env: "LEVEL=debug", Default: "info"},
{"-c, --nb-cars <nb-cars>", "Number of cars", "", ""},
{"-o, --output <output>", "Output file", "", ""},
{"-s, --speed <speed>", "-s, --speed <speed>", "", ""},
},
Args: []showOption{
{"<api-url>", "[env: API_URL=]", "", ""},
{"<FILE>...", "Files to process", "", ""},
},
Subcommand: []showOption{
{"add", "Add file contents to the index", "", ""},
{"mv", "Move or rename a file, a directory, or a symlink", "", ""},
},
MaxNameLen: 30,
ShowUsageDefault: ShowUsageDefault,
}
b := bytes.Buffer{}
w := io.MultiWriter(os.Stdout, &b)
tmpl := newTemplate()
err := tmpl.Execute(w, help)
assert.NoError(t, err)
assert.False(t, haveDefaultInfo(b.Bytes()))
assert.True(t, checkUsage(&b))
}
func Test_Usage_tmpl(t *testing.T) {
help := Help{
ProcessName: "test",
Version: "v0.0.1",
About: "guonaihong development",
//Usage: "--output <output> [--] [FILE]...",
Flags: []showOption{
{Opt: "-d, --debug", Usage: "Activate debug mode", Env: "DEBUG=", Default: "true"},
{"-h, --help", "Prints help information", "", ""},
{"-V, --version", "Prints version information", "", ""},
{"-v, --verbose", "Verbose mode (-v, -vv, -vvv, etc.)", "", ""},
},
Options: []showOption{
{Opt: "-l, --level <level>...", Usage: "admin_level to consider", Env: "LEVEL=debug", Default: "info"},
{"-c, --nb-cars <nb-cars>", "Number of cars", "", ""},
{"-o, --output <output>", "Output file", "", ""},
{"-s, --speed <speed>", "-s, --speed <speed>", "", ""},
},
Args: []showOption{
{"<api-url>", "[env: API_URL=]", "", ""},
{"<FILE>...", "Files to process", "", ""},
},
Envs: []showOption{
{"OMP_NUM_THREAD", "omp num thread", "", ""},
},
Subcommand: []showOption{
{"add", "Add file contents to the index", "", ""},
{"mv", "Move or rename a file, a directory, or a symlink", "", ""},
},
MaxNameLen: 30,
ShowUsageDefault: ShowUsageDefault,
}
b := bytes.Buffer{}
w := io.MultiWriter(os.Stdout, &b)
tmpl := newTemplate()
err := tmpl.Execute(w, help)
assert.NoError(t, err)
assert.True(t, haveDefaultInfo(b.Bytes()))
assert.True(t, checkUsage(&b))
}