rust-llm-tidy-cli 0.8.3

CLI for linting and tidying Rust, C#, and documentation source.
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
//! CLI flag selection tests: how `--include`/`--exclude` combine, override
//! config modes, reject unknown ops, and shape the default pipeline.

use super::common::binary;
use super::temp_dir;
use std::fs;
use std::process::Command;

/// Default pipeline with `exclude: [reorder]` fixes/vis/lints but does
/// not reorder.
///
/// The input is reordered on a normal run; under `reorder` being disabled
/// it must remain in input order.
#[test]
fn all_excludes_reorder_rule() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // Two top-level fns in NON-canonical order: callee precedes caller, so a
    // normal run would reorder them. Canonical is caller before callee, per
    // the reorder phase.
    fs::write(&tmp, "fn callee() {}\nfn caller() { callee(); }\n").unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(
        &cfg,
        "exclude:\n  - paths: [\"lib.rs\"]\n    rules: [\"reorder\"]\n",
    )
    .unwrap();

    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap()])
        .args(["--exclude", "DOC009"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn rust-llm-tidy");
    assert!(
        output.status.success(),
        "pipeline should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // Default pipeline runs fix/reorder/vis/lints.
    //
    // With `reorder` disabled, the non-canonical input order (callee before caller)
    // must be preserved.
    // Without the disable, it would reorder to caller-before-callee.
    let actual = fs::read_to_string(&tmp).unwrap();
    assert!(
        actual.find("fn callee()").unwrap() < actual.find("fn caller()").unwrap(),
        "reorder disabled: non-canonical callee-before-caller must be preserved"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// Default pipeline with `exclude: [DOC001]` suppresses DOC001 findings.
#[test]
fn check_excludes_doc001_rule() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // An undocumented pub fn triggers DOC001 + DOC002 (Result with no Errors).
    fs::write(&tmp, "pub fn load() -> Result<(), String> { Ok(()) }\n").unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(
        &cfg,
        "exclude:\n  - paths: [\"lib.rs\"]\n    rules: [\"DOC001\"]\n",
    )
    .unwrap();

    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap()])
        .arg(&tmp)
        .output()
        .expect("failed to spawn rust-llm-tidy");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("DOC001"),
        "DOC001 must be suppressed by exclude: {stderr:?}"
    );
    // A non-disabled diagnostic (DOC002) must still be reported, proving
    // the filter is selective, not clearing all diagnostics.
    assert!(
        stderr.contains("DOC002"),
        "non-disabled DOC002 must still appear: {stderr:?}"
    );
    let _ = fs::remove_dir_all(&dir);
}

// ── flag exclusivity ──

/// `--config` and `--no-config` are mutually exclusive; supplying both
/// causes a non-zero exit (clap `conflicts_with` enforcement).
#[test]
fn config_and_no_config_are_mutually_exclusive() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(&cfg, "exclude_files: []\n").unwrap();

    let output = Command::new(binary())
        .args([
            "--config",
            cfg.to_str().unwrap(),
            "--no-config",
            "--validate",
        ])
        .output()
        .expect("failed to spawn rust-llm-tidy");
    assert!(
        !output.status.success(),
        "--config and --no-config must be mutually exclusive"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// --exclude additive: even with no config, --exclude lints skips lint failure.
#[test]
fn exclude_flag_additive_skips_lints() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    fs::write(&tmp, "pub fn undocumented() {}\n").unwrap();
    let output = Command::new(binary())
        .args(["--no-config", "--exclude", "lints"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "--exclude lints must skip the lint pass: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let _ = fs::remove_dir_all(&dir);
}

/// Blacklist mode: exclude: [{rules: [lints]}] suppresses all lint failure.
#[test]
fn exclude_lints_op_suppresses_all_lint_failure() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // DOC001 + DOC002 would both fire on a normal run.
    fs::write(&tmp, "pub fn load() -> Result<(), String> { Ok(()) }\n").unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(&cfg, "exclude:\n  - rules: [\"lints\"]\n").unwrap();
    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap()])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "lints op disabled -> run must succeed despite doc gaps: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let _ = fs::remove_dir_all(&dir);
}

/// --exclude lints suppresses all lint codes, including specifically included ones.
#[test]
fn exclude_lints_overrides_included_lint_code() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    fs::write(&tmp, "pub fn undocumented() {}\n").unwrap();
    let output = Command::new(binary())
        .args(["--no-config", "--include", "DOC001", "--exclude", "lints"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "--exclude lints must suppress included DOC001: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!String::from_utf8_lossy(&output.stderr).contains("DOC001"));
    let _ = fs::remove_dir_all(&dir);
}

/// Default pipeline with `exclude_files` for the fixture leaves the file unchanged.
#[test]
fn fix_exclude_skips_file() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("in.md");
    let original = "| a | b |\n|---|---|\n| 1 | 2 |\n";
    fs::write(&tmp, original).unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(&cfg, "exclude_files:\n  - \"in.md\"\n").unwrap();

    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap()])
        .arg(&tmp)
        .output()
        .expect("failed to spawn rust-llm-tidy");
    assert!(
        output.status.success(),
        "default pipeline should succeed even when the file is excluded: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let actual = fs::read_to_string(&tmp).unwrap();
    assert_eq!(actual, original, "excluded file must be unchanged");
    let _ = fs::remove_dir_all(&dir);
}

// ── exclude + exclude_rules on pipeline operations ──

/// Bare default pipeline with `exclude: [links]` does NOT hoist links on a
/// file that needs link hoisting, while tables/fences are still applied.
#[test]
fn fix_excludes_links_rule() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("in.md");
    // A markdown table with multi-char cells that `fix_tables` would pad-align,
    // plus a repeated inline link that `fix_links` would hoist.
    fs::write(
        &tmp,
        "| Name | Value |\n| --- | --- |\n| a | 1 |\n| longname | 200 |\n\nsee [A](http://x) and [A](http://x)\n",
    )
    .unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(
        &cfg,
        "exclude:\n  - paths: [\"in.md\"]\n    rules: [\"links\"]\n",
    )
    .unwrap();

    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap(), "--dry-run"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn rust-llm-tidy");
    assert!(
        !output.status.success(),
        "dry-run must fail for proposed table changes: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Dry-run reports change records on stderr, never reconstructed source.
    assert!(
        output.stdout.is_empty(),
        "dry-run must not print reconstructed source to stdout"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("hoist link"),
        "links must NOT be hoisted when `links` is disabled: {stderr:?}"
    );
    // Tables are still applied (the `links` disable is selective, not blanket).
    assert!(
        stderr.contains("tables were aligned"),
        "tables must still be applied when only `links` is disabled: {stderr:?}"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// --include / --exclude with an unknown op errors.
#[test]
fn flags_reject_unknown_op() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    fs::write(&tmp, "pub fn example() {}\n").unwrap();
    let output = Command::new(binary())
        .args(["--no-config", "--include", "BOGUS"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(!output.status.success(), "--include BOGUS must error");

    let output = Command::new(binary())
        .args(["--no-config", "--exclude", "BOGUS"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(!output.status.success(), "--exclude BOGUS must error");
    let _ = fs::remove_dir_all(&dir);
}

/// --include + --exclude combine in whitelist mode: --include vis,lints then
/// --exclude lints yields enabled={vis}.
///
/// vis narrows the inner fn, but lints does NOT run, so the bare `pub fn f`
/// that would trigger DOC001 stays clean.
#[test]
fn include_and_exclude_cli_combine_in_whitelist_mode() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // vis would narrow `pub fn f`; lints/DOC001 would normally flag it.
    fs::write(&tmp, "pub(crate) mod m {\n    pub fn f() {}\n}\n").unwrap();
    let output = Command::new(binary())
        .args([
            "--no-config",
            "--include",
            "vis",
            "--include",
            "lints",
            "--exclude",
            "lints",
        ])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "whitelist {{vis,lints}} - {{lints}} = {{vis}}: vis runs, lints skipped (no DOC001): {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let actual = fs::read_to_string(&tmp).unwrap();
    assert!(
        actual.contains("pub(crate) fn f"),
        "vis must still narrow despite --exclude lints: {actual}"
    );
    let _ = fs::remove_dir_all(&dir);
}

// ── New tests: include/exclude modes, --include/--exclude flags ──

/// --include override: only run `vis`, no lint failure on an undocumented fn.
#[test]
fn include_flag_overrides_config_mode() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // Bare pub fn -> vis would narrow; lints would error (DOC001). --include vis
    // must override the default mode so lints does NOT run.
    fs::write(&tmp, "pub(crate) mod m {\n    pub fn f() {}\n}\n").unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    // Config is blacklist mode (lints on); --include must override it.
    fs::write(&cfg, "exclude:\n  - rules: [vis]\n").unwrap();
    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap(), "--include", "vis"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "--include vis must override config and skip lints: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let actual = fs::read_to_string(&tmp).unwrap();
    assert!(
        actual.contains("pub(crate) fn f"),
        "vis must narrow: {actual}"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// --include lints with --exclude DOC001 keeps DOC001 disabled in whitelist mode.
#[test]
fn include_lints_exclude_lint_code() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    fs::write(&tmp, "pub fn undocumented() {}\n").unwrap();
    let output = Command::new(binary())
        .args(["--no-config", "--include", "lints", "--exclude", "DOC001"])
        .args(["--exclude", "DOC009"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    assert!(
        output.status.success(),
        "--exclude DOC001 must suppress DOC001 with --include lints: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!String::from_utf8_lossy(&output.stderr).contains("DOC001"));
    let _ = fs::remove_dir_all(&dir);
}

/// Whitelist a single lint code: the lint pass runs scoped to DOC001 only,
/// no other ops/lints run, and the file is unmutated.
#[test]
fn include_single_lint_code_runs_only_that_code() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    // Undocumented Result-returning pub fn triggers DOC001 and DOC002.
    fs::write(&tmp, "pub fn load() -> Result<(), String> { Ok(()) }\n").unwrap();
    let output = Command::new(binary())
        .args(["--no-config", "--include", "DOC001"])
        .arg(&tmp)
        .output()
        .expect("failed to spawn");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !output.status.success(),
        "--include DOC001 must surface DOC001 diagnostics: {}",
        stderr
    );
    assert!(
        stderr.contains("DOC001"),
        "--include DOC001 must report DOC001: {stderr:?}"
    );
    assert!(
        !stderr.contains("DOC002"),
        "--include DOC001 must not report DOC002: {stderr:?}"
    );
    // No fix/reorder/vis in the whitelist -> file untouched.
    let actual = fs::read_to_string(&tmp).unwrap();
    assert_eq!(
        actual, "pub fn load() -> Result<(), String> { Ok(()) }\n",
        "file must be unmutated by the DOC001-only lint pass"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// A non-matching-path config hard-fails a regular command (non-zero exit, not
/// a warning).
#[test]
fn regular_command_hard_fails_on_non_matching_path() {
    let dir = temp_dir();
    fs::create_dir_all(&dir).unwrap();
    let tmp = dir.join("lib.rs");
    fs::write(&tmp, "pub fn example() {}\n").unwrap();
    let cfg = dir.join(".rust-llm-tidy.yml");
    fs::write(&cfg, "exclude_files:\n  - \"missing/**\"\n").unwrap();

    let output = Command::new(binary())
        .args(["--config", cfg.to_str().unwrap()])
        .arg(&tmp)
        .output()
        .expect("failed to spawn rust-llm-tidy");
    assert!(
        !output.status.success(),
        "non-matching-path config must hard-fail, not warn"
    );
    let _ = fs::remove_dir_all(&dir);
}