rumdl 0.2.60

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
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
//! Regression tests for cross-file rule suppression (inline disable +
//! per-file-ignores), especially across the lint-cache fast path.
//!
//! When `rumdl` gets a lint-cache hit but the workspace-index cache is absent
//! or stale, it rebuilds the cross-file index via `build_file_index_only`. That
//! rebuild must honor the same suppression the normal lint path applies:
//! inline `<!-- rumdl-disable -->` blocks. Separately, `per-file-ignores` must
//! suppress cross-file rules for the ignored file on every path.
//!
//! MD051 (cross-file link-fragment validation) is the live cross-file rule used
//! to exercise these paths.

use std::fs;
use std::path::Path;
use std::process::Command;

/// Run the rumdl binary in `dir` with `args`, returning combined stdout+stderr.
///
/// Pins `RUMDL_CACHE_DIR` to `dir/.rumdl_cache` so the cache location is
/// deterministic regardless of any ambient `RUMDL_CACHE_DIR` in the test
/// environment (which would otherwise redirect or share the workspace index).
fn run(dir: &Path, args: &[&str]) -> String {
    run_with_status(dir, args).1
}

fn run_with_status(dir: &Path, args: &[&str]) -> (bool, String) {
    let exe = env!("CARGO_BIN_EXE_rumdl");
    let output = Command::new(exe)
        .current_dir(dir)
        .env("RUMDL_CACHE_DIR", dir.join(".rumdl_cache"))
        .args(args)
        .output()
        .expect("failed to execute rumdl");
    let mut combined = String::from_utf8_lossy(&output.stdout).to_string();
    combined.push_str(&String::from_utf8_lossy(&output.stderr));
    (output.status.success(), combined)
}

/// Delete only the workspace-index cache, leaving the lint cache intact, so the
/// next run takes the lint-cache-hit + index-rebuild path.
fn delete_workspace_index(dir: &Path) {
    let path = dir.join(".rumdl_cache").join("workspace_index.bin");
    assert!(
        path.exists(),
        "expected workspace index cache at {} after first run",
        path.display()
    );
    fs::remove_file(&path).expect("failed to remove workspace_index.bin");
}

/// b.md provides a heading anchor; a.md links to a *missing* fragment in b.md,
/// which is what MD051's cross-file check flags.
fn write_target(dir: &Path) {
    fs::write(dir.join("b.md"), "# Target\n\n## Real Heading\n").unwrap();
}

#[test]
fn md057_cache_misses_when_a_missing_target_appears() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing target must fail on the first run:\n{first}");
    assert!(first.contains("Relative link 'b.md' does not exist"), "got:\n{first}");

    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "creating the target must invalidate the cached MD057 warning:\n{second}"
    );
    assert!(!second.contains("MD057"), "got:\n{second}");
}

#[test]
fn md057_cache_misses_when_an_existing_target_disappears() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "existing target must pass on the first run:\n{first}");

    fs::remove_file(dir.join("b.md")).unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!second_success, "removing the target must invalidate the cached pass");
    assert!(second.contains("Relative link 'b.md' does not exist"), "got:\n{second}");
}

#[test]
fn md057_file_without_relative_links_keeps_hitting_the_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(first_success, "first run must populate a clean cache entry:\n{first}");

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--verbose"]);
    assert!(second_success, "second run must remain clean:\n{second}");
    assert!(
        second.contains("Cache hit for"),
        "expected a measured cache hit:\n{second}"
    );
    assert!(!second.contains("Cache miss for"), "unexpected cache miss:\n{second}");
}

#[test]
fn md057_cache_misses_when_workspace_link_data_is_unavailable() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "first run must populate both caches:\n{first}");
    delete_workspace_index(dir);

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057", "--verbose"]);
    assert!(
        second_success,
        "fallback lint must preserve the clean verdict:\n{second}"
    );
    assert!(
        second.contains("cross-file dependency state is unavailable"),
        "missing workspace data must force a cache miss:\n{second}"
    );
}

#[test]
fn cache_hit_does_not_require_workspace_data_when_md057_is_disabled() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD013"]);
    assert!(first_success, "first run must populate a clean cache entry:\n{first}");
    assert!(!dir.join(".rumdl_cache/workspace_index.bin").exists());

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD013", "--verbose"]);
    assert!(second_success, "second run must remain clean:\n{second}");
    assert!(
        second.contains("Cache hit for"),
        "MD057-disabled run must hit:\n{second}"
    );
}

#[test]
fn md057_cache_misses_when_target_changes_from_file_to_directory() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "file target must pass:\n{first}");

    fs::remove_file(dir.join("b.md")).unwrap();
    fs::create_dir(dir.join("b.md")).unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--verbose"]);
    assert!(second_success, "directory target is also valid:\n{second}");
    assert!(
        second.contains("cross-file dependency state changed"),
        "target kind change must invalidate the cached identity:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_configured_search_paths() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[MD057]\nsearch-paths = [\"assets\"]\n").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n![image](photo.png)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing search-path target must fail:\n{first}");

    fs::create_dir(dir.join("assets")).unwrap();
    fs::write(dir.join("assets/photo.png"), "image").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "search-path target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_obsidian_attachment_folder() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::create_dir(dir.join(".obsidian")).unwrap();
    fs::write(
        dir.join(".obsidian/app.json"),
        r#"{"attachmentFolderPath":"Attachments"}"#,
    )
    .unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n![image](photo.png)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--flavor", "obsidian"]);
    assert!(!first_success, "missing attachment must fail:\n{first}");

    fs::create_dir(dir.join("Attachments")).unwrap();
    fs::write(dir.join("Attachments/photo.png"), "image").unwrap();

    let (second_success, second) =
        run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--flavor", "obsidian"]);
    assert!(
        second_success,
        "attachment target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_absolute_link_roots() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(
        dir.join(".rumdl.toml"),
        "[MD057]\nabsolute-links = \"relative_to_roots\"\nroots = [\"content\"]\n",
    )
    .unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[guide](/guide.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing absolute target must fail:\n{first}");

    fs::create_dir(dir.join("content")).unwrap();
    fs::write(dir.join("content/guide.md"), "# Guide\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(second_success, "root target must invalidate the warning:\n{second}");
}

#[test]
fn md057_cache_tracks_mkdocs_docs_dir() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(
        dir.join(".rumdl.toml"),
        "[MD057]\nabsolute-links = \"relative_to_docs\"\n",
    )
    .unwrap();
    fs::write(dir.join("mkdocs.yml"), "site_name: Test\ndocs_dir: docs\n").unwrap();
    fs::create_dir(dir.join("docs")).unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[guide](/guide.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing docs-dir target must fail:\n{first}");

    fs::write(dir.join("docs/guide.md"), "# Guide\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(second_success, "docs-dir target must invalidate the warning:\n{second}");
}

#[test]
fn md057_cache_tracks_reference_definition_targets() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(
        dir.join("a.md"),
        "# Source\n\n[download][asset]\n\n[asset]: artifact.zip\n",
    )
    .unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing reference target must fail:\n{first}");

    fs::write(dir.join("artifact.zip"), "archive").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "reference target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_checked_frontmatter_targets() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[MD057]\ncheck-frontmatter = true\n").unwrap();
    fs::write(dir.join("a.md"), "---\nmanual: docs/guide.pdf\n---\n\n# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing frontmatter target must fail:\n{first}");

    fs::create_dir(dir.join("docs")).unwrap();
    fs::write(dir.join("docs/guide.pdf"), "guide").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "frontmatter target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_fmt_does_not_replay_a_stale_warning() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        !first_success,
        "missing target must warm a failing cache entry:\n{first}"
    );

    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (_, formatted) = run_with_status(dir, &["fmt", "a.md", "b.md", "--enable", "MD057"]);
    assert!(
        !formatted.contains("MD057"),
        "fmt must not replay the stale warning:\n{formatted}"
    );
}

#[test]
fn cache_hit_respects_inline_disable_for_cross_file_rule() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    write_target(dir);
    fs::write(
        dir.join("a.md"),
        "# Source\n\n\
         <!-- rumdl-disable MD051 -->\n\
         [link](b.md#nonexistent)\n\
         <!-- rumdl-enable MD051 -->\n",
    )
    .unwrap();

    // First run: populate caches. MD051 is suppressed by the inline block.
    let first = run(dir, &["check", "."]);
    assert!(
        !first.contains("MD051"),
        "baseline: inline disable should suppress MD051, got:\n{first}"
    );

    // Drop only the workspace-index cache, forcing the index-rebuild path.
    delete_workspace_index(dir);

    // Second run: lint-cache hit + index rebuild must still honor the disable.
    let second = run(dir, &["check", "."]);
    assert!(
        !second.contains("MD051"),
        "MD051 must stay suppressed on a cache hit (inline disable), got:\n{second}"
    );
}

#[test]
fn per_file_ignores_suppresses_cross_file_rule_without_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[per-file-ignores]\n\"a.md\" = [\"MD051\"]\n").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#nonexistent)\n").unwrap();

    // --no-cache isolates this from the lint cache: per-file-ignores alone must
    // suppress the cross-file rule for a.md.
    let out = run(dir, &["check", ".", "--no-cache"]);
    assert!(
        !out.contains("MD051"),
        "per-file-ignores must suppress cross-file MD051 for a.md, got:\n{out}"
    );
}

#[test]
fn per_file_ignores_suppresses_cross_file_rule_on_cache_hit() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[per-file-ignores]\n\"a.md\" = [\"MD051\"]\n").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#nonexistent)\n").unwrap();

    // First run populates caches.
    let _ = run(dir, &["check", "."]);
    delete_workspace_index(dir);

    // Cache-hit + index rebuild must still honor per-file-ignores.
    let out = run(dir, &["check", "."]);
    assert!(
        !out.contains("MD051"),
        "per-file-ignores must suppress cross-file MD051 on a cache hit, got:\n{out}"
    );
}