rumdl 0.2.78

A fast Markdown linter and formatter written in Rust
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
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
use std::fs;
use std::process::Command;
use tempfile::tempdir;

fn rumdl_bin() -> &'static str {
    env!("CARGO_BIN_EXE_rumdl")
}

/// rumdl config and rumdl config get must produce the same value for global.flavor
#[test]
fn test_flavor_display_is_consistent_between_config_and_config_get() {
    let temp_dir = tempdir().unwrap();

    let config_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "--no-config"])
        .output()
        .unwrap();
    let config_stdout = String::from_utf8_lossy(&config_output.stdout);

    let get_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "global.flavor", "--no-config"])
        .output()
        .unwrap();
    let get_stdout = String::from_utf8_lossy(&get_output.stdout);

    assert!(
        config_stdout.contains("flavor = \"standard\""),
        "`rumdl config` output should contain `flavor = \"standard\"`, got:\n{config_stdout}"
    );
    assert!(
        get_stdout.contains("\"standard\""),
        "`rumdl config get global.flavor` should contain `\"standard\"`, got:\n{get_stdout}"
    );
}

/// rumdl config --no-defaults must show non-default flavor in lowercase quoted form
#[test]
fn test_flavor_display_lowercase_in_no_defaults_mode() {
    let temp_dir = tempdir().unwrap();
    fs::write(temp_dir.path().join(".rumdl.toml"), "[global]\nflavor = \"mkdocs\"\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "--no-defaults"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("flavor = \"mkdocs\""),
        "`rumdl config --no-defaults` should show `flavor = \"mkdocs\"`, got:\n{stdout}"
    );
}

/// Both rumdl config and rumdl config get must agree on the flavor value for non-default flavors.
/// This is the exact scenario from the original bug: formatter.rs used {:?} (Debug) which
/// produced "MkDocs" while config get used Debug + to_lowercase() producing "mkdocs".
#[test]
fn test_flavor_display_consistent_for_non_default_flavor() {
    let temp_dir = tempdir().unwrap();
    fs::write(temp_dir.path().join(".rumdl.toml"), "[global]\nflavor = \"mkdocs\"\n").unwrap();

    let config_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config"])
        .output()
        .unwrap();

    let get_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "global.flavor"])
        .output()
        .unwrap();

    let config_stdout = String::from_utf8_lossy(&config_output.stdout);
    let get_stdout = String::from_utf8_lossy(&get_output.stdout);

    assert!(
        config_stdout.contains("flavor = \"mkdocs\""),
        "`rumdl config` should show `flavor = \"mkdocs\"`, got:\n{config_stdout}"
    );
    assert!(
        get_stdout.contains("\"mkdocs\""),
        "`rumdl config get global.flavor` should contain `\"mkdocs\"`, got:\n{get_stdout}"
    );
    // Both must use the same lowercase quoted format — the original bug produced
    // "MkDocs" (Debug) from rumdl config and "mkdocs" from rumdl config get.
    assert!(
        !config_stdout.contains("MkDocs"),
        "`rumdl config` must not use Debug format (MkDocs), got:\n{config_stdout}"
    );
    assert!(
        !get_stdout.contains("MkDocs"),
        "`rumdl config get` must not use Debug format (MkDocs), got:\n{get_stdout}"
    );
}

/// Flavor value in rumdl config output must be lowercase regardless of which flavor is set
#[test]
fn test_flavor_display_lowercase_when_set_to_mkdocs() {
    let temp_dir = tempdir().unwrap();
    fs::write(temp_dir.path().join(".rumdl.toml"), "[global]\nflavor = \"mkdocs\"\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("flavor = \"mkdocs\""),
        "`rumdl config` with mkdocs flavor should show `flavor = \"mkdocs\"`, got:\n{stdout}"
    );
}

#[test]
fn test_config_get_bare_rule_name_returns_all_keys() {
    // rumdl config get MD076 must return all config keys for rule MD076
    let temp_dir = tempdir().unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD076", "--no-config"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "`rumdl config get MD076` should succeed, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("MD076.style"),
        "Output should contain MD076.style, got:\n{stdout}"
    );
    assert!(
        stdout.contains("MD076.allow-loose-continuation"),
        "Output should contain MD076.allow-loose-continuation, got:\n{stdout}"
    );
    assert!(
        stdout.contains("[from default]"),
        "Output should contain [from default] annotation, got:\n{stdout}"
    );
}

#[test]
fn test_config_get_bare_rule_name_shows_overridden_value() {
    let temp_dir = tempdir().unwrap();
    fs::write(temp_dir.path().join(".rumdl.toml"), "[MD076]\nstyle = \"sublist\"\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD076"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("MD076.style") && stdout.contains("\"sublist\""),
        "Should show overridden style value, got:\n{stdout}"
    );
    assert!(
        stdout.contains("[from project config]"),
        "Overridden value should show project config provenance, got:\n{stdout}"
    );
}

#[test]
fn test_config_get_unknown_bare_name_errors_gracefully() {
    let temp_dir = tempdir().unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD999", "--no-config"])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "`rumdl config get MD999` should fail for unknown rule"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("MD999"),
        "Error message should include the unknown rule name, got:\n{stderr}"
    );
}

/// rumdl config get must accept lowercase rule names (normalize_key handles case)
#[test]
fn test_config_get_bare_rule_name_is_case_insensitive() {
    let temp_dir = tempdir().unwrap();

    let lower = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "md076", "--no-config"])
        .output()
        .unwrap();

    let upper = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD076", "--no-config"])
        .output()
        .unwrap();

    assert!(
        lower.status.success(),
        "`rumdl config get md076` (lowercase) should succeed, stderr:\n{}",
        String::from_utf8_lossy(&lower.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&lower.stdout),
        String::from_utf8_lossy(&upper.stdout),
        "`rumdl config get md076` and `rumdl config get MD076` must produce identical output"
    );
}

/// Fields in bare-rule output must be sorted alphabetically
#[test]
fn test_config_get_bare_rule_name_output_is_sorted() {
    let temp_dir = tempdir().unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD076", "--no-config"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // MD076 has two fields: allow-loose-continuation and style.
    // Alphabetically, allow-loose-continuation < style, so it must appear first.
    let pos_allow = stdout
        .find("allow-loose-continuation")
        .expect("Output should contain allow-loose-continuation");
    let pos_style = stdout.find("MD076.style").expect("Output should contain MD076.style");

    assert!(
        pos_allow < pos_style,
        "Fields must be sorted alphabetically: allow-loose-continuation ({pos_allow}) should precede style ({pos_style})"
    );
}

/// `rumdl config get <alias>` must produce identical output to `rumdl config get <MDxxx>`.
/// heading-increment is the canonical alias for MD001.
#[test]
fn test_config_get_bare_alias_matches_canonical_rule_name() {
    let temp_dir = tempdir().unwrap();

    let alias_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "heading-increment", "--no-config"])
        .output()
        .unwrap();

    let canonical_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD001", "--no-config"])
        .output()
        .unwrap();

    assert!(
        alias_output.status.success(),
        "`rumdl config get heading-increment` should succeed, stderr:\n{}",
        String::from_utf8_lossy(&alias_output.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&alias_output.stdout),
        String::from_utf8_lossy(&canonical_output.stdout),
        "`rumdl config get heading-increment` and `rumdl config get MD001` must produce identical output"
    );
}

/// `rumdl config get <alias>.<field>` must resolve the alias to its canonical rule.
/// line-length is the alias for MD013; `line-length.line-length` queries the line-length field.
#[test]
fn test_config_get_dotted_alias_field_resolves_rule() {
    let temp_dir = tempdir().unwrap();

    let alias_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "line-length.line-length", "--no-config"])
        .output()
        .unwrap();

    let canonical_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "MD013.line-length", "--no-config"])
        .output()
        .unwrap();

    assert!(
        alias_output.status.success(),
        "`rumdl config get line-length.line-length` should succeed, stderr:\n{}",
        String::from_utf8_lossy(&alias_output.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&alias_output.stdout),
        String::from_utf8_lossy(&canonical_output.stdout),
        "`rumdl config get line-length.line-length` and `rumdl config get MD013.line-length` must produce identical output"
    );
}

/// Underscore aliases (e.g. line_length) must work the same as hyphen aliases (line-length).
#[test]
fn test_config_get_underscore_alias_is_normalized() {
    let temp_dir = tempdir().unwrap();

    let underscore_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "line_length", "--no-config"])
        .output()
        .unwrap();

    let hyphen_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "line-length", "--no-config"])
        .output()
        .unwrap();

    assert!(
        underscore_output.status.success(),
        "`rumdl config get line_length` should succeed, stderr:\n{}",
        String::from_utf8_lossy(&underscore_output.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&underscore_output.stdout),
        String::from_utf8_lossy(&hyphen_output.stdout),
        "`rumdl config get line_length` and `rumdl config get line-length` must produce identical output"
    );
}

/// Aliases must be case-insensitive; HEADING-INCREMENT and heading-increment both map to MD001.
#[test]
fn test_config_get_alias_is_case_insensitive() {
    let temp_dir = tempdir().unwrap();

    let upper_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "HEADING-INCREMENT", "--no-config"])
        .output()
        .unwrap();

    let lower_output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "heading-increment", "--no-config"])
        .output()
        .unwrap();

    assert!(
        upper_output.status.success(),
        "`rumdl config get HEADING-INCREMENT` should succeed, stderr:\n{}",
        String::from_utf8_lossy(&upper_output.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&upper_output.stdout),
        String::from_utf8_lossy(&lower_output.stdout),
        "Alias lookup must be case-insensitive"
    );
}

/// When the config overrides a rule by its MDxxx name, querying via alias must show
/// the overridden value with the correct provenance.
#[test]
fn test_config_get_alias_reflects_project_config_override() {
    let temp_dir = tempdir().unwrap();
    fs::write(temp_dir.path().join(".rumdl.toml"), "[MD013]\nline-length = 120\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["config", "get", "line-length.line-length"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("120"),
        "Alias query should show overridden line-length value, got:\n{stdout}"
    );
    assert!(
        stdout.contains("[from project config]"),
        "Alias query should show project config provenance, got:\n{stdout}"
    );
}

/// Split `rumdl config` output into sections of `(text, annotation column)`
/// pairs, where the column is the display width before the `[from ...]` label.
fn annotated_sections(stdout: &str) -> Vec<Vec<(String, Option<usize>)>> {
    use unicode_width::UnicodeWidthStr;
    let mut sections = vec![Vec::new()];
    for line in stdout.lines() {
        if line.is_empty() {
            sections.push(Vec::new());
            continue;
        }
        let entry = match line.rfind(" [from ") {
            Some(idx) => (line[..idx].trim_end().to_string(), Some(line[..=idx].width())),
            None => (line.to_string(), None),
        };
        sections.last_mut().unwrap().push(entry);
    }
    sections.retain(|section| !section.is_empty());
    sections
}

fn assert_aligned_per_section(stdout: &str) {
    use unicode_width::UnicodeWidthStr;
    for line in stdout.lines() {
        assert_eq!(line, line.trim_end(), "trailing whitespace in {line:?}");
    }
    for section in annotated_sections(stdout) {
        let columns: Vec<(usize, usize)> = section
            .iter()
            .filter_map(|(text, column)| column.map(|column| (text.width(), column)))
            .filter(|(width, _)| *width <= 60)
            .collect();
        let Some(widest) = columns.iter().map(|(width, _)| *width).max() else {
            continue;
        };
        for (text, column) in &section {
            if let Some(column) = column
                && text.width() <= 60
            {
                assert_eq!(
                    *column,
                    widest + 1,
                    "label for {text:?} should sit one column past the widest entry in {section:?}"
                );
            }
        }
    }
}

#[test]
fn test_config_aligns_provenance_per_section_without_trailing_padding() {
    let dir = tempdir().unwrap();
    let output = Command::new(rumdl_bin())
        .args(["config", "--no-config", "--color", "never"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert_aligned_per_section(&stdout);
    // A long MD063 word list elsewhere must not push short lines far right.
    let cache = stdout.lines().find(|line| line.starts_with("cache = ")).unwrap();
    assert!(
        cache.len() < 40,
        "`cache` line is padded to {} columns: {cache:?}",
        cache.len()
    );
}

#[test]
fn test_config_no_defaults_aligns_wide_characters_by_display_width() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join(".rumdl.toml"),
        "[MD044]\nnames = [\"日本語のテキスト\"]\ncode-blocks = true\n",
    )
    .unwrap();
    let output = Command::new(rumdl_bin())
        .args(["config", "--no-defaults", "--color", "never"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("日本語のテキスト"), "{stdout}");

    assert_aligned_per_section(&stdout);
}

#[test]
fn test_config_long_help_keeps_each_example_on_its_own_line() {
    let output = Command::new(rumdl_bin()).args(["check", "--help"]).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    for example in ["- Rule option:", "- Global option:", "- Explicit global section:"] {
        let line = stdout
            .lines()
            .find(|line| line.contains(example))
            .unwrap_or_else(|| panic!("{example} missing from --help: {stdout}"));
        assert!(
            line.trim_start().starts_with(example) && line.matches("--config '").count() == 1,
            "{example} is not on its own line: {line:?}"
        );
    }
}

#[test]
fn test_rule_counts_agree_with_a_single_rule() {
    let output = Command::new(rumdl_bin())
        .args(["rule", "--list-categories"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("image (1 rule)\n"), "{stdout}");
    assert!(stdout.lines().any(|line| line.ends_with(" rules)")), "{stdout}");
    assert!(!stdout.contains("(1 rules)"), "{stdout}");

    let output = Command::new(rumdl_bin())
        .args(["rule", "--category", "image"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.trim_end().ends_with("Total: 1 rule"), "{stdout}");
}