Repository navigation
Expand file tree
/
Copy pathcmd_test.go
More file actions
83 lines (72 loc) · 1.73 KB
/
Copy pathcmd_test.go
File metadata and controls
83 lines (72 loc) · 1.73 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
package internal_test
import (
"errors"
"io"
"testing"
"github.com/alecthomas/kong"
"github.com/powerman/check"
"github.com/powerman/kongusage"
"go.uber.org/mock/gomock"
"github.com/powerman/go-scripts-example/scripts/toc/internal"
)
var errApp = errors.New("stub app error")
// kongRun parses args like main and runs the CLI with a.
func kongRun(t *check.C, a internal.App, args ...string) error {
t.Helper()
var cli internal.CLI
k, err := kong.New(&cli,
kong.Writers(io.Discard, io.Discard),
kong.Exit(func(int) {}),
)
t.Nil(err)
ctx, err := k.Parse(args)
if err != nil {
return err
}
ctx.BindTo(a, (*internal.App)(nil))
return ctx.Run()
}
func TestCLI_Run(tt *testing.T) {
tt.Parallel()
tests := []struct {
name string
args []string
want internal.Options
}{
{
name: "Defaults",
want: internal.Options{MinLevel: 2, MaxLevel: 3},
},
{
name: "All",
args: []string{"--check", "--min-level=1", "--max-level", "6", "a.md", "docs/b c.md"},
want: internal.Options{
Files: []string{"a.md", "docs/b c.md"},
MinLevel: 1,
MaxLevel: 6,
Check: true,
},
},
}
for _, tc := range tests {
tt.Run(tc.name, func(tt *testing.T) {
tt.Parallel()
t := check.T(tt).MustAll()
a := NewMockApp(gomock.NewController(t))
a.EXPECT().Update(tc.want).Return(errApp)
err := kongRun(t, a, tc.args...)
t.Err(err, errApp)
})
}
}
func TestCLI_Run_InvalidLevel(tt *testing.T) {
tt.Parallel()
t := check.T(tt).MustAll()
a := NewMockApp(gomock.NewController(t))
err := kongRun(t, a, "--max-level=7")
t.Match(err, `--max-level must be one of "1","2","3","4","5","6" but got "7"`)
}
func TestUsageSpec(t *testing.T) {
t.Parallel()
kongusage.CheckSpec(t, kong.Must(&internal.CLI{}))
}