-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathroot_test.go
75 lines (67 loc) · 1.78 KB
/
root_test.go
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
package cmd
import (
"errors"
"testing"
"github.com/spf13/cobra"
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
)
var cmd *cobra.Command
var service *cobra.Command
var resource *cobra.Command
var operation *cobra.Command
func setupCmd() {
cmd = &cobra.Command{
Use: "stackit",
}
service = &cobra.Command{
Use: "service",
}
resource = &cobra.Command{
Use: "resource",
}
operation = &cobra.Command{
Use: "operation",
}
cmd.AddCommand(service)
service.AddCommand(resource)
resource.AddCommand(operation)
}
func TestBeautifyUnknownAndMissingCommandsError(t *testing.T) {
tests := []struct {
description string
inputError error
command *cobra.Command
expectedMsg string
isNotUnknownFlagError bool
}{
{
description: "root command, extra input is a flag",
inputError: errors.New("unknown flag: --something"),
command: cmd,
expectedMsg: pkgErrors.SUBCOMMAND_MISSING,
},
{
description: "non unknown flag error, return the same",
inputError: errors.New("some error"),
command: cmd,
expectedMsg: "some error",
isNotUnknownFlagError: true,
},
}
setupCmd()
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
actualError := beautifyUnknownAndMissingCommandsError(cmd, tt.inputError)
if tt.isNotUnknownFlagError {
if actualError.Error() != tt.expectedMsg {
t.Fatalf("expected error message to be %s, got %s", tt.expectedMsg, actualError.Error())
}
return
}
appendedErr := pkgErrors.AppendUsageTip(errors.New(tt.expectedMsg), cmd)
if actualError.Error() != appendedErr.Error() {
t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), actualError.Error())
}
})
}
}