aperture-cli 0.1.9

Dynamic CLI generator for OpenAPI specifications
Documentation
//! Integration tests for hidden global flags functionality.
//!
//! Tests that global flags (--jq, --format, --server-var) are hidden from
//! dynamic subcommand help but remain functional.
//!
//! Note: The hiding only affects the DYNAMIC command tree generated from `OpenAPI` specs,
//! not the main CLI help. The main `aperture api <spec> --help` shows static CLI help
//! which includes all flags. The dynamic help is shown at deeper levels (e.g., `users`, `list-users`).

#![cfg(feature = "integration")]

mod test_helpers;

use assert_cmd::Command;
use std::fs;
use tempfile::TempDir;

#[allow(deprecated)] // TODO: Migrate to cargo_bin! macro
fn aperture_cmd() -> Command {
    Command::cargo_bin("aperture").unwrap()
}

fn create_minimal_spec(temp_dir: &TempDir) -> std::path::PathBuf {
    let spec_content = r#"
openapi: "3.0.0"
info:
  title: "Test API"
  version: "1.0.0"
servers:
  - url: https://{region}.api.example.com
    variables:
      region:
        default: us
        enum: [us, eu]
paths:
  /users:
    get:
      operationId: listUsers
      summary: List all users
      tags:
        - users
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: object
                properties:
                  users:
                    type: array
                    items:
                      type: object
                      properties:
                        id:
                          type: integer
                        name:
                          type: string
"#;
    let spec_file = temp_dir.path().join("spec.yaml");
    fs::write(&spec_file, spec_content).unwrap();
    spec_file
}

#[test]
fn test_global_flags_visible_in_main_help() {
    // Check main help - should show --jq, --format (these are in the main CLI)
    let output = aperture_cmd().args(["--help"]).assert().success();

    let stdout = String::from_utf8_lossy(&output.get_output().stdout);

    // These flags should be visible in the main help
    assert!(
        stdout.contains("--jq"),
        "Expected --jq to be visible in main help"
    );
    assert!(
        stdout.contains("--format"),
        "Expected --format to be visible in main help"
    );
}

#[test]
fn test_hidden_flags_still_functional() {
    let temp_dir = TempDir::new().unwrap();
    let spec_file = create_minimal_spec(&temp_dir);

    // Add spec first
    aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args([
            "--quiet",
            "config",
            "add",
            "test-api",
            spec_file.to_str().unwrap(),
        ])
        .assert()
        .success();

    // Test that --format is still functional (even though hidden in dynamic tree)
    aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args([
            "api",
            "test-api",
            "--describe-json",
            "--format",
            "json", // This should be accepted
        ])
        .assert()
        .success();
}

#[test]
fn test_global_flags_hidden_from_dynamic_command_tree() {
    let temp_dir = TempDir::new().unwrap();
    let spec_file = create_minimal_spec(&temp_dir);

    // Add spec first
    aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args([
            "--quiet",
            "config",
            "add",
            "test-api",
            spec_file.to_str().unwrap(),
        ])
        .assert()
        .success();

    // Check help at the operation level - this is the dynamic command tree help
    // Note: Due to how clap handles dynamic commands, --help outputs to stderr
    // and returns a non-zero exit code, but the help content is still shown
    let output = aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args(["api", "test-api", "users", "list-users", "--help"])
        .output()
        .expect("Failed to execute command");

    // The help is output to stderr in this case
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Verify we got some help output (contains Options section)
    assert!(
        stderr.contains("Options:") || stderr.contains("-h, --help"),
        "Expected help output to contain Options section"
    );

    // The hidden flags should NOT appear in the dynamic command help
    assert!(
        !stderr.contains("--jq"),
        "Expected --jq to be hidden from dynamic command help"
    );
    assert!(
        !stderr.contains("--format ") && !stderr.contains("--format\n"),
        "Expected --format to be hidden from dynamic command help"
    );
    assert!(
        !stderr.contains("--server-var"),
        "Expected --server-var to be hidden from dynamic command help"
    );
}

#[test]
fn test_hidden_flags_work_at_operation_level() {
    let temp_dir = TempDir::new().unwrap();
    let spec_file = create_minimal_spec(&temp_dir);

    // Add spec first
    aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args([
            "--quiet",
            "config",
            "add",
            "test-api",
            spec_file.to_str().unwrap(),
        ])
        .assert()
        .success();

    // Verify the command structure via describe-json
    let output = aperture_cmd()
        .env("APERTURE_CONFIG_DIR", temp_dir.path())
        .args(["api", "test-api", "--describe-json"])
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&output.get_output().stdout);

    // The describe-json should work - proving the command structure is correct
    assert!(stdout.contains("listUsers") || stdout.contains("list-users"));
    assert!(stdout.contains("commands"));
}