rumdl 0.1.78

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use std::fs;
use std::process::Command;
use tempfile::tempdir;

#[test]
fn test_config_file_command_with_explicit_config() {
    let temp_dir = tempdir().unwrap();
    let config_path = temp_dir.path().join("test.toml");
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a test config file
    let config_content = r#"
[global]
disable = ["MD013"]

[MD004]
style = "asterisk"
"#;
    fs::write(&config_path, config_content).unwrap();

    // Run the config file command with explicit config
    let output = Command::new(rumdl_exe)
        .args(["config", "file", "--config"])
        .arg(&config_path)
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let absolute_path = fs::canonicalize(&config_path).unwrap();
    assert_eq!(stdout.trim(), absolute_path.to_string_lossy());
}

#[test]
fn test_config_file_command_with_no_config() {
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    let output = Command::new(rumdl_exe)
        .args(["config", "file", "--no-config"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert_eq!(
        stdout.trim(),
        "No configuration file loaded (--no-config/--isolated specified)"
    );
}

#[test]
fn test_config_file_command_with_isolated() {
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    let output = Command::new(rumdl_exe)
        .args(["config", "file", "--isolated"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert_eq!(
        stdout.trim(),
        "No configuration file loaded (--no-config/--isolated specified)"
    );
}

#[test]
fn test_config_file_command_with_nonexistent_config() {
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    let output = Command::new(rumdl_exe)
        .args(["config", "file", "--config", "nonexistent.toml"])
        .output()
        .expect("Failed to execute command");

    // Should exit with code 2 for file not found (tool error)
    assert_eq!(output.status.code(), Some(2), "Expected exit code 2 for file not found");

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("config file not found") || stderr.contains("Failed to read config file"),
        "stderr should mention the missing config file; got: {stderr}"
    );
    assert!(stderr.contains("nonexistent.toml"));
}

#[test]
fn test_config_file_command_auto_discovery() {
    let temp_dir = tempdir().unwrap();

    // Create a .rumdl.toml file for auto-discovery
    let config_content = r#"
[global]
disable = ["MD013"]
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run the config file command (should auto-discover .rumdl.toml)
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");
    let output = Command::new(rumdl_exe)
        .args(["config", "file"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let absolute_path = fs::canonicalize(&config_path).unwrap();

    // The command may find multiple config files (including global ones)
    // We just need to ensure our temp config is in the list
    let found_configs: Vec<&str> = stdout.trim().split('\n').collect();
    assert!(
        found_configs
            .iter()
            .any(|&path| path == absolute_path.to_string_lossy()),
        "Expected config file {} not found in output: {found_configs:?}",
        absolute_path.display()
    );
}

#[test]
fn test_config_file_command_multiple_files() {
    let temp_dir = tempdir().unwrap();

    // Create both pyproject.toml and .rumdl.toml
    let pyproject_content = r#"
[tool.rumdl]
line-length = 120
"#;
    let pyproject_path = temp_dir.path().join("pyproject.toml");
    fs::write(&pyproject_path, pyproject_content).unwrap();

    let rumdl_content = r#"
[global]
disable = ["MD013"]
"#;
    let rumdl_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&rumdl_path, rumdl_content).unwrap();

    // Run the config file command (should find only .rumdl.toml as it has higher precedence)
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");
    let output = Command::new(rumdl_exe)
        .args(["config", "file"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    let rumdl_absolute = fs::canonicalize(&rumdl_path).unwrap();
    let pyproject_absolute = fs::canonicalize(&pyproject_path).unwrap();

    // When both .rumdl.toml and pyproject.toml exist, only .rumdl.toml is loaded
    // (it has higher precedence and stops the search)
    assert!(
        lines.iter().any(|&path| path == rumdl_absolute.to_string_lossy()),
        "Expected .rumdl.toml in output: {lines:?}"
    );

    // pyproject.toml should NOT be listed when .rumdl.toml exists
    // because .rumdl.toml has higher precedence
    assert!(
        !lines.iter().any(|&path| path == pyproject_absolute.to_string_lossy()),
        "pyproject.toml should not be loaded when .rumdl.toml exists: {lines:?}"
    );
}

#[test]
fn test_config_no_defaults_basic() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file with some non-default values
    let config_content = r#"
[global]
disable = ["MD013"]
line_length = 100

[MD004]
style = "asterisk"
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let stderr = String::from_utf8(output.stderr).unwrap();

    // Should contain the non-default values
    assert!(
        stdout.contains("disable = [\"MD013\"]"),
        "Output should contain non-default disable value. stdout: {stdout}, stderr: {stderr}"
    );
    assert!(
        stdout.contains("line_length = 100"),
        "Output should contain non-default line_length value"
    );
    assert!(stdout.contains("[MD004]"), "Output should contain MD004 rule section");
    assert!(
        stdout.contains("style = \"asterisk\""),
        "Output should contain non-default style value"
    );

    // Should NOT contain [from default] annotations (only non-defaults are shown)
    // Actually, non-default values should have their source shown
    assert!(
        stdout.contains("[from"),
        "Output should contain provenance annotations for non-default values"
    );
}

#[test]
fn test_config_no_defaults_all_defaults() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Run 'rumdl config --no-defaults --no-config' (all defaults)
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults", "--no-config"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults --no-config'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should show message that all configs are defaults
    assert!(
        stdout.contains("All configurations are using default values"),
        "Output should indicate all configs are defaults. stdout: {stdout}"
    );
}

#[test]
fn test_config_defaults_and_no_defaults_mutually_exclusive() {
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Run 'rumdl config --defaults --no-defaults' (should error)
    let output = Command::new(rumdl_exe)
        .args(["config", "--defaults", "--no-defaults"])
        .output()
        .expect("Failed to execute command");

    // Should exit with error code
    assert!(!output.status.success(), "Should fail when both flags are used");

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("Cannot use both --defaults and --no-defaults"),
        "Should show error about mutual exclusivity. stderr: {stderr}"
    );
}

#[test]
fn test_config_no_defaults_toml_output() {
    use toml::Value;
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file with some non-default values
    let config_content = r#"
[global]
disable = ["MD013"]
line_length = 100

[MD004]
style = "asterisk"
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults --output toml'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults", "--output", "toml"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults --output toml'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should contain the non-default values
    assert!(
        stdout.contains("disable = [\"MD013\"]"),
        "Output should contain non-default disable value"
    );
    // line_length is serialized as "line-length" (kebab-case) in TOML due to rename_all
    assert!(
        stdout.contains("line-length = 100") || stdout.contains("line_length = 100"),
        "Output should contain non-default line_length value. Output: {stdout}"
    );
    assert!(stdout.contains("[MD004]"), "Output should contain MD004 rule section");
    assert!(
        stdout.contains("style = \"asterisk\""),
        "Output should contain non-default style value"
    );

    // Should NOT contain provenance annotations in TOML output
    assert!(
        !stdout.contains("[from"),
        "TOML output should not contain provenance annotations"
    );

    // Output should be valid TOML
    match toml::from_str::<Value>(&stdout) {
        Ok(_) => {} // Valid TOML
        Err(e) => panic!("Output should be valid TOML, but parsing failed: {e}\nOutput: {stdout}"),
    }
}

#[test]
fn test_config_no_defaults_with_pyproject() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create pyproject.toml with rumdl config
    let pyproject_content = r#"
[tool.rumdl]
line-length = 120
"#;
    let pyproject_path = temp_dir.path().join("pyproject.toml");
    fs::write(&pyproject_path, pyproject_content).unwrap();

    // Run 'rumdl config --no-defaults'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should show the non-default line_length from pyproject.toml
    assert!(
        stdout.contains("line_length = 120"),
        "Output should contain non-default line_length from pyproject.toml"
    );
    assert!(
        stdout.contains("pyproject.toml") || stdout.contains("[from pyproject.toml]"),
        "Output should indicate source is pyproject.toml"
    );
}

#[test]
fn test_config_no_defaults_json_output() {
    use serde_json::Value;
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file with some non-default values
    let config_content = r#"
[global]
disable = ["MD013"]
line_length = 100

[MD004]
style = "asterisk"
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults --output json'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults", "--output", "json"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults --output json'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should be valid JSON
    let json: Value = serde_json::from_str(&stdout).expect("Output should be valid JSON");

    // Should contain the non-default values
    if let Some(global) = json.get("global").and_then(|g| g.as_object()) {
        assert!(
            global.contains_key("disable"),
            "JSON should contain non-default disable value"
        );
        assert!(
            global.contains_key("line-length") || global.contains_key("line_length"),
            "JSON should contain non-default line_length value"
        );
    } else {
        panic!("JSON should contain a 'global' object");
    }

    // Should contain MD004 rule
    assert!(json.get("MD004").is_some(), "JSON should contain MD004 rule section");

    if let Some(md004) = json.get("MD004").and_then(|r| r.as_object()) {
        assert!(
            md004.contains_key("style"),
            "JSON should contain non-default style value"
        );
    }
}

#[test]
fn test_config_no_defaults_mixed_rule_config() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file where a rule has some default and some non-default values
    // MD013 has default line_length=80, but we'll set code_blocks=false (non-default)
    let config_content = r#"
[MD013]
code_blocks = false
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should contain MD013 section
    assert!(stdout.contains("[MD013]"), "Output should contain MD013 rule section");

    // Should contain the non-default code_blocks value
    assert!(
        stdout.contains("code_blocks = false") || stdout.contains("code-blocks = false"),
        "Output should contain non-default code_blocks value"
    );

    // Should NOT contain line_length (which is default)
    assert!(
        !stdout.contains("line_length = 80") && !stdout.contains("line-length = 80"),
        "Output should NOT contain default line_length value"
    );
}

#[test]
fn test_config_no_defaults_per_file_ignores() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file with per-file-ignores
    let config_content = r#"
[global]
disable = ["MD013"]

[per-file-ignores]
"README.md" = ["MD033", "MD041"]
"docs/**/*.md" = ["MD013"]
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should contain per-file-ignores section
    assert!(
        stdout.contains("[per-file-ignores]") || stdout.contains("per-file-ignores"),
        "Output should contain per-file-ignores section"
    );

    // Should contain the ignore patterns
    assert!(
        stdout.contains("README.md") || stdout.contains("\"README.md\""),
        "Output should contain README.md ignore pattern"
    );
}

#[test]
fn test_config_no_defaults_multiple_sources() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create both pyproject.toml and .rumdl.toml with different configs
    let pyproject_content = r#"
[tool.rumdl]
line-length = 120
"#;
    let pyproject_path = temp_dir.path().join("pyproject.toml");
    fs::write(&pyproject_path, pyproject_content).unwrap();

    let rumdl_content = r#"
[global]
disable = ["MD013"]

[MD004]
style = "asterisk"
"#;
    let rumdl_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&rumdl_path, rumdl_content).unwrap();

    // Run 'rumdl config --no-defaults'
    // Note: .rumdl.toml has higher precedence, so pyproject.toml values might not show
    // But if both are loaded, we should see both sources
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should contain values from .rumdl.toml (higher precedence)
    assert!(
        stdout.contains("disable = [\"MD013\"]"),
        "Output should contain disable from .rumdl.toml"
    );
    assert!(
        stdout.contains("style = \"asterisk\""),
        "Output should contain style from .rumdl.toml"
    );

    // Note: pyproject.toml values might be overridden, so we don't assert on them
    // The key is that non-default values are shown with their sources
}

#[test]
fn test_config_no_defaults_empty_arrays_explicit() {
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Create a config file with explicitly set empty arrays
    // This tests the edge case where [] is explicitly set vs default []
    let config_content = r#"
[global]
enable = []
disable = []
"#;
    let config_path = temp_dir.path().join(".rumdl.toml");
    fs::write(&config_path, config_content).unwrap();

    // Run 'rumdl config --no-defaults'
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Explicitly set empty arrays should be shown (they're non-default source)
    // Even though the value is the same as default, the source is different
    assert!(
        stdout.contains("enable = []") || stdout.contains("disable = []"),
        "Output should show explicitly set empty arrays if source is non-default"
    );
}

#[test]
fn test_config_no_defaults_json_all_defaults() {
    use serde_json::Value;
    let temp_dir = tempdir().unwrap();
    let rumdl_exe = env!("CARGO_BIN_EXE_rumdl");

    // Run 'rumdl config --no-defaults --output json --no-config' (all defaults)
    let output = Command::new(rumdl_exe)
        .args(["config", "--no-defaults", "--output", "json", "--no-config"])
        .current_dir(&temp_dir)
        .output()
        .expect("Failed to execute 'rumdl config --no-defaults --output json --no-config'");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should be valid JSON
    let json: Value = serde_json::from_str(&stdout).expect("Output should be valid JSON");

    // When all configs are defaults, JSON output should be minimal
    // It might be {} or have empty/default structures
    // The key is that it's valid JSON and doesn't contain unexpected non-default values
    // We just verify it parses correctly - the actual content depends on serde serialization
    // which might include default structures
    assert!(
        json.is_object() || json.is_null(),
        "JSON should be an object or null when all configs are defaults"
    );
}