cabinpkg 0.17.0

A package manager and build system for C/C++
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
use super::*;
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};

/// Sentinel that the bundled fake tidy recognizes: a source
/// file whose contents contain this marker triggers a
/// non-zero exit and a fake clang-tidy diagnostic on stderr.
const FAIL_MARKER: &str = "// CABIN-TIDY-FAIL";

/// Process-wide lock for tests that rely on
/// `CABIN_FAKE_TIDY_RECORD`: env vars are process-global
/// and cargo runs tests in parallel.
fn tidy_record_lock() -> std::sync::MutexGuard<'static, ()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    LOCK.get_or_init(|| Mutex::new(()))
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// A stand-in compiler that exists on every host (the bundled fake
/// ninja binary).  The tidy planner resolves a toolchain only to
/// thread the compiler path into `compile_commands.json`; it never
/// runs it as a compiler. `/bin/sh` would serve on Unix but is
/// absent on Windows, so a built binary is used instead.
fn tidy_dummy_compiler() -> PathBuf {
    workspace_test_bin("cabin-ninja-fake-ninja")
}

/// Build the integration-test command with `CABIN_TIDY`
/// pointing at the bundled fake tidy.  Also sets `CXX` / `CC` /
/// `AR` to a binary that exists on every host (see
/// [`tidy_dummy_compiler`]) so the tidy planner's toolchain
/// resolver does not fail when the developer's PATH lacks
/// `c++` / `clang++` / `g++`.
fn cabin_with_fake_tidy() -> Command {
    let mut cmd = cabin();
    cmd.env("CABIN_TIDY", fake_tidy_path());
    let dummy = tidy_dummy_compiler();
    cmd.env("CXX", &dummy);
    cmd.env("CC", &dummy);
    cmd.env("AR", &dummy);
    cmd
}

fn fake_tidy_path() -> PathBuf {
    let test_exe = std::env::current_exe().expect("current_exe");
    let mut dir = test_exe
        .parent()
        .expect("test exe should live in a directory")
        .to_path_buf();
    if dir.file_name().and_then(|n| n.to_str()) == Some("deps") {
        dir.pop();
    }
    let candidate = dir.join(format!(
        "cabin-tidy-fake-tidy{}",
        std::env::consts::EXE_SUFFIX
    ));
    assert!(
        candidate.is_file(),
        "expected fake tidy at {}; build cabin-tidy with `--features test-fake-tidy`",
        candidate.display()
    );
    candidate
}

/// Collect the raw record lines the fake tidy appended.
fn read_record(path: &Path) -> Vec<String> {
    std::fs::read_to_string(path)
        .unwrap_or_default()
        .lines()
        .map(str::to_owned)
        .collect()
}

/// Normalize a tidy record (or one of its fields) to forward
/// slashes so path assertions read the same on every host.  The
/// fake tidy escapes each `\` as `\\` in the argv / files fields
/// but leaves the compile-db field raw, so collapse the doubled
/// form first, then any lone separator.
fn normalize(s: &str) -> String {
    s.replace("\\\\", "/").replace('\\', "/")
}

#[test]
fn top_level_help_lists_tidy() {
    let assertion = cabin().arg("--help").assert().success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();
    assert!(
        stdout.contains("Run clang-tidy"),
        "top-level help should describe the tidy subcommand:\n{stdout}"
    );
}

#[test]
fn tidy_help_documents_documented_flags() {
    let assertion = cabin().args(["tidy", "--help"]).assert().success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();
    for snippet in [
        "--fix",
        "--jobs",
        "--build-dir",
        "--exclude",
        "--no-ignore-vcs",
    ] {
        assert!(
            stdout.contains(snippet),
            "`cabin tidy --help` should mention {snippet}: {stdout}"
        );
    }
}

#[test]
fn clean_project_tidies_successfully() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success()
        .stdout(predicate::str::contains("Checked 1 file"));
}

/// `cabin tidy` must analyze `test` and `example` sources, plus
/// default-buildable ones. `cabin fmt` already formats
/// those files; tidy must match its surface.
#[test]
fn tidy_analyses_test_and_example_sources() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[target.demo]
type = "library"
sources = ["src/lib.cc"]

[target.demo_test]
type = "test"
sources = ["tests/lib_test.cc"]
deps = ["demo"]

[target.hello_example]
type = "example"
sources = ["examples/hello.cc"]
deps = ["demo"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int demo() { return 1; }\n")
        .unwrap();
    dir.child("tests/lib_test.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();
    dir.child("examples/hello.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success()
        .stdout(predicate::str::contains("Checked 3 files"));
}

#[test]
fn tidy_skips_gated_target_but_errors_when_everything_is_gated() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
ssl = []

[target.demo]
type = "library"
sources = ["src/lib.cc"]

[target.tls]
type = "library"
sources = ["src/tls.cc"]
required-features = ["ssl"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int demo() { return 1; }\n")
        .unwrap();
    dir.child("src/tls.cc")
        .write_str("int tls() { return 2; }\n")
        .unwrap();
    // The gated target's source stays out of the analyzed set.
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success()
        .stdout(predicate::str::contains("Checked 1 file"));

    // With *every* target gated, tidy must fail loudly instead of
    // reporting a clean empty run.
    let all_gated = TempDir::new().unwrap();
    all_gated
        .child("cabin.toml")
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
ssl = []

[target.tls]
type = "library"
sources = ["src/tls.cc"]
required-features = ["ssl"]
"#,
        )
        .unwrap();
    all_gated
        .child("src/tls.cc")
        .write_str("int tls() { return 2; }\n")
        .unwrap();
    cabin_with_fake_tidy()
        .current_dir(all_gated.path())
        .arg("tidy")
        .assert()
        .failure()
        .stderr(predicate::str::contains("demo:tls"))
        .stderr(predicate::str::contains("[features].default"));
}

#[test]
fn compile_database_is_generated_at_profile_root() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success();

    let cdb = dir.path().join("build/dev/compile_commands.json");
    assert!(
        cdb.is_file(),
        "compile_commands.json must land at build/dev/compile_commands.json: missing {}",
        cdb.display()
    );
    let body = fs::read_to_string(&cdb).unwrap();
    assert!(
        body.contains("main.cc"),
        "compile database should mention the source file: {body}"
    );
}

#[test]
fn dash_p_is_the_compile_database_directory() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let record = dir.path().join("argv.log");

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .arg("tidy")
        .assert()
        .success();

    let lines = read_record(&record);
    assert_eq!(lines.len(), 1, "expected one tidy spawn, got {lines:?}");
    let mut parts = lines[0].split('\t');
    let argv = parts.next().unwrap();
    let _quiet = parts.next().unwrap();
    let _fix = parts.next().unwrap();
    let cdb = parts.next().unwrap();
    assert!(argv.contains("-p"), "argv should contain -p: {argv}");
    // Path canonicalization may resolve symlinks under
    // /tmp -> /private/tmp on macOS, so test for the
    // suffix instead of an exact match.
    assert!(
        normalize(cdb).ends_with("build/dev"),
        "compile DB dir should end at <build>/dev: {cdb}"
    );
}

#[test]
fn custom_build_dir_is_the_compile_database_directory() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let record = dir.path().join("argv.log");

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--build-dir", "out"])
        .assert()
        .success();

    assert!(dir.path().join("out/dev/compile_commands.json").is_file());
    let lines = read_record(&record);
    assert_eq!(lines.len(), 1, "expected one tidy spawn, got {lines:?}");
    let cdb = lines[0].split('\t').nth(3).unwrap();
    assert!(
        normalize(cdb).ends_with("out/dev"),
        "compile DB dir should end at <custom-build>/dev: {cdb}"
    );
}

#[test]
fn fix_flag_is_passed_to_tidy_driver() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let record = dir.path().join("argv.log");

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--fix"])
        .assert()
        .success();

    let lines = read_record(&record);
    let mut parts = lines[0].split('\t');
    let argv = parts.next().unwrap();
    let _quiet = parts.next().unwrap();
    let fix = parts.next().unwrap();
    assert!(argv.contains("-fix"), "argv should contain -fix: {argv}");
    assert_eq!(fix, "true");
}

#[test]
fn fix_mode_clamps_jobs_to_one() {
    // The historical C++ tidy implementation forced `-j 1`
    // in `--fix` mode; preserve the policy here.  Verbose
    // mode prints the override notice so the user can see
    // why their `--jobs 4` was dropped.
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let record = dir.path().join("argv.log");

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--fix", "--jobs", "4", "--verbose"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();

    let lines = read_record(&record);
    let mut parts = lines[0].split('\t');
    let argv = parts.next().unwrap();
    let _quiet = parts.next().unwrap();
    let _fix = parts.next().unwrap();
    let _cdb = parts.next().unwrap();
    let jobs = parts.next().unwrap();
    assert_eq!(jobs, "1", "fix mode must clamp jobs to 1: argv={argv}");
    assert!(
        stdout.contains("--fix forces tidy parallelism to 1"),
        "verbose mode must surface the clamp: {stdout}"
    );
}

#[test]
fn jobs_flag_is_passed_to_tidy_driver_in_check_mode() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let record = dir.path().join("argv.log");

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--jobs", "2"])
        .assert()
        .success();

    let lines = read_record(&record);
    let mut parts = lines[0].split('\t');
    let argv = parts.next().unwrap();
    let _quiet = parts.next().unwrap();
    let _fix = parts.next().unwrap();
    let _cdb = parts.next().unwrap();
    let jobs = parts.next().unwrap();
    assert!(argv.contains("-j 2"), "argv should contain -j 2: {argv}");
    assert_eq!(jobs, "2");
}

#[test]
fn invalid_jobs_value_is_rejected_at_parse_time() {
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .args(["tidy", "--jobs", "0"])
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr).to_string();
    assert!(
        stderr.contains("got 0") || stderr.contains("expected a positive integer"),
        "diagnostic should call out the rejected value: {stderr}"
    );
}

#[test]
fn no_files_succeeds_with_message() {
    // No `[target.*]` declared → no compile commands → no
    // files to check.  Should succeed cleanly with a
    // status line, not error.
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str("[package]\nname = \"empty\"\nversion = \"0.1.0\"\n")
        .unwrap();
    dir.child("docs/README.md")
        .write_str("no sources here\n")
        .unwrap();

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success()
        .stdout(predicate::str::contains("no C/C++ source files"));
}

#[test]
fn fail_marker_causes_non_zero_exit_and_preserves_diagnostic() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml").write_str(VALID_MANIFEST).unwrap();
    dir.child("src/main.cc")
        .write_str("// CABIN-TIDY-FAIL\nint main() {}\n")
        .unwrap();

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .failure();
    let _ = FAIL_MARKER;
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr).to_string();
    assert!(
        stderr.contains("fake-clang-tidy diagnostic"),
        "clang-tidy diagnostic must reach stderr unchanged, got: {stderr}"
    );
}

#[test]
fn exclude_path_skips_named_file() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")

            .write_str("[package]\nname = \"hello\"\nversion = \"0.1.0\"\ncxx-standard = \"c++17\"\n\n[target.hello]\ntype = \"executable\"\nsources = [\"src/main.cc\", \"src/extra.cc\"]\n")

            .unwrap();
    dir.child("src/main.cc")
        .write_str("int main() {}\n")
        .unwrap();
    dir.child("src/extra.cc")
        .write_str("int extra() { return 0; }\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--exclude", "src/extra.cc"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
    assert!(
        !body.contains("src/extra.cc"),
        "excluded file leaked into argv: {body}"
    );
}

#[test]
fn repeated_exclude_accumulates() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")

            .write_str("[package]\nname = \"hello\"\nversion = \"0.1.0\"\ncxx-standard = \"c++17\"\n\n[target.hello]\ntype = \"executable\"\nsources = [\"src/main.cc\", \"src/a.cc\", \"src/b.cc\"]\n")

            .unwrap();
    dir.child("src/main.cc")
        .write_str("int main() {}\n")
        .unwrap();
    dir.child("src/a.cc")
        .write_str("int a() { return 0; }\n")
        .unwrap();
    dir.child("src/b.cc")
        .write_str("int b() { return 0; }\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--exclude", "src/a.cc", "--exclude", "src/b.cc"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
    assert!(!body.contains("src/a.cc"));
    assert!(!body.contains("src/b.cc"));
}

#[test]
fn vcs_ignored_files_are_skipped_by_default() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")

            .write_str("[package]\nname = \"hello\"\nversion = \"0.1.0\"\ncxx-standard = \"c++17\"\n\n[target.hello]\ntype = \"executable\"\nsources = [\"src/main.cc\", \"src/generated.cc\"]\n")

            .unwrap();
    dir.child("src/main.cc")
        .write_str("int main() {}\n")
        .unwrap();
    dir.child("src/generated.cc")
        .write_str("int gen() { return 0; }\n")
        .unwrap();
    dir.child(".gitignore")
        .write_str("src/generated.cc\n")
        .unwrap();
    dir.child(".git/HEAD")
        .write_str("ref: refs/heads/main\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .arg("tidy")
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
    assert!(!body.contains("src/generated.cc"));
}

#[test]
fn no_ignore_vcs_includes_gitignored_files() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")

            .write_str("[package]\nname = \"hello\"\nversion = \"0.1.0\"\ncxx-standard = \"c++17\"\n\n[target.hello]\ntype = \"executable\"\nsources = [\"src/main.cc\", \"src/generated.cc\"]\n")

            .unwrap();
    dir.child("src/main.cc")
        .write_str("int main() {}\n")
        .unwrap();
    dir.child("src/generated.cc")
        .write_str("int gen() { return 0; }\n")
        .unwrap();
    dir.child(".gitignore")
        .write_str("src/generated.cc\n")
        .unwrap();
    dir.child(".git/HEAD")
        .write_str("ref: refs/heads/main\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--no-ignore-vcs"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
    assert!(body.contains("src/generated.cc"));
}

#[test]
fn build_and_cache_directories_are_not_checked() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    // Drop fake artifacts under directories that source
    // discovery is contracted to skip.  None of these
    // declare a target so they could not appear in
    // compile_commands either, but the assertion is the
    // negative one: no path that includes any of them
    // should reach the tidy driver.
    dir.child("build/dev/scratch.cc")
        .write_str("int s() {}\n")
        .unwrap();
    dir.child("target/leftover.cc")
        .write_str("int t() {}\n")
        .unwrap();
    dir.child("dist/staging.cc")
        .write_str("int d() {}\n")
        .unwrap();
    dir.child("node_modules/dep/main.cc")
        .write_str("int dep() {}\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .arg("tidy")
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    for skipped in [
        "build/dev/scratch.cc",
        "target/leftover.cc",
        "dist/staging.cc",
        "node_modules/dep/main.cc",
    ] {
        assert!(
            !body.contains(skipped),
            "{skipped} should not have been checked: {body}"
        );
    }
}

#[test]
fn custom_build_directory_is_not_checked() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    dir.child("out/dev/scratch.cc")
        .write_str("int s() {}\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "--build-dir", "out"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
    assert!(
        !body.contains("out/dev/scratch.cc"),
        "custom build dir should not have been checked: {body}"
    );
}

#[test]
fn missing_tidy_produces_actionable_error() {
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let dummy = tidy_dummy_compiler();
    let assertion = cabin()
        .current_dir(dir.path())
        .env("CABIN_TIDY", "/no-such/run-clang-tidy-binary")
        .env("CXX", &dummy)
        .env("CC", &dummy)
        .env("AR", &dummy)
        .arg("tidy")
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr).to_string();
    assert!(
        stderr.contains("/no-such/run-clang-tidy-binary"),
        "diagnostic should name the missing executable: {stderr}"
    );
    assert!(
        stderr.contains("install `clang-tidy`"),
        "diagnostic should include the install hint: {stderr}"
    );
    assert!(
        stderr.contains("CABIN_TIDY"),
        "diagnostic should reference the override env var: {stderr}"
    );
}

#[test]
fn cabin_tidy_env_override_routes_through_named_binary() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let record = dir.path().join("argv.log");
    let dummy = tidy_dummy_compiler();
    cabin()
        .current_dir(dir.path())
        .env("CABIN_TIDY", fake_tidy_path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .env("CXX", &dummy)
        .env("CC", &dummy)
        .env("AR", &dummy)
        .arg("tidy")
        .assert()
        .success();
    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("src/main.cc"));
}

#[test]
fn clang_tidy_config_survives_unmodified_after_tidy() {
    // Cabin must not generate, modify, or delete a
    // `.clang-tidy` file the user committed alongside their
    // sources.  The integration here is the negative
    // assertion that Cabin does not interfere with
    // clang-tidy's own config story.
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());
    let cfg_path = dir.path().join(".clang-tidy");
    let cfg_body = "Checks: 'modernize-*'\n";
    assert_fs::fixture::ChildPath::new(&cfg_path)
        .write_str(cfg_body)
        .unwrap();

    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .success();

    assert!(cfg_path.is_file(), "Cabin must not delete .clang-tidy");
    let after = fs::read_to_string(&cfg_path).unwrap();
    assert_eq!(after, cfg_body, "Cabin must not modify .clang-tidy");
}

#[test]
fn quiet_mode_suppresses_cabin_status_but_not_tidy_output() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .args(["tidy", "--quiet"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();
    assert!(
        !stdout.contains("cabin: running clang-tidy"),
        "quiet mode must suppress Cabin status: {stdout}"
    );
    assert!(
        !stdout.contains("cabin: checked"),
        "quiet mode must suppress Cabin summary: {stdout}"
    );
}

#[test]
fn verbose_mode_lists_selected_package_and_file_count() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .args(["tidy", "--verbose"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();
    assert!(
        stdout.contains("tidying 1 file"),
        "verbose tidy should report file count: {stdout}"
    );
    assert!(
        stdout.contains("hello"),
        "verbose tidy should name the selected package: {stdout}"
    );
    assert!(
        stdout.contains("compile database ="),
        "verbose tidy should show compile DB path: {stdout}"
    );
}

#[test]
fn very_verbose_lists_command_line() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    write_minimal_project(dir.path());

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .args(["tidy", "-vv"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout).to_string();
    assert!(
        stdout.contains("cabin-tidy-fake-tidy"),
        "very-verbose tidy should echo the executable: {stdout}"
    );
    // Verbose drops the `-quiet` flag from the *spawned*
    // tidy command, but the very-verbose command-line echo
    // reflects what we *would* have run; verbose mode
    // suppresses `-quiet`, so the echo also omits it.
    assert!(
        stdout.contains("-p "),
        "very-verbose tidy should echo the -p flag: {stdout}"
    );
}

#[test]
fn nested_workspace_member_is_not_walked_from_outer_root() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[workspace]
members = ["outer", "outer/nested"]
"#,
        )
        .unwrap();
    dir.child("outer/cabin.toml")
        .write_str(
            r#"[package]
name = "outer"
version = "0.1.0"
cxx-standard = "c++17"

[target.outer]
type = "executable"
sources = ["src/main.cc"]
"#,
        )
        .unwrap();
    dir.child("outer/src/main.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();
    dir.child("outer/nested/cabin.toml")
        .write_str(
            r#"[package]
name = "nested"
version = "0.1.0"
cxx-standard = "c++17"

[target.nested]
type = "executable"
sources = ["src/main.cc"]
"#,
        )
        .unwrap();
    dir.child("outer/nested/src/main.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "-p", "outer"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("outer/src/main.cc"));
    assert!(
        !body.contains("outer/nested/src/main.cc"),
        "nested member's sources leaked into outer's tidy: {body}"
    );
}

#[test]
fn unrelated_versioned_dependency_does_not_block_selected_package() {
    let _guard = tidy_record_lock();
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[workspace]
members = ["clean", "registry-user"]
"#,
        )
        .unwrap();
    dir.child("clean/cabin.toml")
        .write_str(
            r#"[package]
name = "clean"
version = "0.1.0"
cxx-standard = "c++17"

[target.clean]
type = "executable"
sources = ["src/main.cc"]
"#,
        )
        .unwrap();
    dir.child("clean/src/main.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();
    dir.child("registry-user/cabin.toml")
        .write_str(
            r#"[package]
name = "registry-user"
version = "0.1.0"

[dependencies]
fmt = "1.0"
"#,
        )
        .unwrap();

    let record = dir.path().join("argv.log");
    cabin_with_fake_tidy()
        .current_dir(dir.path())
        .env("CABIN_FAKE_TIDY_RECORD", &record)
        .args(["tidy", "-p", "clean"])
        .assert()
        .success();

    let body = normalize(&std::fs::read_to_string(&record).unwrap());
    assert!(body.contains("clean/src/main.cc"));
    assert!(!body.contains("registry-user"));
}

#[test]
fn versioned_dependency_produces_actionable_error() {
    // `cabin tidy` does not run the artifact pipeline; if a
    // selected package declares a versioned registry dep we
    // surface a clear unsupported-shape diagnostic instead of
    // letting the planner fail with a confusing downstream
    // error.
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")

            .write_str("[package]\nname = \"hello\"\nversion = \"0.1.0\"\ncxx-standard = \"c++17\"\n\n[target.hello]\ntype = \"executable\"\nsources = [\"src/main.cc\"]\n\n[dependencies]\nfmt = \"1.0\"\n")

            .unwrap();
    dir.child("src/main.cc")
        .write_str("int main() {}\n")
        .unwrap();

    let assertion = cabin_with_fake_tidy()
        .current_dir(dir.path())
        .arg("tidy")
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr).to_string();
    assert!(
        stderr.contains("versioned registry dependencies"),
        "diagnostic should name the offending situation: {stderr}"
    );
    assert!(
        stderr.contains("does not run the artifact pipeline"),
        "diagnostic should explain why tidy refuses: {stderr}"
    );
    assert!(
        !stderr.contains("cabin build"),
        "diagnostic must not suggest a build/fetch step that tidy cannot use: {stderr}"
    );
}