-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathspec_test.go
More file actions
1228 lines (1156 loc) · 38.6 KB
/
spec_test.go
File metadata and controls
1228 lines (1156 loc) · 38.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build !integration
package cli
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseRepoSpec(t *testing.T) {
tests := []struct {
name string
repoSpec string
wantRepo string
wantVersion string
wantErr bool
errContains string
}{
{
name: "repo with version tag",
repoSpec: "owner/repo@v1.0.0",
wantRepo: "owner/repo",
wantVersion: "v1.0.0",
wantErr: false,
},
{
name: "repo with branch",
repoSpec: "owner/repo@main",
wantRepo: "owner/repo",
wantVersion: "main",
wantErr: false,
},
{
name: "repo without version",
repoSpec: "owner/repo",
wantRepo: "owner/repo",
wantVersion: "",
wantErr: false,
},
{
name: "repo with commit SHA",
repoSpec: "owner/repo@abc123def456",
wantRepo: "owner/repo",
wantVersion: "abc123def456",
wantErr: false,
},
{
name: "invalid format - missing owner",
repoSpec: "repo@v1.0.0",
wantErr: true,
errContains: "must be in format 'org/repo'",
},
{
name: "invalid format - missing repo",
repoSpec: "owner/@v1.0.0",
wantErr: true,
errContains: "must be in format 'org/repo'",
},
{
name: "invalid format - no slash",
repoSpec: "ownerrepo@v1.0.0",
wantErr: true,
errContains: "must be in format 'org/repo'",
},
{
name: "GitHub URL without version",
repoSpec: "https://github.com/owner/repo",
wantRepo: "owner/repo",
wantVersion: "",
wantErr: false,
},
{
name: "GitHub URL with version",
repoSpec: "https://github.com/owner/repo@v1.0.0",
wantRepo: "owner/repo",
wantVersion: "v1.0.0",
wantErr: false,
},
{
name: "GitHub URL with branch",
repoSpec: "https://github.com/githubnext/agentics@main",
wantRepo: "githubnext/agentics",
wantVersion: "main",
wantErr: false,
},
{
name: "invalid GitHub URL - missing repo",
repoSpec: "https://github.com/owner",
wantErr: true,
errContains: "invalid GitHub URL: must be https://github.com/owner/repo",
},
{
name: "invalid GitHub URL - too many path parts",
repoSpec: "https://github.com/owner/repo/extra/path",
wantErr: true,
errContains: "invalid GitHub URL: must be https://github.com/owner/repo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec, err := parseRepoSpec(tt.repoSpec)
if tt.wantErr {
if err == nil {
t.Errorf("parseRepoSpec() expected error, got nil")
return
}
return
}
if err != nil {
t.Errorf("parseRepoSpec() unexpected error: %v", err)
return
}
if spec.RepoSlug != tt.wantRepo {
t.Errorf("parseRepoSpec() repo = %q, want %q", spec.RepoSlug, tt.wantRepo)
}
if spec.Version != tt.wantVersion {
t.Errorf("parseRepoSpec() version = %q, want %q", spec.Version, tt.wantVersion)
}
})
}
}
func TestParseWorkflowSpec(t *testing.T) {
tests := []struct {
name string
spec string
wantRepo string
wantWorkflowPath string
wantWorkflowName string
wantVersion string
wantHost string
wantErr bool
errContains string
}{
{
name: "GitHub URL - blob with main branch",
spec: "https://github.com/github/gh-aw-trial/blob/main/workflows/release-issue-linker.md",
wantRepo: "github/gh-aw-trial",
wantWorkflowPath: "workflows/release-issue-linker.md",
wantWorkflowName: "release-issue-linker",
wantVersion: "main",
wantHost: "github.com",
wantErr: false,
},
{
name: "GitHub URL - blob with version tag",
spec: "https://github.com/owner/repo/blob/v1.0.0/workflows/ci-doctor.md",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/ci-doctor.md",
wantWorkflowName: "ci-doctor",
wantVersion: "v1.0.0",
wantErr: false,
},
{
name: "GitHub URL - tree with branch",
spec: "https://github.com/owner/repo/tree/develop/custom/path/workflow.md",
wantRepo: "owner/repo",
wantWorkflowPath: "custom/path/workflow.md",
wantWorkflowName: "workflow",
wantVersion: "develop",
wantErr: false,
},
{
name: "GitHub URL - raw format",
spec: "https://github.com/owner/repo/raw/main/workflows/helper.md",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/helper.md",
wantWorkflowName: "helper",
wantVersion: "main",
wantErr: false,
},
{
name: "GitHub URL - commit SHA",
spec: "https://github.com/owner/repo/blob/abc123def456789012345678901234567890abcd/workflows/test.md",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/test.md",
wantWorkflowName: "test",
wantVersion: "abc123def456789012345678901234567890abcd",
wantErr: false,
},
{
name: "GitHub URL - GHE.com instance",
spec: "https://myorg.ghe.com/owner/repo/blob/main/workflows/test.md",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/test.md",
wantWorkflowName: "test",
wantVersion: "main",
wantHost: "myorg.ghe.com",
wantErr: false,
},
{
name: "GitHub URL - non-github.com host is rejected (e.g. gitlab.com)",
spec: "https://gitlab.com/owner/repo/blob/main/workflows/test.md",
wantErr: true,
errContains: "github.com",
},
{
name: "GitHub URL - missing file extension",
spec: "https://github.com/owner/repo/blob/main/workflows/test",
wantErr: true,
errContains: "must point to a .md file",
},
{
name: "GitHub URL - invalid path (too short)",
spec: "https://github.com/owner/repo/blob/main",
wantErr: true,
errContains: "path too short",
},
{
name: "GitHub URL - invalid type",
spec: "https://github.com/owner/repo/commits/main/workflows/test.md",
wantErr: true,
errContains: "expected /blob/, /tree/, or /raw/",
},
{
name: "three-part spec with version",
spec: "owner/repo/workflow@v1.0.0",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/workflow.md",
wantWorkflowName: "workflow",
wantVersion: "v1.0.0",
wantErr: false,
},
{
name: "three-part spec without version",
spec: "owner/repo/workflow",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/workflow.md",
wantWorkflowName: "workflow",
wantVersion: "",
wantErr: false,
},
{
name: "four-part spec with workflows prefix",
spec: "owner/repo/workflows/ci-doctor.md@v1.0.0",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/ci-doctor.md",
wantWorkflowName: "ci-doctor",
wantVersion: "v1.0.0",
wantErr: false,
},
{
name: "nested path with version",
spec: "owner/repo/path/to/workflow.md@main",
wantRepo: "owner/repo",
wantWorkflowPath: "path/to/workflow.md",
wantWorkflowName: "workflow",
wantVersion: "main",
wantErr: false,
},
{
name: "invalid - too few parts",
spec: "owner/repo@v1.0.0",
wantErr: true,
errContains: "must be in format",
},
{
name: "invalid - four parts without .md extension",
spec: "owner/repo/workflows/workflow@v1.0.0",
wantErr: true,
errContains: "must end with '.md' extension",
},
{
name: "invalid - empty owner",
spec: "/repo/workflow@v1.0.0",
wantErr: true,
errContains: "owner and repo cannot be empty",
},
{
name: "invalid - empty repo",
spec: "owner//workflow@v1.0.0",
wantErr: true,
errContains: "owner and repo cannot be empty",
},
{
name: "invalid - bad GitHub identifier",
spec: "owner-/repo/workflow@v1.0.0",
wantErr: true,
errContains: "does not look like a valid GitHub repository",
},
{
name: "/files/ format with branch",
spec: "github/gh-aw/files/main/.github/workflows/shared/mcp/serena.md",
wantRepo: "github/gh-aw",
wantWorkflowPath: ".github/workflows/shared/mcp/serena.md",
wantWorkflowName: "serena",
wantVersion: "main",
wantErr: false,
},
{
name: "/files/ format with commit SHA",
spec: "github/gh-aw/files/fc7992627494253a869e177e5d1985d25f3bb316/.github/workflows/shared/mcp/serena.md",
wantRepo: "github/gh-aw",
wantWorkflowPath: ".github/workflows/shared/mcp/serena.md",
wantWorkflowName: "serena",
wantVersion: "fc7992627494253a869e177e5d1985d25f3bb316",
wantErr: false,
},
{
name: "/files/ format with tag",
spec: "owner/repo/files/v1.0.0/workflows/helper.md",
wantRepo: "owner/repo",
wantWorkflowPath: "workflows/helper.md",
wantWorkflowName: "helper",
wantVersion: "v1.0.0",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec, err := parseWorkflowSpec(tt.spec)
if tt.wantErr {
if err == nil {
t.Errorf("parseWorkflowSpec() expected error, got nil")
return
}
return
}
if err != nil {
t.Errorf("parseWorkflowSpec() unexpected error: %v", err)
return
}
if spec.RepoSlug != tt.wantRepo {
t.Errorf("parseWorkflowSpec() repo = %q, want %q", spec.RepoSlug, tt.wantRepo)
}
if spec.WorkflowPath != tt.wantWorkflowPath {
t.Errorf("parseWorkflowSpec() workflowPath = %q, want %q", spec.WorkflowPath, tt.wantWorkflowPath)
}
if spec.WorkflowName != tt.wantWorkflowName {
t.Errorf("parseWorkflowSpec() workflowName = %q, want %q", spec.WorkflowName, tt.wantWorkflowName)
}
if spec.Version != tt.wantVersion {
t.Errorf("parseWorkflowSpec() version = %q, want %q", spec.Version, tt.wantVersion)
}
if tt.wantHost != "" && spec.Host != tt.wantHost {
t.Errorf("parseWorkflowSpec() host = %q, want %q", spec.Host, tt.wantHost)
}
})
}
}
// TestParseWorkflowSpecGHEHostPinning verifies that well-known public-only repos
// (githubnext/agentics, github/gh-aw) always get Host pinned to "github.com"
// when a GHE environment is detected, while other repos use an empty host.
func TestParseWorkflowSpecGHEHostPinning(t *testing.T) {
tests := []struct {
name string
spec string
wantHost string
wantNoHost bool // expect empty host
}{
{
name: "githubnext/agentics three-part spec gets github.com in GHE mode",
spec: "githubnext/agentics/daily-plan",
wantHost: "github.com",
},
{
name: "githubnext/agentics wildcard gets github.com in GHE mode",
spec: "githubnext/agentics/*",
wantHost: "github.com",
},
{
name: "github/gh-aw three-part spec gets github.com in GHE mode",
spec: "github/gh-aw/my-workflow",
wantHost: "github.com",
},
{
name: "non-allowlisted repo has empty host in GHE mode",
spec: "owner/repo/workflow",
wantNoHost: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Simulate a GHE environment
t.Setenv("GITHUB_SERVER_URL", "")
t.Setenv("GITHUB_ENTERPRISE_HOST", "myorg.ghe.com")
t.Setenv("GITHUB_HOST", "")
t.Setenv("GH_HOST", "")
spec, err := parseWorkflowSpec(tt.spec)
if err != nil {
t.Fatalf("parseWorkflowSpec(%q) unexpected error: %v", tt.spec, err)
}
if tt.wantNoHost {
if spec.Host != "" {
t.Errorf("parseWorkflowSpec(%q) host = %q, want empty", tt.spec, spec.Host)
}
} else {
if spec.Host != tt.wantHost {
t.Errorf("parseWorkflowSpec(%q) host = %q, want %q", tt.spec, spec.Host, tt.wantHost)
}
}
})
}
}
// TestParseWorkflowSpecNoGHEHostPinning verifies that on public github.com the
// Host field is always empty for short-form specs (no pinning needed).
func TestParseWorkflowSpecNoGHEHostPinning(t *testing.T) {
// Clear all GHE env vars to simulate standard github.com environment
t.Setenv("GITHUB_SERVER_URL", "")
t.Setenv("GITHUB_ENTERPRISE_HOST", "")
t.Setenv("GITHUB_HOST", "")
t.Setenv("GH_HOST", "")
specs := []string{
"githubnext/agentics/daily-plan",
"githubnext/agentics/*",
"github/gh-aw/my-workflow",
"owner/repo/workflow",
}
for _, s := range specs {
t.Run(s, func(t *testing.T) {
spec, err := parseWorkflowSpec(s)
if err != nil {
t.Fatalf("parseWorkflowSpec(%q) unexpected error: %v", s, err)
}
if spec.Host != "" {
t.Errorf("parseWorkflowSpec(%q) host = %q, want empty (no pinning on public GitHub)", s, spec.Host)
}
})
}
}
func TestParseLocalWorkflowSpec(t *testing.T) {
// Clear the repository slug cache to ensure clean test state
ClearCurrentRepoSlugCache()
tests := []struct {
name string
spec string
wantWorkflowPath string
wantWorkflowName string
wantErr bool
errContains string
}{
{
name: "valid local workflow",
spec: "./workflows/my-workflow.md",
wantWorkflowPath: "./workflows/my-workflow.md",
wantWorkflowName: "my-workflow",
wantErr: false,
},
{
name: "local workflow in current directory",
spec: "./test.md",
wantWorkflowPath: "./test.md",
wantWorkflowName: "test",
wantErr: false,
},
{
name: "local workflow without .md extension",
spec: "./workflows/test",
wantErr: true,
errContains: "must end with '.md' extension",
},
{
name: "nested local workflow",
spec: "./path/to/nested/workflow.md",
wantWorkflowPath: "./path/to/nested/workflow.md",
wantWorkflowName: "workflow",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec, err := parseWorkflowSpec(tt.spec)
if tt.wantErr {
if err == nil {
t.Errorf("parseWorkflowSpec() expected error, got nil")
return
}
if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("parseWorkflowSpec() error = %v, want error containing %q", err, tt.errContains)
}
return
}
if err != nil {
t.Errorf("parseWorkflowSpec() unexpected error: %v", err)
return
}
if spec.WorkflowPath != tt.wantWorkflowPath {
t.Errorf("parseWorkflowSpec() workflowPath = %q, want %q", spec.WorkflowPath, tt.wantWorkflowPath)
}
if spec.WorkflowName != tt.wantWorkflowName {
t.Errorf("parseWorkflowSpec() workflowName = %q, want %q", spec.WorkflowName, tt.wantWorkflowName)
}
// For local workflows: version and repo should both be empty
// (local workflows don't come from a remote source)
if spec.Version != "" {
t.Errorf("parseWorkflowSpec() version = %q, want empty string for local workflow", spec.Version)
}
if spec.RepoSlug != "" {
t.Errorf("parseWorkflowSpec() repo = %q, want empty string for local workflow", spec.RepoSlug)
}
})
}
}
func TestWorkflowSpecString(t *testing.T) {
tests := []struct {
name string
spec *WorkflowSpec
expected string
}{
{
name: "with version",
spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "v1.0.0",
},
WorkflowPath: "workflows/ci-doctor.md",
},
expected: "owner/repo/workflows/ci-doctor.md@v1.0.0",
},
{
name: "without version",
spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "workflows/helper.md",
},
expected: "owner/repo/workflows/helper.md",
},
{
name: "with branch",
spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "githubnext/agentics",
Version: "main",
},
WorkflowPath: "workflows/weekly-research.md",
},
expected: "githubnext/agentics/workflows/weekly-research.md@main",
},
{
name: "local workflow",
spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "./workflows/local-workflow.md",
},
expected: "./workflows/local-workflow.md",
},
{
name: "local workflow in current directory",
spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "./test.md",
},
expected: "./test.md",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.spec.String()
if got != tt.expected {
t.Errorf("WorkflowSpec.String() = %q, want %q", got, tt.expected)
}
})
}
}
func TestParseSourceSpec(t *testing.T) {
tests := []struct {
name string
source string
wantRepo string
wantPath string
wantRef string
wantErr bool
errContains string
}{
{
name: "full spec with tag",
source: "githubnext/agentics/workflows/ci-doctor.md@v1.0.0",
wantRepo: "githubnext/agentics",
wantPath: "workflows/ci-doctor.md",
wantRef: "v1.0.0",
wantErr: false,
},
{
name: "full spec with branch",
source: "githubnext/agentics/workflows/ci-doctor.md@main",
wantRepo: "githubnext/agentics",
wantPath: "workflows/ci-doctor.md",
wantRef: "main",
wantErr: false,
},
{
name: "spec without ref",
source: "githubnext/agentics/workflows/ci-doctor.md",
wantRepo: "githubnext/agentics",
wantPath: "workflows/ci-doctor.md",
wantRef: "",
wantErr: false,
},
{
name: "nested path",
source: "owner/repo/path/to/workflow.md@v2.0.0",
wantRepo: "owner/repo",
wantPath: "path/to/workflow.md",
wantRef: "v2.0.0",
wantErr: false,
},
{
name: "invalid format - too few parts",
source: "owner/repo@v1.0.0",
wantErr: true,
errContains: "invalid source format",
},
{
name: "invalid format - missing owner",
source: "@v1.0.0",
wantErr: true,
errContains: "invalid source format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec, err := parseSourceSpec(tt.source)
if tt.wantErr {
if err == nil {
t.Errorf("parseSourceSpec() expected error containing %q, got nil", tt.errContains)
return
}
return
}
if err != nil {
t.Errorf("parseSourceSpec() unexpected error: %v", err)
return
}
if spec.Repo != tt.wantRepo {
t.Errorf("parseSourceSpec() repo = %q, want %q", spec.Repo, tt.wantRepo)
}
if spec.Path != tt.wantPath {
t.Errorf("parseSourceSpec() path = %q, want %q", spec.Path, tt.wantPath)
}
if spec.Ref != tt.wantRef {
t.Errorf("parseSourceSpec() ref = %q, want %q", spec.Ref, tt.wantRef)
}
})
}
}
func TestBuildSourceStringWithCommitSHA(t *testing.T) {
tests := []struct {
name string
workflow *WorkflowSpec
commitSHA string
expected string
}{
{
name: "with commit SHA",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "v1.0.0",
},
WorkflowPath: "workflows/ci-doctor.md",
},
commitSHA: "abc123def456789012345678901234567890abcd",
expected: "owner/repo/workflows/ci-doctor.md@abc123def456789012345678901234567890abcd",
},
{
name: "with commit SHA overrides version",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "main",
},
WorkflowPath: "workflows/helper.md",
},
commitSHA: "1234567890abcdef1234567890abcdef12345678",
expected: "owner/repo/workflows/helper.md@1234567890abcdef1234567890abcdef12345678",
},
{
name: "without commit SHA falls back to version",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "v2.0.0",
},
WorkflowPath: "workflows/test.md",
},
commitSHA: "",
expected: "owner/repo/workflows/test.md@v2.0.0",
},
{
name: "without commit SHA or version",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "workflows/test.md",
},
commitSHA: "",
expected: "owner/repo/workflows/test.md",
},
{
name: "empty repo with commit SHA",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "",
Version: "v1.0.0",
},
WorkflowPath: "workflows/test.md",
},
commitSHA: "abc123",
expected: "",
},
{
name: "local workflow with commit SHA",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "./workflows/local.md",
},
commitSHA: "abc123def456789012345678901234567890abcd",
expected: "owner/repo/workflows/local.md@abc123def456789012345678901234567890abcd",
},
{
name: "local workflow without commit SHA",
workflow: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "",
},
WorkflowPath: "./test.md",
},
commitSHA: "",
expected: "owner/repo/test.md",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := buildSourceStringWithCommitSHA(tt.workflow, tt.commitSHA)
if result != tt.expected {
t.Errorf("buildSourceStringWithCommitSHA() = %v, want %v", result, tt.expected)
}
})
}
}
func TestIsCommitSHA(t *testing.T) {
tests := []struct {
name string
version string
want bool
}{
{"valid SHA", "abc123def456789012345678901234567890abcd", true},
{"valid SHA lowercase", "abcdef1234567890123456789012345678901234", true},
{"valid SHA uppercase", "ABCDEF1234567890123456789012345678901234", true},
{"valid SHA mixed case", "AbCdEf1234567890123456789012345678901234", true},
{"invalid - too short", "abc123def456", false},
{"invalid - too long", "abc123def456789012345678901234567890abcdef", false},
{"invalid - contains non-hex", "abc123def456789012345678901234567890abcg", false},
{"invalid - empty", "", false},
{"invalid - branch name", "main", false},
{"invalid - version tag", "v1.0.0", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsCommitSHA(tt.version)
if got != tt.want {
t.Errorf("IsCommitSHA(%q) = %v, want %v", tt.version, got, tt.want)
}
})
}
}
// TestSpec_PublicAPI_ValidateWorkflowName validates the documented behavior.
// Spec: empty names and names with invalid characters (non-alphanumeric, non-hyphen, non-underscore) return errors.
func TestSpec_PublicAPI_ValidateWorkflowName(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{name: "valid alphanumeric-hyphen name", input: "my-workflow", wantErr: false},
{name: "valid name with underscores and digits", input: "my_workflow_123", wantErr: false},
{name: "empty name returns error", input: "", wantErr: true},
{name: "name with spaces returns error", input: "my workflow", wantErr: true},
{name: "name with slashes returns error", input: "my/workflow", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWorkflowName(tt.input)
if tt.wantErr {
assert.Error(t, err, "ValidateWorkflowName(%q) should return an error", tt.input)
} else {
assert.NoError(t, err, "ValidateWorkflowName(%q) should not return an error", tt.input)
}
})
}
}
// TestSpec_PublicAPI_GetVersion validates that GetVersion returns a non-empty string.
// Spec: returns the current CLI version.
func TestSpec_PublicAPI_GetVersion(t *testing.T) {
version := GetVersion()
assert.NotEmpty(t, version, "GetVersion should return a non-empty version string")
}
// TestSpec_PublicAPI_IsRunningInCI validates that IsRunningInCI returns a bool without panicking.
// Spec: detects CI environment.
func TestSpec_PublicAPI_IsRunningInCI(t *testing.T) {
result := IsRunningInCI()
_ = result // result is environment-dependent; ensure no panic
}
// TestSpec_Types_ShellType validates the documented ShellType string alias and its constants.
// Spec: "bash", "zsh", "fish", "powershell", "unknown"
func TestSpec_Types_ShellType(t *testing.T) {
assert.Equal(t, ShellBash, ShellType("bash"), "ShellBash constant should be \"bash\"")
assert.Equal(t, ShellZsh, ShellType("zsh"), "ShellZsh constant should be \"zsh\"")
assert.Equal(t, ShellFish, ShellType("fish"), "ShellFish constant should be \"fish\"")
assert.Equal(t, ShellPowerShell, ShellType("powershell"), "ShellPowerShell constant should be \"powershell\"")
assert.Equal(t, ShellUnknown, ShellType("unknown"), "ShellUnknown constant should be \"unknown\"")
}
// TestSpec_PublicAPI_DetectShell validates DetectShell returns one of the documented ShellType values.
func TestSpec_PublicAPI_DetectShell(t *testing.T) {
shell := DetectShell()
validShells := []ShellType{ShellBash, ShellZsh, ShellFish, ShellPowerShell, ShellUnknown}
assert.Contains(t, validShells, shell, "DetectShell should return one of the documented ShellType values")
}
// TestSpec_PublicAPI_ValidEngineNames validates the documented function returns a non-empty list.
// Spec: returns the supported engine names for shell completion.
func TestSpec_PublicAPI_ValidEngineNames(t *testing.T) {
engines := ValidEngineNames()
assert.NotEmpty(t, engines, "ValidEngineNames should return at least one engine name")
for _, name := range engines {
assert.NotEmpty(t, name, "each engine name should be non-empty")
}
}
// TestSpec_PublicAPI_ValidArtifactSetNames validates the documented function returns known artifact sets.
// Spec: returns the valid artifact set name strings.
func TestSpec_PublicAPI_ValidArtifactSetNames(t *testing.T) {
names := ValidArtifactSetNames()
assert.NotEmpty(t, names, "ValidArtifactSetNames should return a non-empty list")
assert.Contains(t, names, "all", "ValidArtifactSetNames should include \"all\"")
}
// TestSpec_PublicAPI_ValidateArtifactSets validates known and unknown artifact sets.
// Spec: validates that all provided artifact set names are known.
func TestSpec_PublicAPI_ValidateArtifactSets(t *testing.T) {
t.Run("known artifact set returns no error", func(t *testing.T) {
err := ValidateArtifactSets([]string{"all"})
assert.NoError(t, err, "ValidateArtifactSets should not error for known set \"all\"")
})
t.Run("unknown artifact set returns error", func(t *testing.T) {
err := ValidateArtifactSets([]string{"unknown-artifact-set-xyz"})
assert.Error(t, err, "ValidateArtifactSets should error for unknown artifact set")
})
t.Run("empty list returns no error", func(t *testing.T) {
err := ValidateArtifactSets([]string{})
assert.NoError(t, err, "ValidateArtifactSets should not error for empty list")
})
}
// TestSpec_PublicAPI_ExtractWorkflowDescription validates extraction of the description field
// from workflow markdown frontmatter.
// Spec: extracts the description field from workflow markdown content.
func TestSpec_PublicAPI_ExtractWorkflowDescription(t *testing.T) {
tests := []struct {
name string
content string
expected string
}{
{
name: "extracts description from frontmatter",
content: "---\ndescription: My workflow description\n---\n\n# Content",
expected: "My workflow description",
},
{
name: "returns empty string when no description field",
content: "---\nengine: copilot\n---\n\n# Content",
expected: "",
},
{
name: "returns empty string for content without frontmatter",
content: "# Just markdown",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractWorkflowDescription(tt.content)
assert.Equal(t, tt.expected, result, "ExtractWorkflowDescription mismatch for %q", tt.name)
})
}
}
// TestSpec_PublicAPI_ExtractWorkflowEngine validates extraction of the engine field.
// Spec: supports both string format (engine: copilot) and nested format (engine: { id: copilot }).
func TestSpec_PublicAPI_ExtractWorkflowEngine(t *testing.T) {
tests := []struct {
name string
content string
expected string
}{
{
name: "extracts engine in string format",
content: "---\nengine: copilot\n---\n\n# Content",
expected: "copilot",
},
{
name: "returns empty string when no engine field",
content: "---\ndescription: My workflow\n---\n\n# Content",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractWorkflowEngine(tt.content)
assert.Equal(t, tt.expected, result, "ExtractWorkflowEngine mismatch for %q", tt.name)
})
}
}
// TestSpec_PublicAPI_ExtractWorkflowPrivate validates extraction of the private flag.
// Spec: returns true if the workflow has private: true in its frontmatter.
func TestSpec_PublicAPI_ExtractWorkflowPrivate(t *testing.T) {
tests := []struct {
name string
content string
expected bool
}{
{
name: "returns true when private: true",
content: "---\nprivate: true\n---\n\n# Content",
expected: true,
},
{
name: "returns false when private: false",
content: "---\nprivate: false\n---\n\n# Content",
expected: false,
},
{
name: "returns false when no private field",
content: "---\nengine: copilot\n---\n\n# Content",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractWorkflowPrivate(tt.content)
assert.Equal(t, tt.expected, result, "ExtractWorkflowPrivate mismatch for %q", tt.name)
})
}
}
// TestSpec_DesignDecision_StderrDiagnostics verifies that stdout is available for structured output.