mdbook-lint 0.13.3

A fast markdown linter for mdBook
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
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
//! Integration tests for auto-fix functionality
//!
//! These tests verify the --fix, --fix-unsafe, and --dry-run flags work correctly
//! including backup creation, fix application, and proper exit codes.

mod common;

use common::*;
use predicates::str::contains;
use std::fs;
use tempfile::TempDir;

#[test]
fn test_fix_flag_applies_fixes() {
    // Test that --fix applies fixable violations
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \nMore content here.\n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file)
        .assert();

    // Should succeed (exit 0 when all fixable issues are resolved)
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify trailing spaces were fixed
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
    assert!(
        fixed_content.contains("spaces."),
        "Content should remain intact"
    );

    // Verify backup was created
    let backup_file = test_file.with_extension("md.bak");
    assert!(backup_file.exists(), "Backup file should be created");

    let backup_content = fs::read_to_string(&backup_file).unwrap();
    assert!(
        backup_content.contains("spaces.   "),
        "Backup should contain original content"
    );
}

#[test]
fn test_fix_with_remaining_violations() {
    // Test fix behavior when some violations cannot be fixed
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\n\n\nThis has trailing spaces.   \n\n\n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file)
        .assert();

    // Should succeed but show remaining violations
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"))
        .stdout(contains("Found"))
        .stdout(contains("warning(s)"));

    // Verify trailing spaces were fixed but multiple blank lines remain
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
    assert!(
        fixed_content.contains("\n\n\n"),
        "Multiple blank lines should remain (not fixable)"
    );
}

#[test]
fn test_fix_with_fail_on_warnings() {
    // Test that --fix with --fail-on-warnings exits with code 1 when issues remain
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\n\n\nThis has trailing spaces.   \n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg("--fail-on-warnings")
        .arg(&test_file)
        .assert();

    // Should exit with code 1 due to remaining violations
    assert
        .code(1)
        .stdout(contains("Fixed"))
        .stdout(contains("Found"))
        .stdout(contains("warning(s)"));
}

#[test]
fn test_dry_run_flag() {
    // Test that --dry-run shows what would be fixed without applying changes
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    let original_content = "# Test Document  \n\nThis has trailing spaces.   \n";
    fs::write(&test_file, original_content).unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg("--dry-run")
        .arg(&test_file)
        .assert();

    // Should succeed and show what would be fixed
    assert.success().stdout(contains("Would fix"));

    // Verify file content is unchanged
    let content_after = fs::read_to_string(&test_file).unwrap();
    assert_eq!(
        content_after, original_content,
        "File should not be modified in dry-run mode"
    );

    // Verify no backup was created
    let backup_file = test_file.with_extension("md.bak");
    assert!(
        !backup_file.exists(),
        "No backup should be created in dry-run mode"
    );
}

#[test]
fn test_fix_unsafe_flag() {
    // Test that --fix-unsafe applies all fixes (including potentially unsafe ones)
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \nMore content.\n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix-unsafe")
        .arg(&test_file)
        .assert();

    // Should succeed
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify fixes were applied
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
}

#[test]
fn test_dry_run_with_fix_unsafe() {
    // Test --dry-run with --fix-unsafe
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    let original_content = "# Test Document  \n\nThis has trailing spaces.   \n";
    fs::write(&test_file, original_content).unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix-unsafe")
        .arg("--dry-run")
        .arg(&test_file)
        .assert();

    // Should succeed
    assert.success().stdout(contains("Would fix"));

    // Verify file content is unchanged
    let content_after = fs::read_to_string(&test_file).unwrap();
    assert_eq!(
        content_after, original_content,
        "File should not be modified in dry-run mode"
    );
}

#[test]
fn test_backup_flag_disabled() {
    // Test --fix with --no-backup
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg("--no-backup")
        .arg(&test_file)
        .assert();

    // Should succeed
    assert.success().stdout(contains("Fixed"));

    // Verify no backup was created
    let backup_file = test_file.with_extension("md.bak");
    assert!(
        !backup_file.exists(),
        "No backup should be created when --no-backup"
    );

    // Verify fixes were still applied
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
}

#[test]
fn test_fix_multiple_files() {
    // Test fixing multiple files at once
    let temp_dir = TempDir::new().unwrap();
    let file1 = temp_dir.path().join("file1.md");
    let file2 = temp_dir.path().join("file2.md");

    fs::write(&file1, "# File One  \n\nTrailing spaces here.   \n").unwrap();

    fs::write(&file2, "# File Two  \n\nMore trailing spaces.    \n").unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&file1)
        .arg(&file2)
        .assert();

    // Should succeed and show fixes for both files
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"))
        .stdout(contains("2 file(s)"));

    // Verify both files were fixed
    let content1 = fs::read_to_string(&file1).unwrap();
    let content2 = fs::read_to_string(&file2).unwrap();

    assert!(
        !content1.contains("here.   "),
        "File 1 trailing spaces should be removed"
    );
    assert!(
        !content2.contains("spaces.    "),
        "File 2 trailing spaces should be removed"
    );

    // Verify backups were created
    assert!(file1.with_extension("md.bak").exists());
    assert!(file2.with_extension("md.bak").exists());
}

#[test]
fn test_fix_directory_recursively() {
    // Test fixing all markdown files in a directory
    let temp_dir = TempDir::new().unwrap();
    let sub_dir = temp_dir.path().join("subdir");
    fs::create_dir_all(&sub_dir).unwrap();

    let file1 = temp_dir.path().join("file1.md");
    let file2 = sub_dir.join("file2.md");

    fs::write(&file1, "# File One  \n\nTrailing spaces.   \n").unwrap();

    fs::write(&file2, "# File Two  \n\nMore trailing spaces.    \n").unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(temp_dir.path())
        .assert();

    // Should succeed
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify both files were fixed
    let content1 = fs::read_to_string(&file1).unwrap();
    let content2 = fs::read_to_string(&file2).unwrap();

    assert!(!content1.contains("spaces.   "));
    assert!(!content2.contains("spaces.    "));
}

#[test]
fn test_fix_no_fixable_violations() {
    // Test fix behavior when no violations have fixes available
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    // Create content that has violations but no fixable ones (MD033 - inline HTML has no fix)
    fs::write(
        &test_file,
        "# Test Document\n\nThis has <b>inline HTML</b> which violates MD033.\n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file)
        .assert();

    // Get output before consuming the assert
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();

    // Should succeed but show no fixes were applied
    assert
        .success()
        .stdout(contains("Found"))
        .stdout(contains("warning(s)"));

    // Should not show "Fixed" or "Applied" messages since no fixes were available
    assert!(
        !stdout.contains("Fixed"),
        "Should not show 'Fixed' when no fixes applied"
    );
    assert!(
        !stdout.contains("Applied"),
        "Should not show 'Applied' when no fixes applied"
    );

    // Verify file content is unchanged
    let content = fs::read_to_string(&test_file).unwrap();
    assert!(
        content.contains("<b>inline HTML</b>"),
        "Content should be unchanged when no fixes applied"
    );

    // Verify no backup was created since no changes were made
    let backup_file = test_file.with_extension("md.bak");
    assert!(
        !backup_file.exists(),
        "No backup should be created when no fixes applied"
    );
}

#[test]
fn test_fix_error_validation() {
    // Test error cases for fix flags

    // Test --dry-run without --fix or --fix-unsafe should fail
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");
    fs::write(&test_file, "# Test\nContent\n").unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--dry-run")
        .arg(&test_file)
        .assert();

    // Should fail with validation error
    assert
        .code(1)
        .stderr(contains("--dry-run requires either --fix or --fix-unsafe"));
}

#[test]
fn test_fix_clean_file() {
    // Test fixing a file that has no violations
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("clean.md");

    // Content needs 50+ words to pass CONTENT003 (short chapter detection)
    // Use # prefix in rust code block to avoid MDBOOK017 warning
    fs::write(
        &test_file,
        "# Clean Document

This file has no violations and contains enough content to pass all rules.
We need to write several sentences here to make sure we have at least fifty
words in total for the content quality checks to pass successfully.

Let me add some more text to ensure we definitely pass the minimum word
count threshold that is required by the linter for content validation.

```rust
# fn main() {
let x = 42;
# }
```
",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file)
        .assert();

    // Get output before consuming the assert
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();

    // Should succeed with no issues found
    assert.success().stdout(contains("No issues found"));

    // Should not show any fix-related output
    assert!(
        !stdout.contains("Fixed"),
        "Should not show 'Fixed' for clean files"
    );
    assert!(
        !stdout.contains("Applied"),
        "Should not show 'Applied' for clean files"
    );

    // Verify no backup was created
    let backup_file = test_file.with_extension("md.bak");
    assert!(
        !backup_file.exists(),
        "No backup should be created for clean files"
    );
}

#[test]
fn test_fix_exit_codes() {
    // Test various exit code scenarios with fixes
    let temp_dir = TempDir::new().unwrap();

    // Test 1: All violations fixed - should exit 0
    let test_file1 = temp_dir.path().join("all_fixable.md");
    fs::write(
        &test_file1,
        "# Test\nTrailing spaces.   \nMore trailing.  \n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file1)
        .assert();

    // Should exit 0 when all issues can be fixed
    assert.success();

    // Test 2: Some violations remain after fix - should exit 0 (warnings don't fail by default)
    let test_file2 = temp_dir.path().join("mixed.md");
    fs::write(&test_file2, "# Test\n\n\n\nTrailing spaces.   \n").unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg(&test_file2)
        .assert();

    // Should exit 0 even with remaining warnings
    assert.success();

    // Test 3: Remaining violations with --fail-on-warnings - should exit 1
    let test_file3 = temp_dir.path().join("mixed2.md");
    fs::write(&test_file3, "# Test\n\n\n\nTrailing spaces.   \n").unwrap();

    let assert = cli_command()
        .arg("lint")
        .arg("--fix")
        .arg("--fail-on-warnings")
        .arg(&test_file3)
        .assert();

    // Should exit 1 with --fail-on-warnings
    assert.code(1);
}

// ============================================================================
// Tests for the `fix` subcommand (shorthand for `lint --fix`)
// ============================================================================

#[test]
fn test_fix_subcommand_applies_fixes() {
    // Test that the `fix` subcommand applies fixable violations
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \nMore content here.\n",
    )
    .unwrap();

    let assert = cli_command().arg("fix").arg(&test_file).assert();

    // Should succeed (exit 0 when all fixable issues are resolved)
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify trailing spaces were fixed
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
}

#[test]
fn test_fix_subcommand_dry_run() {
    // Test that `fix --dry-run` shows what would be fixed without applying changes
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    let original_content = "# Test Document  \n\nThis has trailing spaces.   \n";
    fs::write(&test_file, original_content).unwrap();

    let assert = cli_command()
        .arg("fix")
        .arg("--dry-run")
        .arg(&test_file)
        .assert();

    // Should succeed and show what would be fixed
    assert.success().stdout(contains("Would fix"));

    // Verify file content is unchanged
    let content_after = fs::read_to_string(&test_file).unwrap();
    assert_eq!(
        content_after, original_content,
        "File should not be modified in dry-run mode"
    );
}

#[test]
fn test_fix_subcommand_unsafe_flag() {
    // Test that `fix --fix-unsafe` applies all fixes including potentially unsafe ones
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \nMore content.\n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("fix")
        .arg("--fix-unsafe")
        .arg(&test_file)
        .assert();

    // Should succeed
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify fixes were applied
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
}

#[test]
fn test_fix_subcommand_no_backup() {
    // Test `fix --no-backup`
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    fs::write(
        &test_file,
        "# Test Document  \n\nThis has trailing spaces.   \n",
    )
    .unwrap();

    let assert = cli_command()
        .arg("fix")
        .arg("--no-backup")
        .arg(&test_file)
        .assert();

    // Should succeed
    assert.success().stdout(contains("Fixed"));

    // Verify no backup was created
    let backup_file = test_file.with_extension("md.bak");
    assert!(
        !backup_file.exists(),
        "No backup should be created when --no-backup"
    );

    // Verify fixes were still applied
    let fixed_content = fs::read_to_string(&test_file).unwrap();
    assert!(
        !fixed_content.contains("spaces.   "),
        "Trailing spaces should be removed"
    );
}

#[test]
fn test_fix_subcommand_directory() {
    // Test fixing all markdown files in a directory using fix subcommand
    let temp_dir = TempDir::new().unwrap();
    let sub_dir = temp_dir.path().join("subdir");
    fs::create_dir_all(&sub_dir).unwrap();

    let file1 = temp_dir.path().join("file1.md");
    let file2 = sub_dir.join("file2.md");

    fs::write(&file1, "# File One  \n\nTrailing spaces.   \n").unwrap();
    fs::write(&file2, "# File Two  \n\nMore trailing spaces.    \n").unwrap();

    let assert = cli_command().arg("fix").arg(temp_dir.path()).assert();

    // Should succeed
    assert
        .success()
        .stdout(contains("Fixed"))
        .stdout(contains("Applied"));

    // Verify both files were fixed
    let content1 = fs::read_to_string(&file1).unwrap();
    let content2 = fs::read_to_string(&file2).unwrap();

    assert!(!content1.contains("spaces.   "));
    assert!(!content2.contains("spaces.    "));
}

#[test]
fn test_fix_subcommand_with_disable_flag() {
    // Test `fix --disable MD009` to skip trailing space fixes
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");

    let original_content = "# Test Document  \n\nThis has trailing spaces.   \n";
    fs::write(&test_file, original_content).unwrap();

    let assert = cli_command()
        .arg("fix")
        .arg("--disable")
        .arg("MD009")
        .arg(&test_file)
        .assert();

    // Should succeed but not fix MD009 violations
    assert.success();

    // Verify trailing spaces are still there (MD009 was disabled)
    let content_after = fs::read_to_string(&test_file).unwrap();
    assert!(
        content_after.contains("spaces.   "),
        "Trailing spaces should remain when MD009 is disabled"
    );
}

#[test]
fn test_fix_subcommand_equivalent_to_lint_fix() {
    // Verify that `fix` and `lint --fix` produce the same results
    let temp_dir = TempDir::new().unwrap();

    // Create two identical files
    let file1 = temp_dir.path().join("file1.md");
    let file2 = temp_dir.path().join("file2.md");

    let content = "# Test  \n\nTrailing spaces.   \nMore content.  \n";
    fs::write(&file1, content).unwrap();
    fs::write(&file2, content).unwrap();

    // Fix file1 using `fix` subcommand
    cli_command()
        .arg("fix")
        .arg("--no-backup")
        .arg(&file1)
        .assert()
        .success();

    // Fix file2 using `lint --fix`
    cli_command()
        .arg("lint")
        .arg("--fix")
        .arg("--no-backup")
        .arg(&file2)
        .assert()
        .success();

    // Verify both files have identical content after fixing
    let content1 = fs::read_to_string(&file1).unwrap();
    let content2 = fs::read_to_string(&file2).unwrap();

    assert_eq!(
        content1, content2,
        "`fix` and `lint --fix` should produce identical results"
    );
}