forked from HashLoad/boss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_test.go
More file actions
215 lines (181 loc) · 5.06 KB
/
Copy pathcmd_test.go
File metadata and controls
215 lines (181 loc) · 5.06 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
//nolint:testpackage // Testing internal command registration
package cli
import (
"bytes"
"testing"
"github.com/spf13/cobra"
)
// TestRootCommand tests the root command structure.
func TestRootCommand(t *testing.T) {
// We can't directly test Execute() as it calls os.Exit
// But we can test the command registration
// Create a mock root command to test command registration
root := &cobra.Command{
Use: "boss",
Short: "Dependency Manager for Delphi",
}
// Test that commands can be registered without panic
t.Run("register commands", func(t *testing.T) {
// These should not panic
versionCmdRegister(root)
pubpascalCmdRegister(root)
// Verify command was added
if root.Commands() == nil {
t.Error("Expected commands to be registered")
}
})
}
// TestVersionCommand tests the version command.
func TestVersionCommand(t *testing.T) {
root := &cobra.Command{Use: "boss"}
versionCmdRegister(root)
// Find the version command
var versionCmd *cobra.Command
for _, cmd := range root.Commands() {
if cmd.Use == cmdNameVersion {
versionCmd = cmd
break
}
}
if versionCmd == nil {
t.Fatal("Version command not found")
}
// Test command properties
if versionCmd.Short == "" {
t.Error("Version command should have a short description")
}
// Test aliases
if len(versionCmd.Aliases) == 0 {
t.Error("Version command should have aliases")
}
hasVAlias := false
for _, alias := range versionCmd.Aliases {
if alias == "v" {
hasVAlias = true
break
}
}
if !hasVAlias {
t.Error("Version command should have 'v' alias")
}
}
// TestInstallCommand tests the install command registration.
func TestInstallCommand(t *testing.T) {
root := &cobra.Command{Use: "boss"}
installCmdRegister(root)
// Find the install command
var installCmd *cobra.Command
for _, cmd := range root.Commands() {
if cmd.Use == "install" {
installCmd = cmd
break
}
}
if installCmd == nil {
t.Fatal("Install command not found")
}
// Test aliases
expectedAliases := map[string]bool{"i": false, "add": false}
for _, alias := range installCmd.Aliases {
if _, ok := expectedAliases[alias]; ok {
expectedAliases[alias] = true
}
}
for alias, found := range expectedAliases {
if !found {
t.Errorf("Install command should have '%s' alias", alias)
}
}
// Test flags
noSaveFlag := installCmd.Flags().Lookup("no-save")
if noSaveFlag == nil {
t.Error("Install command should have --no-save flag")
}
}
// TestCommandHelp tests that commands have proper help text.
func TestCommandHelp(t *testing.T) {
root := &cobra.Command{Use: "boss"}
// Register all commands
versionCmdRegister(root)
installCmdRegister(root)
pubpascalCmdRegister(root)
craCmdRegister(root)
for _, cmd := range root.Commands() {
t.Run(cmd.Use, func(t *testing.T) {
if cmd.Short == "" {
t.Errorf("Command %s should have a short description", cmd.Use)
}
if cmd.Long == "" {
t.Errorf("Command %s should have a long description", cmd.Use)
}
})
}
}
// TestCommandOutput captures command output for testing.
func captureOutput(cmd *cobra.Command, args []string) (string, error) {
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetErr(buf)
cmd.SetArgs(args)
err := cmd.Execute()
return buf.String(), err
}
// TestRootHelp tests that root command shows help.
func TestRootHelp(t *testing.T) {
root := &cobra.Command{
Use: "boss",
Short: "Dependency Manager for Delphi",
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}
output, err := captureOutput(root, []string{})
if err != nil {
t.Errorf("Root command should not error: %v", err)
}
if output == "" {
t.Error("Root command should produce help output")
}
}
// findCommand returns the direct sub-command of parent with the given name.
func findCommand(parent *cobra.Command, name string) *cobra.Command {
for _, cmd := range parent.Commands() {
if cmd.Name() == name {
return cmd
}
}
return nil
}
// assertSubcommands reports every expected sub-command missing from parent.
func assertSubcommands(t *testing.T, parent *cobra.Command, label string, names []string) {
t.Helper()
for _, name := range names {
if findCommand(parent, name) == nil {
t.Errorf("%s subcommand '%s' not found", label, name)
}
}
}
// TestPubPascalCommands tests that the PubPascal commands are registered correctly.
func TestPubPascalCommands(t *testing.T) {
root := &cobra.Command{Use: "boss"}
pubpascalCmdRegister(root)
// Check workspace command and its subcommands
workspaceCmd := findCommand(root, "workspace")
if workspaceCmd == nil {
t.Fatal("Workspace command not found")
}
assertSubcommands(t, workspaceCmd, "Workspace", []string{"clone", "status", "update", "push"})
// Check pkg command and root commands
pkgCmd := findCommand(root, "pkg")
if pkgCmd == nil {
t.Fatal("Pkg command not found")
}
if findCommand(root, "sbom") == nil {
t.Error("Root command 'sbom' not found")
}
if findCommand(root, "publish-sbom") == nil {
t.Error("Root command 'publish-sbom' not found")
}
// Check pkg subcommands
assertSubcommands(t, pkgCmd, "Pkg", []string{"spec", "pack"})
}