File tree Expand file tree Collapse file tree 4 files changed +116
-5
lines changed Expand file tree Collapse file tree 4 files changed +116
-5
lines changed Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ type App struct {
47
47
OnCommandNotFound func (* Context , string )
48
48
49
49
// Handler if panic in app.Action() and command.Action()
50
- OnActionPanic func (c * Context , err error )
50
+ OnActionPanic func (* Context , error )
51
51
}
52
52
53
53
// NewApp creates a new cli Application
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 7
7
"strings"
8
8
)
9
9
10
+ // exit variable for tesing hook
11
+ var exit = os .Exit
12
+
10
13
// Context is a type that is passed through to
11
14
// each Handler action in a cli application. Context
12
15
// can be used to retrieve context-specific Args and
@@ -252,15 +255,15 @@ func (c *Context) ShowHelp() {
252
255
// ShowHelpAndExit shows help and exit
253
256
func (c * Context ) ShowHelpAndExit (code int ) {
254
257
c .ShowHelp ()
255
- os . Exit (code )
258
+ exit (code )
256
259
}
257
260
258
261
// ShowError shows error and exit(1)
259
262
func (c * Context ) ShowError (err error ) {
260
263
w := os .Stderr
261
264
fmt .Fprintln (w , err )
262
265
fmt .Fprintln (w , fmt .Sprintf ("\n Run '%s --help' for more information" , c .name ))
263
- os . Exit (1 )
266
+ exit (1 )
264
267
}
265
268
266
269
func (c * Context ) handlePanic () {
@@ -274,6 +277,6 @@ func (c *Context) handlePanic() {
274
277
} else {
275
278
os .Stderr .WriteString (fmt .Sprintf ("fatal: %v\n " , e ))
276
279
}
277
- os . Exit (1 )
280
+ exit (1 )
278
281
}
279
282
}
Original file line number Diff line number Diff line change 5
5
"testing"
6
6
)
7
7
8
- func TestContext (t * testing.T ) {
8
+ func TestContextGet (t * testing.T ) {
9
9
c := & Context {
10
10
flags : []* Flag {
11
11
{Name : "f1" },
@@ -84,3 +84,31 @@ func TestContext(t *testing.T) {
84
84
t .Errorf ("f3 GetStringSlice is wrong, got: %v" , got )
85
85
}
86
86
}
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
+ }
You can’t perform that action at this time.
0 commit comments