rust-llm-tidy-cli 0.8.2

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
//! CLI behavior tests for `rust-llm-tidy`: dry-run and checks-only
//! reporting, failure modes, `--version`, and empty inputs.
//!
//! Every test runs the built CLI binary against a temp file or directory
//! and asserts on its exit code, stdout, and stderr.

use super::{binary, run, run_command, run_dir, temp_dir};
use rstest::rstest;
use std::fs;
use std::process::{Command, Output};

// ── --checks-only ─────────────────────────────────────────────────

/// `--checks-only` still fails on invalid config.
#[test]
fn checks_only_should_fail_for_invalid_config() {
    let (output, _) = preview_with_flags(
        "//! Module docs.\nfn example() {}\n",
        "lints",
        "include: [",
        "text",
        &["--checks-only"],
    );

    assert_eq!(output.status.code(), Some(1), "{output:?}");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("config"), "{stderr}");
}

/// `--checks-only` gates the exit on severity alone, without writing source.
///
/// Lint-code selections still narrow: only `SYM` runs for this config.
#[rstest]
#[case::warning("warning", 0)]
#[case::hint("hint", 0)]
#[case::reminder("reminder", 0)]
#[case::error("error", 1)]
fn checks_only_should_fail_only_for_error_findings(
    #[case] severity: &str,
    #[values("text", "json")] mode: &str,
    #[case] exit: i32,
) {
    let source = "fn f() { needle(); }\n";
    let config = format!(
        "perf_hints: []\nsymbol_rules: [{{regex: needle, title: Review, message: finding, severity: {severity}}}]"
    );

    let (output, consumed) = preview_with_flags(source, "SYM", &config, mode, &["--checks-only"]);

    assert_eq!(output.status.code(), Some(exit), "{output:?}");
    assert_eq!(consumed, source.as_bytes(), "source must stay unchanged");
    if mode == "json" {
        let records: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0]["severity"], severity);
        assert_eq!(records[0]["code"], "SYM");
    } else {
        assert!(output.stdout.is_empty());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains(&format!("{severity}[SYM]")), "{stderr}");
    }
}

/// `--checks-only` preserves configured `exclude` groups: an excluded lint
/// stays off without `--include`.
#[test]
fn checks_only_should_preserve_configured_exclude() {
    let config = "perf_hints: []\n\
                  symbol_rules: [{regex: needle, title: Review, message: finding, severity: error}]\n\
                  exclude:\n  - paths: [input.rs]\n    rules: [SYM]";
    let (output, _) = preview_with_flags(
        "//! Module docs.\nfn example() { needle(); }\n",
        "",
        config,
        "text",
        &["--checks-only"],
    );

    assert_eq!(output.status.code(), Some(0), "{output:?}");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(!stderr.contains("SYM"), "{stderr}");
}

/// `--checks-only` preserves the configured include file scope: files
/// matching no whitelist group run nothing.
#[test]
fn checks_only_should_preserve_configured_include_paths() {
    let config = "include:\n  - paths: [config.yml]\n    rules: [lints]";
    let (output, _) =
        preview_with_flags("fn example() {}\n", "", config, "text", &["--checks-only"]);

    assert_eq!(output.status.code(), Some(0), "{output:?}");
    assert!(output.stderr.is_empty(), "no findings expected");
}

/// `--checks-only` without `--include` preserves a configured `include`
/// whitelist: only the whitelisted lint runs.
#[test]
fn checks_only_should_preserve_configured_include_rules() {
    let config = "perf_hints: []\n\
                  symbol_rules: [{regex: needle, title: Review, message: finding, severity: error}]\n\
                  include:\n  - paths: [input.rs]\n    rules: [SYM]";
    let (output, _) = preview_with_flags(
        "//! Module docs.\npub fn example() { needle(); }\n",
        "",
        config,
        "text",
        &["--checks-only"],
    );

    assert_eq!(output.status.code(), Some(1), "{output:?}");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("error[SYM]"), "{stderr}");
    assert!(!stderr.contains("DOC001"), "{stderr}");
}

/// `--checks-only` still rejects unknown rule selections.
#[test]
fn checks_only_should_reject_unknown_rules() {
    let (output, _) = preview_with_flags(
        "//! Module docs.\nfn example() {}\n",
        "not_a_rule",
        "{}",
        "text",
        &["--checks-only"],
    );

    assert_eq!(output.status.code(), Some(1), "{output:?}");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("unknown op/rule"), "{stderr}");
}

/// Without `--include` or a configured selection, `--checks-only` still runs
/// the default lints.
#[test]
fn checks_only_should_run_default_lints_without_include() {
    let (output, _) = preview_with_flags("fn example() {}\n", "", "{}", "text", &["--checks-only"]);

    assert_eq!(output.status.code(), Some(1), "{output:?}");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("DOC009"), "{stderr}");
}

/// `--checks-only` suppresses transform ops even when explicitly included:
/// the run stays read-only, reports no change records, and exits 0.
#[rstest]
#[case::reorder("//! Module docs.\nfn a() {}\nfn b() { a(); }\n", "reorder")]
#[case::visibility("//! Module docs.\npub(crate) mod m { pub fn f() {} }\n", "vis")]
#[case::links(
    "//! Module docs.\n/// See [A](https://example.invalid).\npub struct A;\n",
    "links"
)]
#[case::tables(
    "//! Module docs.\n/// | Name | Value | Description |\n/// | --- | --- | --- |\n/// | a | 1 | first |\n/// | longname | 200 | second item |\npub fn documented() {}\n",
    "tables"
)]
#[case::fences(
    "//! Module docs.\n/// Nested example:\n/// ```text\n/// ```rust\n/// tidy();\n/// ```\n/// ```\npub fn documented() {}\n",
    "fences"
)]
fn checks_only_should_suppress_included_transforms(#[case] source: &str, #[case] op: &str) {
    let (output, consumed) = preview_with_flags(source, op, "{}", "text", &["--checks-only"]);

    assert_eq!(output.status.code(), Some(0), "{output:?}");
    assert_eq!(consumed, source.as_bytes(), "source must stay unchanged");
    assert!(output.stdout.is_empty());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("success["),
        "suppressed {op} must not report change records: {stderr}"
    );
}

/// A whitelisted transform op stays suppressed under `--checks-only` while
/// whitelisted lints still run.
#[test]
fn checks_only_should_suppress_whitelisted_transforms() {
    let config = "include:\n  - paths: [input.rs]\n    rules: [links, DOC001]";
    let source =
        "//! Module docs.\n/// See [A](https://example.invalid).\npub fn documented() {}\n";
    let (output, consumed) = preview_with_flags(source, "", config, "text", &["--checks-only"]);

    assert_eq!(output.status.code(), Some(0), "{output:?}");
    assert_eq!(consumed, source.as_bytes());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(!stderr.contains("success["), "links must not run: {stderr}");
}

// ── --dry-run ─────────────────────────────────────────────────────

/// Findings retain their severity-based exit status in a non-mutating preview.
#[rstest]
#[case::warning("warning", 0)]
#[case::hint("hint", 0)]
#[case::reminder("reminder", 0)]
#[case::error("error", 1)]
fn dry_run_should_fail_only_for_error_findings(
    #[case] severity: &str,
    #[values("text", "json")] mode: &str,
    #[case] exit: i32,
) {
    let source = "fn f() { needle(); }\n";
    let config = format!(
        "perf_hints: []\nsymbol_rules: [{{regex: needle, title: Review, message: finding, severity: {severity}}}]"
    );

    let (output, consumed) = preview(source, "SYM", &config, mode);

    assert_eq!(output.status.code(), Some(exit), "{output:?}");
    assert_eq!(consumed, source.as_bytes());
    if mode == "json" {
        let records: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0]["severity"], severity);
        assert_eq!(records[0]["code"], "SYM");
    } else {
        assert!(output.stdout.is_empty());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains(&format!("{severity}[SYM]")), "{stderr}");
    }
}

// ── CLI behavior tests ────────────────────────────────────────────

#[test]
fn dry_run_should_preserve_lint_failure_when_edits_are_needed() {
    let source = "pub fn a() {}\npub fn b() { a(); }\n";

    let (output, consumed) = preview(source, "reorder,DOC001", "{}", "json");

    assert_eq!(output.status.code(), Some(1));
    assert_eq!(consumed, source.as_bytes());
    assert!(String::from_utf8_lossy(&output.stderr).contains("found 2 error(s)"));
    let records: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).unwrap();
    assert!(records.iter().any(|record| record["severity"] == "success"));
}

/// Preview reports needed edits or processing failures without writing source.
#[rstest]
#[case::clean("fn a() {}\n", "reorder", 0, None, None)]
#[case::empty("", "reorder", 0, None, None)]
#[case::reorder(
    "fn a() {}\nfn b() { a(); }\n",
    "reorder",
    1,
    Some("REORDER"),
    Some("dry-run found proposed transformations")
)]
#[case::crlf_reorder(
    "fn a() {}\r\nfn b() { a(); }\r\n",
    "reorder",
    1,
    Some("REORDER"),
    Some("dry-run found proposed transformations")
)]
#[case::visibility(
    "pub(crate) mod m { pub fn f() {} }\n",
    "vis",
    1,
    Some("VIS"),
    Some("dry-run found proposed transformations")
)]
#[case::links(
    "/// See [A](https://example.invalid).\npub struct A;\n",
    "links",
    1,
    Some("FIX"),
    Some("dry-run found proposed transformations")
)]
#[case::parse_failure(
    "not valid rust {{{",
    "reorder",
    1,
    None,
    Some("failed to process 1 file(s)")
)]
fn dry_run_should_report_status_without_writing_source(
    #[case] source: &str,
    #[case] rule: &str,
    #[values("text", "json")] mode: &str,
    #[case] exit: i32,
    #[case] change_code: Option<&str>,
    #[case] failure: Option<&str>,
) {
    let (output, consumed) = preview(source, rule, "{}", mode);

    assert_eq!(output.status.code(), Some(exit), "{output:?}");
    assert_eq!(consumed, source.as_bytes());
    let stderr = String::from_utf8_lossy(&output.stderr);
    if let Some(failure) = failure {
        assert!(stderr.contains(failure), "{stderr}");
    }
    if mode == "json" {
        let records: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).unwrap();
        assert_eq!(records.len(), usize::from(change_code.is_some()));
        if let Some(code) = change_code {
            assert_eq!(records[0]["code"], code);
            assert_eq!(records[0]["severity"], "success");
        }
    } else {
        assert!(output.stdout.is_empty());
        if let Some(code) = change_code {
            assert!(stderr.contains(&format!("success[{code}]")), "{stderr}");
            assert!(!stderr.contains(source), "must not echo source: {stderr}");
        }
    }
}

/// An empty directory is accepted and produces no output.
#[test]
fn empty_directory_should_run_cleanly() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();

    let (stdout, stderr, exit) = run_dir(&dir, &[]);
    let _ = fs::remove_dir_all(&dir);

    assert_eq!(exit, 0, "empty directory should exit successfully");
    assert!(
        stdout.is_empty(),
        "stdout should be empty for empty directory"
    );
    assert!(stderr.is_empty(), "stderr should be empty on success");
}

/// Safety check: parse-invalid source must cause an error exit.
/// We verify that rust-llm-tidy exits non-zero when given a non-Rust file.
#[test]
fn invalid_source_should_abort_with_error() {
    let source = "not valid rust {{{";

    let (_stdout, stderr, exit) = run(source, &[]);

    assert_ne!(exit, 0, "rust-llm-tidy should exit non-zero on parse error");
    assert!(!stderr.is_empty(), "stderr should contain error message");
}

/// A non-existent path is rejected with an error exit.
#[test]
fn nonexistent_path_should_fail_with_error() {
    let nonexistent = std::env::temp_dir().join(format!(
        "rust-llm-tidy-missing-{}-{}-{}-{}-{}-{}-{}-{}-{}.rs",
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id(),
        std::process::id()
    ));

    let output = run_command(&["--include", "reorder"], &nonexistent);
    assert!(
        !output.status.success(),
        "non-existent path should exit non-zero"
    );
    assert!(
        !String::from_utf8_lossy(&output.stderr).is_empty(),
        "stderr should report the missing path"
    );
}

/// `--version` prints the CLI package version for release identification.
#[test]
fn version_should_print_package_version() {
    let output = Command::new(binary()).arg("--version").output().unwrap();

    assert_eq!(output.status.code(), Some(0), "{output:?}");
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        format!("rust-llm-tidy {}", env!("CARGO_PKG_VERSION"))
    );
}

/// Run a preview with explicit config and no inherited baseline, retaining bytes.
fn preview(source: &str, rule: &str, config: &str, mode: &str) -> (Output, Vec<u8>) {
    preview_with_flags(source, rule, config, mode, &["--dry-run"])
}

/// Run with explicit config plus extra flags (e.g. `--checks-only`) and no
/// inherited baseline, retaining the consumed bytes.
fn preview_with_flags(
    source: &str,
    rule: &str,
    config: &str,
    mode: &str,
    flags: &[&str],
) -> (Output, Vec<u8>) {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("input.rs");
    fs::write(&path, source).unwrap();
    fs::write(directory.path().join("config.yml"), config).unwrap();

    let output = Command::new(binary())
        .current_dir(directory.path())
        .env_remove("RUST_LLM_TIDY_DIFF_BASE")
        .args([
            "--config",
            "config.yml",
            "--all-lines",
            "--output-mode",
            mode,
            "input.rs",
        ])
        .args(
            rule.split(',')
                .filter(|rule| !rule.is_empty())
                .flat_map(|rule| ["--include", rule]),
        )
        .args(flags)
        .output()
        .unwrap();

    (output, fs::read(path).unwrap())
}