rumdl 0.3.0

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
//! Path-aware document analysis shared by CLI, stdin, LSP, and virtual adapters.

use std::borrow::Cow;
use std::path::{Path, PathBuf};

use crate::config::{Config, MarkdownFlavor};
use crate::fix_coordinator::{FixCoordinator, FixResult};
use crate::rule::{LintError, LintWarning, Rule};
use crate::utils::{LineEnding, NormalizedLineEndingMap, normalize_line_ending};
use crate::workspace_index::FileIndex;

/// The deterministic lint and index result for one document.
/// Fix byte ranges address the supplied text, including any leading BOM.
pub struct DocumentAnalysis {
    pub warnings: Vec<LintWarning>,
    pub file_index: FileIndex,
}

/// Diagnostics and index for the original source, with its prepared rewrite.
/// Keeping the rewrite separate lets adapters finish validation before writing.
pub struct DocumentFormatResult {
    pub warnings: crate::LintResult,
    pub file_index: FileIndex,
    pub fixes: Option<Result<(String, FixResult), String>>,
    /// Findings on the fixed source, when the fix loop reached a stable changed
    /// document and could share its last context. Adapters must discard these
    /// after any further edit and lint that text again.
    pub remaining_warnings: Option<crate::LintResult>,
}

impl DocumentFormatResult {
    pub(crate) fn without_fixes(warnings: crate::LintResult, file_index: FileIndex) -> Self {
        Self {
            warnings,
            file_index,
            fixes: None,
            remaining_warnings: None,
        }
    }
}

/// A document run with path roles made explicit.
///
/// `config_path` is a logical identity used by per-file configuration. A
/// separate `source_file` is exposed to rules that may access the filesystem.
/// Native adapters usually set both; browser and other virtual adapters set only
/// `config_path`.
pub struct DocumentRun<'a> {
    content: &'a str,
    conflict_scan: Option<&'a crate::merge_conflict::ConflictScan<'a>>,
    rules: &'a [Box<dyn Rule>],
    config: &'a Config,
    config_path: Option<&'a Path>,
    source_file: Option<&'a Path>,
    link_target_policy: Option<&'a crate::lint_context::LinkTargetPolicy>,
    invalid_utf8: Option<&'a [crate::encoding::InvalidSeq]>,
    verbose: bool,
}

impl<'a> DocumentRun<'a> {
    pub fn new(content: &'a str, rules: &'a [Box<dyn Rule>], config: &'a Config) -> Self {
        Self {
            content,
            conflict_scan: None,
            rules,
            config,
            config_path: None,
            source_file: None,
            link_target_policy: None,
            invalid_utf8: None,
            verbose: false,
        }
    }

    /// Reuse detection performed before normalization or external tool work.
    /// The scan supplies its own immutable content, so facts cannot be attached
    /// to a different document or retained after an edit.
    pub fn from_scanned(
        scan: &'a crate::merge_conflict::ConflictScan<'a>,
        rules: &'a [Box<dyn Rule>],
        config: &'a Config,
    ) -> Self {
        Self {
            conflict_scan: Some(scan),
            ..Self::new(scan.content(), rules, config)
        }
    }

    /// Set a real file path for both per-file configuration and rule filesystem access.
    pub fn file_path(mut self, path: &'a Path) -> Self {
        self.config_path = Some(path);
        self.source_file = Some(path);
        self
    }

    /// Set a logical path used only for per-file configuration matching.
    pub fn config_path(mut self, path: Option<&'a Path>) -> Self {
        self.config_path = path;
        self
    }

    /// Set the filesystem path visible to rules independently of configuration matching.
    pub fn source_file(mut self, path: Option<&'a Path>) -> Self {
        self.source_file = path;
        self
    }

    /// Supply a run-scoped view of virtual document paths for link validation.
    pub fn link_target_policy(mut self, policy: &'a crate::lint_context::LinkTargetPolicy) -> Self {
        self.link_target_policy = Some(policy);
        self
    }

    /// Mark the content as lossily decoded from bytes holding these invalid
    /// UTF-8 sequences. MD094 reports them, and no finding carries a fix.
    pub fn invalid_utf8(mut self, invalid: Option<&'a [crate::encoding::InvalidSeq]>) -> Self {
        self.invalid_utf8 = invalid;
        self
    }

    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    pub fn flavor(&self) -> MarkdownFlavor {
        self.config_path.map_or_else(
            || self.config.markdown_flavor(),
            |path| self.config.get_flavor_for_file(path),
        )
    }

    pub fn analyze(&self) -> Result<DocumentAnalysis, LintError> {
        let (warnings, file_index) = self.analyze_raw();
        warnings.map(|warnings| DocumentAnalysis { warnings, file_index })
    }

    pub fn analyze_raw(&self) -> (Result<Vec<LintWarning>, LintError>, FileIndex) {
        if let Some(scan) = self.conflict_scan {
            let result = crate::analyze_scanned_document(
                scan,
                self.rules,
                self.verbose,
                self.flavor(),
                self.paths(),
                Some(self.config),
                None,
            );
            return (result.warnings, result.file_index);
        }
        crate::lint_and_index_with_paths(
            self.content,
            self.rules,
            self.verbose,
            self.flavor(),
            self.paths(),
            Some(self.config),
        )
    }

    /// Analyze and prepare a fix while the first context and rule checks are
    /// still available. Every rewrite invalidates that analysis; subsequent
    /// iterations build a fresh context as they do in `fix`.
    pub fn analyze_and_fix(&self, max_iterations: usize) -> DocumentFormatResult {
        // The fix engine works on LF input. Preserve the separate operations'
        // diagnostic offsets and line-ending restoration for other input.
        // Virtual link policies also belong to analysis, while `fix` uses the
        // filesystem context; keep that existing distinction for these runs.
        if self.content.contains('\r') || self.link_target_policy.is_some() {
            let (warnings, file_index) = self.analyze_raw();
            let fixes = self.invalid_utf8.is_none() && warnings.as_ref().is_ok_and(|warnings| !warnings.is_empty());
            let fixes = fixes.then(|| self.fix(max_iterations));
            return DocumentFormatResult {
                warnings,
                file_index,
                fixes,
                remaining_warnings: None,
            };
        }
        let scan;
        let scan = match self.conflict_scan {
            Some(scan) => scan,
            None => {
                scan = crate::merge_conflict::ConflictScan::new(self.content);
                &scan
            }
        };
        crate::analyze_scanned_document(
            scan,
            self.rules,
            self.verbose,
            self.flavor(),
            self.paths(),
            Some(self.config),
            Some(max_iterations),
        )
    }

    /// Apply every fix and return the document in its own line-ending
    /// convention.
    ///
    /// Rules fix LF text: a `fix()` that rebuilds the document joins its lines
    /// with `\n`, and an inserted line ending is `\n`. The CLI normalises a file
    /// to LF before it gets here and restores the endings on write; the same
    /// happens here for every caller (LSP, wasm), so the document comes back
    /// with its line endings exactly as `rumdl fmt` writes it (see
    /// `NormalizedLineEndingMap::restore_fixed`). A document nothing changed
    /// comes back byte-identical.
    pub fn fix(&self, max_iterations: usize) -> Result<(String, FixResult), String> {
        self.fix_with_coordinator(max_iterations, &FixCoordinator::new())
    }

    /// Formatter-only execution with the same bytes, iterations, cycle report
    /// and fixed rule names as `fix`. Only `context_creations` may differ.
    /// Analysis consumers retain `fix`.
    pub fn fix_for_formatter(&self, max_iterations: usize) -> Result<(String, FixResult), String> {
        self.fix_with_coordinator(max_iterations, &FixCoordinator::for_formatter())
    }

    fn fix_with_coordinator(
        &self,
        max_iterations: usize,
        coordinator: &FixCoordinator,
    ) -> Result<(String, FixResult), String> {
        let normalized = normalize_line_ending(self.content, LineEnding::Lf);
        let mut content = normalized.to_string();
        let result = coordinator.apply_fixes_iterative_with_paths(
            self.rules,
            &[],
            &mut content,
            self.config,
            max_iterations,
            self.paths(),
        )?;
        if content == *normalized {
            return Ok((self.content.to_string(), result));
        }
        let restored = match NormalizedLineEndingMap::new(self.content).restore_fixed(&normalized, &content) {
            Cow::Borrowed(_) => content,
            Cow::Owned(restored) => restored,
        };
        Ok((restored, result))
    }

    pub fn config_path_buf(&self) -> Option<PathBuf> {
        self.config_path.map(Path::to_path_buf)
    }

    fn paths(&self) -> crate::DocumentPaths<'a> {
        crate::DocumentPaths {
            config_path: self.config_path,
            source_file: self.source_file,
            link_target_policy: self.link_target_policy,
            invalid_utf8: self.invalid_utf8,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::any::Any;

    use indexmap::IndexMap;

    use super::*;
    use crate::lint_context::LintContext;
    use crate::rule::{LintResult, Severity};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[derive(Clone)]
    struct CountingRewrite {
        name: &'static str,
        from: &'static str,
        to: &'static str,
        checks: Arc<AtomicUsize>,
        category: crate::rule::RuleCategory,
    }

    impl Rule for CountingRewrite {
        fn name(&self) -> &'static str {
            self.name
        }
        fn description(&self) -> &'static str {
            "Count source-dependent checks"
        }
        fn check(&self, ctx: &LintContext) -> LintResult {
            self.checks.fetch_add(1, Ordering::Relaxed);
            Ok(ctx
                .content
                .find(self.from)
                .map(|offset| {
                    let (line, column) = ctx.offset_to_line_col(offset);
                    LintWarning {
                        rule_name: Some(self.name.to_string()),
                        message: self.description().to_string(),
                        line,
                        column,
                        end_line: line,
                        end_column: column + self.from.len(),
                        severity: Severity::Warning,
                        fix: None,
                    }
                })
                .into_iter()
                .collect())
        }
        fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
            Ok(ctx.content.replace(self.from, self.to))
        }
        fn as_any(&self) -> &dyn Any {
            self
        }
        fn category(&self) -> crate::rule::RuleCategory {
            self.category
        }
    }

    fn counting_rule(name: &'static str, from: &'static str, to: &'static str) -> (Box<dyn Rule>, Arc<AtomicUsize>) {
        let checks = Arc::new(AtomicUsize::new(0));
        (
            Box::new(CountingRewrite {
                name,
                from,
                to,
                checks: checks.clone(),
                category: crate::rule::RuleCategory::Other,
            }),
            checks,
        )
    }

    #[test]
    fn combined_run_reuses_the_initial_context_and_checks() {
        let config = Config::default();
        let (rule, checks) = counting_rule("MD009", "text", "text");
        let rules = vec![rule];
        let result = DocumentRun::new("text\n", &rules, &config).analyze_and_fix(100);
        assert_eq!(result.warnings.unwrap().len(), 1);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "text\n");
        assert_eq!(checks.load(Ordering::Relaxed), 1);
        assert_eq!(fixed.context_creations, 0);
        assert!(fixed.converged);
    }

    #[test]
    fn combined_run_invalidates_all_checks_after_a_rewrite() {
        let config = Config::default();
        let (first, first_checks) = counting_rule("MD009", "bad", "middle");
        let (second, second_checks) = counting_rule("MD011", "middle", "good");
        let rules = vec![first, second];
        let result = DocumentRun::new("bad\n", &rules, &config).analyze_and_fix(100);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "good\n");
        assert_eq!(first_checks.load(Ordering::Relaxed), 2);
        assert_eq!(second_checks.load(Ordering::Relaxed), 3);
        assert_eq!(fixed.context_creations, 2);
        assert_eq!(fixed.rules_fixed, 2);
        assert!(fixed.converged);
    }

    #[test]
    fn stable_analysis_reuses_checks_and_still_runs_unfixable_rules() {
        let mut config = Config::default();
        config.global.unfixable.push("MD041".into());
        let (rewrite, rewrite_checks) = counting_rule("MD009", "bad", "good");
        let (unchanged, unchanged_checks) = counting_rule("MD011", "stable", "stable");
        let (unfixable, unfixable_checks) = counting_rule("MD041", "other", "other");
        let rules = vec![rewrite, unchanged, unfixable];
        let result = DocumentRun::new("bad stable other\n", &rules, &config).analyze_and_fix(100);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "good stable other\n");
        assert!(fixed.converged);
        // Both the fix loop and reporting need the final checks, but each
        // source version is checked only once. Warning-only rules still run.
        assert_eq!(rewrite_checks.load(Ordering::Relaxed), 2);
        assert_eq!(unchanged_checks.load(Ordering::Relaxed), 2);
        assert_eq!(unfixable_checks.load(Ordering::Relaxed), 2);
        let remaining = result.remaining_warnings.unwrap().unwrap();
        assert_eq!(remaining.len(), 2);
        assert_eq!(
            format!("{remaining:?}"),
            format!(
                "{:?}",
                DocumentRun::new(&text, &rules, &config).analyze().unwrap().warnings
            ),
        );
    }

    #[test]
    fn remaining_bom_warnings_match_a_fresh_public_analysis() {
        let mut config = Config::default();
        config.global.unfixable.push("MD018".into());
        let rules: Vec<_> = crate::rules::all_rules(&config)
            .into_iter()
            .filter(|r| r.name() == "MD009" || r.name() == "MD018")
            .collect();
        let result = DocumentRun::new("\u{feff}#Title\n\ntext   \n", &rules, &config).analyze_and_fix(100);
        let (text, _) = result.fixes.expect("prepared fix").unwrap();
        assert_eq!(text, "\u{feff}#Title\n\ntext\n");
        let remaining = result.remaining_warnings.expect("final analysis was reused").unwrap();
        assert_eq!(remaining.len(), 1);
        assert!(remaining[0].fix.is_some());
        assert_eq!(remaining[0].fix.as_ref().unwrap().range, 3..9);
        let fresh = DocumentRun::new(&text, &rules, &config).analyze().unwrap().warnings;
        assert_eq!(remaining, fresh);
    }

    #[test]
    fn stable_analysis_preserves_suppression_observers_and_severity() {
        let mut config = Config::default();
        config.rules.insert(
            "MD087".into(),
            crate::config::RuleConfig {
                severity: Some(Severity::Error),
                ..Default::default()
            },
        );
        let (rewrite, _) = counting_rule("MD009", "bad", "good");
        let (suppressed, _) = counting_rule("MD011", "stable", "stable");
        let rules: Vec<Box<dyn Rule>> = vec![rewrite, suppressed, Box::new(crate::rules::MD087UnusedDisableComment)];
        let source =
            "<!-- rumdl-disable-next-line MD011 -->\nstable\n<!-- rumdl-disable-next-line MD009 -->\nquiet\nbad\n";
        let result = DocumentRun::new(source, &rules, &config).analyze_and_fix(100);
        let (text, _) = result.fixes.unwrap().unwrap();
        let remaining = result.remaining_warnings.unwrap().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].rule_name.as_deref(), Some("MD087"));
        assert_eq!(remaining[0].line, 3);
        assert_eq!(remaining[0].severity, Severity::Error);
        assert_eq!(
            format!("{remaining:?}"),
            format!(
                "{:?}",
                DocumentRun::new(&text, &rules, &config).analyze().unwrap().warnings
            ),
        );
    }

    #[test]
    fn stable_analysis_distinguishes_zero_sized_rule_instances() {
        let config = Config::default();
        let rules: Vec<Box<dyn Rule>> = vec![
            Box::new(ContextProbe),
            crate::rules::create_rule_by_name("MD018", &config).unwrap(),
        ];
        let result = DocumentRun::new("#Title\n", &rules, &config).analyze_and_fix(100);
        let (text, _) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "# Title\n");
        let remaining = result.remaining_warnings.unwrap().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].rule_name.as_deref(), Some("TEST001"));
        assert_eq!(
            format!("{remaining:?}"),
            format!(
                "{:?}",
                DocumentRun::new(&text, &rules, &config).analyze().unwrap().warnings
            ),
        );
    }

    #[test]
    fn unfinished_fix_runs_leave_final_analysis_to_the_caller() {
        let config = Config::default();
        let (first, _) = counting_rule("MD009", "bad", "middle");
        let (second, _) = counting_rule("MD011", "middle", "good");
        let rules = vec![first, second];
        let result = DocumentRun::new("bad\n", &rules, &config).analyze_and_fix(1);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "middle\n");
        assert!(!fixed.converged);
        assert!(result.remaining_warnings.is_none());
    }

    #[test]
    fn combined_run_keeps_inline_disabled_findings_out_of_fixes() {
        let config = Config::default();
        let (disabled, checks) = counting_rule("MD009", "bad", "good");
        let (warning_only, _) = counting_rule("MD011", "other", "other");
        let rules = vec![disabled, warning_only];
        let source = "<!-- rumdl-disable MD009 -->\nbad other\n";
        let result = DocumentRun::new(source, &rules, &config).analyze_and_fix(100);
        assert_eq!(result.warnings.unwrap().len(), 1);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, source);
        assert_eq!(checks.load(Ordering::Relaxed), 1);
        assert_eq!(fixed.rules_fixed, 0);
    }

    #[test]
    fn combined_run_checks_rules_that_the_lint_prefilter_did_not_run() {
        let config = Config::default();
        let checks = Arc::new(AtomicUsize::new(0));
        let skipped: Box<dyn Rule> = Box::new(CountingRewrite {
            name: "MD001",
            from: "plain",
            to: "changed",
            checks: checks.clone(),
            category: crate::rule::RuleCategory::Heading,
        });
        let (reported, _) = counting_rule("MD009", "warning", "warning");
        let rules = vec![skipped, reported];
        let result = DocumentRun::new("plain warning\n", &rules, &config).analyze_and_fix(100);
        assert_eq!(result.warnings.unwrap().len(), 1);
        let (text, fixed) = result.fixes.unwrap().unwrap();
        assert_eq!(text, "changed warning\n");
        assert_eq!(checks.load(Ordering::Relaxed), 2);
        assert!(fixed.converged);
    }

    #[test]
    fn combined_run_never_prepares_fixes_for_lossy_input() {
        let config = Config::default();
        let (rule, checks) = counting_rule("MD009", "bad", "good");
        let rules = vec![rule];
        let result = DocumentRun::new("bad\n", &rules, &config)
            .invalid_utf8(Some(&[]))
            .analyze_and_fix(100);
        assert!(!result.warnings.unwrap().is_empty());
        assert!(result.fixes.is_none());
        assert_eq!(checks.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn combined_run_matches_separate_analysis_and_fixing() {
        for flavor in crate::format_proof::ALL_FLAVORS {
            let mut config = Config::default();
            config.global.flavor = flavor;
            let rules = crate::rules::all_rules(&config);
            for source in [
                "#Title\n\ntext  \n",
                "\u{feff}#Title\n\ntext  \n",
                "# Title\r\n\r\ntext  \r\n",
                "<!-- rumdl-disable MD018 -->\n#Title\n",
                "<!-- rumdl-configure-file {\"MD013\":{\"line-length\":12}} -->\n\nA rather long line that can be wrapped.\n",
                "- item\n\n```\ncode\n```\n",
            ] {
                let run = DocumentRun::new(source, &rules, &config);
                let original = run.analyze().unwrap();
                let combined = run.analyze_and_fix(100);
                assert_eq!(
                    format!("{:?}", combined.warnings.unwrap()),
                    format!("{:?}", original.warnings)
                );
                if let Some(fixes) = combined.fixes {
                    let text = fixes.unwrap().0;
                    assert_eq!(text, run.fix(100).unwrap().0, "{flavor}: {source:?}");
                    if let Some(remaining) = combined.remaining_warnings {
                        assert_eq!(
                            format!("{:?}", remaining.unwrap()),
                            format!(
                                "{:?}",
                                DocumentRun::new(&text, &rules, &config).analyze().unwrap().warnings
                            ),
                            "{flavor}: {source:?}",
                        );
                    }
                } else {
                    assert_eq!(run.fix(100).unwrap().0, source, "{flavor}: {source:?}");
                }
            }
        }
    }

    #[derive(Clone)]
    struct ContextProbe;

    impl Rule for ContextProbe {
        fn name(&self) -> &'static str {
            "TEST001"
        }

        fn description(&self) -> &'static str {
            "Probe document context"
        }

        fn check(&self, ctx: &LintContext) -> LintResult {
            let message = format!("flavor={};source={}", ctx.flavor, ctx.source_file().is_some());
            Ok(vec![LintWarning {
                message,
                line: 1,
                column: 1,
                end_line: 1,
                end_column: 1,
                severity: Severity::Warning,
                fix: None,
                rule_name: Some(self.name().to_string()),
            }])
        }

        fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
            Ok(ctx.content.to_string())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[test]
    fn logical_path_selects_flavor_without_exposing_a_filesystem_path() {
        let mut config = Config::default();
        config.per_file_flavor = IndexMap::from([("docs/**".to_string(), MarkdownFlavor::MkDocs)]);
        let rules: Vec<Box<dyn Rule>> = vec![Box::new(ContextProbe)];
        let path = Path::new("docs/page.md");

        let analysis = DocumentRun::new("text", &rules, &config)
            .config_path(Some(path))
            .analyze()
            .unwrap();

        assert_eq!(analysis.warnings[0].message, "flavor=mkdocs;source=false");
    }

    #[test]
    fn native_file_path_selects_flavor_and_reaches_rules() {
        let mut config = Config::default();
        config.per_file_flavor = IndexMap::from([("docs/**".to_string(), MarkdownFlavor::MkDocs)]);
        let rules: Vec<Box<dyn Rule>> = vec![Box::new(ContextProbe)];
        let path = Path::new("docs/page.md");

        let analysis = DocumentRun::new("text", &rules, &config)
            .file_path(path)
            .analyze()
            .unwrap();

        assert_eq!(analysis.warnings[0].message, "flavor=mkdocs;source=true");
    }
}