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
//! End-to-end coverage for `required-features` on targets: default
//! enumeration skips gated targets, explicit requests (a `deps`
//! entry, `cabin test --test`) hard-error, and feature selection
//! (CLI flags or dependency-edge requests) makes gated targets
//! buildable.

use super::*;

/// Single package with an ungated `core` library and a `tls`
/// library gated on the `ssl` feature.
fn write_gated_package(root: &Path) {
    assert_fs::fixture::ChildPath::new(root.join("cabin.toml"))
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
default = []
ssl = []

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

[target.tls]
type = "library"
sources = ["src/tls.cc"]
required-features = ["ssl"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/core.cc"))
        .write_str("int core_value() { return 1; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/tls.cc"))
        .write_str("int tls_value() { return 2; }\n")
        .unwrap();
}

/// The static-archive spellings of `name` across both dialects
/// (`lib<name>.a` for GNU/Clang, `<name>.lib` for MSVC).
fn archive_spellings(name: &str) -> [String; 2] {
    [format!("lib{name}.a"), format!("{name}.lib")]
}

fn archive_exists(build_dir: &Path, package: &str, name: &str) -> bool {
    archive_spellings(name).iter().any(|spelling| {
        build_dir
            .join("dev/packages")
            .join(package)
            .join(spelling)
            .exists()
    })
}

#[test]
fn default_build_skips_gated_target() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_package(dir.path());
    let build_dir = dir.path().join("build");
    cabin()
        .args(["build", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(&build_dir)
        .assert()
        .success();
    assert!(archive_exists(&build_dir, "demo", "core"));
    assert!(
        !archive_exists(&build_dir, "demo", "tls"),
        "feature-gated target must be skipped by the default build"
    );
}

#[test]
fn build_with_feature_builds_gated_target() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_package(dir.path());
    let build_dir = dir.path().join("build");
    cabin()
        .args(["build", "--features", "ssl", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(&build_dir)
        .assert()
        .success();
    assert!(archive_exists(&build_dir, "demo", "tls"));
}

#[test]
fn build_all_features_builds_gated_c_target() {
    // C parity: a gated C library follows the same rules.
    require_c_and_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("cabin.toml"))
        .write_str(
            r#"[package]
name = "cdemo"
version = "0.1.0"
c-standard = "c11"

[features]
compress = []

[target.cz]
type = "library"
sources = ["src/cz.c"]
required-features = ["compress"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("src/cz.c"))
        .write_str("int cz_value(void) { return 3; }\n")
        .unwrap();
    let build_dir = dir.path().join("build");
    cabin()
        .args(["build", "--all-features", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(&build_dir)
        .assert()
        .success();
    assert!(archive_exists(&build_dir, "cdemo", "cz"));
}

#[test]
fn all_gated_default_build_reports_actionable_error() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("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();
    assert_fs::fixture::ChildPath::new(dir.path().join("src/tls.cc"))
        .write_str("int tls_value() { return 2; }\n")
        .unwrap();
    cabin()
        .args(["build", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("demo:tls"))
        .stderr(predicate::str::contains("--features"));
}

/// Workspace fixture: `app` links `netlib:tls`, which requires
/// netlib's `ssl` feature.  `edge_features` controls whether app's
/// dependency edge requests it.
fn write_gated_dep_workspace(root: &Path, edge_features: &str) {
    assert_fs::fixture::ChildPath::new(root.join("cabin.toml"))
        .write_str(
            r#"[workspace]
members = ["packages/*"]
default-members = ["packages/app"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("packages/netlib/cabin.toml"))
        .write_str(
            r#"[package]
name = "netlib"
version = "0.1.0"
cxx-standard = "c++17"

[features]
ssl = []

[target.tls]
type = "library"
sources = ["src/tls.cc"]
include-dirs = ["include"]
required-features = ["ssl"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("packages/netlib/include/tls.h"))
        .write_str("#pragma once\nint tls_value();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("packages/netlib/src/tls.cc"))
        .write_str("#include \"tls.h\"\nint tls_value() { return 2; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("packages/app/cabin.toml"))
        .write_str(&format!(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dependencies]
netlib = {{ path = "../netlib"{edge_features} }}

[target.app]
type = "executable"
sources = ["src/main.cc"]
deps = ["netlib:tls"]
"#
        ))
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("packages/app/src/main.cc"))
        .write_str("#include \"tls.h\"\nint main() { return tls_value() == 2 ? 0 : 1; }\n")
        .unwrap();
}

#[test]
fn dep_on_gated_target_without_feature_fails_with_edge_help() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_dep_workspace(dir.path(), "");
    cabin()
        .args(["build", "-p", "app", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("netlib:tls"))
        .stderr(predicate::str::contains("features = [\"ssl\"]"));
}

#[test]
fn dep_on_gated_target_with_edge_feature_builds() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_dep_workspace(dir.path(), ", features = [\"ssl\"]");
    cabin()
        .args(["build", "-p", "app", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
}

/// Package with one ungated and one feature-gated test target.
fn write_gated_test_package(root: &Path) {
    assert_fs::fixture::ChildPath::new(root.join("cabin.toml"))
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
slow = []

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

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

[target.slow_test]
type = "test"
sources = ["tests/slow.cc"]
deps = ["demo"]
required-features = ["slow"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/lib.cc"))
        .write_str("int lib_value() { return 1; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("tests/fast.cc"))
        .write_str("int main() { return 0; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("tests/slow.cc"))
        .write_str("int main() { return 0; }\n")
        .unwrap();
}

#[test]
fn test_enumeration_skips_gated_test_and_counts_it_filtered() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_test_package(dir.path());
    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("running 1 test"),
        "gated test must be skipped: {stdout}"
    );
    assert!(
        stdout.contains("1 filtered out"),
        "the skip must be visible in the summary: {stdout}"
    );
}

#[test]
fn test_with_feature_runs_gated_test() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_test_package(dir.path());
    let assertion = cabin()
        .args(["test", "--features", "slow", "--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 2 tests"),
        "enabled feature must un-gate the test: {stdout}"
    );
}

#[test]
fn named_gated_test_without_feature_is_a_hard_error() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_test_package(dir.path());
    cabin()
        .args(["test", "--test", "slow_test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("demo:slow_test"))
        .stderr(predicate::str::contains("--features slow"));
}

#[test]
fn all_gated_test_enumeration_names_the_gate() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("cabin.toml"))
        .write_str(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
slow = []

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

[target.slow_test]
type = "test"
sources = ["tests/slow.cc"]
deps = ["demo"]
required-features = ["slow"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("src/lib.cc"))
        .write_str("int lib_value() { return 1; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("tests/slow.cc"))
        .write_str("int main() { return 0; }\n")
        .unwrap();
    // Without `--allow-no-tests`, the failure must name the gated
    // targets rather than claim no test target exists.
    cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("demo:slow_test"))
        .stderr(predicate::str::contains("--features"))
        .stderr(predicate::str::contains("no test targets found").not());
    // With it, the outcome reports the filtered count instead of
    // pretending the package declares no tests.
    let assertion = cabin()
        .args(["test", "--allow-no-tests", "--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("1 filtered out by required-features"),
        "the all-gated skip must be visible: {stdout}"
    );
}

/// Package with one ungated and one feature-gated executable.
fn write_gated_run_package(root: &Path, gated_only: bool) {
    let main_target = if gated_only {
        ""
    } else {
        r#"
[target.main]
type = "executable"
sources = ["src/main.cc"]
"#
    };
    assert_fs::fixture::ChildPath::new(root.join("cabin.toml"))
        .write_str(&format!(
            r#"[package]
name = "demo"
version = "0.1.0"
cxx-standard = "c++17"

[features]
extra = []
{main_target}
[target.extra]
type = "executable"
sources = ["src/extra.cc"]
required-features = ["extra"]
"#
        ))
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/main.cc"))
        .write_str("#include <cstdio>\nint main() { std::puts(\"main ran\"); return 0; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/extra.cc"))
        .write_str("int main() { return 0; }\n")
        .unwrap();
}

#[test]
fn run_enumeration_skips_gated_executable() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_run_package(dir.path(), false);
    // The gated `extra` executable must not make the default run
    // ambiguous; `main` is the only buildable candidate.
    let assertion = cabin()
        .args(["run", "--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("main ran"), "stdout = {stdout}");
}

#[test]
fn run_with_only_gated_executables_reports_missing_features() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    write_gated_run_package(dir.path(), true);
    cabin()
        .args(["run", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("demo:extra"))
        .stderr(predicate::str::contains("--features extra"));
}

#[test]
fn metadata_reports_required_features() {
    let dir = TempDir::new().unwrap();
    write_gated_package(dir.path());
    let assertion = cabin()
        .args(["metadata", "--format", "json", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    let json: serde_json::Value = serde_json::from_str(&stdout).expect("metadata is valid JSON");
    let packages = json["packages"].as_array().expect("packages array");
    let demo = packages
        .iter()
        .find(|p| p["name"] == "demo")
        .expect("demo package present");
    let targets = demo["targets"].as_array().expect("targets array");
    let tls = targets
        .iter()
        .find(|t| t["name"] == "tls")
        .expect("tls target present");
    assert_eq!(tls["required_features"][0], "ssl");
    let core = targets
        .iter()
        .find(|t| t["name"] == "core")
        .expect("core target present");
    assert!(
        core.get("required_features").is_none(),
        "ungated targets keep their previous JSON shape"
    );
}