Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.cli.clap

Requirements index · View definition

evidence Valid

Requirement

The habeas CLI MUST use clap to define and parse its commands and options.

Rationale

The requirement requirements.cli.clap has one obligation: the habeas CLI defines and parses its commands and options with clap. A black-box test can only check this through behavior that clap produces. tests/cli.rs#fn[clap_defines_commands_options_and_usage_errors] is marked {test} and runs the real binary to check that behavior.

  • Command definition: --help succeeds and starts with Usage: habeas <COMMAND>. It lists mdbook, setup, status, info, author and help in clap’s two-space-indented format. It also asserts that the hidden hook command stays out of the help, which checks that #[command(hide = true)] is honored.
  • Option definition: status --help shows --all and --verbose.
  • Alias parsing: author-evidence --help succeeds and shows Usage: habeas author, so the alias resolves to the same command.
  • Parse errors: three invalid invocations cover an unknown option, a missing required argument, and a conflicting argument (author x --all, which hits the ArgGroup and conflict rules). Each asserts exit code 2 and clap’s error text plus a Usage: habeas line on stderr.

I read src/main.rs (struct[Cli], enum[Command]) to confirm the CLI is built from clap’s Parser, Subcommand, command and arg attributes. The test exercises the commands, option kinds, alias, hidden commands and usage errors those attributes define.

Not every subcommand, such as coverage or test, is exercised individually. The requirement does not enumerate commands, and they all go through the same clap derive mechanism, so I don’t count that as a gap. The assertions would fail if parsing or help were hand-rolled rather than done by clap, so I found no adequacy gap.

Inputs

Tests

Input 2: outline tests/cli.rs fn[clap_defines_commands_options_and_usage_errors]
  • File: tests/cli.rs
  • Selector: fn[clap_defines_commands_options_and_usage_errors]
  • Kind: fn
  • tests/cli.rs:43–96
// {test}`requirements.cli.clap`
#[test]
fn clap_defines_commands_options_and_usage_errors() {
    let binary = env!("CARGO_BIN_EXE_habeas");
    let help = Command::new(binary).arg("--help").output().unwrap();
    assert!(help.status.success());
    let help = String::from_utf8(help.stdout).unwrap();
    assert!(help.starts_with("Usage: habeas <COMMAND>"), "{help}");
    for command in ["mdbook", "setup", "status", "info", "author", "help"] {
        assert!(
            help.contains(&format!("\n  {command} ")),
            "{command}: {help}"
        );
    }
    assert!(
        !help.contains("hook"),
        "hidden commands stay out of help: {help}"
    );

    let status = Command::new(binary)
        .args(["status", "--help"])
        .output()
        .unwrap();
    let status = String::from_utf8(status.stdout).unwrap();
    assert!(
        status.contains("--all") && status.contains("--verbose"),
        "{status}"
    );

    // The alias parses to the same command.
    let alias = Command::new(binary)
        .args(["author-evidence", "--help"])
        .output()
        .unwrap();
    assert!(alias.status.success());
    assert!(String::from_utf8_lossy(&alias.stdout).contains("Usage: habeas author"));

    for (args, expected) in [
        (
            ["status", "--bogus"].as_slice(),
            "unexpected argument '--bogus'",
        ),
        (["info"].as_slice(), "required arguments were not provided"),
        (["author", "x", "--all"].as_slice(), "cannot be used with"),
    ] {
        let output = Command::new(binary).args(args).output().unwrap();
        assert_eq!(output.status.code(), Some(2), "{args:?}");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains(expected) && stderr.contains("Usage: habeas"),
            "{args:?}: {stderr}"
        );
    }
}

Other sources

Input 1: requirements selection requirements.cli.clap
:::{req} clap
The `habeas` CLI MUST use `clap` to define and parse its commands and options.
:::