rumdl 0.2.54

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
//! Exit-code contract tests.
//!
//! rumdl follows Ruff's convention: 0 = clean, 1 = lint violations found,
//! 2 = tool error (bad config, unreadable file, missing target). A file that
//! cannot be read, or a target path that does not exist, must surface as a tool
//! error (2), never as "clean" (0) or "violations" (1) - otherwise CI silently
//! passes on corrupt/unreadable files or a mistyped path.

use std::process::Command;
use tempfile::tempdir;

fn rumdl() -> Command {
    Command::new(env!("CARGO_BIN_EXE_rumdl"))
}

const TOOL_ERROR: i32 = 2;

#[test]
fn invalid_utf8_file_is_a_tool_error() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("bad.md");
    // Not valid UTF-8: lone continuation bytes.
    std::fs::write(&path, [b'#', b' ', 0xff, 0xfe, b'\n']).unwrap();

    let status = rumdl()
        .args(["check", "--no-cache"])
        .arg(&path)
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "an unreadable (invalid UTF-8) file must exit with the tool-error code, not report success"
    );
}

#[cfg(unix)]
#[test]
fn unreadable_file_is_a_tool_error() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempdir().unwrap();
    let path = dir.path().join("noperm.md");
    std::fs::write(&path, "# ok\n").unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();

    let status = rumdl()
        .args(["check", "--no-cache"])
        .arg(&path)
        .status()
        .expect("run rumdl check");
    // Restore perms so tempdir cleanup can remove it.
    let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644));
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "a file that cannot be read must exit with the tool-error code"
    );
}

#[test]
fn nonexistent_target_is_a_tool_error() {
    let dir = tempdir().unwrap();
    let missing = dir.path().join("does-not-exist-xyz");

    let status = rumdl()
        .args(["check", "--no-cache"])
        .arg(&missing)
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "a nonexistent target path must exit with the tool-error code, not the violations code"
    );
}

// --- issue #726: --deny-config-warnings turns config problems into tool errors ---
//
// Configuration problems (unknown rule/option in a config file or CLI flag,
// unknown rule in an inline disable comment, shadowed config) are non-fatal
// stderr warnings by default. `--deny-config-warnings` makes any of them exit
// with the tool-error code (2), so CI catches config typos.

/// An unknown rule name in a config file exits 2 under the flag.
#[test]
fn deny_config_warnings_flags_unknown_rule_in_config_file() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[global]\nenable = [\"MD999\"]\n").unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "clean.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "an unknown rule in the config file must exit 2 under --deny-config-warnings"
    );
}

/// The same config problem WITHOUT the flag stays a non-fatal warning: exit 0
/// on a clean file. Locks in that the default behavior is unchanged.
#[test]
fn config_warnings_are_non_fatal_by_default() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[global]\nenable = [\"MD999\"]\n").unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "clean.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(0),
        "config warnings must not affect the exit code by default"
    );
}

/// An unknown rule passed via a CLI flag exits 2 under the flag.
#[test]
fn deny_config_warnings_flags_unknown_rule_in_cli_flag() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();

    let status = rumdl()
        .args([
            "check",
            "--no-cache",
            "--deny-config-warnings",
            "--disable",
            "MD9999",
            "clean.md",
        ])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(status.code(), Some(TOOL_ERROR));
}

/// A clean config plus the flag must not exit 2 (no false positive).
#[test]
fn deny_config_warnings_clean_config_exits_zero() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "clean.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(status.code(), Some(0), "no config problem means the flag has no effect");
}

/// The issue's headline case: an unknown rule in an inline disable comment is a
/// non-fatal warning by default, fatal (exit 2) under the flag.
#[test]
fn deny_config_warnings_flags_unknown_rule_in_inline_comment() {
    let dir = tempdir().unwrap();
    std::fs::write(
        dir.path().join("inline.md"),
        "# Title\n\nSome text.<!-- rumdl-disable-line asdf -->\n",
    )
    .unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "inline.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "an unknown rule in an inline disable comment must exit 2 under the flag"
    );
}

/// The inline case without the flag stays a non-fatal warning (exit 0 on an
/// otherwise clean file).
#[test]
fn inline_config_warning_is_non_fatal_by_default() {
    let dir = tempdir().unwrap();
    std::fs::write(
        dir.path().join("inline.md"),
        "# Title\n\nSome text.<!-- rumdl-disable-line asdf -->\n",
    )
    .unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "inline.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(0),
        "an inline config warning must not affect the exit code by default"
    );
}

/// stdin path: an inline disable comment with an unknown rule exits 2 under the
/// flag, even though stdin has its own exit path.
#[test]
fn deny_config_warnings_stdin_inline_comment() {
    use std::io::Write;
    let mut child = rumdl()
        .args(["check", "--no-cache", "--stdin", "--deny-config-warnings"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("spawn rumdl");
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"# Title\n\nText.<!-- rumdl-disable-line asdf -->\n")
        .unwrap();
    let status = child.wait().expect("wait rumdl");
    assert_eq!(status.code(), Some(TOOL_ERROR));
}

/// stdin FIX/format path: `fmt --stdin` has its own exit before the check-mode
/// --fail-on block, so the deny decision must also guard that branch.
#[test]
fn deny_config_warnings_stdin_fmt_inline_comment() {
    use std::io::Write;
    let mut child = rumdl()
        .args(["fmt", "--no-cache", "--stdin", "--deny-config-warnings"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("spawn rumdl");
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"# Title\n\nText.<!-- rumdl-disable-line asdf -->\n")
        .unwrap();
    let status = child.wait().expect("wait rumdl");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "fmt --stdin must honor --deny-config-warnings despite its separate exit path"
    );
}

/// stdin without the flag stays non-fatal (exit 0 on clean content).
#[test]
fn stdin_inline_config_warning_non_fatal_by_default() {
    use std::io::Write;
    let mut child = rumdl()
        .args(["check", "--no-cache", "--stdin"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("spawn rumdl");
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"# Title\n\nText.<!-- rumdl-disable-line asdf -->\n")
        .unwrap();
    let status = child.wait().expect("wait rumdl");
    assert_eq!(
        status.code(),
        Some(0),
        "stdin inline config warning must not affect exit code by default"
    );
}

/// --silent suppresses the printed notice but the flag must still exit 2.
#[test]
fn deny_config_warnings_silent_still_exits_two() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[global]\nenable = [\"MD999\"]\n").unwrap();

    let output = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "--silent", "clean.md"])
        .current_dir(dir.path())
        .output()
        .expect("run rumdl check");
    assert_eq!(output.status.code(), Some(TOOL_ERROR));
    assert!(
        String::from_utf8_lossy(&output.stderr).is_empty(),
        "--silent must suppress the printed warning even while the flag makes it fatal. stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A config warning outranks a real Markdown violation: exit 2, not 1.
#[test]
fn deny_config_warnings_take_precedence_over_violations() {
    let dir = tempdir().unwrap();
    // MD041: first line is not a top-level heading -> a real violation.
    std::fs::write(dir.path().join("dirty.md"), "no heading here\n").unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[global]\nenable = [\"MD999\"]\n").unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "dirty.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "a config problem must exit 2 even when Markdown violations (exit 1) are also present"
    );
}

/// Sanity check the precedence test's premise: the same violation WITHOUT a
/// config problem exits 1, not 2.
#[test]
fn violations_without_config_problem_exit_one() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("dirty.md"), "no heading here\n").unwrap();

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "dirty.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(1),
        "a plain Markdown violation exits 1; the flag only changes config-problem exits"
    );
}

/// Build a project whose subdirectory carries its own config, and return the
/// directory to run in. `subdir_config` is written verbatim, so a caller can make
/// it unparseable.
fn project_with_subdir_config(subdir_config: &str) -> tempfile::TempDir {
    let dir = tempdir().unwrap();
    // Bound config discovery here, so nothing above the temp directory is read.
    std::fs::create_dir_all(dir.path().join(".git")).unwrap();
    std::fs::create_dir_all(dir.path().join("sub")).unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[MD013]\nline-length = 200\n").unwrap();
    std::fs::write(dir.path().join("sub/.rumdl.toml"), subdir_config).unwrap();
    std::fs::write(dir.path().join("sub/doc.md"), "# Title\n\nText.\n").unwrap();
    dir
}

/// An unparseable TOML table header, so the subdirectory config cannot load.
const BROKEN_SUBDIR_CONFIG: &str = "[MD013\nline-length = 100\n";

/// A subdirectory config that fails to load is a config problem: its files get
/// linted under the root config instead of the settings their author wrote. The
/// flag has to fail the run on it, the way it does for every other config problem.
#[test]
fn deny_config_warnings_flags_unloadable_subdirectory_config() {
    let dir = project_with_subdir_config(BROKEN_SUBDIR_CONFIG);

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "."])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(TOOL_ERROR),
        "a subdirectory config that could not be loaded must exit 2 under the flag"
    );
}

/// Without the flag the same run stays non-fatal, and says on stderr which config
/// it could not load.
#[test]
fn unloadable_subdirectory_config_is_non_fatal_by_default() {
    let dir = project_with_subdir_config(BROKEN_SUBDIR_CONFIG);

    let output = rumdl()
        .args(["check", "--no-cache", "."])
        .current_dir(dir.path())
        .output()
        .expect("run rumdl check");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(
        output.status.code(),
        Some(0),
        "an unloadable subdirectory config must not affect the exit code by default. stderr: {stderr}"
    );
    assert!(
        stderr.contains("Failed to load config"),
        "the run must still report which config it could not load, got: {stderr}"
    );
}

/// --silent suppresses the notice; the flag still fails the run on it.
#[test]
fn unloadable_subdirectory_config_counts_while_silenced() {
    let dir = project_with_subdir_config(BROKEN_SUBDIR_CONFIG);

    let output = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "--silent", "."])
        .current_dir(dir.path())
        .output()
        .expect("run rumdl check");
    assert_eq!(output.status.code(), Some(TOOL_ERROR));
    assert!(
        String::from_utf8_lossy(&output.stderr).is_empty(),
        "--silent must suppress the printed warning even while the flag makes it fatal. stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// The same arrangement with a subdirectory config that loads exits 0, so the
/// test above cannot pass for merely having a subdirectory config.
#[test]
fn loadable_subdirectory_config_exits_zero() {
    let dir = project_with_subdir_config("[MD013]\nline-length = 100\n");

    let status = rumdl()
        .args(["check", "--no-cache", "--deny-config-warnings", "."])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl check");
    assert_eq!(
        status.code(),
        Some(0),
        "a subdirectory config that loads is not a config problem"
    );
}

/// `fmt` shares config loading and the flag; it also exits 2 on a config problem.
#[test]
fn deny_config_warnings_applies_to_fmt() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("clean.md"), "# Title\n\nText.\n").unwrap();
    std::fs::write(dir.path().join(".rumdl.toml"), "[global]\nenable = [\"MD999\"]\n").unwrap();

    let status = rumdl()
        .args(["fmt", "--no-cache", "--deny-config-warnings", "clean.md"])
        .current_dir(dir.path())
        .status()
        .expect("run rumdl fmt");
    assert_eq!(status.code(), Some(TOOL_ERROR));
}