mdbook-lint 0.11.6

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
//! 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::prelude::*;
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("violation"));

    // 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("violation"));
}

#[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("violation"));

    // 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");

    fs::write(
        &test_file,
        "# Clean Document\n\nThis file has no violations.\n\n```rust\nfn main() {}\n```\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 with no issues found
    assert
        .success()
        .stdout(contains("✅ No issues found").or(contains("Found 0 violation")));

    // 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);
}