rust-llm-tidy-cli 0.3.0

CLI for reordering and linting Rust source code. Intended for LLM use.
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
//! Integration tests for `rust-llm-tidy` CLI.
//!
//! Tests are split into two groups:
//!
//! 1. Synthetic fixture tests (`tests/fixtures/reorder/<lang>/*_before.<ext>`
//!    → `*_after.<ext>`): one test per ordering/spacing rule, for `rust`
//!    and `csharp`.  Each fixture's header comment documents the rule and
//!    the expected before/after state.
//!
//! 2. CLI behavior tests: dry-run, in-place writes, directory traversal,
//!    error handling, and idempotency.

use core::sync::atomic::{AtomicU32, Ordering};
use std::fs;
use std::process::Command;

/// Run `rust-llm-tidy --include reorder --dry-run` against
/// `<name>_before.<ext>` in `tests/fixtures/reorder/<lang>/`.
///
/// Returns `(stdout, stderr, exit, before_path, expected_after_content)`.
macro_rules! run_fixture {
    ($lang:ident, $ext:literal, $name:ident) => {{
        let fixture_dir = manifest_dir()
            .join("tests")
            .join("fixtures")
            .join("reorder")
            .join(stringify!($lang));
        let before_path = fixture_dir.join(concat!(stringify!($name), "_before.", $ext));
        let expected_after = include_str!(concat!(
            "fixtures/reorder/",
            stringify!($lang),
            "/",
            stringify!($name),
            "_after.",
            $ext
        ))
        .to_string();

        let (stdout, stderr, exit) = run_dry_run(&before_path);

        (stdout, stderr, exit, before_path, expected_after)
    }};
}

/// Declare a fixture test.  The test name is the fixture rule name.
///
/// Dry-run reports change records on stderr (allowing zero records for an
/// already-tidy fixture) and keeps stdout empty. Byte-for-byte "produces
/// _after" coverage is preserved by re-ordering a temp copy in place and
/// comparing its content.
macro_rules! synthetic_fixture {
    ($lang:ident, $ext:literal, $name:ident) => {
        #[test]
        fn $name() {
            let (stdout, stderr, exit, before_path, expected_after) =
                run_fixture!($lang, $ext, $name);
            assert_eq!(
                exit, 0,
                concat!(stringify!($name), " dry-run should succeed")
            );
            assert!(
                stdout.is_empty(),
                concat!(
                    stringify!($name),
                    " dry-run must not print reconstructed source to stdout"
                )
            );
            // Any stderr output from a reorder dry-run must be change records,
            // never reconstructed source (which would lack the op marker).
            for line in stderr.lines() {
                assert!(
                    line.contains("success[REORDER]"),
                    "{} dry-run stderr must only carry change records: {}",
                    stringify!($name),
                    line
                );
            }
            // In-place reorder still produces the _after fixture byte-for-byte.
            assert_eq!(
                reorder_in_place(&before_path, $ext),
                expected_after,
                concat!(
                    stringify!($name),
                    " fixture: in-place reorder must match _after.",
                    $ext
                )
            );
        }
    };
}

// ── Synthetic fixture tests: one per rule ─────────────────────────

synthetic_fixture!(rust, "rs", phase_extern_crate_stable);

synthetic_fixture!(rust, "rs", phase_other_stable);

synthetic_fixture!(rust, "rs", phase_use_stable);

synthetic_fixture!(rust, "rs", phase_mod_non_test_stable);

synthetic_fixture!(rust, "rs", phase_macro_alphabetical);

synthetic_fixture!(rust, "rs", phase_macro_dependency);

synthetic_fixture!(rust, "rs", phase_macro_invocation_after_def);

synthetic_fixture!(rust, "rs", phase_const_static_alphabetical);

synthetic_fixture!(rust, "rs", phase_const_static_dependency);

synthetic_fixture!(rust, "rs", phase_type_alphabetical);

synthetic_fixture!(rust, "rs", phase_type_dependency);

synthetic_fixture!(rust, "rs", phase_trait_alphabetical);

synthetic_fixture!(rust, "rs", phase_trait_dependency);

synthetic_fixture!(rust, "rs", phase_impl_inherent_before_trait);

synthetic_fixture!(rust, "rs", phase_impl_after_matching_type);

synthetic_fixture!(rust, "rs", phase_impl_orphan_stable);

synthetic_fixture!(rust, "rs", fn_visibility_groups);

synthetic_fixture!(rust, "rs", fn_main_first);

synthetic_fixture!(rust, "rs", fn_callers_before_callees);

synthetic_fixture!(rust, "rs", fn_alphabetical_tie_break);

synthetic_fixture!(rust, "rs", fn_mutual_recursion_contiguous);

synthetic_fixture!(rust, "rs", cfg_test_mod_last_stable);

synthetic_fixture!(rust, "rs", mod_file_decl_stays_in_phase);

synthetic_fixture!(rust, "rs", preamble_preserved);

synthetic_fixture!(rust, "rs", trailer_preserved);

synthetic_fixture!(rust, "rs", fn_interstitial_comment_travels_with_next);

synthetic_fixture!(rust, "rs", docs_attrs_travel);

synthetic_fixture!(rust, "rs", spacing_compact_use_mod_const_static);

synthetic_fixture!(rust, "rs", spacing_blank_line_between_phases);

synthetic_fixture!(rust, "rs", spacing_blank_line_fn_visibility);

synthetic_fixture!(rust, "rs", safety_line_preservation);

// ── Synthetic C# fixture tests: one per rule ──────────────────────

synthetic_fixture!(csharp, "cs", usings_hoist_above_types);

synthetic_fixture!(csharp, "cs", usings_keep_source_order);

synthetic_fixture!(csharp, "cs", top_level_types_keep_source_order);

synthetic_fixture!(csharp, "cs", member_buckets_reorder);

synthetic_fixture!(csharp, "cs", member_fields_keep_source_order);

synthetic_fixture!(csharp, "cs", member_delegates_events_keep_source_order);

synthetic_fixture!(csharp, "cs", member_enums_nested_types_keep_source_order);

synthetic_fixture!(csharp, "cs", member_properties_indexers_keep_source_order);

synthetic_fixture!(csharp, "cs", member_callers_before_callees);

synthetic_fixture!(csharp, "cs", member_methods_keep_source_order);

synthetic_fixture!(csharp, "cs", member_mutual_recursion_contiguous);

synthetic_fixture!(csharp, "cs", member_docs_attributes_travel);

synthetic_fixture!(csharp, "cs", member_leading_comment_travels);

synthetic_fixture!(csharp, "cs", member_compact_spacing_stays_compact);

synthetic_fixture!(csharp, "cs", nested_type_moves_whole);

synthetic_fixture!(csharp, "cs", namespace_usings_pin_first);

synthetic_fixture!(csharp, "cs", header_comment_preserved);

synthetic_fixture!(csharp, "cs", footer_comment_preserved);

synthetic_fixture!(csharp, "cs", spacing_usings_compact_types_separated);

// ── Idempotency: every _after fixture must be unchanged ───────────

static TEST_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Idempotency: every `_after.*` fixture, in every language directory
/// under `tests/fixtures/reorder/`, must be unchanged by a second run.
#[test]
fn all_after_fixtures_should_be_idempotent_on_rerun() {
    let fixture_root = manifest_dir()
        .join("tests")
        .join("fixtures")
        .join("reorder");
    // Each language directory under the fixture root holds its own
    // `<name>_after.<ext>` fixture pairs.
    let mut after_files: Vec<std::path::PathBuf> = Vec::new();
    for lang in fs::read_dir(&fixture_root).unwrap() {
        let lang_dir = lang.unwrap().path().read_dir().unwrap();
        for entry in lang_dir {
            let path = entry.unwrap().path();
            let is_after = path
                .file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|name| name.contains("_after."));
            if is_after {
                after_files.push(path);
            }
        }
    }
    after_files.sort();

    assert!(!after_files.is_empty(), "no _after fixtures found");

    for after_path in &after_files {
        let (stdout, stderr, exit) = run_dry_run(after_path);

        assert_eq!(exit, 0, "{} dry-run should succeed", after_path.display());
        assert!(
            stdout.is_empty(),
            "{} dry-run must not print source to stdout",
            after_path.display()
        );
        assert!(
            stderr.is_empty(),
            "{} is already tidy: dry-run must emit zero change records",
            after_path.display()
        );
    }
}

/// An in-place reorder of `reorder_cs_before.cs` writes the `_after`
/// fixture byte-for-byte: members land in the profile order, the caller
/// precedes its callee, and the trailing `using` hoists to the pinned
/// using block.
#[test]
fn csharp_member_reorder_matches_after_fixture() {
    let before = csharp_reorder_fixture_dir().join("reorder_cs_before.cs");
    let expected_after =
        fs::read_to_string(csharp_reorder_fixture_dir().join("reorder_cs_after.cs")).unwrap();
    let tmp = temp_file_ext("cs");
    fs::write(&tmp, fs::read_to_string(&before).unwrap()).unwrap();

    let output = run_command(&["--include", "reorder"], &tmp);
    assert!(
        output.status.success(),
        "C# reorder should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let actual = fs::read_to_string(&tmp).unwrap();
    let _ = fs::remove_file(&tmp);
    assert_eq!(
        actual, expected_after,
        "in-place C# reorder must match reorder_cs_after.cs"
    );
}

/// A pure-CRLF `.cs` source still reorders - the accept side of the guard
/// that declines lone-`\r` sources: the field hoists above the method with
/// every newline still part of a `\r\n` pair, and a second run emits zero
/// records.
#[test]
fn csharp_reorder_on_pure_crlf_source_preserves_the_endings() {
    let source = "class C\r\n{\r\n    void M() {}\r\n    int F;\r\n}\r\n";
    let tmp = temp_file_ext("cs");
    fs::write(&tmp, source).unwrap();

    let output = run_command(&["--include", "reorder"], &tmp);
    assert!(
        output.status.success(),
        "CRLF C# reorder should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let after = fs::read_to_string(&tmp).unwrap();
    assert!(
        after.find("int F;").unwrap() < after.find("void M()").unwrap(),
        "fields must precede methods under the C# profile: {after:?}"
    );
    assert_eq!(
        after.matches('\n').count(),
        after.matches("\r\n").count(),
        "every newline must stay CRLF after the reorder: {after:?}"
    );

    let dry = run_command(&["--include", "reorder", "--dry-run"], &tmp);
    let _ = fs::remove_file(&tmp);
    assert!(dry.status.success(), "second run should succeed");
    assert!(
        String::from_utf8_lossy(&dry.stderr).is_empty(),
        "second run on the CRLF rewrite must emit zero records"
    );
}

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

/// `--dry-run` reports the would-be reorder move on stderr without printing
/// the reconstructed source to stdout or modifying the file on disk.
#[test]
fn dry_run_should_not_write_files() {
    let source = "fn a() {}\nfn b() { a(); }\n";

    let (stdout, stderr, exit) = run(source, &["--dry-run"]);

    assert_eq!(exit, 0, "dry-run should succeed");
    assert!(stdout.is_empty(), "stdout must be empty on dry-run success");
    assert!(
        stderr.contains("success[REORDER]"),
        "stderr should report the reorder move as a change line: {stderr}"
    );
    assert!(
        !stderr.contains("fn a()"),
        "stderr must not echo reconstructed 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");
}

/// `--extension` allows an extra extension additively: the file runs the
/// tables-only unmapped profile, and without the flag the
/// same file is a silent skip.
#[test]
fn extension_flag_allows_unmapped_extension_tables_only() {
    let source = "| a | b |\n| --- | --- |\n| 1 | 22 |\n";

    // Without the flag the explicit file is a silent skip.
    let file = temp_file_ext("org");
    fs::write(&file, source).unwrap();
    let out = run_command(&["--json"], &file);
    assert!(out.status.success());
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "[]");
    assert_eq!(fs::read_to_string(&file).unwrap(), source);
    let _ = fs::remove_file(&file);

    // With the flag the file is allowed and its GFM table aligns.
    let file = temp_file_ext("org");
    fs::write(&file, source).unwrap();
    let out = run_command(&["--extension", "org"], &file);
    assert!(
        out.status.success(),
        "--extension org should allow the file: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("tables were aligned"),
        "the unmapped profile must run tables: {stderr}"
    );
    assert_ne!(fs::read_to_string(&file).unwrap(), source);
    let _ = fs::remove_file(&file);
}

/// Malformed `--extension` values fail the run with a non-zero exit.
#[test]
fn extension_flag_rejects_malformed_values() {
    let file = temp_file_ext("rs");
    fs::write(&file, "fn a() {}\n").unwrap();
    for bad in [".rs", "a.md", "src/rs", ""] {
        let out = run_command(&["--extension", bad], &file);
        assert!(
            !out.status.success(),
            "--extension `{bad}` must fail the run"
        );
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(
            stderr.contains("invalid extension"),
            "stderr should name the bad extension: {stderr}"
        );
    }
    let _ = fs::remove_file(&file);
}

/// In-place write: copy a synthetic before fixture to a temp file, run without
/// `--dry-run`, and verify the file content matches the after fixture.
#[test]
fn in_place_write_should_match_after_fixture() {
    let expected = include_str!("fixtures/reorder/rust/phase_use_stable_after.rs");

    let dir = std::env::temp_dir();
    let pid = std::process::id();
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let tmp = dir.join(format!("rust-llm-tidy-write-test-{}-{}.rs", pid, seq));

    fs::write(
        &tmp,
        include_str!("fixtures/reorder/rust/phase_use_stable_before.rs"),
    )
    .unwrap();

    let output = run_command(&["--include", "reorder"], &tmp);
    assert!(
        output.status.success(),
        "rust-llm-tidy (no --dry-run) failed"
    );

    let actual = fs::read_to_string(&tmp).unwrap();
    let _ = fs::remove_file(&tmp);

    assert_eq!(
        actual, expected,
        "in-place write: temp file content must match phase_use_stable_after.rs"
    );
}

/// `--include fences` reaches a code language: the nested fence inside
/// `#` comments flips its inner backtick delimiter to a tilde.
#[test]
fn include_fences_flips_the_nested_fence_in_hash_comments() {
    let source = "# ```text\n# ```rust\n# inner\n# ```\n# ```\n";
    let file = temp_file_ext("py");
    fs::write(&file, source).unwrap();
    let out = run_command(&["--include", "fences"], &file);
    assert!(
        out.status.success(),
        "--include fences on .py should succeed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let after = fs::read_to_string(&file).unwrap();
    let _ = fs::remove_file(&file);
    assert!(
        after.contains("# ~~~rust"),
        "inner fence must flip under --include fences: {after}"
    );
}

/// 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");
}

// ── Language tiers ────────────────────────────────────────────────

/// Markdown-family siblings (`.markdown`, `.txt`, `.text`, `.mdx`, and the
/// uppercase `.TXT` variant) behave exactly like `.md` on identical input:
/// same fixed bytes, same stderr records and lint findings, same exit code.
#[test]
fn markdown_family_siblings_match_md_behavior() {
    // Exercises every markdown-family op plus a text lint: a misaligned
    // table, a nested fence, a repeated inline link, and an over-limit
    // line.
    let source = "\
| Name | Value |
| --- | --- |
| a | 1 |
| longname | 200 |

```text
```rust
inner
```
```

see [A](http://example.com/long) and [A](http://example.com/long)

this line is deliberately made far longer than eighty characters so the line-length lint fires
";

    let md = temp_file_ext("md");
    fs::write(&md, source).unwrap();
    let md_out = run_command(&[], &md);
    let md_bytes = fs::read_to_string(&md).unwrap();
    let md_exit = md_out.status.code().unwrap_or(-1);
    let md_stderr = strip_path_prefix(&String::from_utf8_lossy(&md_out.stderr), &md);

    // The .md baseline itself must be non-trivial: fixes applied and the
    // line-length finding reported, or the parity check below proves
    // nothing.
    assert_eq!(md_exit, 0, "md baseline should succeed");
    assert!(
        md_stderr.contains("success[FIX]"),
        "md baseline: {md_stderr}"
    );
    assert!(
        md_stderr.contains("TEXT002"),
        "md baseline lints: {md_stderr}"
    );
    assert_ne!(md_bytes, source, "md baseline must change the file");

    for ext in ["markdown", "txt", "TXT", "text", "mdx"] {
        let file = temp_file_ext(ext);
        fs::write(&file, source).unwrap();
        let out = run_command(&[], &file);

        assert_eq!(
            out.status.code().unwrap_or(-1),
            md_exit,
            ".{ext} exit must match .md"
        );
        assert_eq!(
            fs::read_to_string(&file).unwrap(),
            md_bytes,
            ".{ext} fixed bytes must match .md"
        );
        let stderr = strip_path_prefix(&String::from_utf8_lossy(&out.stderr), &file);
        assert_eq!(stderr, md_stderr, ".{ext} stderr must match .md");
        let _ = fs::remove_file(&file);
    }
    let _ = fs::remove_file(&md);
}

/// 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"
    );
}

/// Directory recursion collects `README.MD`/`lib.RS` case variants and
/// excludes extensions outside the default set (like `notes.org`).
#[test]
fn recursive_dir_collects_uppercase_variants_excludes_others() {
    let dir = temp_dir();
    let nested = dir.join("src");
    fs::create_dir_all(&nested).unwrap();
    fs::write(nested.join("lib.RS"), "fn a() {}\nfn b() { a(); }\n").unwrap();
    fs::write(
        dir.join("README.MD"),
        "| Name | Value | Description |\n| --- | --- | --- |\n| a | 1 | first |\n| longname | 200 | second item |\n",
    )
    .unwrap();
    fs::write(dir.join("notes.org"), "not allowed by default\n").unwrap();

    // Rust-only reorder runs on the nested `.RS` and reports it by path.
    let (_stdout, stderr, exit) = run_dir(&dir, &["--dry-run"]);
    assert_eq!(exit, 0, "dir with .RS/.MD/.org should succeed");
    assert!(
        stderr.contains("lib.RS"),
        "recursion must collect and process lib.RS: {stderr}"
    );

    // Markdown table fix runs on the `.MD` and reports it by path.
    let (_stdout, md_stderr, md_exit) = run_dir(&dir, &["--include", "tables", "--dry-run"]);
    assert_eq!(md_exit, 0, "tables dry-run on dir should succeed");
    assert!(
        md_stderr.contains("README.MD") && md_stderr.contains("success[FIX]"),
        "recursion must collect and process README.MD: {md_stderr}"
    );
    assert!(
        !md_stderr.contains("notes.org") && !stderr.contains("notes.org"),
        "notes.org must be excluded silently"
    );
    let _ = fs::remove_dir_all(&dir);
}

/// `--dry-run` on a directory reports each file's moves as path-labeled change
/// lines on stderr, leaving stdout empty and the files unmodified.
#[test]
fn recursive_directory_dry_run_should_label_each_move_with_path() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();

    let file_a = dir.join("a.rs");
    let file_b = dir.join("b.rs");

    fs::write(&file_a, "fn a() {}\nfn b() { a(); }\n").unwrap();
    fs::write(&file_b, "fn c() {}\nfn d() { c(); }\n").unwrap();

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

    assert_eq!(exit, 0, "dry-run on directory should succeed");
    assert!(stdout.is_empty(), "stdout should be empty on success");
    assert!(
        stderr.contains("a.rs:") && stderr.contains("b.rs:"),
        "multi-file dry-run must label each change line with its path: {stderr}"
    );
    assert!(
        stderr.contains("rearrange fn b from pos 2 to pos 1")
            && stderr.contains("rearrange fn d from pos 2 to pos 1"),
        "directory dry-run should report each file's move on stderr: {stderr}"
    );
}

/// If a directory contains a valid file and an invalid file, the valid file is
/// still reordered and the operation exits non-zero.
#[test]
fn recursive_directory_error_should_still_reorder_valid_file() {
    let dir = temp_dir();
    fs::create_dir(&dir).unwrap();

    let good = dir.join("good.rs");
    let bad = dir.join("bad.rs");

    fs::write(&good, "fn a() {}\nfn b() { a(); }\n").unwrap();
    fs::write(&bad, "not valid rust {{{").unwrap();

    let (_stdout, stderr, exit) = run_dir(&dir, &[]);

    let actual_good = fs::read_to_string(&good).unwrap();
    let _ = fs::remove_dir_all(&dir);

    assert_ne!(exit, 0, "directory with invalid file should exit non-zero");
    assert!(
        !stderr.is_empty(),
        "stderr should contain error message for invalid file"
    );

    let a_pos = actual_good.find("fn a").expect("fn a missing");
    let b_pos = actual_good.find("fn b").expect("fn b missing");
    assert!(
        b_pos < a_pos,
        "valid file should still be reordered despite sibling error"
    );
}

/// A directory is processed recursively, reordering every `.rs` file.
#[test]
fn recursive_directory_should_reorder_every_rs_file() {
    let dir = temp_dir();
    let root_file = dir.join("phase_use.rs");
    let nested_dir = dir.join("utils");
    let nested_file = nested_dir.join("phase_mod.rs");

    fs::create_dir_all(&nested_dir).unwrap();
    fs::write(
        &root_file,
        include_str!("fixtures/reorder/rust/phase_use_stable_before.rs"),
    )
    .unwrap();
    fs::write(
        &nested_file,
        include_str!("fixtures/reorder/rust/phase_mod_non_test_stable_before.rs"),
    )
    .unwrap();

    let output = run_command(&["--include", "reorder"], &dir);
    assert!(
        output.status.success(),
        "directory run failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let expected_root = include_str!("fixtures/reorder/rust/phase_use_stable_after.rs");
    let expected_nested = include_str!("fixtures/reorder/rust/phase_mod_non_test_stable_after.rs");
    let actual_root = fs::read_to_string(&root_file).unwrap();
    let actual_nested = fs::read_to_string(&nested_file).unwrap();

    let _ = fs::remove_dir_all(&dir);

    assert_eq!(
        actual_root, expected_root,
        "phase_use.rs should be reordered in place"
    );
    assert_eq!(
        actual_nested, expected_nested,
        "utils/phase_mod.rs should be reordered in place"
    );
}

/// `reorder --dry-run` on a CRLF reordering source reports its move on stderr
/// and leaves stdout empty (no reconstructed source).
#[test]
fn reorder_dry_run_reports_change_with_empty_stdout() {
    let source = "fn a() {}\r\nfn b() { a(); }\r\n";
    let (stdout, stderr, exit) = run(source, &["--dry-run"]);
    assert_eq!(exit, 0, "dry-run should succeed");
    assert!(stdout.is_empty(), "dry-run must not print source to stdout");
    assert!(
        stderr.contains("success[REORDER]"),
        "dry-run must report a reorder change on stderr: {stderr:?}"
    );
}

/// Reorder a temp copy of `path` (keeping the given extension, which
/// selects the language backend) in place and return the rewritten content.
///
/// Preserves the byte-for-byte "produces the _after fixture" coverage while
/// dry-run keeps stdout empty.
fn reorder_in_place(path: &std::path::Path, ext: &str) -> String {
    let tmp = temp_file_ext(ext);
    fs::copy(path, &tmp).unwrap();
    let output = run_command(&["--include", "reorder"], &tmp);
    assert!(
        output.status.success(),
        "in-place reorder failed on {}: {}",
        path.display(),
        String::from_utf8_lossy(&output.stderr)
    );
    let result = fs::read_to_string(&tmp).unwrap();
    let _ = fs::remove_file(&tmp);
    result
}

/// In-place reorder of a CRLF source preserves every `\r\n` and reorders
/// callers before callees. CRLF input is built in-memory (not from a
/// committed fixture, which git would normalize on checkout).
#[test]
fn reorder_in_place_preserves_crlf() {
    let source = "fn b() { a(); }\r\nfn a() {}\r\n";
    let result = run_and_read(source);

    // Caller (b) before callee (a).
    let a_pos = result.find("fn a").expect("fn a missing");
    let b_pos = result.find("fn b").expect("fn b missing");
    assert!(b_pos < a_pos, "b (caller) before a (callee)");

    // Every `\n` must be part of `\r\n` (no CRLF -> LF flip).
    assert_eq!(
        result.matches('\n').count(),
        result.matches("\r\n").count(),
        "every newline must be CRLF after reorder: {result:?}"
    );
}

/// In-place reorder both writes the reordered file and reports the move as a
/// change line on stderr, mirroring the records a dry-run would have previewed.
#[test]
fn reorder_in_place_reports_change_and_writes() {
    let fixture = manifest_dir()
        .join("tests")
        .join("fixtures")
        .join("reorder")
        .join("rust");
    let expected =
        fs::read_to_string(fixture.join("fn_interstitial_comment_travels_with_next_after.rs"))
            .unwrap();
    let tmp = temp_file();
    fs::copy(
        fixture.join("fn_interstitial_comment_travels_with_next_before.rs"),
        &tmp,
    )
    .unwrap();

    let output = run_command(&["--include", "reorder"], &tmp);
    assert!(
        output.status.success(),
        "in-place reorder on a moving fixture should succeed"
    );

    let actual = fs::read_to_string(&tmp).unwrap();
    let _ = fs::remove_file(&tmp);
    assert_eq!(
        actual, expected,
        "in-place reorder must write the after fixture"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("success[REORDER]"),
        "in-place reorder must also report its change line on stderr: {stderr}"
    );
}

/// Realistic file: struct, impl, use, and multiple fns.
/// Tests that multi-phase ordering keeps use first, struct+impl together, and
/// callers before callees.
#[test]
fn reorder_real_file_should_keep_phase_and_caller_order() {
    let source = "\
use std::fmt;\n\n\
pub struct Config {\n\
    pub name: String,\n}\n\n\
impl Config {\n\
    pub fn new(name: &str) -> Self {\n\
        Config {\n\
            name: name.to_string(),\n\
        }\n\
    }\n}\n\n\
fn validate(c: &Config) -> bool {\n\
    !c.name.is_empty()\n}\n\n\
pub fn build(name: &str) -> Option<Config> {\n\
    let c = Config::new(name);\n\
    if validate(&c) {\n\
        Some(c)\n\
    } else {\n\
        None\n\
    }\n}\n";

    let result = run_and_read(source);

    let use_pos = result.find("use std::fmt").unwrap();
    let struct_pos = result.find("pub struct Config").unwrap();
    let impl_pos = result.find("impl Config").unwrap();
    let build_pos = result.find("pub fn build").unwrap();
    let validate_pos = result.find("fn validate").unwrap();

    assert!(use_pos < struct_pos, "use before struct");
    assert!(struct_pos < impl_pos, "struct before its impl");
    assert!(
        build_pos < validate_pos,
        "build (caller) before validate (callee)"
    );
}

// ── Corpus gate ────────────────────────────────────────────────────

/// The repository corpus gate: a `--dry-run` over this repository's root
/// (repo config active, the same invocation CI's tidy job makes) exits 0
/// and emits zero change records - every tracked file is already tidy.
#[test]
fn repo_corpus_dry_run_emits_zero_change_records() {
    let root = manifest_dir()
        .join("..")
        .join("..")
        .canonicalize()
        .expect("repo root must resolve");
    // Guard against a vacuous pass: only this repository's root holds both
    // the workspace manifest and the tidy config, so the walk below covers
    // real files.
    assert!(root.join("Cargo.toml").is_file());
    assert!(root.join(".rust-llm-tidy.yml").is_file());

    let output = Command::new(binary())
        .current_dir(&root)
        .args(["--dry-run", "."])
        .output()
        .expect("failed to spawn rust-llm-tidy over the repo root");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert_eq!(
        output.status.code(),
        Some(0),
        "corpus dry-run must exit 0: {stderr}"
    );
    assert!(stdout.is_empty(), "dry-run prints nothing to stdout");
    assert!(
        !stderr.contains("success["),
        "the whole repository must emit zero change records: {stderr}"
    );
}

/// An already-sorted file (callers before callees) should be unchanged.
#[test]
fn sorted_file_should_roundtrip_unchanged() {
    let source = "\
fn main() {\n\
    a();\n\
    b();\n}\n\n\
fn a() {\n\
    helper();\n}\n\n\
fn b() {}\n\n\
fn helper() {}\n";

    let result = run_and_read(source);
    let main_pos = result.find("fn main").unwrap();
    let a_pos = result.find("fn a").unwrap();
    let b_pos = result.find("fn b").unwrap();
    let helper_pos = result.find("fn helper").unwrap();

    assert!(main_pos < a_pos, "main before a");
    assert!(a_pos < helper_pos, "a before helper (a calls helper)");
    assert!(b_pos < helper_pos, "b before helper (original order)");
}

/// An explicit `Note.MD` file is allowed, runs markdown fix ops, and
/// never runs the Rust-only reorder op.
#[test]
fn uppercase_md_explicit_file_runs_fix_not_rust_ops() {
    let file = temp_file_ext("MD");
    fs::write(
        &file,
        "| Name | Value | Description |\n| --- | --- | --- |\n| a | 1 | first |\n| longname | 200 | second item |\n",
    )
    .unwrap();

    // Tables (a markdown fix op) run on the `.MD` file.
    let output = run_command(&["--include", "tables"], &file);
    assert!(
        output.status.success(),
        ".MD file should be allowed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("success[FIX]"),
        ".MD must run markdown fix ops: {stderr}"
    );

    // Reorder (a Rust-only op) never runs on a `.MD` file, even when the bytes
    // would reorder as Rust.
    let output = run_command(&["--include", "reorder", "--dry-run"], &file);
    assert!(
        output.status.success(),
        ".MD reorder dry-run should succeed without Rust ops: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        !String::from_utf8_lossy(&output.stderr).contains("success[REORDER]"),
        ".MD must never run the Rust reorder op"
    );
    let _ = fs::remove_file(&file);
}

// ── Case-insensitive allowed extensions ───────

/// An explicit `Foo.RS` file is allowed and runs the Rust reorder op,
/// matching the lowercase `.rs` behavior.
#[test]
fn uppercase_rs_explicit_file_runs_reorder() {
    let file = temp_file_ext("RS");
    fs::write(&file, "fn a() {}\nfn b() { a(); }\n").unwrap();

    let output = run_command(&["--include", "reorder"], &file);
    let _ = fs::remove_file(&file);

    assert!(
        output.status.success(),
        ".RS file should be allowed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("success[REORDER]"),
        ".RS must run the Rust reorder op: {stderr}"
    );
}

// ── C# reorder: member profile + pinned usings ────────────────────

/// The directory holding the C# reorder fixture pair.
fn csharp_reorder_fixture_dir() -> std::path::PathBuf {
    manifest_dir()
        .join("tests")
        .join("fixtures")
        .join("reorder")
        .join("csharp")
}

/// Run rust-llm-tidy on `content` (written to a tempfile) with optional
/// `--dry-run`.
/// Returns (stdout, stderr, exit_code).
fn run(content: &str, args: &[&str]) -> (String, String, i32) {
    let dir = std::env::temp_dir();
    let pid = std::process::id();
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let file = dir.join(format!("rust-llm-tidy-test-{}-{}.rs", pid, seq));
    fs::write(&file, content).unwrap();

    let mut full_args = vec!["--include", "reorder"];
    full_args.extend(args);
    let output = run_command(&full_args, &file);
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let exit = output.status.code().unwrap_or(-1);

    let _ = fs::remove_file(&file);

    (stdout, stderr, exit)
}

/// Read a tempfile after rust-llm-tidy has modified it.
fn run_and_read(content: &str) -> String {
    let dir = std::env::temp_dir();
    let pid = std::process::id();
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let file = dir.join(format!("rust-llm-tidy-test-{}-{}.rs", pid, seq));
    fs::write(&file, content).unwrap();

    let output = run_command(&["--include", "reorder"], &file);
    assert!(
        output.status.success(),
        "rust-llm-tidy failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let result = fs::read_to_string(&file).unwrap();
    let _ = fs::remove_file(&file);
    result
}

/// Run `rust-llm-tidy` on a directory with optional arguments.
fn run_dir(dir: &std::path::Path, args: &[&str]) -> (String, String, i32) {
    let mut full_args = vec!["--include", "reorder"];
    full_args.extend(args);
    let output = run_command(&full_args, dir);
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let exit = output.status.code().unwrap_or(-1);

    (stdout, stderr, exit)
}

/// Run `rust-llm-tidy --include reorder --dry-run` on `path` and return
/// `(stdout, stderr, exit)`.
fn run_dry_run(path: &std::path::Path) -> (String, String, i32) {
    let output = run_command(&["--include", "reorder", "--dry-run"], path);

    assert!(
        output.status.success(),
        "rust-llm-tidy --dry-run failed on {}: {}",
        path.display(),
        String::from_utf8_lossy(&output.stderr)
    );

    (
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.code().unwrap_or(-1),
    )
}

/// Strip the `path:` label from every stderr line so outputs for the same
/// content under different file names compare equal.
fn strip_path_prefix(stderr: &str, path: &std::path::Path) -> String {
    let prefix = format!("{}:", path.display());
    stderr
        .lines()
        .map(|line| line.strip_prefix(&prefix).unwrap_or(line))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Create a numbered temporary directory.
fn temp_dir() -> std::path::PathBuf {
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let pid = std::process::id();
    std::env::temp_dir().join(format!("rust-llm-tidy-dir-{}-{}", pid, seq))
}

/// Create a numbered temporary `.rs` file path for fixture copies that
/// reorder in place.
fn temp_file() -> std::path::PathBuf {
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let pid = std::process::id();
    std::env::temp_dir().join(format!("rust-llm-tidy-file-{}-{}.rs", pid, seq))
}

/// Create a numbered temporary file path with the given extension, for
/// fixture copies whose language the extension selects (`.cs`) and
/// case-sensitivity tests that need `.RS`/`.MD`/`.TXT` (the local
/// `temp_file` is fixed to `.rs`).
fn temp_file_ext(ext: &str) -> std::path::PathBuf {
    let seq = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let pid = std::process::id();
    std::env::temp_dir().join(format!("rust-llm-tidy-ext-{}-{}.{}", pid, seq, ext))
}

// ── Helpers ───────────────────────────────────────────────────────

/// Return `CARGO_MANIFEST_DIR` for resolving fixture paths.
fn manifest_dir() -> std::path::PathBuf {
    std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

/// Build `rust-llm-tidy <args> <path>` and run it, returning captured output.
fn run_command(args: &[&str], path: &std::path::Path) -> std::process::Output {
    let mut cmd = Command::new(binary());
    cmd.args(["--no-config"]).args(args).arg(path);
    cmd.output()
        .unwrap_or_else(|e| panic!("failed to spawn rust-llm-tidy on {}: {e}", path.display()))
}

/// Returns the path to the `rust-llm-tidy` binary for spawning in tests.
///
/// Resolution order:
/// 1. `CARGO_BIN_EXE_rust-llm-tidy`; modern Cargo keeps the hyphen.
/// 2. `CARGO_BIN_EXE_rust_llm_tidy`; older Cargo normalized it.
/// 3. Walk up from the test executable to the `target/<profile>` dir that
///    holds the peer binary.
///
/// Panics when none resolve.
fn binary() -> std::path::PathBuf {
    for var in ["CARGO_BIN_EXE_rust-llm-tidy", "CARGO_BIN_EXE_rust_llm_tidy"] {
        if let Some(path) = std::env::var_os(var) {
            return std::path::PathBuf::from(path);
        }
    }

    // Fallback for direct runs: the test binary lives in `<profile>/deps/`
    // (stable) or the build-out dir (newer Cargo); both sit under the
    // `<profile>` dir that holds the peer binary.
    let mut dir = std::env::current_exe()
        .expect("current_exe must resolve")
        .parent()
        .expect("current_exe must have a parent")
        .to_path_buf();
    loop {
        for bin in ["rust-llm-tidy", "rust-llm-tidy.exe"] {
            let candidate = dir.join(bin);
            if candidate.is_file() {
                return candidate;
            }
        }
        if !dir.pop() {
            break;
        }
    }
    panic!("could not locate the rust-llm-tidy binary next to the test executable");
}