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
use super::*;
use flate2::read::GzDecoder;
use std::collections::BTreeSet;

fn write_simple_package(root: &Path) {
    assert_fs::fixture::ChildPath::new(root.join("cabin.toml"))
        .write_str(
            r#"[package]
name = "fmt"
version = "10.2.1"
cxx-standard = "c++17"

[target.fmt]
type = "library"
sources = ["src/fmt.cc"]
include-dirs = ["include"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("include/example.h"))
        .write_str("#pragma once\nvoid say_hello();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("src/fmt.cc"))
        .write_str("#include \"example.h\"\nvoid say_hello() {}\n")
        .unwrap();
}

fn read_archive_entries(archive: &Path) -> BTreeSet<String> {
    let f = fs::File::open(archive).unwrap();
    let dec = GzDecoder::new(f);
    let mut tar = tar::Archive::new(dec);
    let mut out = BTreeSet::new();
    for entry in tar.entries().unwrap() {
        let entry = entry.unwrap();
        out.insert(entry.path().unwrap().to_string_lossy().into_owned());
    }
    out
}

#[test]
fn package_creates_archive_and_metadata() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();

    let archive = dist.join("fmt-10.2.1.tar.gz");
    let metadata = dist.join("fmt-10.2.1.json");
    assert!(archive.is_file(), "archive missing: {archive:?}");
    assert!(metadata.is_file(), "metadata missing: {metadata:?}");

    let body = fs::read_to_string(&metadata).unwrap();
    let value: serde_json::Value = serde_json::from_str(&body).unwrap();
    assert_eq!(value["schema"], 1);
    assert_eq!(value["name"], "fmt");
    assert_eq!(value["version"], "10.2.1");
    assert_eq!(value["yanked"], false);
    assert!(value["checksum"].as_str().unwrap().starts_with("sha256:"));
    assert_eq!(value["source"]["type"], "archive");
    assert_eq!(value["source"]["format"], "tar.gz");
    assert!(
        value["source"]["path"]
            .as_str()
            .unwrap()
            .ends_with("fmt-10.2.1.tar.gz")
    );
}

#[test]
fn package_metadata_preserves_manifest_compiler_wrapper_setting() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let manifest_path = dir.path().join("cabin.toml");
    let mut manifest = fs::read_to_string(&manifest_path).unwrap();
    manifest.push_str(
        r#"
[build]
compiler-wrapper = "ccache"
"#,
    );
    assert_fs::fixture::ChildPath::new(&manifest_path)
        .write_str(&manifest)
        .unwrap();
    let dist = dir.path().join("dist");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(&manifest_path)
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();

    let body = fs::read_to_string(dist.join("fmt-10.2.1.json")).unwrap();
    let value: serde_json::Value = serde_json::from_str(&body).unwrap();
    assert_eq!(
        value["compiler_wrapper"],
        serde_json::json!({"kind": "use", "wrapper": "ccache"})
    );
}

#[test]
fn package_json_format_emits_machine_readable_summary() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    let value = run_json(
        cabin()
            .args(["package", "--manifest-path"])
            .arg(dir.path().join("cabin.toml"))
            .arg("--output-dir")
            .arg(&dist)
            .args(["--format", "json"]),
    );
    assert_eq!(value["name"], "fmt");
    assert_eq!(value["version"], "10.2.1");
    assert!(
        value["archive_path"]
            .as_str()
            .unwrap()
            .ends_with("fmt-10.2.1.tar.gz")
    );
    assert!(
        value["metadata_path"]
            .as_str()
            .unwrap()
            .ends_with("fmt-10.2.1.json")
    );
    assert!(value["checksum"].as_str().unwrap().starts_with("sha256:"));
}

#[test]
fn package_excludes_generated_and_vcs_files() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    // Files that must NOT appear in the archive.
    dir.child(".git/config").write_str("leak-this").unwrap();
    dir.child("build/build.ninja")
        .write_str("leak-this")
        .unwrap();
    dir.child("dist/old.tar.gz").write_str("leak-this").unwrap();
    dir.child(".cabin/cache/x").write_str("leak-this").unwrap();
    dir.child("node_modules/foo/x")
        .write_str("leak-this")
        .unwrap();
    dir.child("compile_commands.json")
        .write_str("leak-this")
        .unwrap();
    dir.child("cabin.lock").write_str("leak-this").unwrap();
    dir.child("build.ninja").write_str("leak-this").unwrap();

    let dist = dir.path().join("artifact-out");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();

    let entries = read_archive_entries(&dist.join("fmt-10.2.1.tar.gz"));
    assert!(entries.contains("cabin.toml"));
    assert!(entries.contains("src/fmt.cc"));
    assert!(entries.contains("include/example.h"));
    for forbidden in &[
        ".git/config",
        "build/build.ninja",
        "dist/old.tar.gz",
        ".cabin/cache/x",
        "node_modules/foo/x",
        "compile_commands.json",
        "cabin.lock",
        "build.ninja",
    ] {
        assert!(
            !entries.iter().any(|e| e == forbidden),
            "archive leaked {forbidden}: {entries:?}"
        );
    }
}

#[test]
fn package_excludes_in_tree_custom_output_dir() {
    // A custom --output-dir living inside the package source
    // tree (and not on the hard-coded EXCLUDED_DIR_NAMES list)
    // must be skipped during staging so the next archive does
    // not embed last run's `.tar.gz` / `.json` and the
    // idempotent-rewrite check stays meaningful.
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let out = dir.path().join("myoutput");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&out)
        .assert()
        .success();
    // Second run uses the same in-tree output dir.  With the
    // bug present, the staging walker pulls last run's
    // archive into the new archive, the bytes drift, and the
    // idempotent rewrite refuses the differing existing
    // archive.  With the fix, the second run is a no-op.
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&out)
        .assert()
        .success();
    let entries = read_archive_entries(&out.join("fmt-10.2.1.tar.gz"));
    assert!(entries.contains("cabin.toml"));
    assert!(entries.contains("src/fmt.cc"));
    assert!(
        !entries.iter().any(|e| e.starts_with("myoutput/")),
        "custom output dir leaked into archive: {entries:?}"
    );
}

#[test]
fn package_rejects_output_dir_equal_to_package_root() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let assertion = cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(dir.path())
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stderr.contains("equals the package source root"),
        "expected output_dir == package_root rejection, got: {stderr}"
    );
}

#[test]
fn package_is_byte_deterministic_across_runs() {
    // Write the package and the two output directories in
    // *separate* trees: `pkg-a/` and `pkg-b/`.  Both packages have
    // identical source content.  Each run targets an output dir
    // outside the package root so neither archive picks up the
    // other run's `dist-*/` contents.
    let dir = TempDir::new().unwrap();
    let pkg_a = dir.path().join("pkg-a");
    let pkg_b = dir.path().join("pkg-b");
    write_simple_package(&pkg_a);
    write_simple_package(&pkg_b);

    let dist_a = dir.path().join("dist-a");
    let dist_b = dir.path().join("dist-b");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(pkg_a.join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist_a)
        .assert()
        .success();
    cabin()
        .args(["package", "--manifest-path"])
        .arg(pkg_b.join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist_b)
        .assert()
        .success();

    let bytes_a = fs::read(dist_a.join("fmt-10.2.1.tar.gz")).unwrap();
    let bytes_b = fs::read(dist_b.join("fmt-10.2.1.tar.gz")).unwrap();
    assert_eq!(bytes_a, bytes_b, "archives must be byte-identical");
}

#[test]
fn publish_dry_run_creates_archive_and_reports_no_registry_modified() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    let output = cabin()
        .args(["publish", "--dry-run", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success()
        .get_output()
        .clone();
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Publish dry-run"));
    assert!(stdout.contains("No registry was modified"));
    assert!(dist.join("fmt-10.2.1.tar.gz").is_file());
    assert!(dist.join("fmt-10.2.1.json").is_file());
}

#[test]
fn publish_dry_run_json_format_is_valid_json() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    let value = run_json(
        cabin()
            .args(["publish", "--dry-run", "--manifest-path"])
            .arg(dir.path().join("cabin.toml"))
            .arg("--output-dir")
            .arg(&dist)
            .args(["--format", "json"]),
    );
    assert_eq!(value["dry_run"], true);
    assert_eq!(value["registry_modified"], false);
    assert_eq!(value["name"], "fmt");
    assert_eq!(value["version"], "10.2.1");
}

#[test]
fn publish_without_dry_run_fails_clearly() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    cabin()
        .args(["publish", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("--dry-run"));
}

#[test]
fn package_with_path_dependency_fails_clearly() {
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
local = { path = "../local" }
"#,
        )
        .unwrap();
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("path dependencies"));
}

#[test]
fn package_workspace_root_without_project_fails_clearly() {
    let dir = TempDir::new().unwrap();
    dir.child("cabin.toml")
        .write_str(
            r#"[workspace]
members = ["packages/*"]
"#,
        )
        .unwrap();
    dir.child("packages/a/cabin.toml")
        .write_str(
            r#"[package]
name = "a"
version = "0.1.0"
"#,
        )
        .unwrap();
    // `cabin package` against a workspace root must refuse
    // without a single `--package <name>` selection.
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("--package <name>"));
}

#[test]
fn package_overwrite_with_identical_bytes_succeeds() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();
    // Second run with the same input must succeed silently.
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();
}

#[test]
fn package_overwrite_with_different_bytes_fails() {
    let dir = TempDir::new().unwrap();
    write_simple_package(dir.path());
    let dist = dir.path().join("dist");
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .success();
    // Stomp on the existing archive with junk; a re-run must fail.
    assert_fs::fixture::ChildPath::new(dist.join("fmt-10.2.1.tar.gz"))
        .write_binary(b"not the same bytes")
        .unwrap();
    cabin()
        .args(["package", "--manifest-path"])
        .arg(dir.path().join("cabin.toml"))
        .arg("--output-dir")
        .arg(&dist)
        .assert()
        .failure()
        .stderr(predicate::str::contains("already exists"));
}