cabinpkg-core 0.15.0

Stable internal data model for Cabin
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
use super::report::{ar_capabilities_as_json, cxx_capabilities_as_json};
use super::*;

#[test]
fn parses_clang_first_line() {
    let id = parse_cxx_version_output(
        "clang version 17.0.6\nTarget: x86_64-unknown-linux-gnu\nThread model: posix\n",
    );
    assert_eq!(id.kind, CompilerKind::Clang);
    let v = id.version.expect("version parsed");
    assert_eq!(v.major, 17);
    assert_eq!(v.minor, Some(0));
    assert_eq!(v.patch, Some(6));
    assert_eq!(id.target.as_deref(), Some("x86_64-unknown-linux-gnu"));
}

#[test]
fn parses_apple_clang() {
    let id = parse_cxx_version_output(
        "Apple clang version 14.0.3 (clang-1403.0.22.14.1)\nTarget: arm64-apple-darwin22.5.0\nThread model: posix\n",
    );
    assert_eq!(id.kind, CompilerKind::AppleClang);
    let v = id.version.unwrap();
    assert_eq!((v.major, v.minor, v.patch), (14, Some(0), Some(3)));
}

#[test]
fn parses_gcc_with_distro_prefix() {
    let id = parse_cxx_version_output(
        "g++ (Ubuntu 11.4.0-1ubuntu1) 11.4.0\nCopyright (C) 2021 Free Software Foundation, Inc.\n",
    );
    assert_eq!(id.kind, CompilerKind::Gcc);
    let v = id.version.unwrap();
    assert_eq!((v.major, v.minor, v.patch), (11, Some(4), Some(0)));
}

#[test]
fn parses_msvc_first_line() {
    let id = parse_cxx_version_output(
        "Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x64\n",
    );
    assert_eq!(id.kind, CompilerKind::Msvc);
    let v = id.version.unwrap();
    assert_eq!(v.major, 19);
}

#[test]
fn unknown_when_unrecognized() {
    let id = parse_cxx_version_output("My funky compiler 0.0\n");
    assert_eq!(id.kind, CompilerKind::Unknown);
    assert!(id.version.is_none());
}

#[test]
fn empty_output_is_unknown() {
    let id = parse_cxx_version_output("");
    assert_eq!(id.kind, CompilerKind::Unknown);
    assert!(id.raw_version_line.is_empty());
}

#[test]
fn parses_gnu_ar() {
    let id = parse_ar_version_output(
        "GNU ar (GNU Binutils for Debian) 2.40\nCopyright (C) 2023 Free Software Foundation, Inc.\n",
    );
    assert_eq!(id.kind, ArchiverKind::Ar);
    let v = id.version.unwrap();
    assert_eq!(v.major, 2);
}

#[test]
fn parses_llvm_ar_version() {
    let id = parse_ar_version_output(
        "LLVM (http://llvm.org/):\n  LLVM version 17.0.6\n  Optimized build.\n",
    );
    assert_eq!(id.kind, ArchiverKind::LlvmAr);
    let v = id.version.unwrap();
    assert_eq!(v.major, 17);
}

#[test]
fn detects_lib_exe_as_unsupported() {
    let id = parse_ar_version_output(
        "Microsoft (R) Library Manager Version 14.39.33523.0\nCopyright (C) Microsoft Corporation.\n",
    );
    assert_eq!(id.kind, ArchiverKind::Lib);
}

#[test]
fn unknown_archiver_classification() {
    let id = parse_ar_version_output("just-some-archiver 0.1\n");
    assert_eq!(id.kind, ArchiverKind::Unknown);
    assert!(id.version.is_none());
}

#[test]
fn clang_capabilities_include_gcc_style_and_cxx17() {
    let id = CompilerIdentity {
        kind: CompilerKind::Clang,
        version: CompilerVersion::parse("17.0.6"),
        target: None,
        raw_version_line: "clang version 17.0.6".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(caps.gcc_style_flags.supported);
    assert!(caps.depfile_mmd_mf.supported);
    assert!(caps.std_flag.supported);
    assert!(caps.cxx_standard_17.supported);
}

#[test]
fn gcc_pre_5_does_not_claim_cxx17() {
    let id = CompilerIdentity {
        kind: CompilerKind::Gcc,
        version: CompilerVersion::parse("4.8.5"),
        target: None,
        raw_version_line: "g++ 4.8.5".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(caps.gcc_style_flags.supported);
    assert!(!caps.cxx_standard_17.supported);
}

#[test]
fn msvc_capabilities_reject_gcc_style() {
    let id = CompilerIdentity {
        kind: CompilerKind::Msvc,
        version: CompilerVersion::parse("19.39.0"),
        target: None,
        raw_version_line: "Microsoft Optimizing Compiler".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(!caps.gcc_style_flags.supported);
    assert_eq!(caps.gcc_style_flags.source, CapabilitySource::Unsupported);
    assert!(caps.msvc_style_flags.supported);
}

#[test]
fn unknown_compiler_capabilities_are_conservative() {
    let id = CompilerIdentity::unknown("strange compiler");
    let caps = derive_cxx_capabilities(&id);
    assert!(!caps.gcc_style_flags.supported);
    assert_eq!(
        caps.gcc_style_flags.source,
        CapabilitySource::AssumedDefault
    );
    assert!(!caps.depfile_mmd_mf.supported);
}

#[test]
fn ar_capabilities_recognize_gnu_ar() {
    let id = ArchiverIdentity {
        kind: ArchiverKind::Ar,
        version: CompilerVersion::parse("2.40"),
        raw_version_line: "GNU ar".into(),
    };
    let caps = derive_ar_capabilities(&id);
    assert!(caps.ar_crs.supported);
    assert!(caps.static_library_output.supported);
}

#[test]
fn msvc_lib_archives_without_ar_crs() {
    // `lib.exe` does not accept GNU `crs` mode flags, but it
    // does produce a static library (`lib /OUT:`), so metadata
    // must report `static_library_output` as supported.
    let id = ArchiverIdentity {
        kind: ArchiverKind::Lib,
        version: None,
        raw_version_line: "Microsoft Library Manager".into(),
    };
    let caps = derive_ar_capabilities(&id);
    assert!(!caps.ar_crs.supported);
    assert_eq!(caps.ar_crs.source, CapabilitySource::Unsupported);
    assert!(caps.static_library_output.supported);
}

#[test]
fn validate_accepts_msvc_cxx() {
    // MSVC drives the `cl.exe` backend; detection reports
    // `msvc_style_flags`, so validation must accept it.
    let id = CompilerIdentity {
        kind: CompilerKind::Msvc,
        version: None,
        target: None,
        raw_version_line: "MSVC".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(caps.msvc_style_flags.supported);
    assert!(validate_cxx_for_backend("cl.exe", &id, &caps).is_ok());
}

fn msvc_identity(version: &str) -> CompilerIdentity {
    CompilerIdentity {
        kind: CompilerKind::Msvc,
        version: CompilerVersion::parse(version),
        target: None,
        raw_version_line: format!("Microsoft Optimizing Compiler {version}"),
    }
}

#[test]
fn msvc_std_capabilities_are_version_gated() {
    // `/std:c++17` needs cl 19.11 (VS2017 15.3); `/std:c11` needs
    // cl 19.28 (VS2019 16.8).
    let modern = derive_cxx_capabilities(&msvc_identity("19.39.33523"));
    assert!(modern.cxx_standard_17.supported);
    assert_eq!(modern.cxx_standard_17.source, CapabilitySource::Version);
    assert!(modern.c_standard_11.supported);
    assert_eq!(modern.c_standard_11.source, CapabilitySource::Version);

    // cl 19.20 (VS2019 16.0) takes /std:c++17 but predates /std:c11.
    let mid = derive_cxx_capabilities(&msvc_identity("19.20.0"));
    assert!(mid.cxx_standard_17.supported);
    assert!(!mid.c_standard_11.supported);
    assert_eq!(mid.c_standard_11.source, CapabilitySource::Version);

    // cl 19.00 (VS2015) predates both switches.
    let old = derive_cxx_capabilities(&msvc_identity("19.00.24210"));
    assert!(!old.cxx_standard_17.supported);
    assert!(!old.c_standard_11.supported);
}

#[test]
fn msvc_unparsed_version_assumes_modern_support() {
    // A real `cl` always reports a version; a parse miss
    // (`version: None`) must NOT reject an otherwise-modern
    // compiler, so the gate defaults to supported/assumed-default.
    let caps = derive_cxx_capabilities(&CompilerIdentity {
        kind: CompilerKind::Msvc,
        version: None,
        target: None,
        raw_version_line: "Microsoft Optimizing Compiler".into(),
    });
    assert!(caps.cxx_standard_17.supported);
    assert_eq!(
        caps.cxx_standard_17.source,
        CapabilitySource::AssumedDefault
    );
    assert!(caps.c_standard_11.supported);
    assert_eq!(caps.c_standard_11.source, CapabilitySource::AssumedDefault);
}

#[test]
fn gnu_c_standard_11_is_unconditional() {
    // `-std=c11` has been available far longer than `-std=c++17`,
    // so every recognized GCC/Clang reports it regardless of major.
    for id in [
        CompilerIdentity {
            kind: CompilerKind::Gcc,
            version: CompilerVersion::parse("4.8.5"),
            target: None,
            raw_version_line: "g++ 4.8.5".into(),
        },
        CompilerIdentity {
            kind: CompilerKind::Clang,
            version: CompilerVersion::parse("3.4"),
            target: None,
            raw_version_line: "clang version 3.4".into(),
        },
    ] {
        assert!(derive_cxx_capabilities(&id).c_standard_11.supported);
    }
}

#[test]
fn validate_rejects_msvc_too_old_for_std_flags() {
    let old = msvc_identity("19.00.24210");
    let caps = derive_cxx_capabilities(&old);
    // C++ build: rejected for lacking /std:c++17.
    assert!(matches!(
        validate_cxx_for_backend("cl.exe", &old, &caps),
        Err(ToolDetectionError::CxxLacksStdCxx17 { .. })
    ));
    // C build: rejected for lacking /std:c11.
    assert!(matches!(
        validate_cc_for_backend("cl.exe", &old, &caps),
        Err(ToolDetectionError::CLacksStdC11 { .. })
    ));
}

#[test]
fn clang_cl_has_msvc_dialect_with_clang_diagnostics() {
    // `clang-cl` reports a clang version, but speaks the MSVC
    // dialect: MSVC-style flags yes, GCC-style/depfile no, while
    // keeping Clang's color/json/response-file capabilities and
    // version-independent C++17/C11 support.
    let id = CompilerIdentity {
        kind: CompilerKind::ClangCl,
        version: CompilerVersion::parse("17.0.6"),
        target: None,
        raw_version_line: "clang version 17.0.6".into(),
    };
    assert!(id.kind.speaks_msvc_dialect());
    assert!(id.kind.is_clang_like());
    assert!(!id.kind.supports_gcc_style_command_line());

    let caps = derive_cxx_capabilities(&id);
    assert!(caps.msvc_style_flags.supported);
    assert!(!caps.gcc_style_flags.supported);
    assert!(!caps.depfile_mmd_mf.supported);
    assert!(caps.cxx_standard_17.supported);
    assert!(caps.c_standard_11.supported);

    // Validates against both the C++ and C MSVC backends.
    assert!(validate_cxx_for_backend("clang-cl", &id, &caps).is_ok());
    assert!(validate_cc_for_backend("clang-cl", &id, &caps).is_ok());
}

#[test]
fn validate_accepts_modern_and_unversioned_msvc_c() {
    for id in [
        msvc_identity("19.39.33523"),
        CompilerIdentity {
            kind: CompilerKind::Msvc,
            version: None,
            target: None,
            raw_version_line: "MSVC".into(),
        },
    ] {
        let caps = derive_cxx_capabilities(&id);
        assert!(validate_cc_for_backend("cl.exe", &id, &caps).is_ok());
    }
}

#[test]
fn validate_rejects_unknown_cxx() {
    let id = CompilerIdentity::unknown("???");
    let caps = derive_cxx_capabilities(&id);
    let err = validate_cxx_for_backend("custom-cxx", &id, &caps).unwrap_err();
    assert!(matches!(
        err,
        ToolDetectionError::UnknownCxxRequiresGccStyle { .. }
    ));
}

#[test]
fn validate_accepts_clang() {
    let id = CompilerIdentity {
        kind: CompilerKind::Clang,
        version: CompilerVersion::parse("17.0.6"),
        target: None,
        raw_version_line: "clang version 17.0.6".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(validate_cxx_for_backend("clang++", &id, &caps).is_ok());
}

#[test]
fn validate_rejects_gcc_too_old_for_cxx17() {
    let id = CompilerIdentity {
        kind: CompilerKind::Gcc,
        version: CompilerVersion::parse("4.8.5"),
        target: None,
        raw_version_line: "g++ 4.8".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    let err = validate_cxx_for_backend("g++", &id, &caps).unwrap_err();
    assert!(matches!(err, ToolDetectionError::CxxLacksStdCxx17 { .. }));
}

#[test]
fn validate_cc_accepts_pure_c_clang_without_cxx17_capability() {
    // The C-side validator must accept a compiler that
    // would *not* satisfy the C++ contract (no
    // `cxx_standard_17`). A bare `cc` driver on a system
    // that ships only C headers is a legitimate case; only
    // GCC-style flags + depfile are required for the C
    // backend.
    let id = CompilerIdentity {
        kind: CompilerKind::Clang,
        version: CompilerVersion::parse("17.0.6"),
        target: None,
        raw_version_line: "clang version 17.0.6".into(),
    };
    let mut caps = derive_cxx_capabilities(&id);
    // Force `cxx_standard_17` off so we can be certain the
    // C validator does not gate on it.
    caps.cxx_standard_17 = Capability {
        supported: false,
        source: CapabilitySource::Unsupported,
    };
    assert!(validate_cc_for_backend("cc", &id, &caps).is_ok());
    // Sanity: the equivalent CXX validation would now reject
    // the same compiler. Asserting both directions
    // documents the design constraint that C/C++
    // capability gating differ.
    assert!(matches!(
        validate_cxx_for_backend("cc", &id, &caps).unwrap_err(),
        ToolDetectionError::CxxLacksStdCxx17 { .. }
    ));
}

#[test]
fn validate_cc_accepts_msvc() {
    let id = CompilerIdentity {
        kind: CompilerKind::Msvc,
        version: None,
        target: None,
        raw_version_line: "MSVC".into(),
    };
    let caps = derive_cxx_capabilities(&id);
    assert!(validate_cc_for_backend("cl.exe", &id, &caps).is_ok());
}

#[test]
fn validate_cc_rejects_unknown_compiler_without_gcc_style() {
    // Unknown identity + missing `gcc_style_flags` capability
    // is the unrecoverable case: the planner cannot tell
    // whether the compiler accepts `-c -o` etc.
    let id = CompilerIdentity::unknown("???");
    let caps = derive_cxx_capabilities(&id);
    let err = validate_cc_for_backend("custom-cc", &id, &caps).unwrap_err();
    assert!(matches!(
        err,
        ToolDetectionError::UnknownCRequiresGccStyle { .. }
    ));
}

#[test]
fn validate_cc_rejects_gcc_without_depfile_support() {
    // GCC identity but without `-MMD -MF` support — Cabin
    // emits a depfile flag for every compile so the C
    // contract requires it, even though `cxx_standard_17`
    // is not relevant.
    let id = CompilerIdentity {
        kind: CompilerKind::Gcc,
        version: CompilerVersion::parse("9.4.0"),
        target: None,
        raw_version_line: "gcc 9.4".into(),
    };
    let mut caps = derive_cxx_capabilities(&id);
    caps.depfile_mmd_mf = Capability {
        supported: false,
        source: CapabilitySource::Unsupported,
    };
    let err = validate_cc_for_backend("cc", &id, &caps).unwrap_err();
    assert!(matches!(err, ToolDetectionError::CLacksDepfile { .. }));
}

#[test]
fn validate_accepts_msvc_archiver() {
    // `lib.exe` is the MSVC static-library archiver.
    let id = ArchiverIdentity {
        kind: ArchiverKind::Lib,
        version: None,
        raw_version_line: "Microsoft Library Manager".into(),
    };
    let caps = derive_ar_capabilities(&id);
    assert!(validate_ar_for_backend("lib.exe", &id, &caps).is_ok());
}

#[test]
fn version_display_truncates_unset_components() {
    let v = CompilerVersion::parse("11").unwrap();
    assert_eq!(v.to_display_string(), "11");
    let v = CompilerVersion::parse("11.4").unwrap();
    assert_eq!(v.to_display_string(), "11.4");
    let v = CompilerVersion::parse("11.4.0").unwrap();
    assert_eq!(v.to_display_string(), "11.4.0");
}

// --------------------------------------------------------------
// Golden / fixture tests.
//
// These pin the JSON shape that downstream tooling
// (`cabin metadata`, IDE integrations) reads out of a
// `ToolchainDetectionReport`. Any accidental change to the
// field names or serialization order here is user-visible
// and should be deliberate.
// --------------------------------------------------------------

fn pretty(value: &serde_json::Value) -> String {
    serde_json::to_string_pretty(value).unwrap()
}

fn cxx_identity_and_capabilities_json(version_output: &str) -> String {
    let id = parse_cxx_version_output(version_output);
    let caps = derive_cxx_capabilities(&id);
    pretty(&serde_json::json!({
        "identity": id.as_json(),
        "capabilities": cxx_capabilities_as_json(&caps),
    }))
}

fn ar_identity_and_capabilities_json(version_output: &str) -> String {
    let id = parse_ar_version_output(version_output);
    let caps = derive_ar_capabilities(&id);
    pretty(&serde_json::json!({
        "identity": id.as_json(),
        "capabilities": ar_capabilities_as_json(&caps),
    }))
}

#[test]
fn snapshot_clang_identity_and_capabilities() {
    let actual = cxx_identity_and_capabilities_json(
        "clang version 17.0.6\nTarget: x86_64-unknown-linux-gnu\nThread model: posix\n",
    );
    let expected = r#"{
  "identity": {
    "kind": "clang",
    "version": "17.0.6",
    "target": "x86_64-unknown-linux-gnu",
    "raw_version_line": "clang version 17.0.6"
  },
  "capabilities": {
    "c_standard_11": {
      "supported": true,
      "source": "version"
    },
    "cxx_standard_17": {
      "supported": true,
      "source": "version"
    },
    "depfile_mmd_mf": {
      "supported": true,
      "source": "version"
    },
    "gcc_style_flags": {
      "supported": true,
      "source": "version"
    },
    "msvc_style_flags": {
      "supported": false,
      "source": "assumed-default"
    },
    "std_flag": {
      "supported": true,
      "source": "version"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_apple_clang_identity_and_capabilities() {
    let actual = cxx_identity_and_capabilities_json(
        "Apple clang version 14.0.3 (clang-1403.0.22.14.1)\nTarget: arm64-apple-darwin22.5.0\nThread model: posix\n",
    );
    let expected = r#"{
  "identity": {
    "kind": "apple-clang",
    "version": "14.0.3",
    "target": "arm64-apple-darwin22.5.0",
    "raw_version_line": "Apple clang version 14.0.3 (clang-1403.0.22.14.1)"
  },
  "capabilities": {
    "c_standard_11": {
      "supported": true,
      "source": "version"
    },
    "cxx_standard_17": {
      "supported": true,
      "source": "version"
    },
    "depfile_mmd_mf": {
      "supported": true,
      "source": "version"
    },
    "gcc_style_flags": {
      "supported": true,
      "source": "version"
    },
    "msvc_style_flags": {
      "supported": false,
      "source": "assumed-default"
    },
    "std_flag": {
      "supported": true,
      "source": "version"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_gcc_identity_and_capabilities() {
    let actual = cxx_identity_and_capabilities_json(
        "g++ (Ubuntu 11.4.0-1ubuntu1) 11.4.0\nCopyright (C) 2021 Free Software Foundation, Inc.\n",
    );
    let expected = r#"{
  "identity": {
    "kind": "gcc",
    "version": "11.4.0",
    "raw_version_line": "g++ (Ubuntu 11.4.0-1ubuntu1) 11.4.0"
  },
  "capabilities": {
    "c_standard_11": {
      "supported": true,
      "source": "version"
    },
    "cxx_standard_17": {
      "supported": true,
      "source": "version"
    },
    "depfile_mmd_mf": {
      "supported": true,
      "source": "version"
    },
    "gcc_style_flags": {
      "supported": true,
      "source": "version"
    },
    "msvc_style_flags": {
      "supported": false,
      "source": "assumed-default"
    },
    "std_flag": {
      "supported": true,
      "source": "version"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_msvc_identity_and_capabilities() {
    let actual = cxx_identity_and_capabilities_json(
        "Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x64\n",
    );
    // A modern `cl` (19.39 == VS2022 17.9) accepts the
    // `/std:c++17` and `/std:c11` switches Cabin emits, so both
    // standard capabilities are version-supported; the GCC-style
    // and depfile capabilities stay unsupported because MSVC
    // drives its own dialect.
    let expected = r#"{
  "identity": {
    "kind": "msvc",
    "version": "19.39.33523",
    "raw_version_line": "Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x64"
  },
  "capabilities": {
    "c_standard_11": {
      "supported": true,
      "source": "version"
    },
    "cxx_standard_17": {
      "supported": true,
      "source": "version"
    },
    "depfile_mmd_mf": {
      "supported": false,
      "source": "unsupported"
    },
    "gcc_style_flags": {
      "supported": false,
      "source": "unsupported"
    },
    "msvc_style_flags": {
      "supported": true,
      "source": "version"
    },
    "std_flag": {
      "supported": false,
      "source": "unsupported"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_unknown_compiler_capabilities_are_conservative() {
    let actual = cxx_identity_and_capabilities_json("My funky compiler 0.0\n");
    let expected = r#"{
  "identity": {
    "kind": "unknown",
    "raw_version_line": "My funky compiler 0.0"
  },
  "capabilities": {
    "c_standard_11": {
      "supported": false,
      "source": "assumed-default"
    },
    "cxx_standard_17": {
      "supported": false,
      "source": "assumed-default"
    },
    "depfile_mmd_mf": {
      "supported": false,
      "source": "assumed-default"
    },
    "gcc_style_flags": {
      "supported": false,
      "source": "assumed-default"
    },
    "msvc_style_flags": {
      "supported": false,
      "source": "assumed-default"
    },
    "std_flag": {
      "supported": false,
      "source": "assumed-default"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_gnu_ar_identity_and_capabilities() {
    let actual = ar_identity_and_capabilities_json(
        "GNU ar (GNU Binutils for Debian) 2.40\nCopyright (C) 2023 Free Software Foundation, Inc.\n",
    );
    let expected = r#"{
  "identity": {
    "kind": "ar",
    "version": "2.40",
    "raw_version_line": "GNU ar (GNU Binutils for Debian) 2.40"
  },
  "capabilities": {
    "ar_crs": {
      "supported": true,
      "source": "version"
    },
    "static_library_output": {
      "supported": true,
      "source": "version"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_msvc_lib_archiver_produces_static_library_without_ar_crs() {
    let actual = ar_identity_and_capabilities_json(
        "Microsoft (R) Library Manager Version 14.39.33523.0\nCopyright (C) Microsoft Corporation.\n",
    );
    let expected = r#"{
  "identity": {
    "kind": "lib",
    "version": "14.39.33523",
    "raw_version_line": "Microsoft (R) Library Manager Version 14.39.33523.0"
  },
  "capabilities": {
    "ar_crs": {
      "supported": false,
      "source": "unsupported"
    },
    "static_library_output": {
      "supported": true,
      "source": "version"
    }
  }
}"#;
    assert_eq!(actual, expected);
}

#[test]
fn snapshot_full_detection_report_for_clang_plus_gnu_ar() {
    // End-to-end snapshot of `ToolchainDetectionReport::as_json`
    // for a typical Linux clang + GNU ar setup. Pins the
    // top-level shape `{ cxx, [cc,] ar }` plus all nested
    // fields in their insertion order.
    let cxx_id =
        parse_cxx_version_output("clang version 17.0.6\nTarget: x86_64-unknown-linux-gnu\n");
    let cxx_caps = derive_cxx_capabilities(&cxx_id);
    let ar_id = parse_ar_version_output("GNU ar (GNU Binutils) 2.40\n");
    let ar_caps = derive_ar_capabilities(&ar_id);
    let report = ToolchainDetectionReport {
        cxx: ToolDetection {
            path: camino::Utf8PathBuf::from("/opt/llvm/bin/clang++"),
            identity: cxx_id,
            capabilities: cxx_caps,
        },
        cc: None,
        ar: ToolDetection {
            path: camino::Utf8PathBuf::from("/usr/bin/ar"),
            identity: ar_id,
            capabilities: ar_caps,
        },
    };
    let actual = pretty(&report.as_json());
    let expected = r#"{
  "cxx": {
    "path": "/opt/llvm/bin/clang++",
    "identity": {
      "kind": "clang",
      "version": "17.0.6",
      "target": "x86_64-unknown-linux-gnu",
      "raw_version_line": "clang version 17.0.6"
    },
    "capabilities": {
      "c_standard_11": {
        "supported": true,
        "source": "version"
      },
      "cxx_standard_17": {
        "supported": true,
        "source": "version"
      },
      "depfile_mmd_mf": {
        "supported": true,
        "source": "version"
      },
      "gcc_style_flags": {
        "supported": true,
        "source": "version"
      },
      "msvc_style_flags": {
        "supported": false,
        "source": "assumed-default"
      },
      "std_flag": {
        "supported": true,
        "source": "version"
      }
    }
  },
  "ar": {
    "path": "/usr/bin/ar",
    "identity": {
      "kind": "ar",
      "version": "2.40",
      "raw_version_line": "GNU ar (GNU Binutils) 2.40"
    },
    "capabilities": {
      "ar_crs": {
        "supported": true,
        "source": "version"
      },
      "static_library_output": {
        "supported": true,
        "source": "version"
      }
    }
  }
}"#;
    assert_eq!(actual, expected);
}