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
//! BDD (Behavior-Driven Development) tests for the shipper status command.
//!
//! These tests describe the expected behavior of `shipper status` in various
//! scenarios using Given-When-Then style documentation.

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

use assert_cmd::Command;
use predicates::str::contains;
use tempfile::tempdir;
use tiny_http::{Header, Response, Server, StatusCode};

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 shipper_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("shipper-cli"))
}

struct TestRegistry {
    base_url: String,
    handle: thread::JoinHandle<()>,
}

impl TestRegistry {
    fn join(self) {
        self.handle.join().expect("join server");
    }
}

/// Spawn a mock registry that responds with the given HTTP status codes.
/// `statuses` is cycled for each request; `expected_requests` is how many
/// requests the mock will serve before shutting down.
fn spawn_registry(statuses: Vec<u16>, expected_requests: usize) -> TestRegistry {
    let server = Server::http("127.0.0.1:0").expect("server");
    let base_url = format!("http://{}", server.server_addr());
    let handle = thread::spawn(move || {
        for idx in 0..expected_requests {
            let req = match server.recv_timeout(std::time::Duration::from_secs(30)) {
                Ok(Some(r)) => r,
                _ => break,
            };
            let status = statuses
                .get(idx)
                .copied()
                .or_else(|| statuses.last().copied())
                .unwrap_or(404);
            let resp = Response::from_string("{}")
                .with_status_code(StatusCode(status))
                .with_header(
                    Header::from_bytes("Content-Type", "application/json").expect("header"),
                );
            req.respond(resp).expect("respond");
        }
    });
    TestRegistry { base_url, handle }
}

/// Create a simple workspace with a single crate.
fn create_simple_workspace(root: &Path) {
    write_file(
        &root.join("Cargo.toml"),
        r#"
[workspace]
members = ["alpha"]
resolver = "2"
"#,
    );

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

/// Create a workspace with multiple crates that have inter-dependencies.
fn create_multi_crate_workspace(root: &Path) {
    write_file(
        &root.join("Cargo.toml"),
        r#"
[workspace]
members = ["core-lib", "mid-lib", "top-app"]
resolver = "2"
"#,
    );

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

    write_file(
        &root.join("mid-lib/Cargo.toml"),
        r#"
[package]
name = "mid-lib"
version = "0.3.0"
edition = "2021"

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

    write_file(
        &root.join("top-app/Cargo.toml"),
        r#"
[package]
name = "top-app"
version = "0.4.0"
edition = "2021"

[dependencies]
mid-lib = { path = "../mid-lib" }
"#,
    );
    write_file(
        &root.join("top-app/src/lib.rs"),
        "pub fn top() { mid_lib::mid(); }\n",
    );
}

// ============================================================================
// Feature: Status Command — Version Reporting
// ============================================================================

mod version_reporting {
    use super::*;

    // Scenario 1: All package versions are shown
    #[test]
    fn given_a_workspace_when_running_status_then_all_package_versions_are_shown() {
        // Given: A workspace with three crates at different versions
        let td = tempdir().expect("tempdir");
        create_multi_crate_workspace(td.path());

        // Registry returns 404 for all → all "missing"
        let registry = spawn_registry(vec![404], 3);

        // When: We run shipper status
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("status")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let stdout = String::from_utf8(output).expect("utf8");

        // Then: All package versions are shown in the output
        assert!(
            stdout.contains("core-lib@0.2.0"),
            "expected core-lib@0.2.0 in output, got: {stdout}"
        );
        assert!(
            stdout.contains("mid-lib@0.3.0"),
            "expected mid-lib@0.3.0 in output, got: {stdout}"
        );
        assert!(
            stdout.contains("top-app@0.4.0"),
            "expected top-app@0.4.0 in output, got: {stdout}"
        );

        registry.join();
    }

    // Scenario 1b: Single crate workspace shows its version
    #[test]
    fn given_a_single_crate_workspace_when_running_status_then_version_is_shown() {
        // Given: A workspace with one crate
        let td = tempdir().expect("tempdir");
        create_simple_workspace(td.path());

        let registry = spawn_registry(vec![404], 1);

        // When: We run shipper status
        // Then: The single crate version is shown
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("status")
            .assert()
            .success()
            .stdout(contains("alpha@0.1.0"));

        registry.join();
    }
}

// ============================================================================
// Feature: Status Command — Published Status Detection
// ============================================================================

mod published_status_detection {
    use super::*;

    // Scenario 2: Published crates are shown as published
    #[test]
    fn given_workspace_with_published_crates_when_running_status_then_published_status_is_shown() {
        // Given: A workspace with a single crate
        let td = tempdir().expect("tempdir");
        create_simple_workspace(td.path());

        // And: The registry reports the version exists (200)
        let registry = spawn_registry(vec![200], 1);

        // When: We run shipper status
        // Then: The crate is shown as "published"
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("status")
            .assert()
            .success()
            .stdout(contains("alpha@0.1.0: published"));

        registry.join();
    }

    // Scenario 2b: Unpublished crates are shown as missing
    #[test]
    fn given_workspace_with_unpublished_crates_when_running_status_then_missing_status_is_shown() {
        // Given: A workspace with a single crate
        let td = tempdir().expect("tempdir");
        create_simple_workspace(td.path());

        // And: The registry reports version not found (404)
        let registry = spawn_registry(vec![404], 1);

        // When: We run shipper status
        // Then: The crate is shown as "missing"
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("status")
            .assert()
            .success()
            .stdout(contains("alpha@0.1.0: missing"));

        registry.join();
    }

    // Scenario 2c: Mixed published/missing workspace
    #[test]
    fn given_workspace_with_mixed_versions_when_running_status_then_each_status_is_correct() {
        // Given: A multi-crate workspace
        let td = tempdir().expect("tempdir");
        create_multi_crate_workspace(td.path());

        // And: First crate is published (200), remaining are missing (404)
        let registry = spawn_registry(vec![200, 404, 404], 3);

        // When: We run shipper status
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("status")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let stdout = String::from_utf8(output).expect("utf8");

        // Then: At least one crate is shown as published and at least one as missing
        assert!(
            stdout.contains("published"),
            "expected at least one published crate"
        );
        assert!(
            stdout.contains("missing"),
            "expected at least one missing crate"
        );

        registry.join();
    }
}

// ============================================================================
// Feature: Status Command — Package Filtering
// ============================================================================

mod package_filtering {
    use super::*;

    // Scenario 3: --package filter shows only the specified package
    #[test]
    fn given_workspace_with_mixed_versions_when_running_status_with_package_then_only_that_package_is_shown()
     {
        // Given: A multi-crate workspace
        let td = tempdir().expect("tempdir");
        create_multi_crate_workspace(td.path());

        // Only one crate should be queried
        let registry = spawn_registry(vec![404], 1);

        // When: We run shipper status --package core-lib
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("--package")
            .arg("core-lib")
            .arg("status")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let stdout = String::from_utf8(output).expect("utf8");

        // Then: Only core-lib is shown
        assert!(
            stdout.contains("core-lib@0.2.0"),
            "expected core-lib@0.2.0 in output"
        );
        // And: Other packages are NOT shown
        assert!(
            !stdout.contains("mid-lib"),
            "mid-lib should not appear in filtered output"
        );
        assert!(
            !stdout.contains("top-app"),
            "top-app should not appear in filtered output"
        );

        registry.join();
    }

    // Scenario 3b: --package with multiple packages
    #[test]
    fn given_workspace_when_running_status_with_multiple_packages_then_only_those_are_shown() {
        // Given: A multi-crate workspace
        let td = tempdir().expect("tempdir");
        create_multi_crate_workspace(td.path());

        // Two crates queried
        let registry = spawn_registry(vec![404], 2);

        // When: We run shipper status --package core-lib --package mid-lib
        let output = shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&registry.base_url)
            .arg("--package")
            .arg("core-lib")
            .arg("--package")
            .arg("mid-lib")
            .arg("status")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let stdout = String::from_utf8(output).expect("utf8");

        // Then: Both filtered packages are shown
        assert!(stdout.contains("core-lib@0.2.0"), "expected core-lib");
        assert!(stdout.contains("mid-lib@0.3.0"), "expected mid-lib");
        // And: top-app is NOT shown
        assert!(
            !stdout.contains("top-app"),
            "top-app should not appear in filtered output"
        );

        registry.join();
    }
}

// ============================================================================
// Feature: Status Command — Error Handling (No Workspace)
// ============================================================================

mod no_workspace_error {
    use super::*;

    // Scenario 4: No workspace → clear error message
    #[test]
    fn given_no_workspace_when_running_status_then_error_message_is_clear() {
        // Given: A directory with no Cargo.toml (not a workspace)
        let td = tempdir().expect("tempdir");
        write_file(&td.path().join("README.md"), "not a workspace");

        // When: We run shipper status pointing at the non-workspace directory
        // Then: The command fails
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("status")
            .assert()
            .failure();
    }

    // Scenario 4b: Empty directory → failure with no panic
    #[test]
    fn given_empty_directory_when_running_status_then_command_fails_gracefully() {
        // Given: An empty temp directory
        let td = tempdir().expect("tempdir");

        // When: We run shipper status
        // Then: The command fails (no panic, exits with non-zero)
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("status")
            .assert()
            .failure();
    }
}

// ============================================================================
// Feature: Status Command — Connectivity Failures
// ============================================================================

mod connectivity_failures {
    use super::*;

    // Scenario 5: Registry unreachable → error is handled gracefully
    #[test]
    fn given_workspace_with_no_connectivity_when_running_status_then_error_is_handled_gracefully() {
        // Given: A workspace with a single crate
        let td = tempdir().expect("tempdir");
        create_simple_workspace(td.path());

        // And: The registry endpoint points to a port that is not listening
        //      (bind then immediately drop to get a guaranteed-closed port)
        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind ephemeral port");
        let dead_port = listener.local_addr().expect("local_addr").port();
        drop(listener);

        let dead_url = format!("http://127.0.0.1:{dead_port}");

        // When: We run shipper status against the unreachable registry
        // Then: The command fails without panicking (exit code != 0)
        shipper_cmd()
            .arg("--manifest-path")
            .arg(td.path().join("Cargo.toml"))
            .arg("--api-base")
            .arg(&dead_url)
            .arg("status")
            .assert()
            .failure();
    }
}