Skip to content

Commit 38f2eeb

Browse files
committed
Add tests for root command structure, help output, and utility functions
1 parent ddddef8 commit 38f2eeb

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

cmd/root_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package cmd
22

33
import (
4+
"bytes"
5+
"fmt"
46
"testing"
57

68
"github.com/spf13/cobra"
79
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
811
)
912

1013
func TestIsAuthExempt(t *testing.T) {
@@ -77,3 +80,90 @@ func TestIsAuthExempt(t *testing.T) {
7780
})
7881
}
7982
}
83+
84+
func TestRootCommandSubcommands(t *testing.T) {
85+
expected := []string{
86+
"app",
87+
"browser-pools",
88+
"browsers",
89+
"create",
90+
"credentials",
91+
"deploy",
92+
"extensions",
93+
"invoke",
94+
"login",
95+
"logout",
96+
"profiles",
97+
"proxies",
98+
}
99+
100+
registered := make(map[string]bool)
101+
for _, sub := range rootCmd.Commands() {
102+
registered[sub.Name()] = true
103+
}
104+
105+
for _, name := range expected {
106+
assert.True(t, registered[name], "expected subcommand %q to be registered on rootCmd", name)
107+
}
108+
}
109+
110+
func TestRootCommandHelpOutput(t *testing.T) {
111+
var buf bytes.Buffer
112+
rootCmd.SetOut(&buf)
113+
rootCmd.SetErr(&buf)
114+
rootCmd.SetArgs([]string{"--help"})
115+
116+
err := rootCmd.Execute()
117+
require.NoError(t, err)
118+
119+
output := buf.String()
120+
assert.Contains(t, output, "kernel")
121+
assert.Contains(t, output, "deploy")
122+
assert.Contains(t, output, "invoke")
123+
assert.Contains(t, output, "browsers")
124+
}
125+
126+
func TestLogLevelToPterm(t *testing.T) {
127+
tests := []struct {
128+
input string
129+
expected string
130+
}{
131+
{"trace", "trace"},
132+
{"debug", "debug"},
133+
{"info", "info"},
134+
{"warn", "warn"},
135+
{"error", "error"},
136+
{"fatal", "fatal"},
137+
{"print", "print"},
138+
{"garbage", "info"},
139+
{"", "info"},
140+
}
141+
142+
for _, tt := range tests {
143+
t.Run(tt.input, func(t *testing.T) {
144+
got := logLevelToPterm(tt.input)
145+
expected := logLevelToPterm(tt.expected)
146+
assert.Equal(t, expected, got)
147+
})
148+
}
149+
}
150+
151+
func TestIsUsageError(t *testing.T) {
152+
tests := []struct {
153+
name string
154+
err string
155+
expected bool
156+
}{
157+
{"unknown flag", "unknown flag: --foo", true},
158+
{"unknown command", "unknown command \"bogus\"", true},
159+
{"invalid argument", "invalid argument \"x\" for \"--count\"", true},
160+
{"random error", "connection refused", false},
161+
}
162+
163+
for _, tt := range tests {
164+
t.Run(tt.name, func(t *testing.T) {
165+
err := fmt.Errorf(tt.err)
166+
assert.Equal(t, tt.expected, isUsageError(err))
167+
})
168+
}
169+
}

0 commit comments

Comments
 (0)