rumdl 0.2.10

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
//! End-to-end execution tests for built-in code-block tools.
//!
//! These run the real external tool through the real `rumdl` binary and assert the
//! tool actually lints/formats a fenced code block as expected, rather than only
//! checking the registry command string. Each test is gated on the tool being
//! installed (`tool_available`), so it runs wherever the tool exists (locally, or any
//! CI that installs it) and skips otherwise. This is what would have caught the
//! shuck (no stdin), eslint (needs a config), and shellcheck (missing `--shell`)
//! problems before they shipped.
//!
//! ## Adding a built-in tool
//!
//! 1. Install the tool and run it through rumdl on a fenced block (a temp `.rumdl.toml`
//!    with `[code-block-tools]` plus `rumdl check`/`fmt`). Confirm it reads stdin and
//!    its output parses into real diagnostics / formatted code. If it can't be made to
//!    work over stdin, do not ship it (see the removed eslint/shuck/rubocop entries).
//! 2. Add an execution test below and list its registry id in `VERIFIED`. For a pure
//!    extension/subcommand variant of an already-tested tool (e.g. `prettier:json`),
//!    add it to `EXEMPT` with the reason instead.
//!
//! `every_builtin_tool_is_verified_or_exempt` is a CI gate: a new registry entry that
//! is neither tested nor exempted fails the suite, so unverified tools cannot ship.

use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

/// True if `tool` is on PATH.
fn tool_available(tool: &str) -> bool {
    let finder = if cfg!(windows) { "where" } else { "which" };
    Command::new(finder)
        .arg(tool)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Write a `.rumdl.toml` and a markdown file with a single fenced block, in a temp dir.
fn setup(config_lang: &str, slot: &str, tool: &str, lang_tag: &str, code: &str) -> TempDir {
    let dir = tempfile::tempdir().unwrap();
    // `exact` language resolution so config_lang == lang_tag deterministically
    // (avoids linguist-alias surprises like cpp -> c++).
    let config = format!(
        "[code-block-tools]\nenabled = true\nnormalize-language = \"exact\"\non-error = \"warn\"\n\n\
         [code-block-tools.languages]\n{config_lang} = {{ {slot} = [\"{tool}\"] }}\n"
    );
    fs::write(dir.path().join(".rumdl.toml"), config).unwrap();
    fs::write(dir.path().join("t.md"), format!("# T\n\n```{lang_tag}\n{code}\n```\n")).unwrap();
    dir
}

fn run(dir: &Path, args: &[&str]) -> String {
    let output = Command::new(env!("CARGO_BIN_EXE_rumdl"))
        .current_dir(dir)
        .args(args)
        .output()
        .expect("failed to run rumdl");
    // Diagnostics can land on either stream depending on format; combine for assertions.
    format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    )
}

/// Lint a code block with `tool` and return rumdl's combined output.
fn lint(config_lang: &str, tool: &str, lang_tag: &str, code: &str) -> String {
    let dir = setup(config_lang, "lint", tool, lang_tag, code);
    run(dir.path(), &["check", "--no-cache", "t.md"])
}

/// Format a code block with `tool` and return the resulting file contents.
fn format(config_lang: &str, tool: &str, lang_tag: &str, code: &str) -> String {
    let dir = setup(config_lang, "format", tool, lang_tag, code);
    run(dir.path(), &["fmt", "--no-cache", "t.md"]);
    fs::read_to_string(dir.path().join("t.md")).unwrap()
}

macro_rules! require_tool {
    ($tool:expr) => {
        if !tool_available($tool) {
            eprintln!("skipping: `{}` not installed", $tool);
            return;
        }
    };
}

// ---- linters --------------------------------------------------------------

#[test]
fn ruff_check_lints_python() {
    require_tool!("ruff");
    let out = lint("python", "ruff:check", "python", "import sys\nx = 1\n");
    assert!(out.contains("F401"), "ruff:check should flag the unused import:\n{out}");
}

#[test]
fn shellcheck_lints_shell() {
    require_tool!("shellcheck");
    // Regression guard for the `--shell=bash` fix: without it, a shebang-less snippet
    // yields a "target shell unknown" tip instead of real diagnostics. rumdl strips the
    // SCxxxx code from the message, so assert on the diagnostic text.
    let out = lint("shell", "shellcheck", "shell", "echo $foo\n");
    assert!(
        out.contains("Double quote to prevent globbing"),
        "shellcheck should flag the unquoted variable (SC2086):\n{out}"
    );
    assert!(
        !out.contains("target shell"),
        "shellcheck should not emit the shell-unknown tip with --shell=bash:\n{out}"
    );
}

#[test]
fn jq_lints_invalid_json() {
    require_tool!("jq");
    let out = lint("json", "jq", "json", "{\"a\": 1,}");
    assert!(
        out.contains("parse error"),
        "jq should report a JSON parse error:\n{out}"
    );
}

// ---- formatters -----------------------------------------------------------

#[test]
fn ruff_format_formats_python() {
    require_tool!("ruff");
    let out = format("python", "ruff:format", "python", "x=1");
    assert!(out.contains("x = 1"), "ruff:format should reformat the block:\n{out}");
}

#[test]
fn prettier_formats_javascript() {
    require_tool!("prettier");
    let out = format("javascript", "prettier", "javascript", "const x=1");
    assert!(
        out.contains("const x = 1;"),
        "prettier should reformat the block:\n{out}"
    );
}

#[test]
fn rustfmt_formats_rust() {
    require_tool!("rustfmt");
    let out = format("rust", "rustfmt", "rust", "fn  main(){let x=1;}");
    assert!(out.contains("fn main()"), "rustfmt should reformat the block:\n{out}");
}

#[test]
fn gofmt_formats_go() {
    require_tool!("gofmt");
    let out = format("go", "gofmt", "go", "package main\nfunc  main(){}");
    assert!(out.contains("func main()"), "gofmt should reformat the block:\n{out}");
}

#[test]
fn jq_formats_json() {
    require_tool!("jq");
    let out = format("json", "jq", "json", "{\"a\":1,\"b\":2}");
    assert!(
        out.contains("\"a\": 1") && out.contains('\n'),
        "jq should pretty-print the JSON block:\n{out}"
    );
}

#[test]
fn deno_fmt_formats_typescript() {
    require_tool!("deno");
    let out = format("typescript", "deno-fmt:ts", "typescript", "const   x=1");
    assert!(
        out.contains("const x = 1;"),
        "deno-fmt:ts should reformat the block:\n{out}"
    );
}

#[test]
fn black_formats_python() {
    require_tool!("black");
    let out = format("python", "black", "python", "x=1");
    assert!(out.contains("x = 1"), "black should reformat the block:\n{out}");
}

#[test]
fn shfmt_formats_shell() {
    require_tool!("shfmt");
    let out = format("shell", "shfmt", "shell", "if true;then echo hi;fi");
    assert!(out.contains("; then"), "shfmt should reformat the block:\n{out}");
}

#[test]
fn goimports_formats_go() {
    require_tool!("goimports");
    let out = format("go", "goimports", "go", "package main\nfunc  main(){}");
    assert!(
        out.contains("func main()"),
        "goimports should reformat the block:\n{out}"
    );
}

#[test]
fn clang_format_formats_cpp() {
    require_tool!("clang-format");
    let out = format("cpp", "clang-format", "cpp", "int  main(){return 0;}");
    assert!(
        out.contains("int main()"),
        "clang-format should reformat the block:\n{out}"
    );
}

#[test]
fn yamlfmt_formats_yaml() {
    require_tool!("yamlfmt");
    let out = format("yaml", "yamlfmt", "yaml", "a:   1");
    assert!(out.contains("a: 1"), "yamlfmt should reformat the block:\n{out}");
}

#[test]
fn taplo_formats_toml() {
    require_tool!("taplo");
    let out = format("toml", "taplo", "toml", "a=1");
    assert!(out.contains("a = 1"), "taplo should reformat the block:\n{out}");
}

#[test]
fn terraform_fmt_formats_terraform() {
    require_tool!("terraform");
    let out = format("terraform", "terraform-fmt", "terraform", "a=1");
    assert!(out.contains("a = 1"), "terraform fmt should reformat the block:\n{out}");
}

#[test]
fn stylua_formats_lua() {
    require_tool!("stylua");
    let out = format("lua", "stylua", "lua", "x=1");
    assert!(out.contains("x = 1"), "stylua should reformat the block:\n{out}");
}

#[test]
fn oxfmt_formats_javascript() {
    require_tool!("oxfmt");
    let out = format("javascript", "oxfmt", "javascript", "const x=1");
    assert!(out.contains("const x = 1;"), "oxfmt should reformat the block:\n{out}");
}

#[test]
fn tombi_formats_toml() {
    require_tool!("tombi");
    let out = format("toml", "tombi:format", "toml", "a=1");
    assert!(out.contains("a = 1"), "tombi:format should reformat the block:\n{out}");
}

#[test]
fn beautysh_formats_shell() {
    require_tool!("beautysh");
    let out = format("shell", "beautysh", "shell", "if true\nthen\necho hi\nfi");
    assert!(out.contains("    echo hi"), "beautysh should indent the block:\n{out}");
}

#[test]
fn nixfmt_formats_nix() {
    require_tool!("nixfmt");
    let out = format("nix", "nixfmt", "nix", "{ a=1; }");
    assert!(out.contains("a = 1"), "nixfmt should reformat the block:\n{out}");
}

#[test]
fn ormolu_formats_haskell() {
    require_tool!("ormolu");
    let out = format("haskell", "ormolu", "haskell", "main=putStrLn \"hi\"");
    assert!(
        out.contains("main = putStrLn"),
        "ormolu should reformat the block:\n{out}"
    );
}

#[test]
fn swift_format_formats_swift() {
    require_tool!("swift-format");
    let out = format("swift", "swift-format", "swift", "let x  =  1");
    assert!(
        out.contains("let x = 1"),
        "swift-format should reformat the block:\n{out}"
    );
}

#[test]
fn ktfmt_formats_kotlin() {
    require_tool!("ktfmt");
    let out = format("kotlin", "ktfmt", "kotlin", "fun main(){}");
    assert!(out.contains("fun main() {}"), "ktfmt should reformat the block:\n{out}");
}

#[test]
fn elm_format_formats_elm() {
    require_tool!("elm-format");
    let out = format("elm", "elm-format", "elm", "module Main exposing (main)\nmain= 1");
    // elm-format moves the body onto its own indented line.
    assert!(
        out.contains("main =\n    1"),
        "elm-format should reformat the block:\n{out}"
    );
}

#[test]
fn sqlfluff_lints_sql_with_dialect() {
    require_tool!("sqlfluff");
    // Regression guard for the `--dialect ansi` fix: without it sqlfluff errors
    // ("No dialect was specified") instead of linting.
    let out = lint("sql", "sqlfluff:lint", "sql", "select 1");
    assert!(
        !out.contains("No dialect") && !out.contains("User Error"),
        "sqlfluff should lint with a dialect, not error:\n{out}"
    );
}

#[test]
fn djlint_lints_html() {
    require_tool!("djlint");
    let out = lint("html", "djlint", "html", "<div><p>hi</div>");
    assert!(out.contains("orphan"), "djlint should flag the orphan tag:\n{out}");
}

// ---- coverage gate --------------------------------------------------------

/// Built-in tool ids that have a dedicated execution test above.
const VERIFIED: &[&str] = &[
    "ruff:check",
    "ruff:format",
    "black",
    "prettier",
    "shellcheck",
    "shfmt",
    "rustfmt",
    "gofmt",
    "goimports",
    "clang-format",
    "sqlfluff:lint",
    "jq",
    "yamlfmt",
    "taplo",
    "terraform-fmt",
    "nixfmt",
    "stylua",
    "ormolu",
    "elm-format",
    "swift-format",
    "ktfmt",
    "djlint",
    "beautysh",
    "tombi:format",
    "oxfmt",
    "deno-fmt:ts",
];

/// Built-in tool ids without a dedicated test because they are pure
/// extension/subcommand variants of a VERIFIED tool (same binary), with the reason.
const EXEMPT: &[(&str, &str)] = &[
    (
        "prettier:json",
        "prettier variant (different --stdin-filepath extension)",
    ),
    ("prettier:yaml", "prettier variant"),
    ("prettier:html", "prettier variant"),
    ("prettier:css", "prettier variant"),
    ("prettier:markdown", "prettier variant"),
    ("sqlfluff:fix", "sqlfluff variant (sqlfluff:lint verified)"),
    ("djlint:lint", "djlint variant"),
    ("djlint:reformat", "djlint variant"),
    ("tombi", "tombi variant (tombi:format verified)"),
    ("tombi:lint", "tombi variant"),
    ("oxfmt:js", "oxfmt variant"),
    ("oxfmt:ts", "oxfmt variant"),
    ("oxfmt:jsx", "oxfmt variant"),
    ("oxfmt:tsx", "oxfmt variant"),
    ("oxfmt:json", "oxfmt variant"),
    ("oxfmt:css", "oxfmt variant"),
    ("deno-fmt", "deno-fmt variant (deno-fmt:ts verified)"),
    ("deno-fmt:js", "deno-fmt variant"),
    ("deno-fmt:json", "deno-fmt variant"),
    ("deno-fmt:jsonc", "deno-fmt variant"),
    ("deno-fmt:md", "deno-fmt variant"),
];

/// Gate: every built-in must have an execution test or an explicit exemption, so a new
/// registry entry cannot ship unverified. Fails if a tool is uncovered, double-listed,
/// or if VERIFIED/EXEMPT reference a tool no longer in the registry.
#[test]
fn every_builtin_tool_is_verified_or_exempt() {
    use std::collections::BTreeSet;

    let registry: BTreeSet<&str> = rumdl_lib::code_block_tools::builtin_tool_ids().into_iter().collect();
    let verified: BTreeSet<&str> = VERIFIED.iter().copied().collect();
    let exempt: BTreeSet<&str> = EXEMPT.iter().map(|(id, _)| *id).collect();

    let both: Vec<&&str> = verified.intersection(&exempt).collect();
    assert!(both.is_empty(), "ids listed as both verified and exempt: {both:?}");

    let covered: BTreeSet<&str> = verified.union(&exempt).copied().collect();

    let uncovered: Vec<&&str> = registry.difference(&covered).collect();
    assert!(
        uncovered.is_empty(),
        "built-in tools with no execution test or exemption (add a test to VERIFIED or an entry to EXEMPT): {uncovered:?}"
    );

    let stale: Vec<&&str> = covered.difference(&registry).collect();
    assert!(
        stale.is_empty(),
        "VERIFIED/EXEMPT reference tools no longer in the registry (remove them): {stale:?}"
    );
}