-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
71 lines (61 loc) · 1.52 KB
/
app_test.go
File metadata and controls
71 lines (61 loc) · 1.52 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
package commandline_test
import (
"bytes"
"errors"
"io"
"testing"
"github.com/spf13/cobra"
"github.com/wavesoftware/go-commandline"
"gotest.tools/v3/assert"
)
func TestExecuteOrDie(t *testing.T) {
var buf bytes.Buffer
var retcode int
var err error
commandline.New(new(testApp)).ExecuteOrDie(
commandline.WithCommand(func(cmd *cobra.Command) {
cmd.SetOut(&buf)
cmd.SetIn(bytes.NewBufferString("Input"))
cmd.SetArgs([]string{"arg1", "arg2"})
}),
commandline.WithExit(func(code int) {
retcode = code
}),
commandline.WithErrorHandler(func(merr error, _ *cobra.Command) bool {
err = merr
return false
}),
)
assert.Equal(t, `example Input: ["arg1" "arg2"]`, buf.String())
assert.Equal(t, 133, retcode)
assert.Assert(t, err != nil)
}
func TestExit(t *testing.T) {
app := commandline.App{CobraProvider: nil}
err := app.Execute()
assert.ErrorIs(t, err, commandline.ErrNoRootCommand)
app = commandline.App{CobraProvider: nilApp{}}
err = app.Execute()
assert.ErrorIs(t, err, commandline.ErrNoRootCommand)
}
var errExample = errors.New("example error")
type testApp struct{}
func (t testApp) Command() *cobra.Command {
return &cobra.Command{
Use: "example",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
in, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
cmd.Printf("%s %s: %q", cmd.Use, in, args)
return errExample
},
}
}
type nilApp struct{}
func (n nilApp) Command() *cobra.Command {
return nil
}