-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_test.go
More file actions
250 lines (213 loc) · 6.44 KB
/
root_test.go
File metadata and controls
250 lines (213 loc) · 6.44 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cmd
import (
"errors"
"os"
"strings"
"testing"
"github.com/aaearon/grant-cli/internal/sca/models"
"github.com/aaearon/grant-cli/internal/ui"
"github.com/cyberark/idsec-sdk-golang/pkg/config"
"github.com/spf13/cobra"
)
func TestNewRootCommand_SilenceFlags(t *testing.T) {
cmd := newRootCommand(nil)
if !cmd.SilenceErrors {
t.Error("expected SilenceErrors to be true")
}
if !cmd.SilenceUsage {
t.Error("expected SilenceUsage to be true")
}
}
func TestNewRootCommand_FlagsRegistered(t *testing.T) {
cmd := newRootCommand(nil)
flags := []string{"verbose", "provider", "target", "role", "favorite", "refresh", "groups", "group"}
for _, flag := range flags {
if cmd.Flags().Lookup(flag) == nil && cmd.PersistentFlags().Lookup(flag) == nil {
t.Errorf("expected --%s flag to be registered", flag)
}
}
}
func TestNewTestRootCommand_ReturnsValidCommand(t *testing.T) {
cmd := newTestRootCommand()
if cmd.Use != "grant" {
t.Errorf("expected Use='grant', got %q", cmd.Use)
}
if cmd.SilenceErrors != true {
t.Error("expected SilenceErrors to be true")
}
}
func TestPersistentPreRunE_VerboseEnabled(t *testing.T) {
t.Setenv(config.IdsecLogLevelEnvVar, "CRITICAL")
root := newTestRootCommand()
// Add a no-op subcommand to exercise PersistentPreRunE
root.AddCommand(newNoOpCommand())
_, err := executeCommand(root, "--verbose", "noop")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
level := os.Getenv(config.IdsecLogLevelEnvVar)
if level != "INFO" {
t.Errorf("expected IDSEC_LOG_LEVEL=INFO, got %q", level)
}
}
func TestPersistentPreRunE_VerboseDisabled(t *testing.T) {
t.Setenv(config.IdsecLogLevelEnvVar, "DEBUG")
root := newTestRootCommand()
root.AddCommand(newNoOpCommand())
_, err := executeCommand(root, "noop")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
level := os.Getenv(config.IdsecLogLevelEnvVar)
if level != "CRITICAL" {
t.Errorf("expected IDSEC_LOG_LEVEL=CRITICAL, got %q", level)
}
}
func TestVerboseHintSuppressedForArgErrors(t *testing.T) {
tests := []struct {
name string
args []string
wantHint bool
}{
{
name: "arg error - hint suppressed",
args: []string{"favorites", "remove"}, // missing required arg
wantHint: false,
},
{
name: "runtime error - hint shown",
args: []string{"favorites", "remove", "nonexistent"},
wantHint: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Build a command tree that exercises the hint logic
root := newRootCommand(nil)
root.AddCommand(NewFavoritesCommand())
// Simulate what Execute() does
root.SetArgs(tt.args)
passedArgValidation = false
defer func() { passedArgValidation = false }()
err := root.Execute()
var hint string
if err != nil && !verbose && !passedArgValidation {
hint = ""
} else if err != nil && !verbose {
hint = "Hint: re-run with --verbose for more details"
}
if tt.wantHint && hint == "" {
t.Error("expected verbose hint to be shown, but it was suppressed")
}
if !tt.wantHint && hint != "" {
t.Error("expected verbose hint to be suppressed, but it was shown")
}
})
}
}
func TestVerboseHintSuppressedForUnknownSubcommand(t *testing.T) {
root := newRootCommand(nil)
root.AddCommand(NewFavoritesCommand())
root.SetArgs([]string{"nonexistent-command"})
passedArgValidation = false
defer func() { passedArgValidation = false }()
err := root.Execute()
if err == nil {
t.Fatal("expected error for unknown subcommand")
}
if passedArgValidation {
t.Error("passedArgValidation should be false for unknown subcommand")
}
}
func TestVerboseHintShownForRuntimeErrors(t *testing.T) {
root := newRootCommand(func(cmd *cobra.Command, args []string) error {
return errors.New("runtime failure")
})
root.SetArgs([]string{})
passedArgValidation = false
defer func() { passedArgValidation = false }()
err := root.Execute()
if err == nil {
t.Fatal("expected runtime error")
}
if !passedArgValidation {
t.Error("passedArgValidation should be true for runtime errors")
}
// Simulate Execute() hint logic
if verbose || !passedArgValidation {
t.Skip("hint would be suppressed, but shouldn't be for runtime errors")
}
}
func TestExecuteHintOutput(t *testing.T) {
// Test the full executeWithHint helper to verify the hint text
tests := []struct {
name string
setupCmd func() *cobra.Command
args []string
wantHint bool
wantErrStr string
}{
{
name: "arg error suppresses hint",
setupCmd: func() *cobra.Command {
root := newRootCommand(nil)
root.AddCommand(NewFavoritesCommand())
return root
},
args: []string{"favorites", "remove"},
wantHint: false,
wantErrStr: "requires a favorite name",
},
{
name: "runtime error shows hint",
setupCmd: func() *cobra.Command {
return newRootCommand(func(cmd *cobra.Command, args []string) error {
return errors.New("something went wrong")
})
},
args: []string{},
wantHint: true,
wantErrStr: "something went wrong",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := tt.setupCmd()
errOutput := executeWithHint(cmd, tt.args)
if !strings.Contains(errOutput, tt.wantErrStr) {
t.Errorf("expected error output to contain %q, got:\n%s", tt.wantErrStr, errOutput)
}
hasHint := strings.Contains(errOutput, "Hint: re-run with --verbose")
if tt.wantHint && !hasHint {
t.Errorf("expected verbose hint in output, got:\n%s", errOutput)
}
if !tt.wantHint && hasHint {
t.Errorf("expected no verbose hint in output, got:\n%s", errOutput)
}
})
}
}
func TestUnifiedSelector_NonTTY(t *testing.T) {
original := ui.IsTerminalFunc
defer func() { ui.IsTerminalFunc = original }()
ui.IsTerminalFunc = func(fd uintptr) bool { return false }
selector := &uiUnifiedSelector{}
items := []selectionItem{
{kind: selectionCloud, cloud: &models.EligibleTarget{
WorkspaceName: "Sub A",
WorkspaceType: models.WorkspaceTypeSubscription,
RoleInfo: models.RoleInfo{Name: "Owner"},
}},
}
_, err := selector.SelectItem(items)
if err == nil {
t.Fatal("expected error for non-TTY")
}
if !errors.Is(err, ui.ErrNotInteractive) {
t.Errorf("expected ErrNotInteractive, got: %v", err)
}
errMsg := err.Error()
if !strings.Contains(errMsg, "--target") || !strings.Contains(errMsg, "--group") || !strings.Contains(errMsg, "--favorite") {
t.Errorf("error should mention --target/--role, --group, and --favorite, got: %v", err)
}
}