rumdl 0.2.76

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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
//! A run that checks zero files must say so.
//!
//! Checking nothing and checking everything cleanly both exit 0 with no findings,
//! so an over-broad ignore file or a mistyped `include` pattern can silently turn
//! rumdl into a no-op that passes CI. These tests pin the notice that separates
//! the two: which stream it goes to, which flags suppress it, what it attributes
//! the emptiness to, and that machine-readable output stays parseable.

use std::fs;
use std::process::{Command, Output};
use tempfile::TempDir;

/// Runs `rumdl check` in `dir` with config discovery and the cache disabled, so
/// the result depends only on the tree the test built.
fn check(dir: &std::path::Path, args: &[&str]) -> Output {
    let mut command = Command::new(env!("CARGO_BIN_EXE_rumdl"));
    command
        .current_dir(dir)
        .arg("check")
        .arg("--no-config")
        .arg("--no-cache");
    command.args(args);
    command.output().expect("failed to execute rumdl")
}

/// Runs `rumdl check` in `dir` with config discovery left on, for the cases that
/// turn on what a config file says.
fn check_configured(dir: &std::path::Path, args: &[&str]) -> Output {
    let mut command = Command::new(env!("CARGO_BIN_EXE_rumdl"));
    command.current_dir(dir).arg("check").arg("--no-cache");
    command.args(args);
    command.output().expect("failed to execute rumdl")
}

fn stdout_of(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).into_owned()
}

fn stderr_of(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

/// A directory holding one markdown file, plus whatever `extra` files the test needs.
fn tree(extra: &[(&str, &str)]) -> TempDir {
    let temp_dir = TempDir::new().unwrap();
    fs::write(temp_dir.path().join("guide.md"), "# Guide\n").unwrap();
    for (path, contents) in extra {
        let full_path = temp_dir.path().join(path);
        if let Some(parent) = full_path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(full_path, contents).unwrap();
    }
    temp_dir
}

#[test]
fn ignore_file_swallowing_every_file_is_reported() {
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    let output = check(temp_dir.path(), &["."]);
    let stderr = stderr_of(&output);

    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );
    assert!(
        stderr.contains("1 by ignore files"),
        "should attribute the emptiness to ignore files. stderr: {stderr}"
    );
    assert!(
        stderr.contains("--respect-gitignore=false"),
        "should name the flag that undoes it. stderr: {stderr}"
    );
}

#[test]
fn include_pattern_matching_nothing_is_named() {
    let temp_dir = tree(&[]);

    // A plausible typo: the directory is `docs`, the pattern says `dcos`.
    let output = check(temp_dir.path(), &[".", "--include", "dcos/**/*.md"]);
    let stderr = stderr_of(&output);

    assert!(
        stderr.contains("1 by include patterns"),
        "should attribute the emptiness to the include patterns. stderr: {stderr}"
    );
    assert!(
        stderr.contains("include pattern 'dcos/**/*.md' matches no file"),
        "should name the pattern that selected nothing. stderr: {stderr}"
    );
}

#[test]
fn include_pattern_that_matches_is_not_reported_as_unmatched() {
    // Both patterns select a real file, but an exclude removes them, so the run is
    // empty without either include being at fault. Naming them here would send the
    // user after the wrong pattern.
    let temp_dir = tree(&[("docs/api.md", "# API\n")]);

    let output = check(
        temp_dir.path(),
        &[".", "--include", "docs/**/*.md,guide.md", "--exclude", "**/*.md"],
    );
    let stderr = stderr_of(&output);

    assert!(stderr.contains("2 by exclude patterns"), "stderr: {stderr}");
    assert!(
        !stderr.contains("matches no file"),
        "no include pattern is unmatched here. stderr: {stderr}"
    );
}

#[test]
fn a_non_markdown_include_target_counts_as_a_filtered_file() {
    // An include pattern is what makes a non-markdown file lintable at all, so a
    // run that includes one and then excludes it filtered a file out. Calling
    // that an empty directory sends the user looking for files that were never
    // the problem.
    let temp_dir = TempDir::new().unwrap();
    fs::write(temp_dir.path().join("notes.txt"), "not markdown\n").unwrap();

    // Control: with the include alone the file really is checked.
    let checked = check(temp_dir.path(), &[".", "--include", "*.txt"]);
    assert!(
        stdout_of(&checked).contains("notes.txt"),
        "the include should reach the file. stdout: {}",
        stdout_of(&checked)
    );

    let output = check(
        temp_dir.path(),
        &[
            ".",
            "--include",
            "*.txt",
            "--exclude",
            "*.txt",
            "--deny-config-warnings",
        ],
    );
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );
    assert!(stderr.contains("1 by exclude patterns"), "stderr: {stderr}");
    assert_eq!(
        output.status.code(),
        Some(2),
        "an all-filtered run is a configuration problem whatever the extension. stderr: {stderr}"
    );
}

#[test]
fn a_named_file_excluded_beside_a_directory_still_counts() {
    let temp_dir = tree(&[]);
    fs::create_dir(temp_dir.path().join("empty")).unwrap();

    // A directory argument in the same invocation must not lose the excluded
    // argument: the run is still one file short, not an absence of files.
    let mixed = check(
        temp_dir.path(),
        &["--exclude", "guide.md", "guide.md", "empty", "--deny-config-warnings"],
    );
    let stderr = stderr_of(&mixed);
    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );
    assert_eq!(mixed.status.code(), Some(2), "stderr: {stderr}");

    // The same file named *and* rediscovered by the walked directory is one
    // file, not two.
    let overlapping = check(
        temp_dir.path(),
        &["--exclude", "guide.md", "guide.md", ".", "--deny-config-warnings"],
    );
    let stderr = stderr_of(&overlapping);
    assert!(
        stderr.contains("1 file found was filtered out."),
        "a file counted twice would read as 2. stderr: {stderr}"
    );
}

#[test]
fn a_filtered_file_under_a_vendor_directory_still_counts() {
    // rumdl checks markdown wherever it lives, including `node_modules`, `target`
    // and `.git`. A diagnosis that skipped those would report a file it really
    // would have checked as one that never existed, which is the silent absence
    // this notice exists to replace.
    let temp_dir = TempDir::new().unwrap();
    for vendor_dir in ["node_modules/pkg", "target/debug", ".git/hooks"] {
        let path = temp_dir.path().join(vendor_dir);
        fs::create_dir_all(&path).unwrap();
        fs::write(path.join("readme.md"), "# Readme\n").unwrap();
    }

    // Control: the walk being explained really does reach all three.
    let checked = check(temp_dir.path(), &["."]);
    assert!(
        stdout_of(&checked).contains("3 files"),
        "the walk should reach every vendor directory. stdout: {}",
        stdout_of(&checked)
    );

    let output = check(
        temp_dir.path(),
        &[".", "--exclude", "**/*.md", "--deny-config-warnings"],
    );
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("No markdown files left to check: 3 files found were filtered out."),
        "stderr: {stderr}"
    );
    assert_eq!(output.status.code(), Some(2), "stderr: {stderr}");
}

#[test]
fn a_config_include_pinning_no_extension_does_not_invent_a_filtered_file() {
    // `docs/**` widens the walk but names no file type, so rumdl never checks
    // docs/notes.txt under any setting. Reporting it as filtered out would
    // invent a configuration problem and fail a CI run that is working.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/notes.txt"), "plain text\n").unwrap();
    fs::write(
        temp_dir.path().join(".rumdl.toml"),
        "[global]\ninclude = [\"docs/**\"]\n",
    )
    .unwrap();

    let output = check_configured(temp_dir.path(), &[".", "--deny-config-warnings"]);
    let stderr = stderr_of(&output);
    assert_eq!(
        stderr.trim(),
        "No markdown files found to check.",
        "the file was never lintable, so nothing was filtered out"
    );
    assert_eq!(output.status.code(), Some(0), "stderr: {stderr}");

    // Control: a pattern that does name the file type makes it lintable, and
    // then excluding it really is a file the configuration removed.
    fs::write(
        temp_dir.path().join(".rumdl.toml"),
        "[global]\ninclude = [\"docs/*.txt\"]\n",
    )
    .unwrap();
    let checked = check_configured(temp_dir.path(), &["."]);
    assert!(
        stdout_of(&checked).contains("notes.txt"),
        "the pinned pattern should reach the file. stdout: {}",
        stdout_of(&checked)
    );

    let excluded = check_configured(
        temp_dir.path(),
        &[".", "--exclude", "docs/*.txt", "--deny-config-warnings"],
    );
    let stderr = stderr_of(&excluded);
    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );
    assert_eq!(excluded.status.code(), Some(2), "stderr: {stderr}");
}

#[test]
fn a_directory_without_markdown_reports_plain_absence() {
    let temp_dir = TempDir::new().unwrap();
    fs::write(temp_dir.path().join("notes.txt"), "not markdown\n").unwrap();

    let output = check(temp_dir.path(), &["."]);
    let stderr = stderr_of(&output);

    // Nothing was filtered out, so nothing is blamed.
    assert_eq!(stderr.trim(), "No markdown files found to check.");
}

#[test]
fn the_notice_goes_to_stderr_and_never_to_stdout() {
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    let output = check(temp_dir.path(), &["."]);

    assert!(
        !stdout_of(&output).contains("No markdown files"),
        "stdout carries machine-readable output. stdout: {}",
        stdout_of(&output)
    );
    assert!(stderr_of(&output).contains("No markdown files"));
}

#[test]
fn quiet_keeps_the_notice_and_silent_suppresses_it() {
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    // -q suppresses summary lines, not diagnostics, and an empty run is a
    // diagnostic about the run itself.
    let quiet = check(temp_dir.path(), &[".", "-q"]);
    assert!(
        stderr_of(&quiet).contains("No markdown files left to check"),
        "stderr: {}",
        stderr_of(&quiet)
    );

    for flag in ["-s", "--silent"] {
        let silent = check(temp_dir.path(), &[".", flag]);
        assert_eq!(stderr_of(&silent), "", "{flag} should suppress the notice");
        assert_eq!(stdout_of(&silent), "", "{flag} should suppress all output");
    }
}

#[test]
fn an_empty_run_still_exits_zero_by_default() {
    for extra in [vec![(".gitignore", "*.md\n")], vec![]] {
        let temp_dir = tree(&extra);
        let output = check(temp_dir.path(), &["."]);
        assert_eq!(
            output.status.code(),
            Some(0),
            "checking nothing is not a violation. stderr: {}",
            stderr_of(&output)
        );
    }
}

#[test]
fn deny_config_warnings_separates_misconfiguration_from_absence() {
    // Every file filtered away points at the configuration, so an opted-in run fails.
    let filtered = tree(&[(".gitignore", "*.md\n")]);
    let output = check(filtered.path(), &[".", "--deny-config-warnings"]);
    assert_eq!(
        output.status.code(),
        Some(2),
        "an all-filtered run is a configuration problem. stderr: {}",
        stderr_of(&output)
    );

    // A directory that simply holds no markdown is not a configuration problem.
    let empty = TempDir::new().unwrap();
    fs::write(empty.path().join("notes.txt"), "not markdown\n").unwrap();
    let output = check(empty.path(), &[".", "--deny-config-warnings"]);
    assert_eq!(
        output.status.code(),
        Some(0),
        "an absence of markdown is not a configuration problem. stderr: {}",
        stderr_of(&output)
    );
}

#[test]
fn machine_readable_output_stays_a_valid_empty_document() {
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    // A human sentence on stdout, or nothing at all, leaves a consumer unable to
    // tell an empty run from a broken one.
    for format in ["json", "gitlab"] {
        let output = check(temp_dir.path(), &[".", "--output-format", format]);
        let stdout = stdout_of(&output);
        let parsed: serde_json::Value = serde_json::from_str(&stdout)
            .unwrap_or_else(|e| panic!("{format} output is not valid JSON ({e}): {stdout:?}"));
        assert_eq!(
            parsed.as_array().map(Vec::len),
            Some(0),
            "{format} output should be an empty array: {stdout:?}"
        );
    }

    let sarif = stdout_of(&check(temp_dir.path(), &[".", "--output-format", "sarif"]));
    let parsed: serde_json::Value =
        serde_json::from_str(&sarif).unwrap_or_else(|e| panic!("sarif output is not valid JSON ({e}): {sarif:?}"));
    assert!(parsed.get("runs").is_some(), "sarif output should be a full document");
}

#[test]
fn an_include_pattern_takes_the_blame_over_the_ignore_file_it_outranks() {
    // An include pattern outranks an ignore file, so a gitignored file the
    // pattern selects is still checked. That makes the ignore file the wrong
    // thing to blame when an include is active: the run stays empty with ignore
    // handling switched off, and the suggested remedy fixes nothing.
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    // Control: the include really does reach past the ignore file.
    let reached = check(temp_dir.path(), &[".", "--include", "guide.md"]);
    assert!(
        stdout_of(&reached).contains("1 file"),
        "an include should outrank the ignore file. stdout: {}",
        stdout_of(&reached)
    );

    let output = check(temp_dir.path(), &[".", "--include", "dcos/**/*.md"]);
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("1 by include patterns"),
        "the typo'd include is what keeps the run empty. stderr: {stderr}"
    );
    assert!(
        !stderr.contains("ignore files"),
        "--respect-gitignore=false would leave the run just as empty. stderr: {stderr}"
    );

    // Control: with no include pattern the ignore file really is the cause, and
    // the remedy it names works.
    let output = check(temp_dir.path(), &["."]);
    let stderr = stderr_of(&output);
    assert!(stderr.contains("1 by ignore files"), "stderr: {stderr}");
    let kept = check(temp_dir.path(), &[".", "--respect-gitignore=false"]);
    assert!(
        stdout_of(&kept).contains("1 file"),
        "the named remedy should check the file. stdout: {}",
        stdout_of(&kept)
    );
}

#[test]
fn the_notice_yields_the_stream_the_output_was_routed_to() {
    // --stderr sends diagnostics to stderr, so the notice has to move to stdout.
    // Sharing the stream would put a human sentence in front of the document and
    // leave an empty run unparseable, which is the failure this notice exists to
    // prevent.
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    for format in ["json", "gitlab"] {
        let output = check(temp_dir.path(), &[".", "--output-format", format, "--stderr"]);
        let stderr = stderr_of(&output);
        let parsed: serde_json::Value = serde_json::from_str(&stderr)
            .unwrap_or_else(|e| panic!("{format} output on stderr is not valid JSON ({e}): {stderr:?}"));
        assert_eq!(parsed.as_array().map(Vec::len), Some(0), "{format}: {stderr:?}");
        assert!(
            stdout_of(&output).contains("No markdown files left to check"),
            "the notice should move to the free stream. stdout: {}",
            stdout_of(&output)
        );
    }

    // Control: without --stderr the notice stays on stderr and stdout carries
    // the document.
    let output = check(temp_dir.path(), &[".", "--output-format", "json"]);
    assert!(
        stdout_of(&output).trim().starts_with('['),
        "stdout: {}",
        stdout_of(&output)
    );
    assert!(
        stderr_of(&output).contains("No markdown files left to check"),
        "stderr: {}",
        stderr_of(&output)
    );

    // Control: --silent still suppresses both.
    let quiet = check(
        temp_dir.path(),
        &[".", "--output-format", "json", "--stderr", "--silent"],
    );
    assert_eq!(stdout_of(&quiet), "", "stderr: {}", stderr_of(&quiet));
    assert_eq!(stderr_of(&quiet), "");
}

#[test]
fn overlapping_roots_count_a_file_once() {
    // `rumdl check . docs` hands the walker docs/a.md twice, and the run it
    // explains reduces that to one file. A tally that grew with the argument
    // list would contradict the walk it is describing.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/a.md"), "# A\n").unwrap();

    // Control: the walk itself counts the file once across both roots.
    let checked = check(temp_dir.path(), &[".", "docs"]);
    assert!(
        stdout_of(&checked).contains("1 file"),
        "the walk should count it once. stdout: {}",
        stdout_of(&checked)
    );

    let overlapping = check(temp_dir.path(), &["--exclude", "*.md", ".", "docs"]);
    let stderr = stderr_of(&overlapping);
    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );

    // Control: roots that do not overlap still count both files.
    fs::create_dir(temp_dir.path().join("spec")).unwrap();
    fs::write(temp_dir.path().join("spec/b.md"), "# B\n").unwrap();
    let disjoint = check(temp_dir.path(), &["--exclude", "*.md", "docs", "spec"]);
    let stderr = stderr_of(&disjoint);
    assert!(
        stderr.contains("No markdown files left to check: 2 files found were filtered out."),
        "stderr: {stderr}"
    );

    // The walk that runs without ignore files has to dedupe on the same terms.
    fs::write(temp_dir.path().join(".gitignore"), "*.md\n").unwrap();
    let ignored = check(temp_dir.path(), &[".", "docs"]);
    let stderr = stderr_of(&ignored);
    assert!(
        stderr.contains("No markdown files left to check: 2 files found were filtered out."),
        "docs/a.md is reached by both roots and spec/b.md by one. stderr: {stderr}"
    );
    assert!(stderr.contains("2 by ignore files"), "stderr: {stderr}");
}

#[test]
fn an_include_reaching_past_an_ignore_file_hands_the_blame_to_the_exclude() {
    // The include outranks the ignore file, so the walk really did reach this
    // file and only the exclude can have dropped it. Blaming the ignore file
    // here would name a knob whose remedy leaves the run just as empty.
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    let output = check(
        temp_dir.path(),
        &[".", "--include", "guide.md", "--exclude", "guide.md"],
    );
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("1 by exclude patterns"),
        "the exclude is what the walk applied last. stderr: {stderr}"
    );
    assert!(
        !stderr.contains("by ignore files"),
        "the include overrode the ignore file. stderr: {stderr}"
    );

    // The remedy has to end the emptiness on its own, which is what separates
    // this from naming the ignore file.
    let remedied = check(temp_dir.path(), &[".", "--include", "guide.md", "--no-exclude"]);
    assert!(
        stdout_of(&remedied).contains("No issues found in 1 file"),
        "stdout: {}",
        stdout_of(&remedied)
    );

    // Control: without an include the ignore file does hide the path, and then
    // it is what gets named however many other filters would also have matched.
    let no_include = check(temp_dir.path(), &[".", "--exclude", "guide.md"]);
    assert!(
        stderr_of(&no_include).contains("1 by ignore files"),
        "stderr: {}",
        stderr_of(&no_include)
    );
}

#[test]
fn an_include_cannot_reach_into_a_directory_the_ignore_file_pruned() {
    // An include outranks the ignore files for a file, but not for the directory
    // holding it: a directory they hid is pruned before the walk descends, so
    // nothing inside it was ever offered to the include. Handing the blame to the
    // exclude patterns here would name a knob the run does not even have set.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/guide.md"), "#  Guide\n").unwrap();
    fs::write(temp_dir.path().join(".gitignore"), "docs/\n").unwrap();

    let output = check(temp_dir.path(), &[".", "--include", "docs/**/*.md"]);
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("1 by ignore files"),
        "the pruned directory is the ignore file's doing. stderr: {stderr}"
    );
    assert!(
        !stderr.contains("by exclude patterns"),
        "no exclude pattern was involved. stderr: {stderr}"
    );

    // The remedy has to end the emptiness on its own.
    let remedied = check(
        temp_dir.path(),
        &[".", "--include", "docs/**/*.md", "--respect-gitignore=false"],
    );
    assert!(
        stdout_of(&remedied).contains("in 1 file"),
        "stdout: {}",
        stdout_of(&remedied)
    );

    // Control: the include still outranks an ignore file that names the file
    // itself, since the walk reaches the directory holding it.
    let file_pattern = TempDir::new().unwrap();
    fs::create_dir(file_pattern.path().join("docs")).unwrap();
    fs::write(file_pattern.path().join("docs/guide.md"), "#  Guide\n").unwrap();
    fs::write(file_pattern.path().join(".gitignore"), "*.md\n").unwrap();
    let rescued = check(file_pattern.path(), &[".", "--include", "docs/**/*.md"]);
    assert!(
        stdout_of(&rescued).contains("in 1 file"),
        "stdout: {}",
        stdout_of(&rescued)
    );
}

#[test]
fn a_capitalized_extension_is_checked_rather_than_declared_filtered() {
    // A walk that drops a file the linter would otherwise accept leaves the
    // notice with a file to report and no setting to blame for it, which reads
    // as a configuration problem the user cannot act on.
    let temp_dir = TempDir::new().unwrap();
    fs::write(temp_dir.path().join("README.MD"), "#  Shouting\n").unwrap();

    let output = check(temp_dir.path(), &[".", "--deny-config-warnings"]);
    let stderr = stderr_of(&output);
    assert!(
        !stderr.contains("filtered out"),
        "nothing filtered this file. stderr: {stderr}"
    );
    assert_eq!(output.status.code(), Some(1), "stderr: {stderr}");
    assert!(
        stdout_of(&output).contains("README.MD"),
        "the file should be linted. stdout: {}",
        stdout_of(&output)
    );

    // Control: naming the file already linted it, so a walk that skips it is
    // rumdl disagreeing with itself rather than a rule about capitalization.
    let named = check(temp_dir.path(), &["README.MD"]);
    assert_eq!(named.status.code(), Some(1), "stderr: {}", stderr_of(&named));

    // Control: widening case does not widen the extension set.
    let other = TempDir::new().unwrap();
    fs::write(other.path().join("README.MDX.txt"), "#  Shouting\n").unwrap();
    assert!(
        stderr_of(&check(other.path(), &["."])).contains("No markdown files found to check."),
        "stderr: {}",
        stderr_of(&check(other.path(), &["."]))
    );
}

#[test]
fn a_hidden_file_is_not_blamed_on_a_filter_that_never_saw_it() {
    // Ignore files hide a path from the walk, so its exclude patterns never get
    // to judge it. Reporting an exclude here would answer for a matcher that
    // never ran, and a run whose files are both ignored and excluded needs both
    // knobs undone whichever one is named first.
    let temp_dir = tree(&[(".gitignore", "*.md\n")]);

    let output = check(temp_dir.path(), &[".", "--exclude", "*.md"]);
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("1 by ignore files"),
        "the ignore file is what the walk actually applied. stderr: {stderr}"
    );
    assert!(
        !stderr.contains("by exclude patterns"),
        "the exclude never saw the file. stderr: {stderr}"
    );

    // Control: once the file is visible the exclude really does judge it, and
    // then it is what gets reported.
    let visible = check(
        temp_dir.path(),
        &[".", "--exclude", "*.md", "--respect-gitignore=false"],
    );
    assert!(
        stderr_of(&visible).contains("1 by exclude patterns"),
        "stderr: {}",
        stderr_of(&visible)
    );

    // Control: an include is not a guess. A matching one overrides the ignore
    // file, so one that selects nothing is why the ignore file applied.
    let missed = check(temp_dir.path(), &[".", "--include", "dcos/**/*.md"]);
    assert!(
        stderr_of(&missed).contains("1 by include patterns"),
        "stderr: {}",
        stderr_of(&missed)
    );
}

#[test]
fn naming_the_excluded_file_does_not_change_the_diagnosis() {
    // Once a candidate has an established cause the diagnosis stops, because an
    // empty run needs one setting to undo rather than a census of everything
    // the ignore files also hid. Whether that candidate was named on the
    // command line or found by the walk is an accident of invocation, so the
    // two forms have to agree on the same tree.
    let temp_dir = tree(&[("ignored.md", "# Ignored\n"), (".gitignore", "ignored.md\n")]);

    let named = check(temp_dir.path(), &["--exclude", "guide.md", "guide.md", "."]);
    let walked = check(temp_dir.path(), &["--exclude", "guide.md", "."]);
    assert_eq!(
        stderr_of(&named),
        stderr_of(&walked),
        "naming the excluded file should not change what the run reports"
    );
    assert!(
        stderr_of(&named).contains("1 by exclude patterns"),
        "stderr: {}",
        stderr_of(&named)
    );

    // Control: the cause reported is the one that ends the empty run.
    let undone = check(
        temp_dir.path(),
        &["--exclude", "guide.md", "guide.md", ".", "--no-exclude"],
    );
    assert!(
        stdout_of(&undone).contains("1 file"),
        "--no-exclude should end the emptiness. stdout: {}",
        stdout_of(&undone)
    );
}

#[test]
fn naming_one_excluded_file_repeatedly_still_counts_one_file() {
    // Duplicate arguments, and spellings that resolve to the same path, describe
    // one file. A count that grows with the argument list overstates how much
    // the configuration removed.
    let temp_dir = tree(&[]);

    let output = check(
        temp_dir.path(),
        &["--exclude", "guide.md", "guide.md", "guide.md", "./guide.md"],
    );
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("No markdown files left to check: 1 file found was filtered out."),
        "stderr: {stderr}"
    );

    // Control: distinct files are still counted separately.
    fs::write(temp_dir.path().join("notes.md"), "# Notes\n").unwrap();
    let output = check(temp_dir.path(), &["--exclude", "*.md", "guide.md", "notes.md"]);
    let stderr = stderr_of(&output);
    assert!(
        stderr.contains("No markdown files left to check: 2 files found were filtered out."),
        "stderr: {stderr}"
    );
}

#[test]
fn a_markdownlintignore_swallowing_every_file_is_attributed_to_it() {
    // `.markdownlintignore` applies whatever --respect-gitignore says, so a run it
    // emptied was filtered, not empty, and the flag is not the way out. Calling it
    // plain absence hides the file that did it; pointing at the flag names a
    // remedy that changes nothing.
    let temp_dir = tree(&[(".markdownlintignore", "*.md\n")]);

    for args in [&["."][..], &[".", "--respect-gitignore=false"][..]] {
        let stderr = stderr_of(&check(temp_dir.path(), args));
        assert!(
            stderr.contains("No markdown files left to check: 1 file found was filtered out."),
            "`check {}` stderr: {stderr}",
            args.join(" ")
        );
        assert!(
            stderr.contains("1 by .markdownlintignore;"),
            "`check {}` should name the file that hid it. stderr: {stderr}",
            args.join(" ")
        );
        assert!(
            !stderr.contains("--respect-gitignore=false"),
            "`check {}` must not offer a flag that does not undo it. stderr: {stderr}",
            args.join(" ")
        );
    }

    // The remedy it offers has to end the emptiness on its own.
    let named = check(temp_dir.path(), &["guide.md"]);
    assert!(
        stdout_of(&named).contains("in 1 file"),
        "a named file bypasses .markdownlintignore. stdout: {}",
        stdout_of(&named)
    );
    // Control: the flag alone does not bring the file back, which is why the
    // notice must not suggest it.
    let flagged = check(temp_dir.path(), &[".", "--respect-gitignore=false"]);
    assert!(
        !stdout_of(&flagged).contains("in 1 file"),
        "stdout: {}",
        stdout_of(&flagged)
    );
}

#[test]
fn the_flag_line_names_only_the_ignore_files_the_flag_controls() {
    for ignore_file in [".gitignore", ".ignore"] {
        let temp_dir = tree(&[(ignore_file, "*.md\n")]);
        let stderr = stderr_of(&check(temp_dir.path(), &["."]));
        assert!(
            stderr.contains("1 by ignore files (") && stderr.contains("--respect-gitignore=false"),
            "{ignore_file} is undone by the flag. stderr: {stderr}"
        );
        assert!(
            !stderr.contains(".markdownlintignore"),
            "{ignore_file} alone emptied the run, and the flag does not cover .markdownlintignore. stderr: {stderr}"
        );

        let remedied = check(temp_dir.path(), &[".", "--respect-gitignore=false"]);
        assert!(
            stdout_of(&remedied).contains("in 1 file"),
            "{ignore_file}: stdout: {}",
            stdout_of(&remedied)
        );
    }
}

#[test]
fn each_ignore_source_is_credited_with_its_own_files() {
    let temp_dir = TempDir::new().unwrap();
    for name in ["git.md", "lint.md", "both.md"] {
        fs::write(temp_dir.path().join(name), "# Title\n").unwrap();
    }
    fs::write(temp_dir.path().join(".gitignore"), "git.md\nboth.md\n").unwrap();
    fs::write(temp_dir.path().join(".markdownlintignore"), "lint.md\nboth.md\n").unwrap();

    let stderr = stderr_of(&check(temp_dir.path(), &["."]));
    assert!(
        stderr.contains("No markdown files left to check: 3 files found were filtered out."),
        "stderr: {stderr}"
    );
    assert!(stderr.contains("\n  1 by ignore files ("), "stderr: {stderr}");
    assert!(stderr.contains("\n  1 by .markdownlintignore;"), "stderr: {stderr}");
    // A file both hide comes back only when both are undone, so neither single
    // line may claim it.
    assert!(
        stderr.contains("\n  1 by both .markdownlintignore and ignore files"),
        "stderr: {stderr}"
    );
}

#[test]
fn an_include_that_outranks_one_ignore_source_leaves_the_other_to_blame() {
    // An include naming a file outranks an ignore file listing that file, but
    // cannot reach into a directory an ignore file pruned. So when one source
    // prunes the directory and the other lists the file, undoing the pruning
    // source alone brings the file back, and the notice must say only that.
    for (pruner, lister, expected) in [
        (".gitignore", ".markdownlintignore", "\n  1 by ignore files ("),
        (".markdownlintignore", ".gitignore", "\n  1 by .markdownlintignore;"),
    ] {
        let temp_dir = TempDir::new().unwrap();
        fs::create_dir(temp_dir.path().join("docs")).unwrap();
        fs::write(temp_dir.path().join("docs/guide.md"), "# Guide\n").unwrap();
        fs::write(temp_dir.path().join(pruner), "docs/\n").unwrap();
        fs::write(temp_dir.path().join(lister), "guide.md\n").unwrap();

        let stderr = stderr_of(&check(temp_dir.path(), &[".", "--include", "docs/guide.md"]));
        assert!(stderr.contains(expected), "{pruner} prunes docs/. stderr: {stderr}");
        assert!(!stderr.contains(" by both "), "{pruner} prunes docs/. stderr: {stderr}");
    }

    // The remedy the notice gives for the pruning `.gitignore` really is enough.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/guide.md"), "# Guide\n").unwrap();
    fs::write(temp_dir.path().join(".gitignore"), "docs/\n").unwrap();
    fs::write(temp_dir.path().join(".markdownlintignore"), "guide.md\n").unwrap();
    let output = check(
        temp_dir.path(),
        &[".", "--include", "docs/guide.md", "--respect-gitignore=false"],
    );
    assert!(
        stdout_of(&output).contains("1 file"),
        "the flag alone should restore the file. stdout: {} stderr: {}",
        stdout_of(&output),
        stderr_of(&output)
    );
}

#[test]
fn an_exclude_matching_an_ignored_file_does_not_blame_the_other_source() {
    // An exclude never sees a path an ignore file already hid, so it has no say
    // in which ignore source did the hiding. A `.markdownlintignore` that lists
    // nothing relevant must not be named just because it exists.
    let temp_dir = tree(&[(".gitignore", "guide.md\n"), (".markdownlintignore", "other.md\n")]);

    let stderr = stderr_of(&check(temp_dir.path(), &[".", "--exclude", "guide.md"]));
    assert!(stderr.contains("\n  1 by ignore files ("), "stderr: {stderr}");
    assert!(!stderr.contains(".markdownlintignore"), "stderr: {stderr}");
}

#[test]
fn a_markdownlintignore_pruned_directory_is_attributed_to_it() {
    // A directory the file hides is never descended into, so the files inside are
    // found only by a walk that does not read it.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/guide.md"), "# Guide\n").unwrap();
    fs::write(temp_dir.path().join(".markdownlintignore"), "docs/\n").unwrap();

    let stderr = stderr_of(&check(temp_dir.path(), &["."]));
    assert!(stderr.contains("1 by .markdownlintignore;"), "stderr: {stderr}");
    assert!(!stderr.contains("by ignore files"), "stderr: {stderr}");
}

#[test]
fn a_markdownlintignore_above_the_walked_directory_is_attributed_to_it() {
    // The walk reads ignore files in the directories above the one it was given,
    // and never yields them as entries, so they have to be looked for there.
    let temp_dir = TempDir::new().unwrap();
    fs::create_dir(temp_dir.path().join("docs")).unwrap();
    fs::write(temp_dir.path().join("docs/guide.md"), "# Guide\n").unwrap();
    fs::write(temp_dir.path().join(".markdownlintignore"), "guide.md\n").unwrap();

    for args in [&["docs"][..], &["docs", "--respect-gitignore=false"][..]] {
        let stderr = stderr_of(&check(temp_dir.path(), args));
        assert!(
            stderr.contains("1 by .markdownlintignore;"),
            "`check {}` stderr: {stderr}",
            args.join(" ")
        );
        assert!(
            !stderr.contains("by ignore files"),
            "`check {}` stderr: {stderr}",
            args.join(" ")
        );
    }
}