-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs_test.go
More file actions
182 lines (160 loc) · 6.34 KB
/
args_test.go
File metadata and controls
182 lines (160 loc) · 6.34 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
package cobrax
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ──────────────────────────────────────────────────────────────────────────────
// parseArgSpecs tests
// ──────────────────────────────────────────────────────────────────────────────
func TestParseArgSpecs_NoArgs(t *testing.T) {
cmd := &cobra.Command{Use: "serve"}
specs := parseArgSpecs(cmd)
assert.Nil(t, specs, "no arg tokens → nil specs")
}
func TestParseArgSpecs_FlagsOnly(t *testing.T) {
cmd := &cobra.Command{Use: "list [flags]"}
specs := parseArgSpecs(cmd)
assert.Nil(t, specs, "[flags] sentinel should be ignored")
}
func TestParseArgSpecs_OptionalSingleArg(t *testing.T) {
cmd := &cobra.Command{Use: "get [resource]"}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 1)
s := specs[0]
assert.Equal(t, "resource", s.Name)
assert.False(t, s.Required)
assert.False(t, s.Variadic)
assert.Equal(t, 0, s.Index)
}
func TestParseArgSpecs_RequiredSingleArg(t *testing.T) {
cmd := &cobra.Command{Use: "delete <name>"}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 1)
s := specs[0]
assert.Equal(t, "name", s.Name)
assert.True(t, s.Required)
assert.False(t, s.Variadic)
}
func TestParseArgSpecs_VariadicOptional(t *testing.T) {
cmd := &cobra.Command{Use: "make [targets...]"}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 1)
s := specs[0]
assert.Equal(t, "targets", s.Name)
assert.False(t, s.Required)
assert.True(t, s.Variadic)
}
func TestParseArgSpecs_VariadicRequired(t *testing.T) {
cmd := &cobra.Command{Use: "run <files...>"}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 1)
s := specs[0]
assert.Equal(t, "files", s.Name)
assert.True(t, s.Required)
assert.True(t, s.Variadic)
}
func TestParseArgSpecs_MultipleArgs(t *testing.T) {
// Simulates: /oncall <module> [title]
cmd := &cobra.Command{Use: "oncall <module> [title]"}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 2)
assert.Equal(t, "module", specs[0].Name)
assert.True(t, specs[0].Required)
assert.Equal(t, 0, specs[0].Index)
assert.Equal(t, "title", specs[1].Name)
assert.False(t, specs[1].Required)
assert.Equal(t, 1, specs[1].Index)
}
func TestParseArgSpecs_AnnotationDescription(t *testing.T) {
cmd := &cobra.Command{
Use: "oncall <module> [title]",
Annotations: map[string]string{
AnnotationArgPrefix + "0": "The on-call module (e.g. bke, kafka)",
AnnotationArgPrefix + "1": "Short incident title",
},
}
specs := parseArgSpecs(cmd)
require.Len(t, specs, 2)
assert.Equal(t, "The on-call module (e.g. bke, kafka)", specs[0].Description)
assert.Equal(t, "Short incident title", specs[1].Description)
}
func TestParseArgSpecs_FlagsAndArgsMixed(t *testing.T) {
cmd := &cobra.Command{Use: "push <image> [tag] [flags]"}
specs := parseArgSpecs(cmd)
// [flags] is stripped before regex matching
require.Len(t, specs, 2)
assert.Equal(t, "image", specs[0].Name)
assert.Equal(t, "tag", specs[1].Name)
}
// ──────────────────────────────────────────────────────────────────────────────
// buildArgsSchema tests
// ──────────────────────────────────────────────────────────────────────────────
func TestBuildArgsSchema_RequiredAndOptional(t *testing.T) {
specs := []ArgSpec{
{Name: "module", Required: true, Variadic: false, Index: 0},
{Name: "title", Required: false, Variadic: false, Index: 1},
}
s := buildArgsSchema(specs)
assert.Equal(t, "object", s.Type)
require.Contains(t, s.Properties, "module")
require.Contains(t, s.Properties, "title")
assert.Equal(t, "string", s.Properties["module"].Type)
assert.Equal(t, "string", s.Properties["title"].Type)
assert.Contains(t, s.Required, "module")
assert.NotContains(t, s.Required, "title")
}
func TestBuildArgsSchema_Variadic(t *testing.T) {
specs := []ArgSpec{
{Name: "targets", Required: false, Variadic: true, Index: 0},
}
s := buildArgsSchema(specs)
require.Contains(t, s.Properties, "targets")
assert.Equal(t, "array", s.Properties["targets"].Type)
assert.NotNil(t, s.Properties["targets"].Items)
assert.Equal(t, "string", s.Properties["targets"].Items.Type)
}
// ──────────────────────────────────────────────────────────────────────────────
// collectPositionalArgs tests
// ──────────────────────────────────────────────────────────────────────────────
func TestCollectPositionalArgs_OrderPreserved(t *testing.T) {
specs := []ArgSpec{
{Name: "module", Index: 0},
{Name: "title", Index: 1},
}
m := map[string]any{
"title": "disk full on prod",
"module": "bke",
}
result := collectPositionalArgs(specs, m)
// Must be in spec order: module first, title second
assert.Equal(t, []string{"bke", "disk full on prod"}, result)
}
func TestCollectPositionalArgs_VariadicExpanded(t *testing.T) {
specs := []ArgSpec{
{Name: "targets", Variadic: true, Index: 0},
}
m := map[string]any{
"targets": []any{"build", "test", "lint"},
}
result := collectPositionalArgs(specs, m)
assert.Equal(t, []string{"build", "test", "lint"}, result)
}
func TestCollectPositionalArgs_OptionalAbsent(t *testing.T) {
specs := []ArgSpec{
{Name: "module", Required: true, Index: 0},
{Name: "title", Required: false, Index: 1},
}
m := map[string]any{
"module": "kafka",
// "title" is intentionally absent
}
result := collectPositionalArgs(specs, m)
assert.Equal(t, []string{"kafka"}, result)
}
func TestCollectPositionalArgs_EmptyMap(t *testing.T) {
specs := []ArgSpec{{Name: "module", Index: 0}}
result := collectPositionalArgs(specs, nil)
assert.Nil(t, result)
}