rumdl 0.1.51

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
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
//! Integration tests for the --fail-on CLI flag
//!
//! Tests that the --fail-on flag correctly controls exit code behavior based on
//! violation severity.

use std::fs;
use std::process::Command;
use tempfile::tempdir;

/// Get the path to the rumdl binary
fn rumdl_bin() -> &'static str {
    env!("CARGO_BIN_EXE_rumdl")
}

/// Create a test file with only warning-level violations (MD007 - indent issue)
fn create_warning_only_file(dir: &std::path::Path) -> std::path::PathBuf {
    let path = dir.join("warning_only.md");
    // MD007 triggers on incorrect list indentation (warning severity)
    fs::write(
        &path,
        r#"# Test

- Item 1
   - Nested item with wrong indent (3 spaces instead of 2 or 4)
"#,
    )
    .unwrap();
    path
}

/// Create a test file with error-level violations (MD042 - empty link)
fn create_error_file(dir: &std::path::Path) -> std::path::PathBuf {
    let path = dir.join("error.md");
    // MD042 triggers on empty links (error severity)
    fs::write(
        &path,
        r#"# Test

[empty link]()
"#,
    )
    .unwrap();
    path
}

/// Create a config file that only enables MD007 (warning) and MD042 (error)
fn create_config(dir: &std::path::Path) {
    fs::write(
        dir.join(".rumdl.toml"),
        r#"[global]
enable = ["MD007", "MD042"]
"#,
    )
    .unwrap();
}

#[test]
fn test_fail_on_never_with_errors_exits_zero() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap(), "--fail-on", "never"])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "--fail-on never should exit 0 even with errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_never_with_warnings_exits_zero() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let warning_file = create_warning_only_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", warning_file.to_str().unwrap(), "--fail-on", "never"])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "--fail-on never should exit 0 even with warnings\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_error_with_only_warnings_exits_zero() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let warning_file = create_warning_only_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", warning_file.to_str().unwrap(), "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    // Verify that warnings are still reported
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("MD007") || !output.status.success() || stderr.is_empty(),
        "Expected MD007 warning to be reported or file to have no issues"
    );

    assert!(
        output.status.success(),
        "--fail-on error should exit 0 when only warnings exist\nstderr: {stderr}"
    );
}

#[test]
fn test_fail_on_error_with_errors_exits_one() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap(), "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "--fail-on error should exit 1 when errors exist\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        output.status.code(),
        Some(1),
        "Expected exit code 1 for errors with --fail-on error"
    );
}

#[test]
fn test_fail_on_any_with_warnings_exits_one() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let warning_file = create_warning_only_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", warning_file.to_str().unwrap(), "--fail-on", "any"])
        .output()
        .expect("Failed to execute command");

    // If there are warnings, exit code should be 1
    let stderr = String::from_utf8_lossy(&output.stderr);
    if stderr.contains("MD007") {
        assert!(
            !output.status.success(),
            "--fail-on any should exit 1 on any violation\nstderr: {stderr}"
        );
    }
}

#[test]
fn test_fail_on_any_with_errors_exits_one() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap(), "--fail-on", "any"])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "--fail-on any should exit 1 on errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_default_is_any() {
    // Without --fail-on flag, default should be "any" (exit 1 on any violation)
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "Default --fail-on (any) should exit 1 on violations\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_with_no_violations_exits_zero() {
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let clean_file = temp_dir.path().join("clean.md");
    fs::write(&clean_file, "# Clean File\n\nNo issues here.\n").unwrap();

    // Test all modes exit 0 when there are no violations
    for mode in ["any", "error", "never"] {
        let output = Command::new(rumdl_bin())
            .current_dir(temp_dir.path())
            .args(["check", clean_file.to_str().unwrap(), "--fail-on", mode])
            .output()
            .expect("Failed to execute command");

        assert!(
            output.status.success(),
            "--fail-on {} should exit 0 when no violations\nstderr: {}",
            mode,
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

#[test]
fn test_fail_on_with_stdin() {
    // Test that --fail-on works with stdin input

    // Test --fail-on never with error content
    let output = Command::new(rumdl_bin())
        .args(["check", "--stdin", "--fail-on", "never", "--enable", "MD042"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child.stdin.take().unwrap().write_all(b"# Test\n\n[empty]()\n").unwrap();
            child.wait_with_output()
        })
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "--fail-on never should exit 0 with stdin errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Test --fail-on error with error content
    let output = Command::new(rumdl_bin())
        .args(["check", "--stdin", "--fail-on", "error", "--enable", "MD042"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child.stdin.take().unwrap().write_all(b"# Test\n\n[empty]()\n").unwrap();
            child.wait_with_output()
        })
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "--fail-on error should exit 1 with stdin errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_invalid_value_rejected() {
    let output = Command::new(rumdl_bin())
        .args(["check", ".", "--fail-on", "invalid"])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success(), "Invalid --fail-on value should be rejected");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("invalid") || stderr.contains("error"),
        "Error message should indicate invalid value\nstderr: {stderr}"
    );
}

// =============================================================================
// Edge Cases
// =============================================================================

/// Create a file with BOTH warning and error violations
fn create_mixed_file(dir: &std::path::Path) -> std::path::PathBuf {
    let path = dir.join("mixed.md");
    // MD007 (warning) + MD042 (error) in same file
    fs::write(
        &path,
        r#"# Test

- Item 1
   - Nested item with wrong indent

[empty link]()
"#,
    )
    .unwrap();
    path
}

/// Create a file with an unfixable error (MD042 - empty links can't be auto-fixed)
fn create_unfixable_error_file(dir: &std::path::Path) -> std::path::PathBuf {
    let path = dir.join("unfixable_error.md");
    fs::write(
        &path,
        r#"# Test

[empty link]()
"#,
    )
    .unwrap();
    path
}

#[test]
fn test_fail_on_error_with_mixed_warnings_and_errors_exits_one() {
    // When a file has BOTH warnings and errors, --fail-on error should exit 1
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let mixed_file = create_mixed_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", mixed_file.to_str().unwrap(), "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    // Output can go to stdout or stderr depending on mode
    let combined_output = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify error is reported
    assert!(
        combined_output.contains("MD042"),
        "Error MD042 should be reported\noutput: {combined_output}"
    );

    assert!(
        !output.status.success(),
        "--fail-on error should exit 1 when errors exist (even with warnings)\noutput: {combined_output}"
    );
}

#[test]
fn test_fail_on_error_with_multiple_files_mixed_severities() {
    // One file with only warnings, another with only errors
    // --fail-on error should exit 1 because one file has errors
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let warning_file = create_warning_only_file(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args([
            "check",
            warning_file.to_str().unwrap(),
            error_file.to_str().unwrap(),
            "--fail-on",
            "error",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "--fail-on error should exit 1 when any file has errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_error_with_multiple_warning_only_files_exits_zero() {
    // Multiple files, all with only warnings
    // --fail-on error should exit 0
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());

    let warning_file1 = temp_dir.path().join("warning1.md");
    let warning_file2 = temp_dir.path().join("warning2.md");
    fs::write(&warning_file1, "# Test\n\n- Item\n   - Bad indent\n").unwrap();
    fs::write(&warning_file2, "# Test\n\n- Item\n   - Bad indent\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args([
            "check",
            warning_file1.to_str().unwrap(),
            warning_file2.to_str().unwrap(),
            "--fail-on",
            "error",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "--fail-on error should exit 0 when all files have only warnings\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_never_still_reports_issues() {
    // --fail-on never should still report issues in output, just exit 0
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap(), "--fail-on", "never"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "Should exit 0");

    // Output can go to stdout or stderr depending on mode
    let combined_output = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined_output.contains("MD042"),
        "Issues should still be reported even with --fail-on never\noutput: {combined_output}"
    );
}

#[test]
fn test_fail_on_error_still_reports_warnings() {
    // --fail-on error should still report warnings in output, just exit 0 for them
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let warning_file = create_warning_only_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", warning_file.to_str().unwrap(), "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "Should exit 0 for warnings-only");

    // Output can go to stdout or stderr depending on mode
    let combined_output = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined_output.contains("MD007"),
        "Warnings should still be reported even with --fail-on error\noutput: {combined_output}"
    );
}

#[test]
fn test_fail_on_with_fix_mode_remaining_errors() {
    // After --fix, if unfixable errors remain, --fail-on error should exit 1
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_unfixable_error_file(temp_dir.path());

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", error_file.to_str().unwrap(), "--fix", "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    // MD042 (empty links) cannot be auto-fixed, so error should remain
    assert!(
        !output.status.success(),
        "--fix with --fail-on error should exit 1 when unfixable errors remain\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_with_fix_mode_remaining_warnings_only() {
    // After --fix, if only warnings remain (or warnings can't be fixed), --fail-on error should exit 0
    let temp_dir = tempdir().unwrap();

    // Create config that only enables MD009 (trailing spaces - fixable warning)
    // and create a file where after fixing there are no issues
    fs::write(
        temp_dir.path().join(".rumdl.toml"),
        r#"[global]
enable = ["MD009"]
"#,
    )
    .unwrap();

    let test_file = temp_dir.path().join("trailing.md");
    fs::write(&test_file, "# Test  \n\nSome text  \n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", test_file.to_str().unwrap(), "--fix", "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    // MD009 warnings are fixable, so after fix there should be no issues
    assert!(
        output.status.success(),
        "--fix with --fail-on error should exit 0 when only warnings existed and were fixed\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_with_fmt_subcommand() {
    // fmt subcommand should also respect --fail-on
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());
    let error_file = create_unfixable_error_file(temp_dir.path());

    // fmt with --fail-on error and unfixable errors should...
    // Actually, fmt mode always exits 0 by design (FixMode::Format)
    // So --fail-on should be irrelevant for fmt
    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["fmt", error_file.to_str().unwrap(), "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    // fmt always exits 0 regardless of --fail-on (by design)
    assert!(
        output.status.success(),
        "fmt should always exit 0 (format mode ignores --fail-on)\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_stdin_with_warning_only() {
    // stdin with --fail-on error and only warnings should exit 0
    let output = Command::new(rumdl_bin())
        .args([
            "check",
            "--stdin",
            "--fail-on",
            "error",
            "--enable",
            "MD009", // Trailing spaces - warning severity
        ])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child
                .stdin
                .take()
                .unwrap()
                .write_all(b"# Test  \n\nTrailing spaces  \n")
                .unwrap();
            child.wait_with_output()
        })
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "--fail-on error with stdin warnings should exit 0\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fail_on_directory_scan_with_mixed_files() {
    // Scanning a directory with mixed severity files
    let temp_dir = tempdir().unwrap();
    create_config(temp_dir.path());

    // Create subdir with files
    let subdir = temp_dir.path().join("docs");
    fs::create_dir(&subdir).unwrap();

    // File with only warning
    fs::write(subdir.join("warning.md"), "# Test\n\n- Item\n   - Bad\n").unwrap();
    // File with error
    fs::write(subdir.join("error.md"), "# Test\n\n[empty]()\n").unwrap();
    // Clean file
    fs::write(subdir.join("clean.md"), "# Clean\n\nNo issues.\n").unwrap();

    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", "docs", "--fail-on", "error"])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "Directory scan with --fail-on error should exit 1 when any file has errors\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Same directory with --fail-on never should exit 0
    let output = Command::new(rumdl_bin())
        .current_dir(temp_dir.path())
        .args(["check", "docs", "--fail-on", "never"])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "Directory scan with --fail-on never should exit 0\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}