cabinpkg 0.15.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
//! 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"

[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"

[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"

[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();
    // `-v` keeps Ninja's `[N/M] AR / CXX / LINK …` progress
    // lines on stdout so the assertion below can pin the
    // archive action.  At the default verbosity the lines
    // are filtered to match cargo's terser banner shape.
    let assertion = cabin()
        .args(["build", "-v", "--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);
    // The library object/archive must build; the dev-only
    // targets must NOT appear in the ninja output.
    assert!(
        stdout.contains("AR"),
        "library archive should build: {stdout}"
    );
    for forbidden in ["demo_test", "hello_example"] {
        assert!(
            !stdout.contains(forbidden),
            "default build must not produce {forbidden}: {stdout}"
        );
    }
}

#[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}"
    );
    assert!(
        stdout.contains("test result: ok. 1 passed; 0 failed"),
        "expected passing summary, 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"

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

[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"

[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"

[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()
}