-
Notifications
You must be signed in to change notification settings - Fork 1
/
help_test.go
109 lines (99 loc) · 1.97 KB
/
help_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
package cli
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCLIWritesHelp(t *testing.T) {
b := &strings.Builder{}
cli := CLI{
HelpWriter: b,
}
err := cli.New("test", nil).
ParseArgs([]string{"--help"}).
Run()
assert.Equal(t, err, ErrHelp)
assert.NotEmpty(t, b.String())
}
func TestCLIInvalidUsageWritesHelp(t *testing.T) {
b := &strings.Builder{}
cli := CLI{
HelpWriter: b,
}
err := cli.New("test", nil).
ParseArgs([]string{"--undefined"}).
Run()
assert.Error(t, err)
assert.NotEmpty(t, b.String())
}
type helpTestCommand struct {
beforeErr error
runErr error
}
func (cmd *helpTestCommand) Before() error {
return cmd.beforeErr
}
func (cmd *helpTestCommand) Run() error {
return cmd.runErr
}
func TestCLIUsageErrors(t *testing.T) {
boom := fmt.Errorf("boom!")
testCases := []struct {
beforeErr error
runErr error
shouldPrintHelp bool
}{
{
beforeErr: nil,
runErr: nil,
shouldPrintHelp: false,
},
{
beforeErr: boom,
runErr: nil,
shouldPrintHelp: false,
},
{
beforeErr: nil,
runErr: boom,
shouldPrintHelp: false,
},
{
beforeErr: UsageError(boom),
runErr: nil,
shouldPrintHelp: true,
},
{
beforeErr: nil,
runErr: UsageError(boom),
shouldPrintHelp: true,
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
b := &strings.Builder{}
cli := CLI{
HelpWriter: b,
}
cmd := &helpTestCommand{
beforeErr: testCase.beforeErr,
runErr: testCase.runErr,
}
err := cli.New("test", cmd).
ParseArgs([]string{}).
Run()
if testCase.beforeErr != nil {
assert.Equal(t, testCase.beforeErr, err)
}
if testCase.runErr != nil {
assert.Equal(t, testCase.runErr, err)
}
if testCase.shouldPrintHelp {
assert.NotEmpty(t, b.String())
} else {
assert.Empty(t, b.String())
}
})
}
}