-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags_test.go
More file actions
148 lines (145 loc) · 3.96 KB
/
flags_test.go
File metadata and controls
148 lines (145 loc) · 3.96 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"flag"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestUsage(t *testing.T) {
t.Parallel()
tests := []struct{
name string
args []string
wantGoMod string
wantCoverProfile string
wantPath string
wantOut string
stdErr string
err string // zero value means no error expected (err113)
}{
{
name: "valid",
args: []string{
"-gomod", "foo",
"-coverprofile", "bar",
"-path", "baz",
},
wantGoMod: "foo",
wantCoverProfile: "bar",
wantPath: "baz",
},
{
name: "missing -gomod",
err: "no value specified for -gomod",
wantOut: strings.Join([]string{
"missing -gomod usage:",
"",
" -coverprofile string",
" path to Go test coverage profile file",
" -gomod string",
" path to the root go.mod file",
" -path string",
" path where HTML files will be written",
"\n"}, "\n"),
},
{
name: "missing -coverprofile",
args: []string{"-gomod", "foo"},
err: "no value specified for -coverprofile",
wantOut: strings.Join([]string{
"missing -coverprofile usage:",
"",
" -coverprofile string",
" path to Go test coverage profile file",
" -gomod string",
" path to the root go.mod file",
" -path string",
" path where HTML files will be written",
"\n"}, "\n"),
},
{
name: "missing -path",
args: []string{
"-gomod", "foo",
"-coverprofile", "bar",
},
err: "no value specified for -path",
wantOut: strings.Join([]string{
"missing -path usage:",
"",
" -coverprofile string",
" path to Go test coverage profile file",
" -gomod string",
" path to the root go.mod file",
" -path string",
" path where HTML files will be written",
"\n"}, "\n"),
},
{
name: "ignored args",
args: []string{
"-gomod", "foo",
"-coverprofile", "bar",
"-path", "baz",
"bug",
"boo",
},
wantGoMod: "foo",
wantCoverProfile: "bar",
wantPath: "baz",
stdErr: "ignored arguments: bug, boo\n",
},
{
name: "invalid",
args: []string{"-invalid"},
wantOut: strings.Join([]string{
"flag provided but not defined: -invalid",
"invalid usage:",
"",
" -coverprofile string",
" path to Go test coverage profile file",
" -gomod string",
" path to the root go.mod file",
" -path string",
" path where HTML files will be written",
"\n"}, "\n"),
err: "flag provided but not defined: -invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
gotOut := new(bytes.Buffer)
gotErr := new(bytes.Buffer)
fs := flag.NewFlagSet(tt.name, flag.ContinueOnError)
fs.SetOutput(gotOut)
var err error
var gotGoMod, gotCoverProfile, gotPath string
gotGoMod, gotCoverProfile, gotPath, err = flags(fs, tt.args, gotErr)
if tt.err != "" {
if err == nil {
t.Errorf("flags(%q) did not fail", tt.name)
}
if tt.err != err.Error() {
t.Errorf("flags(%q) returned %q; expected %q\n", tt.name, err, tt.err)
}
}
if diff := cmp.Diff(tt.stdErr, gotErr.String()); diff != "" {
t.Errorf("flags(%q) stderr mismatch (-want +got):\n%s", tt.name, diff)
}
if diff := cmp.Diff(tt.wantOut, gotOut.String()); diff != "" {
t.Errorf("flags(%q) usage message mismatch (-want +got):\n%s", tt.name, diff)
}
if diff := cmp.Diff(tt.wantGoMod, gotGoMod); diff != "" {
t.Errorf("flags(%q) goMod mismatch (-want +got):\n%s", tt.name, diff)
}
if diff := cmp.Diff(tt.wantCoverProfile, gotCoverProfile); diff != "" {
t.Errorf("flags(%q) coverProfile mismatch (-want +got):\n%s", tt.name, diff)
}
if diff := cmp.Diff(tt.wantPath, gotPath); diff != "" {
t.Errorf("flags(%q) path mismatch (-want +got):\n%s", tt.name, diff)
}
})
}
}