-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathmain_test.go
More file actions
69 lines (61 loc) · 1.62 KB
/
main_test.go
File metadata and controls
69 lines (61 loc) · 1.62 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
package main
import (
"bytes"
"testing"
"github.com/spf13/cobra"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/roxctl/common"
"github.com/stackrox/rox/roxctl/maincommand"
"github.com/stretchr/testify/assert"
)
func executeCommand(root *cobra.Command, args ...string) (output string, err error) {
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetErr(buf)
root.SetArgs(args)
err = root.Execute()
return buf.String(), err
}
func mockRun(_ *cobra.Command, _ []string) {}
func mockRunE(_ *cobra.Command, _ []string) error { return nil }
func TestCommandReconstruction(t *testing.T) {
root := maincommand.Command()
type testCase struct {
args []string
command string
}
for _, c := range []testCase{
{
[]string{"central", "--insecure", "whoami", "-e", "test"},
"central whoami",
},
{
[]string{"declarative-config", "create", "auth-provider", "iap", "--audience", "test"},
"declarative-config create auth-provider iap",
},
{
[]string{"central", "--insecure=false", "whoami"},
// endpoint is always 'changed' in the common/flags/endpoint.go.
"central whoami",
},
{
[]string{"central", "db", "restore", "positional_argument"},
"central db restore",
},
} {
var command string
once := sync.Once{}
common.PatchPersistentPreRunHooks(root, func(cmd *cobra.Command, _ []string) {
once.Do(func() {
command = getCommandPath(cmd)
})
// Do not actually run the command:
cmd.Run = mockRun
cmd.RunE = mockRunE
})
output, err := executeCommand(root, c.args...)
assert.NoError(t, err)
assert.Equal(t, "", output)
assert.Equal(t, c.command, command)
}
}