Repository navigation
Expand file tree
/
Copy pathroot_test.go
More file actions
105 lines (94 loc) · 2.18 KB
/
Copy pathroot_test.go
File metadata and controls
105 lines (94 loc) · 2.18 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
package cmd
import (
"bytes"
"testing"
)
func TestRootCommand(t *testing.T) {
tests := []struct {
name string
args []string
wantErr bool
}{
{
name: "No arguments shows help",
args: []string{},
wantErr: false,
},
{
name: "Help flag",
args: []string{"--help"},
wantErr: false,
},
{
name: "Version command",
args: []string{"version"},
wantErr: false,
},
{
name: "Invalid command",
args: []string{"invalid"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset command for testing
rootCmd.SetArgs(tt.args)
// Capture output
var stdout, stderr bytes.Buffer
rootCmd.SetOut(&stdout)
rootCmd.SetErr(&stderr)
err := rootCmd.Execute()
if tt.wantErr {
if err == nil {
t.Error("Expected error but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
// Note: Cobra output goes to the configured streams,
// so we don't check specific output content in these tests
})
}
}
func TestCommandStructure(t *testing.T) {
// Verify all expected commands are registered
// Note: Cobra automatically adds "completion" and "help" commands
expectedCommands := []string{
"ca", // Certificate Authority generation
"completion", // Auto-added by Cobra
"convert",
"csr", // Certificate Signing Request generation
"generate",
"help", // Auto-added by Cobra
"inspect",
"sign", // Sign CSRs with CA
"tls", // TLS version testing
"update",
"verify",
"version",
}
commands := rootCmd.Commands()
commandMap := make(map[string]bool)
for _, cmd := range commands {
commandMap[cmd.Name()] = true
}
for _, expected := range expectedCommands {
if !commandMap[expected] {
t.Errorf("Expected command %q not found", expected)
}
}
// Check that we don't have extra unexpected commands
if len(commands) != len(expectedCommands) {
t.Errorf("Expected %d commands, got %d", len(expectedCommands), len(commands))
t.Logf("Actual commands: %v", func() []string {
var names []string
for _, cmd := range commands {
names = append(names, cmd.Name())
}
return names
}())
}
}