-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathadd_command_test.go
More file actions
339 lines (294 loc) · 9.43 KB
/
add_command_test.go
File metadata and controls
339 lines (294 loc) · 9.43 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//go:build !integration
package cli
import (
"context"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// validateEngineStub is a stub validation function for testing
func validateEngineStub(engine string) error {
return nil
}
func TestNewAddCommand(t *testing.T) {
cmd := NewAddCommand(validateEngineStub)
require.NotNil(t, cmd, "NewAddCommand should not return nil")
assert.Equal(t, "add <workflow>...", cmd.Use, "Command use should be 'add <workflow>...'")
assert.Equal(t, "Add agentic workflows from repositories to .github/workflows", cmd.Short, "Command short description should match")
assert.Contains(t, cmd.Long, "Add one or more agentic workflows", "Command long description should contain expected text")
// Verify Args validator is set
assert.NotNil(t, cmd.Args, "Args validator should be set")
// Verify flags are registered
flags := cmd.Flags()
// Check name flag
nameFlag := flags.Lookup("name")
assert.NotNil(t, nameFlag, "Should have 'name' flag")
assert.Equal(t, "n", nameFlag.Shorthand, "Name flag shorthand should be 'n'")
// Check engine flag
engineFlag := flags.Lookup("engine")
assert.NotNil(t, engineFlag, "Should have 'engine' flag")
// Check repo flag
repoFlag := flags.Lookup("repo")
assert.NotNil(t, repoFlag, "Should have 'repo' flag")
assert.Equal(t, "r", repoFlag.Shorthand, "Repo flag shorthand should be 'r'")
// Check PR flags
createPRFlag := flags.Lookup("create-pull-request")
assert.NotNil(t, createPRFlag, "Should have 'create-pull-request' flag")
prFlag := flags.Lookup("pr")
assert.NotNil(t, prFlag, "Should have 'pr' flag (alias)")
// Check force flag
forceFlag := flags.Lookup("force")
assert.NotNil(t, forceFlag, "Should have 'force' flag")
// Check append flag
appendFlag := flags.Lookup("append")
assert.NotNil(t, appendFlag, "Should have 'append' flag")
// Check no-gitattributes flag
noGitattributesFlag := flags.Lookup("no-gitattributes")
assert.NotNil(t, noGitattributesFlag, "Should have 'no-gitattributes' flag")
// Check dir flag
dirFlag := flags.Lookup("dir")
assert.NotNil(t, dirFlag, "Should have 'dir' flag")
assert.Equal(t, "d", dirFlag.Shorthand, "Dir flag shorthand should be 'd'")
// Check no-stop-after flag
noStopAfterFlag := flags.Lookup("no-stop-after")
assert.NotNil(t, noStopAfterFlag, "Should have 'no-stop-after' flag")
// Check stop-after flag
stopAfterFlag := flags.Lookup("stop-after")
assert.NotNil(t, stopAfterFlag, "Should have 'stop-after' flag")
}
func TestAddWorkflows(t *testing.T) {
tests := []struct {
name string
workflows []string
expectError bool
errorContains string
}{
{
name: "empty workflows list",
workflows: []string{},
expectError: true,
errorContains: "at least one workflow",
},
{
name: "repo-only spec (requires workflow path)",
workflows: []string{"owner/repo"},
expectError: true,
errorContains: "workflow specification must be in format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := AddOptions{}
_, err := AddWorkflows(context.Background(), tt.workflows, opts)
if tt.expectError {
require.Error(t, err, "Expected error for test case: %s", tt.name)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains, "Error should contain expected message")
}
} else {
assert.NoError(t, err, "Should not error for test case: %s", tt.name)
}
})
}
}
// TestAddCommandStructure removed - redundant with TestNewAddCommand
func TestAddResolvedWorkflows(t *testing.T) {
tests := []struct {
name string
expectError bool
errorContains string
}{
{
name: "valid workflow",
expectError: true, // Will still error due to missing git repo, but validates basic flow
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a minimal resolved workflow structure
resolved := &ResolvedWorkflows{
Workflows: []*ResolvedWorkflow{
{
Spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "test/repo",
},
WorkflowName: "test-workflow",
WorkflowPath: "test.md",
},
},
},
}
opts := AddOptions{}
_, err := AddResolvedWorkflows(
[]string{"test/repo/test-workflow"},
resolved,
opts,
)
if tt.expectError {
require.Error(t, err, "Expected error for test case: %s", tt.name)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains, "Error should contain expected message")
}
} else {
assert.NoError(t, err, "Should not error for test case: %s", tt.name)
}
})
}
}
func TestAddWorkflowsResult(t *testing.T) {
tests := []struct {
name string
prNumber int
prURL string
hasWorkflowDispatch bool
}{
{
name: "default values",
prNumber: 0,
prURL: "",
hasWorkflowDispatch: false,
},
{
name: "with PR number",
prNumber: 123,
prURL: "",
hasWorkflowDispatch: false,
},
{
name: "with PR URL",
prNumber: 0,
prURL: "https://github.com/owner/repo/pull/123",
hasWorkflowDispatch: false,
},
{
name: "with workflow dispatch",
prNumber: 0,
prURL: "",
hasWorkflowDispatch: true,
},
{
name: "all fields set",
prNumber: 456,
prURL: "https://github.com/owner/repo/pull/456",
hasWorkflowDispatch: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &AddWorkflowsResult{
PRNumber: tt.prNumber,
PRURL: tt.prURL,
HasWorkflowDispatch: tt.hasWorkflowDispatch,
}
// Verify all fields are accessible and have expected values
assert.Equal(t, tt.prNumber, result.PRNumber, "PRNumber should match")
assert.Equal(t, tt.prURL, result.PRURL, "PRURL should match")
assert.Equal(t, tt.hasWorkflowDispatch, result.HasWorkflowDispatch, "HasWorkflowDispatch should match")
})
}
}
func TestAddCommandFlagInteractions(t *testing.T) {
tests := []struct {
name string
flagSetup func(cmd *cobra.Command)
expectValid bool
description string
}{
{
name: "no-stop-after and stop-after together",
flagSetup: func(cmd *cobra.Command) {
cmd.Flags().Set("no-stop-after", "true")
cmd.Flags().Set("stop-after", "+48h")
},
expectValid: true, // Both flags can be set, stop-after takes precedence
description: "Both no-stop-after and stop-after flags can be set",
},
{
name: "create-pull-request and pr alias",
flagSetup: func(cmd *cobra.Command) {
cmd.Flags().Set("create-pull-request", "true")
cmd.Flags().Set("pr", "true")
},
expectValid: true, // Both aliases should work
description: "Both create-pull-request and pr flags can be set (aliases)",
},
{
name: "force flag with number",
flagSetup: func(cmd *cobra.Command) {
cmd.Flags().Set("force", "true")
cmd.Flags().Set("number", "3")
},
expectValid: true,
description: "Force flag should work with multiple numbered copies",
},
{
name: "dir flag with subdirectory",
flagSetup: func(cmd *cobra.Command) {
cmd.Flags().Set("dir", "shared")
},
expectValid: true,
description: "Dir flag should accept subdirectory name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewAddCommand(validateEngineStub)
// Apply flag setup
tt.flagSetup(cmd)
// Verify flags are set correctly
flags := cmd.Flags()
assert.NotNil(t, flags, "Command flags should not be nil")
// The actual validation happens during RunE execution
// Here we just verify the flags can be set without panic
assert.True(t, tt.expectValid, tt.description)
})
}
}
func TestAddCommandFlagDefaults(t *testing.T) {
cmd := NewAddCommand(validateEngineStub)
flags := cmd.Flags()
tests := []struct {
flagName string
defaultValue string
}{
{"name", ""},
{"engine", ""},
{"repo", ""},
{"append", ""},
{"dir", ""},
{"stop-after", ""},
}
for _, tt := range tests {
t.Run(tt.flagName, func(t *testing.T) {
flag := flags.Lookup(tt.flagName)
require.NotNil(t, flag, "Flag should exist: %s", tt.flagName)
assert.Equal(t, tt.defaultValue, flag.DefValue, "Default value should match for flag: %s", tt.flagName)
})
}
}
func TestAddCommandBooleanFlags(t *testing.T) {
cmd := NewAddCommand(validateEngineStub)
flags := cmd.Flags()
boolFlags := []string{"create-pull-request", "pr", "force", "no-gitattributes", "no-stop-after"}
for _, flagName := range boolFlags {
t.Run(flagName, func(t *testing.T) {
flag := flags.Lookup(flagName)
require.NotNil(t, flag, "Boolean flag should exist: %s", flagName)
assert.Equal(t, "false", flag.DefValue, "Boolean flag should default to false: %s", flagName)
})
}
}
func TestAddCommandArgs(t *testing.T) {
cmd := NewAddCommand(validateEngineStub)
// Test that Args validator is set (MinimumNArgs(1))
require.NotNil(t, cmd.Args, "Args validator should be set")
// Verify it requires at least 1 arg
err := cmd.Args(cmd, []string{})
require.Error(t, err, "Should error with no arguments")
err = cmd.Args(cmd, []string{"workflow1"})
require.NoError(t, err, "Should not error with 1 argument")
err = cmd.Args(cmd, []string{"workflow1", "workflow2"})
require.NoError(t, err, "Should not error with multiple arguments")
}