-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathusage_test.go
128 lines (104 loc) · 3.69 KB
/
usage_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
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.
package cli_test
import (
"os"
"testing"
"github.com/teris-io/cli"
)
func setupUsageApp() cli.App {
co := cli.NewCommand("checkout", "Check out a branch or revision").
WithShortcut("co").
WithArg(cli.NewArg("revision", "branch or revision to checkout")).
WithArg(cli.NewArg("fallback", "branch to fallback").AsOptional()).
WithOption(cli.NewOption("branch", "create branch if missing").WithChar('b').WithType(cli.TypeBool)).
WithAction(func(args []string, options map[string]string) int {
return 25
}).
WithCommand(cli.NewCommand("sub-cmd1", "First sub-command")).
WithCommand(cli.NewCommand("sub-cmd2", "Second sub-command"))
rmt := cli.NewCommand("remote", "Work with git remotes")
return cli.New("git tool").
WithCommand(co).
WithCommand(rmt).
WithOption(cli.NewOption("verbose", "Verbose execution").WithChar('v').WithType(cli.TypeBool))
}
type stringwriter struct {
str string
}
func (s *stringwriter) Write(p []byte) (n int, err error) {
s.str = s.str + string(p)
return len(p), nil
}
func TestApp_Usage_NestedCommandHelp_ok(t *testing.T) {
a := setupUsageApp()
w := &stringwriter{}
a.Run([]string{"./foo", "co", "-hb", "5.5.5"}, w)
expected := `foo checkout [--verbose] [--branch] <revision> [fallback]
Description:
Check out a branch or revision
Arguments:
revision branch or revision to checkout
fallback branch to fallback, optional
Options:
-v, --verbose Verbose execution
-b, --branch create branch if missing
Sub-commands:
foo checkout sub-cmd1 First sub-command
foo checkout sub-cmd2 Second sub-command
`
assertAppUsageOk(t, expected, w.str)
}
func TestApp_Usage_NestedCommandParsginError_ok(t *testing.T) {
a := setupUsageApp()
w := &stringwriter{}
a.Run([]string{"./foo", "co"}, w)
expected := `fatal: missing required argument revision
usage: foo checkout [--verbose] [--branch] <revision> [fallback]
`
assertAppUsageOk(t, expected, w.str)
}
func TestApp_Usage_TopWithNoAction(t *testing.T) {
a := setupUsageApp()
w := &stringwriter{}
code := a.Run([]string{"./foo"}, w)
if code != 1 {
t.Errorf("expected exit code 1, found %v", code)
}
expected := `foo [--verbose]
Description:
git tool
Options:
-v, --verbose Verbose execution
Sub-commands:
foo checkout Check out a branch or revision, shortcut: co
foo remote Work with git remotes
`
assertAppUsageOk(t, expected, w.str)
}
func TestApp_Usage_README(t *testing.T) {
co := cli.NewCommand("checkout", "checkout a branch or revision").
WithShortcut("co").
WithArg(cli.NewArg("revision", "branch or revision to checkout")).
WithOption(cli.NewOption("branch", "Create branch if missing").WithChar('b').WithType(cli.TypeBool)).
WithOption(cli.NewOption("upstream", "Set upstream for the branch").WithChar('u').WithType(cli.TypeBool)).
WithAction(func(args []string, options map[string]string) int {
// do something
return 0
})
add := cli.NewCommand("add", "add a remote").
WithArg(cli.NewArg("remote", "remote to add"))
rmt := cli.NewCommand("remote", "Work with git remotes").
WithCommand(add)
app := cli.New("git tool").
WithOption(cli.NewOption("verbose", "Verbose execution").WithChar('v').WithType(cli.TypeBool)).
WithCommand(co).
WithCommand(rmt)
// no action attached, just print usage when executed
app.Run([]string{"./gitc", "co", "-f", "dev"}, os.Stdout)
}
func assertAppUsageOk(t *testing.T, expectedOutput, actualOutput string) {
if expectedOutput != actualOutput {
t.Errorf("expected output: %v, found: %v", expectedOutput, actualOutput)
}
}