-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule_test.go
More file actions
3598 lines (3321 loc) · 94.5 KB
/
Copy pathrule_test.go
File metadata and controls
3598 lines (3321 loc) · 94.5 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
package catalog
import (
"os"
"path/filepath"
"strings"
"testing"
"testing/fstest"
"github.com/jeduden/mdsmith/internal/lint"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// helper creates a *lint.File with the given source and attaches the given FS.
func newTestFile(t *testing.T, path, source string, fs ...fstest.MapFS) *lint.File {
t.Helper()
f, err := lint.NewFile(path, []byte(source))
require.NoError(t, err)
if len(fs) > 0 {
f.FS = fs[0]
}
return f
}
// expectDiags asserts the number of diagnostics and returns them.
func expectDiags(t *testing.T, diags []lint.Diagnostic, count int) {
t.Helper()
if len(diags) != count {
msgs := make([]string, len(diags))
for i, d := range diags {
msgs[i] = d.Message
}
t.Fatalf("expected %d diagnostic(s), got %d: %v", count, len(diags), msgs)
}
}
// expectDiagMsg asserts the first diagnostic has the given message substring.
func expectDiagMsg(t *testing.T, diags []lint.Diagnostic, msg string) {
t.Helper()
if len(diags) == 0 {
t.Fatalf("expected diagnostic with message %q, got none", msg)
}
assert.Contains(t, diags[0].Message, msg,
"expected diagnostic message containing %q, got %q", msg, diags[0].Message)
}
// expectDiagLine asserts the first diagnostic is on the given line.
func expectDiagLine(t *testing.T, diags []lint.Diagnostic, line int) {
t.Helper()
if len(diags) == 0 {
t.Fatalf("expected diagnostic on line %d, got none", line)
}
if diags[0].Line != line {
t.Errorf("expected diagnostic on line %d, got line %d (message: %s)", line, diags[0].Line, diags[0].Message)
}
}
// =====================================================================
// Rule metadata
// =====================================================================
func TestRule_ID(t *testing.T) {
r := &Rule{}
if r.ID() != "MDS019" {
t.Errorf("expected ID MDS019, got %s", r.ID())
}
}
func TestRule_Name(t *testing.T) {
r := &Rule{}
if r.Name() != "catalog" {
t.Errorf("expected Name catalog, got %s", r.Name())
}
}
// =====================================================================
// Core rendering
// =====================================================================
func TestRendering_MinimalMode(t *testing.T) {
// Minimal mode (glob only) produces plain bullet list with basenames as link text.
src := `<?catalog
glob: "docs/*.md"
?>
- [api.md](docs/api.md)
- [guide.md](docs/guide.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("# API\n")},
"docs/guide.md": {Data: []byte("# Guide\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_ListTemplateWithFrontMatter(t *testing.T) {
// List template renders per-file with front matter fields.
src := `<?catalog
glob: "docs/*.md"
row: "- [{title}]({filename}) -- {description}"
?>
- [API Reference](docs/api.md) -- Complete API docs
- [Getting Started](docs/guide.md) -- How to get started
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("---\ntitle: API Reference\ndescription: Complete API docs\n---\n# API\n")},
"docs/guide.md": {Data: []byte("---\ntitle: Getting Started\ndescription: How to get started\n---\n# Guide\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_TableHeaderRows(t *testing.T) {
// Table template renders static header + per-file rows.
src := `<?catalog
glob: "docs/*.md"
header: |
| Title | Description |
|-------|-------------|
row: "| [{title}]({filename}) | {description} |"
?>
| Title | Description |
|----------------------------------|--------------------|
| [API Reference](docs/api.md) | Complete API docs |
| [Getting Started](docs/guide.md) | How to get started |
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("---\ntitle: API Reference\ndescription: Complete API docs\n---\n# API\n")},
"docs/guide.md": {Data: []byte("---\ntitle: Getting Started\ndescription: How to get started\n---\n# Guide\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_MultilineRowPipe(t *testing.T) {
// Multi-line `row` value with YAML `|` produces multi-line output per file.
// YAML `|` clips trailing newline to one.
src := `<?catalog
glob: "docs/*.md"
row: |
### {title}
{description}
?>
### API
Complete API docs
### Guide
How to get started
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("---\ntitle: API\ndescription: Complete API docs\n---\n")},
"docs/guide.md": {Data: []byte("---\ntitle: Guide\ndescription: How to get started\n---\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_MultilineRowPipePlus(t *testing.T) {
// Multi-line `row` value with YAML `|+` preserves trailing blank lines between entries.
src := `<?catalog
glob: "docs/*.md"
row: |+
### [{title}]({filename})
{description}
?>
### [API](docs/api.md)
Complete API docs
### [Guide](docs/guide.md)
How to get started
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("---\ntitle: API\ndescription: Complete API docs\n---\n")},
"docs/guide.md": {Data: []byte("---\ntitle: Guide\ndescription: How to get started\n---\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_PipeStripImplicitNewline(t *testing.T) {
// YAML `|-` strips trailing newlines; implicit `\n` rule adds one back.
src := `<?catalog
glob: "*.md"
row: |-
- {filename}
?>
- a.md
- b.md
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
"b.md": {Data: []byte("# B\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_EachValueTerminatedByNewline(t *testing.T) {
// Each rendered value (header, row, footer, empty) is terminated by implicit trailing `\n`.
src := `<?catalog
glob: "*.md"
header: "| Title |"
row: "| {filename} |"
footer: "---"
?>
| Title |
| a.md |
---
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_FooterRendersAfterRows(t *testing.T) {
// `footer` renders static content after rows.
src := `<?catalog
glob: "*.md"
header: |
| Title | Description |
|-------|-------------|
row: "| [{title}]({filename}) | {description} |"
footer: |
---
?>
| Title | Description |
|-------------|-------------|
| [API](a.md) | docs |
---
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("---\ntitle: API\ndescription: docs\n---\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_EmptyFallbackRendersWhenNoMatches(t *testing.T) {
// `empty` renders fallback text when glob matches zero files.
src := `<?catalog
glob: "nonexistent/*.md"
empty: No documents found.
?>
No documents found.
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_EmptyAloneWithoutRowIsValid(t *testing.T) {
// `empty` alone without `row` is valid (no diagnostic).
src := `<?catalog
glob: "nonexistent/*.md"
empty: No documents found.
?>
No documents found.
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_EmptyPlusHeaderWithoutRowProducesDiag(t *testing.T) {
// `empty` + `header` without `row` produces missing-row diagnostic.
src := `<?catalog
glob: "*.md"
empty: No docs.
header: |
| Title |
|-------|
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 1)
expectDiagMsg(t, diags, `generated section template missing required "row" key`)
}
func TestRendering_EmptyValueGetsTrailingNewline(t *testing.T) {
// `empty` value gets trailing `\n`.
src := `<?catalog
glob: "nonexistent/*.md"
empty: "Nothing here"
?>
Nothing here
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_NoEmptyNoMatchesEmptyContent(t *testing.T) {
// No `empty` + no matches produces empty content between markers.
src := `<?catalog
glob: "nonexistent/*.md"
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_WhenEmptyRendersHeaderFooterNotIncluded(t *testing.T) {
// When `empty` renders, `header`/`footer` are not included.
src := `<?catalog
glob: "nonexistent/*.md"
header: "| Title |"
row: "| {filename} |"
footer: "---"
empty: No documents.
?>
No documents.
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_WhenGlobMatchesFilesEmptyIgnored(t *testing.T) {
// When glob matches files and `empty` is defined, `empty` is ignored.
src := `<?catalog
glob: "*.md"
row: "- {filename}"
empty: No documents.
?>
- a.md
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
// =====================================================================
// Diagnostics
// =====================================================================
func TestDiag_OrphanedEndMarker(t *testing.T) {
src := `Some text
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 1)
expectDiagMsg(t, diags, "unexpected generated section end marker")
// Orphaned end markers are reported on the end marker line.
expectDiagLine(t, diags, 2)
}
func TestDiag_NestedStartMarkers(t *testing.T) {
// Unbalanced nesting: inner start marker consumes the only end
// marker, leaving the outer pair unclosed.
src := `<?catalog
glob: "*.md"
?>
<?catalog
glob: "other/*.md"
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
found := false
for _, d := range diags {
if strings.Contains(d.Message, "no closing marker") {
found = true
if d.Line != 1 {
t.Errorf("expected unclosed marker diagnostic on line 1, got %d", d.Line)
}
break
}
}
assert.True(t, found, "expected 'no closing marker' diagnostic for unbalanced nesting")
}
func TestDiag_NonStringYAMLValues(t *testing.T) {
src := `<?catalog
glob: 42
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
found := false
for _, d := range diags {
if strings.Contains(d.Message, `non-string value for key "glob"`) {
found = true
break
}
}
assert.True(t, found, "expected non-string value diagnostic for glob, got %v", diags)
}
func TestDiag_NonStringMultipleKeys(t *testing.T) {
// Non-string YAML values produce diagnostic per key.
src := `<?catalog
glob: 42
sort: true
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
nonStringCount := 0
for _, d := range diags {
if strings.Contains(d.Message, "non-string value for key") {
nonStringCount++
}
}
if nonStringCount < 2 {
t.Errorf("expected at least 2 non-string value diagnostics, got %d", nonStringCount)
}
}
// =====================================================================
// Sort
// =====================================================================
// =====================================================================
// Template fields
// =====================================================================
func TestFields_FilenameResolvesToRelativePath(t *testing.T) {
// `{filename}` resolves to path relative to linted file's directory, never has leading `./`.
src := `<?catalog
glob: "docs/*.md"
row: "- {filename}"
?>
- docs/api.md
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("# API\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestFields_MissingFrontMatterFieldsEmpty(t *testing.T) {
// Files without front matter resolve fields to empty string.
src := `<?catalog
glob: "*.md"
row: "- [{title}]({filename})"
?>
- [](a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# No front matter\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestFields_HeaderFooterContainTemplateLiterals(t *testing.T) {
// `header`/`footer` containing `{...}` render literally (no template expansion).
src := `<?catalog
glob: "*.md"
header: "{title} header"
row: "- {filename}"
footer: "{footer} end"
?>
{title} header
- a.md
{footer} end
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestFields_EmptyContainsTemplateLiterals(t *testing.T) {
// `empty` containing `{...}` renders literally.
src := `<?catalog
glob: "nonexistent/*.md"
empty: "{something} no data"
?>
{something} no data
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
// =====================================================================
// Nested front-matter access (CUE paths)
// =====================================================================
func TestRendering_NestedFrontMatter(t *testing.T) {
src := `<?catalog
glob: "docs/*.md"
row: "- [{params.subtitle}]({filename})"
?>
- [Overview](docs/api.md)
- [Intro](docs/guide.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/api.md": {Data: []byte("---\nparams:\n subtitle: Overview\n---\n# API\n")},
"docs/guide.md": {Data: []byte("---\nparams:\n subtitle: Intro\n---\n# Guide\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_QuotedKeyFrontMatter(t *testing.T) {
src := `<?catalog
glob: "docs/*.md"
row: '- [{"my-key"}]({filename})'
?>
- [value](docs/a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/a.md": {Data: []byte("---\nmy-key: value\n---\n# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_DeepNestedFrontMatter(t *testing.T) {
src := `<?catalog
glob: "docs/*.md"
row: "- {a.b.c}"
?>
- deep
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/a.md": {Data: []byte("---\na:\n b:\n c: deep\n---\n# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestRendering_MissingNestedFieldEmpty(t *testing.T) {
src := `<?catalog
glob: "docs/*.md"
row: "- [{params.missing}]({filename})"
?>
- [](docs/a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"docs/a.md": {Data: []byte("---\nparams:\n subtitle: Overview\n---\n# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestDiag_YAMLFlowMappingConflict(t *testing.T) {
src := `<?catalog
glob: "*.md"
row: {title}
?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 1)
expectDiagMsg(t, diags, "quote")
}
// =====================================================================
// Fix behavior
// =====================================================================
func TestFix_IdempotentOnFreshContent(t *testing.T) {
src := `<?catalog
glob: "*.md"
?>
- [a.md](a.md)
- [b.md](b.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
"b.md": {Data: []byte("# B\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
result := r.Fix(f)
if string(result) != src {
t.Errorf("Fix on fresh content should be idempotent.\nExpected:\n%s\nGot:\n%s", src, string(result))
}
}
func TestFix_LeavesTemplateErrorSectionsUnchanged(t *testing.T) {
// Invalid template syntax -> fix leaves section unchanged.
src := `<?catalog
glob: "*.md"
row: "{title"
?>
old content
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
result := r.Fix(f)
if string(result) != src {
t.Errorf("Fix should leave template-error sections unchanged.\nExpected:\n%s\nGot:\n%s", src, string(result))
}
}
func TestFix_FullCycleIdempotent(t *testing.T) {
// First fix generates content, second fix should leave it unchanged.
src := `<?catalog
glob: "*.md"
row: "- [{title}]({filename})"
?>
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("---\ntitle: Alpha\n---\n# A\n")},
"b.md": {Data: []byte("---\ntitle: Beta\n---\n# B\n")},
}
// First fix.
f1 := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
result1 := r.Fix(f1)
// Second fix on the result of the first fix.
f2 := newTestFile(t, "index.md", string(result1), mapFS)
result2 := r.Fix(f2)
if string(result1) != string(result2) {
t.Errorf("Fix is not idempotent.\nFirst:\n%s\nSecond:\n%s", string(result1), string(result2))
}
}
func TestFix_MultipleMarkerPairs(t *testing.T) {
src := `<?catalog
glob: "a/*.md"
?>
old
<?/catalog?>
<?catalog
glob: "b/*.md"
?>
old
<?/catalog?>
`
mapFS := fstest.MapFS{
"a/one.md": {Data: []byte("# One\n")},
"b/two.md": {Data: []byte("# Two\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
result := r.Fix(f)
assert.Contains(t, string(result), "- [one.md](a/one.md)", "Fix should regenerate first section with a/one.md")
assert.Contains(t, string(result), "- [two.md](b/two.md)", "Fix should regenerate second section with b/two.md")
}
// =====================================================================
// Edge cases
// =====================================================================
func TestEdge_MarkersInsideFencedCodeBlock(t *testing.T) {
src := "```\n<?catalog\nglob: \"*.md\"\n?>\n<?/catalog?>\n```\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_MarkersInsideIndentedCodeBlock(t *testing.T) {
// Indented code blocks (4-space indent) should also ignore markers.
src := "Paragraph before.\n\n" +
" <?catalog\n glob: \"*.md\"\n ?>\n" +
" <?/catalog?>\n\nParagraph after.\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_MarkersInsideHTMLBlock(t *testing.T) {
// goldmark treats <div>...</div> as an HTML block.
src := "<div>\n<?catalog\nglob: \"*.md\"\n?>\n<?/catalog?>\n</div>\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_MarkersInsideHTMLBlockWithClosure(t *testing.T) {
// HTML block type 6 with a closing blank line as closure.
// <table> is recognized as an HTML block that includes content until a blank line.
src := "<table>\n<tr><td><?catalog\nglob: \"*.md\"\n?></td></tr>\n</table>\n\nText after.\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
// Markers inside HTML block should be ignored. No structural errors expected.
for _, d := range diags {
if strings.Contains(d.Message, "no closing marker") ||
strings.Contains(d.Message, "unexpected") {
t.Errorf("markers inside HTML block should be ignored: %s", d.Message)
}
}
}
func TestEdge_MarkersInsidePreBlock(t *testing.T) {
// goldmark HTML block type 1 (<pre>) has explicit closure (</pre>).
// Markers inside should be ignored.
src := "<pre>\n<?catalog\nglob: \"*.md\"\n?>\n<?/catalog?>\n</pre>\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_MarkersInsideScriptBlock(t *testing.T) {
// goldmark HTML block type 1 (<script>) has explicit closure (</script>).
src := "<script>\n<?catalog\nglob: \"*.md\"\n?>\n<?/catalog?>\n</script>\n"
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_TerminatorAllowsLeadingTrailingWhitespace(t *testing.T) {
// `?>` terminator allows leading/trailing whitespace.
src := `<?catalog
glob: "*.md"
?>
- [a.md](a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# Hello\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_EndMarkerWithWhitespace(t *testing.T) {
src := `<?catalog
glob: "*.md"
?>
- [a.md](a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# Hello\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_SingleLineStartMarker(t *testing.T) {
// Single-line start marker has empty YAML body (triggers missing-parameter diagnostic).
src := `<?catalog?>
<?/catalog?>
`
mapFS := fstest.MapFS{}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 1)
expectDiagMsg(t, diags, `missing required "glob" parameter`)
}
func TestEdge_StdinInputSkipsRule(t *testing.T) {
// Stdin input skips the rule (`f.FS == nil`).
src := `<?catalog
glob: "*.md"
?>
stale content
<?/catalog?>
`
f := newTestFile(t, "index.md", src) // no FS set -> nil
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_StdinInputFixReturnsSourceUnchanged(t *testing.T) {
src := `<?catalog
glob: "*.md"
?>
stale content
<?/catalog?>
`
f := newTestFile(t, "index.md", src) // no FS set -> nil
r := &Rule{}
result := r.Fix(f)
if string(result) != src {
t.Errorf("Fix with nil FS should return source unchanged")
}
}
func TestEdge_GlobMatchingDirectorySkipped(t *testing.T) {
// Directories matched by glob should be silently skipped.
src := `<?catalog
glob: "*"
?>
- [a.md](a.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
"subdir/": {Data: []byte("")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_MultipleMarkerPairsIndependent(t *testing.T) {
// Multiple marker pairs in one file processed independently.
src := `<?catalog
glob: "a/*.md"
?>
- [one.md](a/one.md)
<?/catalog?>
Text between sections.
<?catalog
glob: "b/*.md"
?>
- [two.md](b/two.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"a/one.md": {Data: []byte("# One\n")},
"b/two.md": {Data: []byte("# Two\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}
diags := r.Check(f)
expectDiags(t, diags, 0)
}
func TestEdge_AllDiagnosticsReportColumn1(t *testing.T) {
tests := []struct {
name string
src string
}{
{
"stale",
"<?catalog\nglob: \"*.md\"\n?>\nold\n<?/catalog?>\n",
},
{
"unclosed",
"<?catalog\nglob: \"*.md\"\n?>\nold\n",
},
{
"orphaned",
"<?/catalog?>\n",
},
{
"missing glob",
"<?catalog\nsort: path\n?>\n<?/catalog?>\n",
},
{
"empty glob",
"<?catalog\nglob: \"\"\n?>\n<?/catalog?>\n",
},
{
"empty sort",
"<?catalog\nglob: \"*.md\"\nsort: \"\"\n?>\n<?/catalog?>\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mapFS := fstest.MapFS{
"a.md": {Data: []byte("# A\n")},
}
f := newTestFile(t, "index.md", tc.src, mapFS)
r := &Rule{}
diags := r.Check(f)
for _, d := range diags {
if d.Column != 1 {
t.Errorf("expected column 1, got %d for message: %s", d.Column, d.Message)
}
}
})
}
}
func TestEdge_RecursiveGlobPatterns(t *testing.T) {
// Recursive `**` glob patterns are supported.
src := `<?catalog
glob: "**/*.md"
?>
- [deep.md](a/b/c/deep.md)
- [top.md](top.md)
<?/catalog?>
`
mapFS := fstest.MapFS{
"top.md": {Data: []byte("# Top\n")},
"a/b/c/deep.md": {Data: []byte("# Deep\n")},
}
f := newTestFile(t, "index.md", src, mapFS)
r := &Rule{}