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
//! End-to-end coverage for `test` / `example` target kinds and
//! the `cabin test` command.

use super::*;

/// Single-package fixture with one library plus one passing test
/// target.  Returns the temp dir guard so the caller can drive
/// commands against it.
fn passing_test_project() -> TempDir {
    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"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int demo() { return 42; }\n")
        .unwrap();
    dir.child("tests/lib_test.cc")
        .write_str("int main() { return 0; }\n")
        .unwrap();
    dir
}

fn project_with_dev_kinds() -> TempDir {
    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();
    dir
}

#[test]
fn metadata_lists_test_and_example_target_kinds() {
    let dir = project_with_dev_kinds();
    let value = run_metadata(&dir.path().join("cabin.toml"));
    let demo = package_in(&value, "demo");
    let kinds: std::collections::BTreeMap<String, String> = demo["targets"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| {
            (
                t["name"].as_str().unwrap().to_owned(),
                t["kind"].as_str().unwrap().to_owned(),
            )
        })
        .collect();
    assert_eq!(kinds.get("demo").map(String::as_str), Some("library"));
    assert_eq!(kinds.get("demo_test").map(String::as_str), Some("test"));
    assert_eq!(
        kinds.get("hello_example").map(String::as_str),
        Some("example")
    );
}

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

[target.broken]
type = "cpp_tests"
sources = ["src/x.cc"]
"#,
        )
        .unwrap();
    let assertion = cabin()
        .args(["metadata", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    // Wording is stable: enumerate the supported kinds so the
    // user can correct the typo without reading docs.
    assert!(
        stderr.contains("\"test\"")
            && stderr.contains("\"library\"")
            && stderr.contains("\"executable\"")
            && stderr.contains("\"header-only\"")
            && stderr.contains("\"example\""),
        "expected target-type error mentioning the supported kinds, got: {stderr}"
    );
}

#[test]
fn build_default_does_not_build_dev_only_targets() {
    require_cxx_build_tools();
    let dir = project_with_dev_kinds();
    cabin()
        .args(["build", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();

    // The library object/archive must build; the dev-only
    // targets must NOT appear in the generated default build plan.
    let archive = dir
        .path()
        .join("build/dev/packages/demo")
        .join(host_static_lib("demo"));
    assert!(
        archive.exists(),
        "library archive should build at {}",
        archive.display()
    );
    let ninja = fs::read_to_string(dir.path().join("build/dev/build.ninja")).unwrap();
    for forbidden in ["demo_test", "hello_example"] {
        assert!(
            !ninja.contains(forbidden),
            "default build must not plan {forbidden}: {ninja}"
        );
    }
}

#[test]
fn cabin_test_builds_and_runs_passing_test() {
    require_cxx_build_tools();
    let dir = passing_test_project();
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test demo:demo_test ... ok"),
        "expected per-test result line, got: {stdout}"
    );
    // The summary line carries the full `cargo test` field
    // shape; only the trailing wall-clock time is variable.
    assert!(
        stdout.contains(
            "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in"
        ),
        "expected passing summary, got: {stdout}"
    );
}

/// Single-package fixture with one library and three passing
/// test targets, for `--test <NAME>` selection coverage.
fn three_test_project() -> TempDir {
    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.alpha_test]
type = "test"
sources = ["tests/alpha_test.cc"]
deps = ["demo"]

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

[target.gamma_test]
type = "test"
sources = ["tests/gamma_test.cc"]
deps = ["demo"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int demo() { return 42; }\n")
        .unwrap();
    for name in ["alpha_test", "beta_test", "gamma_test"] {
        dir.child(format!("tests/{name}.cc"))
            .write_str("int main() { return 0; }\n")
            .unwrap();
    }
    dir
}

#[test]
fn cabin_test_runs_only_the_named_test_target() {
    require_cxx_build_tools();
    let dir = three_test_project();
    let assertion = cabin()
        .args(["test", "--test", "beta_test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("running 1 test"),
        "expected single-test header, got: {stdout}"
    );
    assert!(
        stdout.contains("test demo:beta_test ... ok"),
        "expected the named test to run, got: {stdout}"
    );
    for absent in ["test demo:alpha_test", "test demo:gamma_test"] {
        assert!(
            !stdout.contains(absent),
            "deselected test must not run ({absent}): {stdout}"
        );
    }
    // The two deselected test targets surface in the summary's
    // `filtered out` field.
    assert!(
        stdout
            .contains("test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out"),
        "expected filtered-out count in summary, got: {stdout}"
    );
}

#[test]
fn cabin_test_repeated_test_flags_run_each_named_target() {
    require_cxx_build_tools();
    let dir = three_test_project();
    let assertion = cabin()
        .args([
            "test",
            "--test",
            "alpha_test",
            "--test",
            "gamma_test",
            "--manifest-path",
        ])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    for expected in ["test demo:alpha_test ... ok", "test demo:gamma_test ... ok"] {
        assert!(
            stdout.contains(expected),
            "expected `{expected}`, got: {stdout}"
        );
    }
    assert!(
        !stdout.contains("test demo:beta_test"),
        "deselected test must not run: {stdout}"
    );
    assert!(
        stdout
            .contains("test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out"),
        "expected filtered-out count in summary, got: {stdout}"
    );
}

#[test]
fn cabin_test_unknown_named_test_errors_even_with_allow_no_tests() {
    let dir = passing_test_project();
    // `--allow-no-tests` must not soften an explicitly named
    // test that does not exist.
    let assertion = cabin()
        .args([
            "test",
            "--test",
            "nope_test",
            "--allow-no-tests",
            "--manifest-path",
        ])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stderr.contains("--test `nope_test` was not found in the selected packages"),
        "expected unknown-test error, got: {stderr}"
    );
}

#[test]
fn cabin_test_named_target_of_other_kind_errors() {
    let dir = passing_test_project();
    // `demo` exists, but as the library target.
    let assertion = cabin()
        .args(["test", "--test", "demo", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stderr.contains("--test `demo` matched a target of kind `library`; expected `test`"),
        "expected kind-mismatch error, got: {stderr}"
    );
}

#[test]
fn cabin_test_named_target_runs_in_every_matching_package() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // Two members declare a same-named test target; `--test`
    // keeps every match across the selected packages.
    dir.child("cabin.toml")
        .write_str(
            r#"[workspace]
members = ["packages/a", "packages/b"]
"#,
        )
        .unwrap();
    for member in ["a", "b"] {
        dir.child(format!("packages/{member}/cabin.toml"))
            .write_str(&format!(
                r#"[package]
name = "{member}"
version = "0.1.0"
cxx-standard = "c++17"

[target.{member}]
type = "library"
sources = ["src/lib.cc"]

[target.common_test]
type = "test"
sources = ["tests/lib_test.cc"]
deps = ["{member}"]
"#
            ))
            .unwrap();
        dir.child(format!("packages/{member}/src/lib.cc"))
            .write_str("int x() { return 0; }\n")
            .unwrap();
        dir.child(format!("packages/{member}/tests/lib_test.cc"))
            .write_str("int main() { return 0; }\n")
            .unwrap();
    }
    let assertion = cabin()
        .args([
            "test",
            "--workspace",
            "--test",
            "common_test",
            "--manifest-path",
        ])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    for expected in ["test a:common_test ... ok", "test b:common_test ... ok"] {
        assert!(
            stdout.contains(expected),
            "expected `{expected}`, got: {stdout}"
        );
    }
    assert!(
        stdout
            .contains("test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out"),
        "expected both matches to run, got: {stdout}"
    );
}

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

[target.env_test]
type = "test"
sources = ["tests/env_test.cc"]
"#,
        )
        .unwrap();
    dir.child("tests/env_test.cc")
        .write_str(
            r#"#include <cstdio>
#include <cstdlib>
#include <cstring>

static int status = 0;

void keep(const char* name, const char* expected) {
    const char* v = std::getenv(name);
    if (v == nullptr) {
        std::printf("MISSING %s\n", name);
        status |= 1;
        return;
    }
    std::printf("KEEP %s=%s\n", name, v);
    if (expected != nullptr && std::strcmp(v, expected) != 0) {
        status |= 2;
    }
}

void keep_present(const char* name) {
    const char* v = std::getenv(name);
    if (v == nullptr || v[0] == '\0') {
        std::printf("MISSING %s\n", name);
        status |= 1;
        return;
    }
    std::printf("KEEP %s\n", name);
}

void must_be_absent(const char* name) {
    if (std::getenv(name) != nullptr) {
        std::printf("LEAK %s\n", name);
        status |= 4;
    } else {
        std::printf("ABSENT %s\n", name);
    }
}

int main() {
    keep("CABIN_PACKAGE_NAME", "env_demo");
    keep("CABIN_PACKAGE_VERSION", "0.1.0");
    keep("CABIN_PROFILE", "dev");
    keep_present("CABIN_MANIFEST_DIR");
    keep_present("CABIN_MANIFEST_PATH");
    keep_present("CABIN_BUILD_DIR");
    must_be_absent("CABIN");
    must_be_absent("CABIN_PACKAGE_NAME_CANONICAL");
    must_be_absent("CABIN_BIN_NAME");
    must_be_absent("CABIN_BIN_NAME_CANONICAL");
    must_be_absent("CABIN_TEST_NAME");
    must_be_absent("CABIN_TEST_NAME_CANONICAL");
    must_be_absent("CABIN_TARGET_KIND");
    must_be_absent("CABIN_TARGET_TRIPLE");
    must_be_absent("CABIN_HOST_TRIPLE");
    must_be_absent("CABIN_BUILD_CONFIGURATION_FINGERPRINT");
    return status;
}
"#,
        )
        .unwrap();

    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    for expected in [
        "KEEP CABIN_PACKAGE_NAME=env_demo",
        "KEEP CABIN_PACKAGE_VERSION=0.1.0",
        "KEEP CABIN_PROFILE=dev",
        "KEEP CABIN_MANIFEST_DIR",
        "KEEP CABIN_MANIFEST_PATH",
        "KEEP CABIN_BUILD_DIR",
        "ABSENT CABIN",
        "ABSENT CABIN_PACKAGE_NAME_CANONICAL",
        "ABSENT CABIN_BIN_NAME",
        "ABSENT CABIN_BIN_NAME_CANONICAL",
        "ABSENT CABIN_TEST_NAME",
        "ABSENT CABIN_TEST_NAME_CANONICAL",
        "ABSENT CABIN_TARGET_KIND",
        "ABSENT CABIN_TARGET_TRIPLE",
        "ABSENT CABIN_HOST_TRIPLE",
        "ABSENT CABIN_BUILD_CONFIGURATION_FINGERPRINT",
        "test env_demo:env_test ... ok",
    ] {
        assert!(
            stdout.contains(expected),
            "expected `{expected}` in test output, got: {stdout}"
        );
    }
    assert!(
        !stdout.contains("LEAK "),
        "no removed CABIN_* variable may be injected, got: {stdout}"
    );
}

#[test]
fn cabin_test_exits_non_zero_on_failure() {
    require_cxx_build_tools();
    let dir = passing_test_project();
    dir.child("tests/lib_test.cc")
        .write_str("int main() { return 17; }\n")
        .unwrap();
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stdout.contains("test demo:demo_test ... FAILED (exit 17)"),
        "expected per-test failure line, got stdout: {stdout}"
    );
    // The cargo-style epilogue recaps the failed test names
    // before the summary line.
    assert!(
        stdout.contains("failures:\n    demo:demo_test"),
        "expected failures recap, got stdout: {stdout}"
    );
    assert!(
        stdout.contains("test result: FAILED. 0 passed; 1 failed"),
        "expected failing summary line, got stdout: {stdout}"
    );
    assert!(
        stderr.contains("test failures: 1 of 1"),
        "expected failure summary in stderr, got: {stderr}"
    );
}

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

[target.lib_only]
type = "library"
sources = ["src/lib.cc"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int x() { return 1; }\n")
        .unwrap();
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stderr.contains("no test targets found"),
        "expected no-test-targets error, got: {stderr}"
    );
}

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

[target.lib_only]
type = "library"
sources = ["src/lib.cc"]
"#,
        )
        .unwrap();
    dir.child("src/lib.cc")
        .write_str("int x() { return 1; }\n")
        .unwrap();
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .arg("--allow-no-tests")
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("no test targets found"),
        "expected explanatory line, got: {stdout}"
    );
}

#[test]
fn cabin_test_runs_in_deterministic_package_then_target_order() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // Workspace with two members; member `b` declares its
    // tests *before* member `a` in TOML order, but the runner
    // must sort by package then target.
    dir.child("cabin.toml")
        .write_str(
            r#"[workspace]
members = ["packages/b", "packages/a"]
"#,
        )
        .unwrap();
    for (member, deps_table) in [("a", "[target.a_z_test]"), ("b", "[target.b_a_test]")] {
        assert_fs::fixture::ChildPath::new(
            dir.path().join(format!("packages/{member}/cabin.toml")),
        )
        .write_str(&format!(
            r#"[package]
name = "{member}"
version = "0.1.0"
cxx-standard = "c++17"

[target.{member}]
type = "library"
sources = ["src/lib.cc"]

{deps_table}
type = "test"
sources = ["tests/lib_test.cc"]
deps = ["{member}"]
"#
        ))
        .unwrap();
        dir.child(format!("packages/{member}/src/lib.cc"))
            .write_str("int x() { return 0; }\n")
            .unwrap();
        dir.child(format!("packages/{member}/tests/lib_test.cc"))
            .write_str("int main() { return 0; }\n")
            .unwrap();
    }
    let assertion = cabin()
        .args(["test", "--workspace", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    // Both tests must appear, with `a:a_z_test` before
    // `b:b_a_test` regardless of TOML declaration order.
    let a_pos = stdout
        .find("test a:a_z_test ... ok")
        .unwrap_or_else(|| panic!("missing a:a_z_test in: {stdout}"));
    let b_pos = stdout
        .find("test b:b_a_test ... ok")
        .unwrap_or_else(|| panic!("missing b:b_a_test in: {stdout}"));
    assert!(
        a_pos < b_pos,
        "tests must run in (package, target) ascending order; got: {stdout}"
    );
}

#[test]
fn cabin_test_rejects_target_flag_as_unknown_argument() {
    // `cabin test` mirrors `cabin build`: the historic
    // `--target` manifest-target selector is gone, with the
    // flag name reserved for a future platform/toolchain
    // target. clap must reject the flag at parse time so the
    // overload cannot creep back in.
    cabin()
        .args(["test", "--target", "foo"])
        .assert()
        .failure()
        .code(2)
        .stderr(predicate::str::contains(
            "unexpected argument '--target' found",
        ));
}

#[test]
fn package_archive_includes_test_and_example_sources() {
    let dir = project_with_dev_kinds();
    let out = dir.path().join("dist");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .args(["--output-dir"])
        .arg(&out)
        .assert()
        .success();
    // The archive must carry every declared source - including
    // dev-only target sources - so the package round-trips.
    let archive = out.join("demo-0.1.0.tar.gz");
    let bytes = fs::read(&archive).expect("archive readable");
    let listing = list_tar_gz_paths(&bytes);
    for expected in ["src/lib.cc", "tests/lib_test.cc", "examples/hello.cc"] {
        assert!(
            listing.iter().any(|p| p.ends_with(expected)),
            "archive missing {expected}; got: {listing:?}"
        );
    }
}

fn list_tar_gz_paths(bytes: &[u8]) -> Vec<String> {
    let decoder = flate2::read::GzDecoder::new(bytes);
    let mut archive = tar::Archive::new(decoder);
    archive
        .entries()
        .expect("entries iterator")
        .map(|e| {
            e.expect("entry")
                .path()
                .expect("path")
                .to_string_lossy()
                .into_owned()
        })
        .collect()
}