cargo-tangle 0.5.0-alpha.13

A command-line tool to create and deploy blueprints on Tangle Network
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! `cargo tangle blueprint ship` — one-command blueprint release.
//!
//! Wraps the existing `publish-version` + `set-active-version` + `set-policy`
//! paths into a single interactive flow:
//!
//! 1. Detect the workspace, blueprint id, network, and signer.
//! 2. (Optional) `cargo build --release` for the chosen package.
//! 3. Hash the binary, optionally pin to IPFS, optionally derive
//!    `attestationHash` from a sigstore/SLSA bundle.
//! 4. `publishBinaryVersion`, optionally `setActiveBinaryVersion`,
//!    optionally bulk `setServiceUpgradePolicy(AUTO)` on a service list.
//! 5. Print the resulting trust score and a one-shot summary.
//!
//! CI mode (`--yes --pin-ipfs --promote`) skips every prompt and is the
//! glue the `tangle-network/blueprint/.github/actions/ship-release` action
//! consumes.
//!
//! Auto-detection convention:
//! - `--blueprint-id` flag wins.
//! - Otherwise read `<cwd>/settings.env` for `BLUEPRINT_ID=`.
//! - Otherwise read `<cwd>/metadata/blueprint-metadata.json` for a
//!   `blueprintId` top-level key.
//! - Otherwise fail fast with an actionable message.

use std::path::{Path, PathBuf};
use std::process::Command;

use alloy_primitives::B256;
use color_eyre::eyre::{Context, Result, bail, eyre};
use dialoguer::console::style;
use dialoguer::{Confirm, Input, theme::ColorfulTheme};
use serde_json::json;

use crate::command::tangle::TangleClientArgs;
use crate::command::upgrade::{
    PinnedArtifact, PublishVersionResult, TxContext, UpgradePolicyArg, hash_file, parse_b256,
    pin_file_to_ipfs, publish_version, set_active_version, set_service_policy,
};

/// Inputs to the ship wizard. Every field except `network` may be either
/// resolved interactively or passed via flag — interactive mode is opt-in
/// via `--yes` being absent.
#[derive(Debug, Clone)]
pub struct ShipArgs {
    pub network: TangleClientArgs,
    /// Skip all prompts; accept defaults derived from flags + auto-detection.
    pub yes: bool,
    /// Don't run `cargo build --release` — caller will provide `--binary`.
    pub no_build: bool,
    /// Cargo package whose `--release` artifact should be built+shipped. When
    /// omitted, the wizard tries to read the current workspace's default
    /// binary.
    pub package: Option<String>,
    /// Path to a pre-built binary. Skips the build step.
    pub binary: Option<PathBuf>,
    /// Pre-existing artifact URI (`ipfs://...`, `https://...`). Skips IPFS
    /// pin.
    pub binary_uri: Option<String>,
    /// Pin the binary to IPFS via the same credentials `upgrade::pin_file_to_ipfs` reads.
    pub pin_ipfs: bool,
    /// sigstore/SLSA attestation bundle whose sha256 lands on-chain as
    /// `attestationHash`.
    pub attestation_bundle: Option<PathBuf>,
    /// Explicit `attestationHash` override (32-byte hex). Conflicts with
    /// `--attestation-bundle`.
    pub attestation_hash: Option<String>,
    /// Promote the new version to active (`setActiveBinaryVersion`).
    pub promote: bool,
    /// Explicitly do NOT promote, even if interactive default is yes.
    pub no_promote: bool,
    /// Optional comma-separated service IDs to bulk-flip into AUTO policy
    /// alongside the publish.
    pub policy_services: Option<String>,
    /// Validate everything end-to-end without submitting any transactions.
    pub dry_run: bool,
    /// Caller-provided blueprint id. Wins over auto-detection.
    pub blueprint_id: Option<u64>,
    /// JSON output instead of pretty banners. Implied by `--yes` for CI.
    pub json: bool,
}

#[derive(Debug)]
struct DetectedWorkspace {
    cwd: PathBuf,
    blueprint_id: u64,
    package: Option<String>,
}

/// Single entrypoint. Returns Ok on success; any prompt cancellation /
/// validation failure is surfaced as a normal error.
pub async fn run(args: ShipArgs) -> Result<()> {
    let cwd = std::env::current_dir().context("reading cwd")?;
    let detected = detect_workspace(&cwd, &args)?;
    let ctx = build_tx_context(&args.network)?;
    let signer = signer_address(&ctx)?;

    print_header(&detected, &args.network, signer, &args)?;

    // --- 1. Build
    let binary_path = resolve_binary(&detected, &args)?;
    let (sha256_hash, size_bytes) = hash_file(&binary_path)?;
    println!(
        "  {}      {sha256_hash:#x}  ({:.2} MB)",
        style("sha256").bold(),
        size_bytes as f64 / 1_048_576.0
    );

    // --- 2. IPFS
    let binary_uri = resolve_binary_uri(&binary_path, &args).await?;
    println!("  {}    {binary_uri}", style("binaryUri").bold());

    // --- 3. Attestation
    let attestation_hash = resolve_attestation_hash(&args)?;
    println!("  {} {attestation_hash:#x}", style("attestation").bold(),);

    if args.dry_run {
        print_dry_run_summary(
            &detected,
            &sha256_hash,
            &binary_uri,
            &attestation_hash,
            args.promote,
            args.json,
        );
        return Ok(());
    }

    // --- 4. Publish
    let confirm_publish = if args.yes {
        true
    } else {
        Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt(format!(
                "Publish v? on-chain (blueprint={})?",
                detected.blueprint_id
            ))
            .default(true)
            .interact()?
    };
    if !confirm_publish {
        bail!("publish aborted by user");
    }
    let publish_result = publish_version(
        &ctx,
        detected.blueprint_id,
        sha256_hash,
        binary_uri.clone(),
        attestation_hash,
    )
    .await?;
    println!(
        "  {} v{} ({})",
        style("✓ Published").green().bold(),
        publish_result.version_id,
        publish_result
            .block_number
            .map(|b| format!("block {b}"))
            .unwrap_or_else(|| "pending".into()),
    );

    // --- 5. Promote
    let promote = decide_promote(&args)?;
    if promote {
        let tx = set_active_version(&ctx, detected.blueprint_id, publish_result.version_id).await?;
        println!(
            "  {} v{} (tx {:#x})",
            style("✓ Promoted").green().bold(),
            publish_result.version_id,
            tx.tx_hash,
        );
    }

    // --- 6. Optional bulk policy flip
    if let Some(services_csv) = &args.policy_services {
        let ids = parse_service_ids(services_csv)?;
        for sid in ids {
            let tx = set_service_policy(&ctx, sid, UpgradePolicyArg::Auto).await?;
            println!(
                "  {} service {} -> AUTO (tx {:#x})",
                style("✓ Policy").green().bold(),
                sid,
                tx.tx_hash
            );
        }
    }

    print_shipped_summary(&detected, &publish_result, promote, args.json);
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// Detection / inputs
// ─────────────────────────────────────────────────────────────────────────────

fn detect_workspace(cwd: &Path, args: &ShipArgs) -> Result<DetectedWorkspace> {
    let blueprint_id = if let Some(id) = args.blueprint_id {
        id
    } else if let Some(id) = read_blueprint_id_from_env_file(cwd)? {
        id
    } else if let Some(id) = read_blueprint_id_from_metadata(cwd)? {
        id
    } else {
        bail!(
            "could not auto-detect blueprint id. Pass --blueprint-id, or define BLUEPRINT_ID \
             in ./settings.env, or add a top-level `blueprintId` field to \
             metadata/blueprint-metadata.json."
        );
    };
    Ok(DetectedWorkspace {
        cwd: cwd.to_path_buf(),
        blueprint_id,
        package: args.package.clone(),
    })
}

/// Read `BLUEPRINT_ID=` from a key=value `settings.env` file if present.
/// Accepts both quoted and unquoted values and trims surrounding whitespace.
fn read_blueprint_id_from_env_file(cwd: &Path) -> Result<Option<u64>> {
    let path = cwd.join("settings.env");
    if !path.exists() {
        return Ok(None);
    }
    let text = std::fs::read_to_string(&path).with_context(|| format!("reading {path:?}"))?;
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some(rest) = line.strip_prefix("BLUEPRINT_ID=") {
            let raw = rest.trim().trim_matches('"').trim_matches('\'');
            if raw == "0" || raw.is_empty() {
                // The example file ships `BLUEPRINT_ID=0` as a placeholder; treat
                // it as "not configured" so we fall through to the next source.
                return Ok(None);
            }
            let id: u64 = raw
                .parse()
                .with_context(|| format!("parsing BLUEPRINT_ID from {path:?}"))?;
            return Ok(Some(id));
        }
    }
    Ok(None)
}

/// Read a top-level `blueprintId` from a JSON metadata file.
fn read_blueprint_id_from_metadata(cwd: &Path) -> Result<Option<u64>> {
    let path = cwd.join("metadata").join("blueprint-metadata.json");
    if !path.exists() {
        return Ok(None);
    }
    let text = std::fs::read_to_string(&path).with_context(|| format!("reading {path:?}"))?;
    let value: serde_json::Value =
        serde_json::from_str(&text).with_context(|| format!("parsing {path:?}"))?;
    Ok(value.get("blueprintId").and_then(|v| v.as_u64()))
}

fn build_tx_context(network: &TangleClientArgs) -> Result<TxContext> {
    let cfg = network.client_config(0, None)?;
    Ok(TxContext {
        http_rpc_url: cfg.http_rpc_endpoint.clone(),
        tangle_contract: cfg.settings.tangle_contract,
        keystore_path: PathBuf::from(cfg.keystore_uri.clone()),
    })
}

fn signer_address(ctx: &TxContext) -> Result<alloy_primitives::Address> {
    use crate::command::signer::load_evm_signer;
    let signer = load_evm_signer(&ctx.keystore_path)?;
    Ok(signer.operator_address)
}

// ─────────────────────────────────────────────────────────────────────────────
// Build / hash / pin / attest
// ─────────────────────────────────────────────────────────────────────────────

fn resolve_binary(workspace: &DetectedWorkspace, args: &ShipArgs) -> Result<PathBuf> {
    if let Some(path) = &args.binary {
        if !path.exists() {
            bail!("--binary {} does not exist", path.display());
        }
        return Ok(path.clone());
    }
    if args.no_build {
        bail!(
            "--no-build was passed but no --binary supplied; pass a path to the pre-built artifact"
        );
    }
    let want_build = if args.yes {
        true
    } else {
        Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt("Build a release binary now?")
            .default(true)
            .interact()?
    };
    if !want_build {
        bail!("no binary available — pass --binary or accept the build prompt");
    }

    let mut cmd = Command::new("cargo");
    cmd.current_dir(&workspace.cwd)
        .arg("build")
        .arg("--release");
    if let Some(pkg) = &workspace.package {
        cmd.arg("-p").arg(pkg);
    }
    println!(
        "  {} cargo build --release{}",
        style("> Building:").cyan().bold(),
        workspace
            .package
            .as_ref()
            .map(|p| format!(" -p {p}"))
            .unwrap_or_default()
    );
    let status = cmd
        .status()
        .with_context(|| "spawning cargo build --release".to_string())?;
    if !status.success() {
        bail!("cargo build --release failed (exit {:?})", status.code());
    }

    let target_dir = workspace.cwd.join("target").join("release");
    let binary_name = workspace
        .package
        .clone()
        .or_else(|| guess_default_binary_name(&workspace.cwd))
        .ok_or_else(|| {
            eyre!(
                "could not determine release binary name; pass --binary <path-to-target/release/foo>"
            )
        })?;
    let path = target_dir.join(&binary_name);
    if !path.exists() {
        bail!(
            "expected built binary at {} but the file is missing — pass --binary <path>",
            path.display()
        );
    }
    Ok(path)
}

/// Cheap heuristic: read `Cargo.toml` and pick the first `[[bin]] name = "..."`
/// or fall back to the workspace's `package.name`. Anything more elaborate
/// is `--binary <path>` territory.
fn guess_default_binary_name(cwd: &Path) -> Option<String> {
    let manifest = cwd.join("Cargo.toml");
    let text = std::fs::read_to_string(&manifest).ok()?;
    // Naive scan — avoid pulling in a full toml dependency on the hot path.
    for (i, line) in text.lines().enumerate() {
        if line.trim_start().starts_with("[[bin]]") {
            // Look ahead a handful of lines for a `name = "..."` entry.
            for follow in text.lines().skip(i + 1).take(6) {
                let t = follow.trim();
                if let Some(rest) = t.strip_prefix("name") {
                    let raw = rest.trim_start_matches([' ', '=', '"']).trim();
                    if let Some(end) = raw.find('"') {
                        return Some(raw[..end].to_string());
                    }
                }
                if t.starts_with('[') {
                    break;
                }
            }
        }
    }
    // Fallback: `[package] name = "..."`
    for line in text.lines() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix("name") {
            let raw = rest.trim_start_matches([' ', '=', '"']).trim();
            if let Some(end) = raw.find('"') {
                return Some(raw[..end].to_string());
            }
        }
    }
    None
}

async fn resolve_binary_uri(binary: &Path, args: &ShipArgs) -> Result<String> {
    if let Some(uri) = &args.binary_uri {
        return Ok(uri.clone());
    }
    if args.pin_ipfs {
        let pinned: PinnedArtifact = pin_file_to_ipfs(binary).await?;
        return Ok(pinned.uri);
    }
    if args.yes {
        bail!("--yes was passed but no IPFS strategy: pass --binary-uri or --pin-ipfs");
    }
    let want_pin = Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Pin binary to IPFS now? (uses IPFS_API_URL+IPFS_API_TOKEN or PINATA_JWT)")
        .default(true)
        .interact()?;
    if want_pin {
        let pinned = pin_file_to_ipfs(binary).await?;
        return Ok(pinned.uri);
    }
    let uri: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt("Existing binary URI (ipfs://... or https://...)")
        .interact_text()?;
    if uri.trim().is_empty() {
        bail!("a binaryUri is required");
    }
    Ok(uri.trim().to_string())
}

fn resolve_attestation_hash(args: &ShipArgs) -> Result<B256> {
    if let Some(hex) = &args.attestation_hash {
        return parse_b256(hex, "--attestation-hash");
    }
    if let Some(path) = &args.attestation_bundle {
        if !path.exists() {
            bail!("--attestation-bundle {} does not exist", path.display());
        }
        let (digest, _) = hash_file(path)?;
        return Ok(digest);
    }
    Ok(B256::ZERO)
}

fn decide_promote(args: &ShipArgs) -> Result<bool> {
    if args.no_promote {
        return Ok(false);
    }
    if args.promote || args.yes {
        return Ok(true);
    }
    Ok(Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Promote to active version (setActiveBinaryVersion)?")
        .default(true)
        .interact()?)
}

fn parse_service_ids(csv: &str) -> Result<Vec<u64>> {
    csv.split(',')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .map(|s| {
            s.parse::<u64>()
                .with_context(|| format!("parsing service id `{s}`"))
        })
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Pretty-printers
// ─────────────────────────────────────────────────────────────────────────────

fn print_header(
    detected: &DetectedWorkspace,
    network: &TangleClientArgs,
    signer: alloy_primitives::Address,
    args: &ShipArgs,
) -> Result<()> {
    println!(
        "{}  blueprintId={}  ({})",
        style("🚀 Shipping blueprint:").cyan().bold(),
        detected.blueprint_id,
        detected.cwd.display(),
    );
    if let Ok(http) = network.http_rpc_url() {
        println!("  {}    {}", style("RPC").bold(), http);
    }
    println!("  {} {}", style("Wallet").bold(), signer);
    if args.dry_run {
        println!(
            "  {}",
            style("(dry-run — no transactions will be sent)").yellow()
        );
    }
    Ok(())
}

fn print_dry_run_summary(
    detected: &DetectedWorkspace,
    sha256: &B256,
    binary_uri: &str,
    attestation: &B256,
    promote: bool,
    json: bool,
) {
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&json!({
                "event": "ship_dry_run",
                "blueprint_id": detected.blueprint_id,
                "sha256": format!("{sha256:#x}"),
                "binary_uri": binary_uri,
                "attestation_hash": format!("{attestation:#x}"),
                "would_promote": promote,
            }))
            .unwrap()
        );
        return;
    }
    println!();
    println!(
        "{}",
        style("── Dry-run summary ──────────────────────────────").dim()
    );
    println!("  blueprint_id     {}", detected.blueprint_id);
    println!("  sha256           {sha256:#x}");
    println!("  binaryUri        {binary_uri}");
    println!("  attestationHash  {attestation:#x}");
    println!("  would_promote    {promote}");
    println!("  No transactions submitted.");
}

fn print_shipped_summary(
    detected: &DetectedWorkspace,
    result: &PublishVersionResult,
    promoted: bool,
    json: bool,
) {
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&json!({
                "event": "ship_complete",
                "blueprint_id": detected.blueprint_id,
                "version_id": result.version_id,
                "sha256": format!("{:#x}", result.sha256_hash),
                "binary_uri": result.binary_uri,
                "attestation_hash": format!("{:#x}", result.attestation_hash),
                "tx_hash": format!("{:#x}", result.tx_hash),
                "block_number": result.block_number,
                "promoted": promoted,
            }))
            .unwrap()
        );
        return;
    }
    println!();
    let banner = format!(
        "── Shipped v{} ──────────────────────────────",
        result.version_id
    );
    println!("{}", style(&banner).green().bold());
    println!("  sha256     {:#x}", result.sha256_hash);
    println!("  binaryUri  {}", result.binary_uri);
    println!("  promoted   {}", promoted);
    if let Some(block) = result.block_number {
        println!("  block      {block}");
    }
    println!("  tx_hash    {:#x}", result.tx_hash);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    fn write_settings(dir: &Path, contents: &str) {
        let mut f = std::fs::File::create(dir.join("settings.env")).unwrap();
        f.write_all(contents.as_bytes()).unwrap();
    }

    #[test]
    fn settings_env_with_id_is_parsed() {
        let tmp = tempfile::tempdir().unwrap();
        write_settings(tmp.path(), "FOO=bar\nBLUEPRINT_ID=42\n");
        assert_eq!(
            read_blueprint_id_from_env_file(tmp.path()).unwrap(),
            Some(42)
        );
    }

    #[test]
    fn settings_env_with_quoted_id_works() {
        // Real settings files in the wild quote env values; the parser must
        // strip both single and double quotes before parsing.
        let tmp = tempfile::tempdir().unwrap();
        write_settings(tmp.path(), "BLUEPRINT_ID=\"99\"\n");
        assert_eq!(
            read_blueprint_id_from_env_file(tmp.path()).unwrap(),
            Some(99)
        );
    }

    #[test]
    fn settings_env_zero_treated_as_unset() {
        // The example file ships `BLUEPRINT_ID=0` as a placeholder — if we
        // accepted it literally we'd ship onto a nonexistent blueprint.
        let tmp = tempfile::tempdir().unwrap();
        write_settings(tmp.path(), "BLUEPRINT_ID=0\n");
        assert_eq!(read_blueprint_id_from_env_file(tmp.path()).unwrap(), None);
    }

    #[test]
    fn settings_env_missing_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        assert_eq!(read_blueprint_id_from_env_file(tmp.path()).unwrap(), None);
    }

    #[test]
    fn metadata_with_blueprint_id_field_parses() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(tmp.path().join("metadata")).unwrap();
        std::fs::write(
            tmp.path().join("metadata").join("blueprint-metadata.json"),
            r#"{"name":"X","blueprintId":7}"#,
        )
        .unwrap();
        assert_eq!(
            read_blueprint_id_from_metadata(tmp.path()).unwrap(),
            Some(7)
        );
    }

    #[test]
    fn parse_service_ids_supports_csv_with_whitespace() {
        assert_eq!(parse_service_ids("42, 43,44 ").unwrap(), vec![42, 43, 44]);
        assert_eq!(parse_service_ids("").unwrap(), Vec::<u64>::new());
        assert!(parse_service_ids("not-a-number").is_err());
    }

    #[test]
    fn guess_binary_name_prefers_first_bin_section() {
        let tmp = tempfile::tempdir().unwrap();
        let manifest = r#"
[package]
name = "wrapper"
version = "0.1.0"

[[bin]]
name = "wrapped-bin"
path = "src/main.rs"
"#;
        std::fs::write(tmp.path().join("Cargo.toml"), manifest).unwrap();
        assert_eq!(
            guess_default_binary_name(tmp.path()).as_deref(),
            Some("wrapped-bin")
        );
    }

    #[test]
    fn guess_binary_name_falls_back_to_package_name() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("Cargo.toml"),
            "[package]\nname = \"lonely\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        assert_eq!(
            guess_default_binary_name(tmp.path()).as_deref(),
            Some("lonely")
        );
    }
}