-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathroot_test.go
More file actions
169 lines (155 loc) · 3.43 KB
/
Copy pathroot_test.go
File metadata and controls
169 lines (155 loc) · 3.43 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cmd
import (
"bytes"
"fmt"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsAuthExempt(t *testing.T) {
tests := []struct {
name string
cmd *cobra.Command
expected bool
}{
{
name: "root command is exempt",
cmd: rootCmd,
expected: true,
},
{
name: "login command is exempt",
cmd: loginCmd,
expected: true,
},
{
name: "logout command is exempt",
cmd: logoutCmd,
expected: true,
},
{
name: "top-level create command is exempt",
cmd: createCmd,
expected: true,
},
{
name: "browser-pools create subcommand requires auth",
cmd: browserPoolsCreateCmd,
expected: false,
},
{
name: "browsers create subcommand requires auth",
cmd: browsersCreateCmd,
expected: false,
},
{
name: "profiles create subcommand requires auth",
cmd: profilesCreateCmd,
expected: false,
},
{
name: "browser-pools list requires auth",
cmd: browserPoolsListCmd,
expected: false,
},
{
name: "browsers list requires auth",
cmd: browsersListCmd,
expected: false,
},
{
name: "deploy command requires auth",
cmd: deployCmd,
expected: false,
},
{
name: "invoke command requires auth",
cmd: invokeCmd,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isAuthExempt(tt.cmd)
assert.Equal(t, tt.expected, result, "isAuthExempt(%s) = %v, want %v", tt.cmd.Name(), result, tt.expected)
})
}
}
func TestRootCommandSubcommands(t *testing.T) {
expected := []string{
"app",
"browser-pools",
"browsers",
"create",
"credentials",
"deploy",
"extensions",
"invoke",
"login",
"logout",
"profiles",
"proxies",
}
registered := make(map[string]bool)
for _, sub := range rootCmd.Commands() {
registered[sub.Name()] = true
}
for _, name := range expected {
assert.True(t, registered[name], "expected subcommand %q to be registered on rootCmd", name)
}
}
func TestRootCommandHelpOutput(t *testing.T) {
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"--help"})
err := rootCmd.Execute()
require.NoError(t, err)
output := buf.String()
assert.Contains(t, output, "kernel")
assert.Contains(t, output, "deploy")
assert.Contains(t, output, "invoke")
assert.Contains(t, output, "browsers")
}
func TestLogLevelToPterm(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"trace", "trace"},
{"debug", "debug"},
{"info", "info"},
{"warn", "warn"},
{"error", "error"},
{"fatal", "fatal"},
{"print", "print"},
{"garbage", "info"},
{"", "info"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := logLevelToPterm(tt.input)
expected := logLevelToPterm(tt.expected)
assert.Equal(t, expected, got)
})
}
}
func TestIsUsageError(t *testing.T) {
tests := []struct {
name string
err string
expected bool
}{
{"unknown flag", "unknown flag: --foo", true},
{"unknown command", "unknown command \"bogus\"", true},
{"invalid argument", "invalid argument \"x\" for \"--count\"", true},
{"random error", "connection refused", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := fmt.Errorf(tt.err)
assert.Equal(t, tt.expected, isUsageError(err))
})
}
}