cleanlib-cli 0.1.1

Terminal interface to CleanLibrary — query dependency verdicts and scan package manifests for ALLOW / DENY / WARN signals from the terminal or CI pipelines.
//! CLEANLIB-132 / Jira CLEANLIB-29: invoking `cleanlib` with no subcommand
//! must print help to stderr and exit non-zero (clap convention: exit 2 =
//! "user error"). Pre-fix behavior was silent exit 0 with no output.

use std::process::Command;

/// Use the `cleanlib` bin built by cargo for this crate.
fn cleanlib_bin() -> std::path::PathBuf {
    // CARGO_BIN_EXE_<name> is set by Cargo for integration tests of
    // crates that produce a binary.
    let p = std::env!("CARGO_BIN_EXE_cleanlib");
    std::path::PathBuf::from(p)
}

#[test]
fn no_subcommand_exits_non_zero() {
    let output = Command::new(cleanlib_bin())
        .output()
        .expect("failed to invoke cleanlib bin");
    // clap `arg_required_else_help` exits 2 on missing subcommand.
    assert!(
        !output.status.success(),
        "expected non-zero exit for bare `cleanlib`; got success.\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn no_subcommand_prints_help_to_stderr() {
    let output = Command::new(cleanlib_bin())
        .output()
        .expect("failed to invoke cleanlib bin");
    let stderr = String::from_utf8_lossy(&output.stderr);
    // Clap's auto-generated help includes the canonical "Usage:" header
    // + lists known subcommands like `status`, `login`, `verdict`.
    assert!(
        stderr.contains("Usage:") || stderr.contains("USAGE:"),
        "expected help output on stderr; got: {stderr}"
    );
}

#[test]
fn help_flag_prints_to_stdout_and_exits_zero() {
    // Sanity-check: explicit --help is a success path (exit 0); only the
    // missing-subcommand path is the user-error path (exit 2).
    let output = Command::new(cleanlib_bin())
        .arg("--help")
        .output()
        .expect("failed to invoke cleanlib --help");
    assert!(output.status.success(), "expected --help to succeed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Usage:") || stdout.contains("USAGE:"),
        "expected Usage header on stdout for --help"
    );
}