shipper-cli 0.3.0-rc.2

CLI adapter for Shipper. Install with `cargo install shipper --locked`; this crate is for embedders who want the exact CLI surface programmatically.
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
//! BDD (Behavior-Driven Development) tests for error recovery and retry scenarios.
//!
//! These tests describe the expected behavior of shipper when encountering
//! failures, corrupted files, and retry/resume workflows using Given-When-Then
//! style documentation.

use std::fs;
use std::path::Path;

use assert_cmd::Command;
use predicates::str::contains;
use tempfile::tempdir;

fn write_file(path: &Path, content: &str) {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).expect("mkdir");
    }
    fs::write(path, content).expect("write");
}

fn create_single_crate_workspace(root: &Path) {
    write_file(
        &root.join("Cargo.toml"),
        r#"
[workspace]
members = ["demo"]
resolver = "2"
"#,
    );

    write_file(
        &root.join("demo/Cargo.toml"),
        r#"
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
"#,
    );
    write_file(&root.join("demo/src/lib.rs"), "pub fn demo() {}\n");
}

fn create_multi_crate_workspace(root: &Path) {
    write_file(
        &root.join("Cargo.toml"),
        r#"
[workspace]
members = ["core", "utils"]
resolver = "2"
"#,
    );

    write_file(
        &root.join("core/Cargo.toml"),
        r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"
"#,
    );
    write_file(&root.join("core/src/lib.rs"), "pub fn core() {}\n");

    write_file(
        &root.join("utils/Cargo.toml"),
        r#"
[package]
name = "utils"
version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core" }
"#,
    );
    write_file(&root.join("utils/src/lib.rs"), "pub fn utils() {}\n");
}

fn shipper_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("shipper-cli"))
}

// ============================================================================
// Feature: Failure Classification
// ============================================================================

mod failure_classification {

    // Scenario: Retryable failure is classified correctly by the library
    #[test]
    fn given_retryable_stderr_when_classifying_then_retryable() {
        // Given: cargo publish output containing a retryable pattern (rate limit)
        let outcome =
            shipper_core::cargo_failure::classify_publish_failure("HTTP 429 too many requests", "");

        // Then: The failure is classified as retryable
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Retryable
        );
    }

    // Scenario: Permanent failure is classified correctly by the library
    #[test]
    fn given_permanent_stderr_when_classifying_then_permanent() {
        // Given: cargo publish output containing a permanent failure pattern
        let outcome =
            shipper_core::cargo_failure::classify_publish_failure("permission denied", "");

        // Then: The failure is classified as permanent
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Permanent
        );
    }

    // Scenario: Ambiguous failure when no known pattern matches
    #[test]
    fn given_unknown_error_when_classifying_then_ambiguous() {
        // Given: cargo publish output with no recognizable pattern
        let outcome = shipper_core::cargo_failure::classify_publish_failure(
            "something completely unexpected happened",
            "",
        );

        // Then: The failure is classified as ambiguous
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Ambiguous
        );
    }

    // Scenario: Retryable patterns take precedence over permanent patterns
    #[test]
    fn given_both_retryable_and_permanent_patterns_when_classifying_then_retryable_wins() {
        // Given: cargo publish output containing both retryable and permanent patterns
        let outcome = shipper_core::cargo_failure::classify_publish_failure(
            "permission denied and 429 too many requests",
            "",
        );

        // Then: Retryable takes precedence
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Retryable
        );
    }

    // Scenario: Network timeout patterns are classified as retryable
    #[test]
    fn given_timeout_error_when_classifying_then_retryable() {
        let outcome = shipper_core::cargo_failure::classify_publish_failure(
            "operation timed out after 30s",
            "",
        );
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Retryable
        );
    }

    // Scenario: Authentication failures are classified as permanent
    #[test]
    fn given_auth_failure_when_classifying_then_permanent() {
        let outcome = shipper_core::cargo_failure::classify_publish_failure("401 unauthorized", "");
        assert_eq!(
            outcome.class,
            shipper_core::cargo_failure::CargoFailureClass::Permanent
        );
    }
}

// ============================================================================
// Feature: State Persistence After Failure
// ============================================================================

mod state_persistence_after_failure {
    use super::*;

    // Scenario: Resume with corrupted state file reports parse error
    #[test]
    fn given_corrupted_state_file_when_resume_then_reports_parse_error() {
        // Given: A workspace with a corrupted state.json in the state directory
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("custom-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("state.json"), "NOT VALID JSON {{{").expect("write corrupt state");

        // When: We run shipper resume
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            // Then: It fails with a JSON parse error
            .assert()
            .failure()
            .stderr(contains("failed to parse state JSON"));
    }

    // Scenario: State file with wrong plan_id is rejected on resume
    #[test]
    fn given_state_with_mismatched_plan_id_when_publish_then_rejected() {
        // Given: A workspace and a state file with a different plan_id
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("custom-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");

        let state_json = serde_json::json!({
            "state_version": "shipper.state.v1",
            "plan_id": "wrong-plan-id-12345",
            "registry": {
                "name": "crates-io",
                "api_base": "https://crates.io",
                "index_base": "https://index.crates.io"
            },
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "packages": {}
        });
        fs::write(
            state_dir.join("state.json"),
            serde_json::to_string_pretty(&state_json).expect("serialize"),
        )
        .expect("write state");

        // When: We run shipper publish (which will load existing state)
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("--allow-dirty")
            .arg("publish")
            // Then: It fails because the plan_id doesn't match
            .assert()
            .failure()
            .stderr(contains("does not match"));
    }

    // Scenario: Empty state file is treated as invalid JSON
    #[test]
    fn given_empty_state_file_when_resume_then_reports_parse_error() {
        // Given: A workspace with an empty state.json
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("empty-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("state.json"), "").expect("write empty state");

        // When: We run shipper resume
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            // Then: It fails with a parse error
            .assert()
            .failure()
            .stderr(contains("failed to parse state JSON"));
    }
}

// ============================================================================
// Feature: Resume After Failure Skips Completed Packages
// ============================================================================

mod resume_skips_completed {
    use super::*;

    // Scenario: Resume with state showing a published package skips it
    #[test]
    fn given_state_with_published_package_when_resume_then_skips_completed() {
        // Given: A multi-crate workspace where core is already published in state
        let td = tempdir().expect("tempdir");
        create_multi_crate_workspace(td.path());

        // Use the library directly to get the deterministic plan_id
        let spec = shipper_core::types::ReleaseSpec {
            manifest_path: td.path().join("Cargo.toml"),
            registry: shipper_core::types::Registry::crates_io(),
            selected_packages: None,
        };
        let ws = shipper_core::plan::build_plan(&spec).expect("build plan");
        let plan_id = &ws.plan.plan_id;

        let state_dir = td.path().join("custom-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");

        // Write state with "core" already published
        let state_json = serde_json::json!({
            "state_version": "shipper.state.v1",
            "plan_id": plan_id,
            "registry": {
                "name": "crates-io",
                "api_base": "https://crates.io",
                "index_base": "https://index.crates.io"
            },
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "packages": {
                "core@0.1.0": {
                    "name": "core",
                    "version": "0.1.0",
                    "attempts": 1,
                    "state": { "state": "published" },
                    "last_updated_at": "2024-01-01T00:00:00Z"
                },
                "utils@0.1.0": {
                    "name": "utils",
                    "version": "0.1.0",
                    "attempts": 0,
                    "state": { "state": "pending" },
                    "last_updated_at": "2024-01-01T00:00:00Z"
                }
            }
        });
        fs::write(
            state_dir.join("state.json"),
            serde_json::to_string_pretty(&state_json).expect("serialize"),
        )
        .expect("write state");

        // When: We run shipper resume (it will fail eventually trying to publish
        // to crates.io, but the key behavior is that it skips the published package)
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("--allow-dirty")
            .arg("resume")
            .output()
            .expect("resume");

        let stderr = String::from_utf8_lossy(&output.stderr);

        // Then: The output indicates core was already complete (skipped)
        assert!(
            stderr.contains("already complete") || stderr.contains("already published"),
            "expected 'already complete' or 'already published' in stderr, got:\n{stderr}"
        );
    }

    // Scenario: No state file means resume fails
    #[test]
    fn given_no_state_file_when_resume_then_fails() {
        // Given: A valid workspace with no prior publish state
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("empty-state");
        fs::create_dir_all(&state_dir).expect("mkdir");

        // When: We run shipper resume
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            // Then: It fails because there's no state to resume from
            .assert()
            .failure()
            .stderr(contains("no existing state found"));
    }
}

// ============================================================================
// Feature: Invalid State File Handling
// ============================================================================

mod invalid_state_handling {
    use super::*;

    // Scenario: State file with invalid JSON structure (valid JSON but wrong schema)
    #[test]
    fn given_state_with_wrong_schema_when_resume_then_fails_gracefully() {
        // Given: A workspace and a state file that is valid JSON but wrong structure
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("bad-schema-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(
            state_dir.join("state.json"),
            r#"{"unexpected": "schema", "not": "execution_state"}"#,
        )
        .expect("write bad schema state");

        // When: We run shipper resume
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            .output()
            .expect("resume");

        // Then: It fails gracefully without a panic (non-zero exit, error on stderr)
        assert!(!output.status.success());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("failed to parse state JSON") || stderr.contains("missing field"),
            "expected parse/schema error in stderr, got:\n{stderr}"
        );
    }

    // Scenario: State file with truncated JSON
    #[test]
    fn given_truncated_state_json_when_resume_then_fails_gracefully() {
        // Given: A workspace with a truncated (incomplete) state.json
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("truncated-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(
            state_dir.join("state.json"),
            r#"{"state_version": "shipper.state.v1", "plan_id": "abc"#,
        )
        .expect("write truncated state");

        // When: We run shipper resume
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            // Then: It reports a parse error without panicking
            .assert()
            .failure()
            .stderr(contains("failed to parse state JSON"));
    }

    // Scenario: State file containing a JSON array instead of object
    #[test]
    fn given_state_as_json_array_when_resume_then_fails_gracefully() {
        // Given: A state file that is a JSON array
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("array-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("state.json"), r#"[1, 2, 3]"#).expect("write array state");

        // When: We run shipper resume
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("resume")
            // Then: It fails with a parse error
            .assert()
            .failure()
            .stderr(contains("failed to parse state JSON"));
    }
}

// ============================================================================
// Feature: Corrupted Receipt File Handling
// ============================================================================

mod corrupted_receipt_handling {
    use super::*;

    // Scenario: Corrupted receipt.json doesn't crash inspect-receipt
    #[test]
    fn given_corrupted_receipt_when_inspect_receipt_then_fails_gracefully() {
        // Given: A workspace with a corrupted receipt.json
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("custom-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(
            state_dir.join("receipt.json"),
            "CORRUPTED RECEIPT DATA {{{}}}",
        )
        .expect("write corrupt receipt");

        // When: We run shipper inspect-receipt
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("inspect-receipt")
            .output()
            .expect("inspect-receipt");

        // Then: It fails with an error message (not a panic/crash)
        assert!(!output.status.success());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("failed to parse receipt") || stderr.contains("failed to read receipt"),
            "expected receipt parse error in stderr, got:\n{stderr}"
        );
    }

    // Scenario: Empty receipt.json doesn't crash the CLI
    #[test]
    fn given_empty_receipt_when_inspect_receipt_then_fails_gracefully() {
        // Given: A workspace with an empty receipt.json
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("empty-receipt-state");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("receipt.json"), "").expect("write empty receipt");

        // When: We run shipper inspect-receipt
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("inspect-receipt")
            .output()
            .expect("inspect-receipt");

        // Then: It fails gracefully
        assert!(!output.status.success());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("failed to parse receipt") || stderr.contains("failed to read receipt"),
            "expected receipt error in stderr, got:\n{stderr}"
        );
    }

    // Scenario: Receipt with wrong schema doesn't crash the CLI
    #[test]
    fn given_receipt_with_wrong_schema_when_inspect_receipt_then_fails_gracefully() {
        // Given: A workspace with a receipt that is valid JSON but wrong structure
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join("wrong-schema-receipt");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("receipt.json"), r#"{"not": "a receipt"}"#)
            .expect("write bad receipt");

        // When: We run shipper inspect-receipt
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--state-dir")
            .arg(&state_dir)
            .arg("inspect-receipt")
            .output()
            .expect("inspect-receipt");

        // Then: It fails gracefully without panicking
        assert!(!output.status.success());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("failed to parse receipt") || stderr.contains("missing field"),
            "expected receipt parse error in stderr, got:\n{stderr}"
        );
    }

    // Scenario: Publish still works even if a corrupted receipt exists from a prior run
    #[test]
    fn given_corrupted_receipt_from_prior_run_when_plan_then_still_works() {
        // Given: A workspace with a leftover corrupted receipt.json
        let td = tempdir().expect("tempdir");
        create_single_crate_workspace(td.path());
        let state_dir = td.path().join(".shipper");
        fs::create_dir_all(&state_dir).expect("mkdir state dir");
        fs::write(state_dir.join("receipt.json"), "BROKEN LEFTOVER RECEIPT")
            .expect("write corrupt receipt");

        // When: We run shipper plan (which doesn't read receipt)
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("plan")
            // Then: Plan succeeds regardless of corrupted receipt
            .assert()
            .success();
    }
}