cargo-unmaintained 2.0.1

Find unmaintained packages in Rust projects
Documentation
use anyhow::{Context, Result, ensure};
use elaborate::std::{fs::OpenOptionsContext, process::CommandContext};
use std::{fs::OpenOptions, io::Write, process::Command};
use tempfile::{TempDir, tempdir};

pub fn temp_package(name: &str) -> Result<TempDir> {
    let tempdir = tempdir().with_context(|| "failed to create temporary directory")?;

    // smoelius: Passing `--vcs=none` adds a tiny bit of speedup. This is useful when `cargo
    // unmaintained` is called repeatedly, e.g., in the `rustsec_advisories` test.
    let status = Command::new("cargo")
        .args([
            "init",
            &format!("--name={name}-temp-package"),
            "--quiet",
            "--vcs=none",
        ])
        .current_dir(&tempdir)
        .status_wc()?;
    ensure!(status.success());

    let path_buf = tempdir.path().join("Cargo.toml");
    let mut manifest = OpenOptions::new().append(true).open_wc(&path_buf)?;
    writeln!(manifest, r#"{name} = "*""#)
        .with_context(|| format!("failed to write to `{}`", path_buf.display()))?;

    Ok(tempdir)
}