forked from hidevopsio/hiboot
/
testapplication.go
47 lines (39 loc) · 1.03 KB
/
testapplication.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
package cli
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
)
// TestApplication the interface of cli test application
type TestApplication interface {
Initialize() error
SetProperty(name string, value ...interface{}) TestApplication
Run(args ...string) (output string, err error)
}
type testApplication struct {
application
}
// NewTestApplication is the test application constructor
func NewTestApplication(t *testing.T, cmd ...interface{}) TestApplication {
a := new(testApplication)
err := a.initialize(cmd...)
assert.Equal(t, nil, err)
err = a.build()
assert.Equal(t, nil, err)
return a
}
// SetProperty set application property
func (a *testApplication) SetProperty(name string, value ...interface{}) TestApplication {
a.BaseApplication.SetProperty(name, value...)
return a
}
func (a *testApplication) Run(args ...string) (output string, err error) {
buf := new(bytes.Buffer)
if a.root != nil {
a.root.SetOutput(buf)
a.root.SetArgs(args)
_, err = a.root.ExecuteC()
output = buf.String()
}
return
}