Skip to content

Commit 2bbc9ed

Browse files
committed
Add unit tests for buildCatalogEntries error handling
Cover the new (entries, diagnostics) return signature: - Size-limit failure emits a "cannot read front matter" diagnostic attached to the directive file+line - Normal path returns entries with no diagnostics https://claude.ai/code/session_01HHssHCn4zfbMv1praKoxGJ
1 parent 940e00d commit 2bbc9ed

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

internal/rules/catalog/rule_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,3 +3182,56 @@ func TestCatalogInjection_BothNewlineAndLink(t *testing.T) {
31823182
diags := checkCatalogInjection("index.md", 5, entries)
31833183
assert.Len(t, diags, 2, "should report both newline and ]( issues")
31843184
}
3185+
3186+
// =====================================================================
3187+
// buildCatalogEntries error diagnostics (file-size limit)
3188+
// =====================================================================
3189+
3190+
func TestBuildCatalogEntries_SizeLimit_EmitsDiagnostic(t *testing.T) {
3191+
// When a file matched by the glob exceeds MaxInputBytes, we
3192+
// should surface a diagnostic instead of silently omitting it.
3193+
bigContent := strings.Repeat("x", 100)
3194+
fsys := fstest.MapFS{
3195+
"big.md": &fstest.MapFile{
3196+
Data: []byte("---\ntitle: Big\n---\n" + bigContent),
3197+
},
3198+
}
3199+
f := &lint.File{
3200+
Path: "index.md",
3201+
FS: fsys,
3202+
MaxInputBytes: 20,
3203+
}
3204+
params := map[string]string{
3205+
"glob": "big.md",
3206+
"row": "- [{title}](big.md)",
3207+
}
3208+
3209+
entries, diags := buildCatalogEntries(f, params, "index.md", 1)
3210+
assert.Empty(t, entries, "entry skipped due to read failure")
3211+
require.Len(t, diags, 1, "expected one diagnostic")
3212+
assert.Contains(t, diags[0].Message, "cannot read front matter")
3213+
assert.Contains(t, diags[0].Message, "file too large")
3214+
}
3215+
3216+
func TestBuildCatalogEntries_Normal_NoDiagnostics(t *testing.T) {
3217+
// Files within the size limit produce entries and no diagnostics.
3218+
fsys := fstest.MapFS{
3219+
"a.md": &fstest.MapFile{
3220+
Data: []byte("---\ntitle: A\n---\n# A\n"),
3221+
},
3222+
}
3223+
f := &lint.File{
3224+
Path: "index.md",
3225+
FS: fsys,
3226+
MaxInputBytes: 1000,
3227+
}
3228+
params := map[string]string{
3229+
"glob": "a.md",
3230+
"row": "- [{title}](a.md)",
3231+
}
3232+
3233+
entries, diags := buildCatalogEntries(f, params, "index.md", 1)
3234+
assert.Len(t, entries, 1, "entry should be kept")
3235+
assert.Empty(t, diags)
3236+
assert.Equal(t, "A", entries[0].fields["title"])
3237+
}

0 commit comments

Comments
 (0)