-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
172 lines (157 loc) · 3.92 KB
/
main_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"errors"
"flag"
"strings"
"testing"
"github.com/baalimago/go_away_boilerplate/pkg/testboil"
"github.com/baalimago/wd-41/cmd"
)
type MockCommand struct {
runFunc func(context.Context) error
helpFunc func() string
describeFunc func() string
}
func (m MockCommand) Run(ctx context.Context) error {
return m.runFunc(ctx)
}
func (m MockCommand) Help() string {
return m.helpFunc()
}
func (m MockCommand) Describe() string {
return m.describeFunc()
}
func (m MockCommand) Setup() error {
return nil
}
func (m MockCommand) Flagset() *flag.FlagSet {
return flag.NewFlagSet("test", flag.ContinueOnError)
}
func Test_printHelp_ExitCodes(t *testing.T) {
mCmd := MockCommand{
helpFunc: func() string { return "Help message" },
describeFunc: func() string { return "Describe message" },
}
tests := []struct {
name string
command cmd.Command
err error
expected int
}{
{
name: "It should exit with code 1 on ArgNotFoundError",
command: mCmd,
err: cmd.ArgNotFoundError("test"),
expected: 1,
},
{
name: "it should exit with code 0 on HelpfulError",
command: mCmd,
err: cmd.ErrHelpful,
expected: 0,
},
{
name: "it should exit with code 1 on unknown errors",
command: mCmd,
err: errors.New("unknown error"),
expected: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := printHelp(tt.command, tt.err, func() {})
if result != tt.expected {
t.Errorf("printHelp() = %v, want %v", result, tt.expected)
}
})
}
}
func Test_printHelp_output(t *testing.T) {
t.Run("it should print cmd help on cmd.HelpfulError", func(t *testing.T) {
want := "hello here is helpful message"
mCmd := MockCommand{
helpFunc: func() string { return want },
}
got := testboil.CaptureStdout(t, func(t *testing.T) {
t.Helper()
printHelp(mCmd, cmd.ErrHelpful, func() {})
})
// add the printline since we actually want a newline at end
want = want + "\n"
if got != want {
t.Fatalf("expected: '%v', got: '%v'", want, got)
}
})
t.Run("it should print error and usage on invalid argument", func(t *testing.T) {
wantErr := "here is an error message"
wantCode := 1
usageHasBenePrinted := false
mockUsagePrinter := func() {
usageHasBenePrinted = true
}
gotCode := 0
gotStdErr := testboil.CaptureStderr(t, func(t *testing.T) {
t.Helper()
gotCode = printHelp(MockCommand{}, cmd.ArgNotFoundError(wantErr), mockUsagePrinter)
})
if gotCode != wantCode {
t.Fatalf("expected: %v, got: %v", wantCode, gotCode)
}
if !usageHasBenePrinted {
t.Fatal("expected usage to have been printed")
}
if !strings.Contains(gotStdErr, wantErr) {
t.Fatalf("expected stdout to contain: '%v', got out: '%v'", wantErr, gotStdErr)
}
})
}
func Test_Run_ExitCodes(t *testing.T) {
tests := []struct {
name string
args []string
argParser cmd.ArgParser
expected int
}{
{
name: "on invalid arg, it should return exit code 1",
args: []string{"invalid"},
argParser: func(s []string) (cmd.Command, error) {
return nil, errors.New("some error")
},
expected: 1,
},
{
name: "on run error, it should return exit code 1",
args: []string{"valid"},
argParser: func(s []string) (cmd.Command, error) {
return MockCommand{
runFunc: func(ctx context.Context) error {
return errors.New("whopsidops, error ojoj..!")
},
}, nil
},
expected: 1,
},
{
name: "on success, error code 0",
args: []string{"valid"},
argParser: func(s []string) (cmd.Command, error) {
return MockCommand{
runFunc: func(ctx context.Context) error {
return nil
},
}, nil
},
expected: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := run(context.Background(), tt.args, tt.argParser)
if result != tt.expected {
t.Errorf("run() = %v, want %v", result, tt.expected)
}
})
}
}