-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
88 lines (78 loc) · 2.31 KB
/
Copy pathintegration_test.go
File metadata and controls
88 lines (78 loc) · 2.31 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
84
85
86
87
88
package urfave_cli_integration
import (
"testing"
"github.com/componego/componego"
"github.com/componego/componego/impl/application"
"github.com/urfave/cli/v2"
)
func TestRunApplication(t *testing.T) {
currentArgs := cliArgs
t.Cleanup(func() {
cliArgs = currentArgs
})
cliArgs = []string{"application"}
t.Run("basic test", func(t *testing.T) {
expectedExitCode := 123
called := false
appFactory := application.NewFactory("Test Application")
appFactory.SetApplicationAction(func(_ componego.Environment, options any) (int, error) {
if _, ok := options.(*cli.Context); !ok {
t.Fatal("context not found")
}
called = true
return expectedExitCode, nil
})
actualExitCode := Run(appFactory.Build(), componego.TestMode)
if expectedExitCode != actualExitCode {
t.Fatal("wrong exit code")
}
if !called {
t.Fatal("the required function was not called")
}
})
t.Run("command line configuration", func(t *testing.T) {
called := false
app := &testApplication{
applicationCLIFunc: func(cmd *cli.Command) {
cmd.Usage = "usage text"
},
applicationAction: func(_ componego.Environment, options any) (int, error) {
ctxCli, ok := options.(*cli.Context)
if !ok {
t.Fatal("context not found")
}
if ctxCli.Command.Usage != "usage text" {
t.Fatal("data doesn't match")
}
called = true
return componego.SuccessExitCode, nil
},
}
if Run(app, componego.TestMode) != componego.SuccessExitCode {
t.Fatal("wrong exit code")
}
if !called {
t.Fatal("the required function was not called")
}
})
}
type testApplication struct {
applicationCLIFunc func(cmd *cli.Command)
applicationAction func(env componego.Environment, options any) (int, error)
}
// ApplicationName belongs to interface componego.Application.
func (t *testApplication) ApplicationName() string {
return "Test App v0.0.1"
}
// ApplicationCLI belongs to interface ApplicationCLI.
func (t *testApplication) ApplicationCLI(cmd *cli.Command) {
t.applicationCLIFunc(cmd)
}
// ApplicationAction belongs to interface componego.Application.
func (t *testApplication) ApplicationAction(env componego.Environment, options any) (int, error) {
return t.applicationAction(env, options)
}
var (
_ componego.Application = (*testApplication)(nil)
_ ApplicationCLI = (*testApplication)(nil)
)