-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_test.go
More file actions
99 lines (92 loc) · 2.67 KB
/
Copy pathhelp_test.go
File metadata and controls
99 lines (92 loc) · 2.67 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
package main
import (
"bytes"
"flag"
"strings"
"testing"
)
func helpOutput() string {
var b bytes.Buffer
usage(&b)
return b.String()
}
// Every flag the program defines must be documented in --help.
func TestHelpDocumentsEveryFlag(t *testing.T) {
out := helpOutput()
flag.VisitAll(func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, "test.") && !strings.Contains(out, "--"+f.Name) {
t.Errorf("--%s missing from --help", f.Name)
}
})
if strings.Contains(out, "%!") {
t.Fatal("broken format verb in help text")
}
}
// Every ./pingping example in --help must be accepted by the real parsers, so
// the help cannot drift from the code.
func TestHelpExamplesParse(t *testing.T) {
n := 0
for _, line := range strings.Split(helpOutput(), "\n") {
line = strings.TrimPrefix(strings.TrimSpace(line), "nohup ")
if !strings.HasPrefix(line, "./pingping") || strings.HasPrefix(line, "./pingping [") {
continue
}
n++
fs := flag.NewFlagSet("example", flag.ContinueOnError)
listen := fs.String("listen", defaultConfig().Listen, "")
local := fs.Bool("localhost", false, "")
fs.Bool("version", false, "")
args := strings.Fields(strings.SplitN(strings.SplitN(line, "#", 2)[0], ">", 2)[0])[1:]
if err := fs.Parse(args); err != nil {
t.Errorf("%q: %v", line, err)
continue
}
if _, err := parseAuthArgs(fs.Args()); err != nil {
t.Errorf("%q: %v", line, err)
}
if _, err := listenAddr(*listen, *local); err != nil {
t.Errorf("%q: %v", line, err)
}
}
if n < 5 {
t.Fatalf("only %d examples found", n)
}
}
// Every echo line in --help must be a valid target line for its list.
func TestHelpTargetLinesParse(t *testing.T) {
n := 0
for _, line := range strings.Split(helpOutput(), "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "echo \"") {
continue
}
n++
body := line[len(`echo "`) : strings.Index(line[len(`echo "`):], `"`)+len(`echo "`)]
typ := "icmp"
if strings.HasSuffix(line, "tcp.list") {
typ = "tcp"
}
if _, err := parseListLine(body, typ); err != nil {
t.Errorf("%q: %v", line, err)
}
}
if n < 4 {
t.Fatalf("only %d target examples found", n)
}
}
func TestWantsHelp(t *testing.T) {
for _, a := range [][]string{{"--help"}, {"-h"}, {"help"}, {"--listen", "0.0.0.0:1", "-help"}} {
if !wantsHelp(a) {
t.Errorf("%v: want help", a)
}
}
if wantsHelp([]string{"user=help", "passwd=x"}) {
t.Error("a user named help is not a help request")
}
}
func TestFlagAfterAuthArgsIsExplained(t *testing.T) {
_, err := parseAuthArgs([]string{"user=a", "passwd=b", "--listen", "0.0.0.0:9000"})
if err == nil || !strings.Contains(err.Error(), "put flags first") {
t.Fatalf("want a put-flags-first hint, got %v", err)
}
}