Skip to content

Commit 8f15aaa

Browse files
committed
add more testing
1 parent 83c6399 commit 8f15aaa

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type App struct {
4747
OnCommandNotFound func(*Context, string)
4848

4949
// Handler if panic in app.Action() and command.Action()
50-
OnActionPanic func(c *Context, err error)
50+
OnActionPanic func(*Context, error)
5151
}
5252

5353
// NewApp creates a new cli Application

app_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAppRun(t *testing.T) {
8+
run := false
9+
app := &App{
10+
Action: func(ctx *Context) {
11+
run = true
12+
},
13+
}
14+
15+
app.Run([]string{"app"})
16+
17+
if run == false {
18+
t.Fatal("no app run")
19+
}
20+
}
21+
22+
func TestAppRunCmd(t *testing.T) {
23+
run := false
24+
app := &App{
25+
Commands: []*Command{
26+
{
27+
Name: "cmd",
28+
Action: func(ctx *Context) {
29+
run = true
30+
},
31+
},
32+
},
33+
}
34+
35+
app.Run([]string{"app", "cmd"})
36+
37+
if run == false {
38+
t.Fatal("no command run")
39+
}
40+
}
41+
42+
func TestAppRunCmdNotFound(t *testing.T) {
43+
run := false
44+
app := &App{
45+
Commands: []*Command{
46+
{
47+
Name: "xx",
48+
},
49+
},
50+
OnCommandNotFound: func(*Context, string) {
51+
run = true
52+
},
53+
}
54+
55+
app.Run([]string{"app", "cmd", "xxx"})
56+
57+
if run == false {
58+
t.Fatal("OnCommandNotFound not hit")
59+
}
60+
}
61+
62+
func TestAppRunPanic(t *testing.T) {
63+
run := false
64+
app := &App{
65+
Action: func(ctx *Context) {
66+
panic("err")
67+
},
68+
OnActionPanic: func(*Context, error) {
69+
run = true
70+
},
71+
}
72+
73+
exit = func(int) {}
74+
75+
app.Run([]string{"app"})
76+
77+
if run == false {
78+
t.Fatal("OnActionPanic not hit")
79+
}
80+
}

context.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"strings"
88
)
99

10+
// exit variable for tesing hook
11+
var exit = os.Exit
12+
1013
// Context is a type that is passed through to
1114
// each Handler action in a cli application. Context
1215
// can be used to retrieve context-specific Args and
@@ -252,15 +255,15 @@ func (c *Context) ShowHelp() {
252255
// ShowHelpAndExit shows help and exit
253256
func (c *Context) ShowHelpAndExit(code int) {
254257
c.ShowHelp()
255-
os.Exit(code)
258+
exit(code)
256259
}
257260

258261
// ShowError shows error and exit(1)
259262
func (c *Context) ShowError(err error) {
260263
w := os.Stderr
261264
fmt.Fprintln(w, err)
262265
fmt.Fprintln(w, fmt.Sprintf("\nRun '%s --help' for more information", c.name))
263-
os.Exit(1)
266+
exit(1)
264267
}
265268

266269
func (c *Context) handlePanic() {
@@ -274,6 +277,6 @@ func (c *Context) handlePanic() {
274277
} else {
275278
os.Stderr.WriteString(fmt.Sprintf("fatal: %v\n", e))
276279
}
277-
os.Exit(1)
280+
exit(1)
278281
}
279282
}

context_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
)
77

8-
func TestContext(t *testing.T) {
8+
func TestContextGet(t *testing.T) {
99
c := &Context{
1010
flags: []*Flag{
1111
{Name: "f1"},
@@ -84,3 +84,31 @@ func TestContext(t *testing.T) {
8484
t.Errorf("f3 GetStringSlice is wrong, got: %v", got)
8585
}
8686
}
87+
88+
func TestContextArg(t *testing.T) {
89+
c := &Context{
90+
args: []string{"a", "b", "c"},
91+
}
92+
93+
if c.NArg() != 3 {
94+
t.Error("NArg() != 3")
95+
}
96+
if c.Arg(0) != "a" {
97+
t.Error("Arg(0) != 'a'")
98+
}
99+
if !reflect.DeepEqual(c.Args(), c.args) {
100+
t.Error("Args() is wrong")
101+
}
102+
}
103+
104+
func TestContextParent(t *testing.T) {
105+
p := &Context{name: "p"}
106+
c := &Context{parent: p}
107+
108+
if c.Parent().Name() != "p" {
109+
t.Error("Parent() is wrong")
110+
}
111+
if c.Global().Name() != "p" {
112+
t.Error("Global() is wrong")
113+
}
114+
}

0 commit comments

Comments
 (0)