capy-cli 0.7.2

The command-line interface for the Capy web framework.
Documentation
//! Offline validation and non-mutating environment diagnostics.
use capy_cli::{new_project_with_frontend, FrontendDriver};
use std::{fs, path::Path, process::Command};

fn run(root: &Path, command: &str) -> std::process::Output {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_capy"));
    for (key, _) in std::env::vars().filter(|(key, _)| {
        ["APP_", "DATABASE_", "SERVER_", "HTTP_"]
            .iter()
            .any(|prefix| key.starts_with(prefix))
    }) {
        cmd.env_remove(key);
    }
    cmd.arg(command).current_dir(root).output().unwrap()
}

#[test]
fn doctor_adapts_to_frontend_selection_and_never_creates_sqlite_files() {
    let temp = tempfile::tempdir().unwrap();
    for driver in [FrontendDriver::None, FrontendDriver::React] {
        let root = temp.path().join(driver.as_str());
        new_project_with_frontend(&root, driver.as_str(), driver).unwrap();
        let result = run(&root, "doctor");
        assert!(!result.status.success());
        let output = String::from_utf8(result.stdout).unwrap();
        assert!(output.contains("Capy Doctor"));
        assert!(output.contains("SQLite file missing"), "{output}");
        assert!(!root.join("database/app.sqlite").exists());
        assert!(!root.join("frontend/node_modules").exists());
        if driver == FrontendDriver::None {
            assert!(output.contains("Node.js and pnpm are not required"));
        } else {
            assert!(output.contains("frontend dependencies"));
        }
    }
}

#[test]
fn check_rejects_bad_settings_and_missing_assets_before_invoking_cargo() {
    let temp = tempfile::tempdir().unwrap();
    let root = temp.path().join("diagnostic-app");
    new_project_with_frontend(&root, "diagnostic-app", FrontendDriver::React).unwrap();
    let missing = run(&root, "check");
    assert!(!missing.status.success());
    assert!(String::from_utf8_lossy(&missing.stderr).contains("Production frontend build missing"));
    fs::write(
        root.join("config/server.toml"),
        "shutdown_timeout_seconds = 0",
    )
    .unwrap();
    let invalid = run(&root, "check");
    assert!(!invalid.status.success());
    assert!(String::from_utf8_lossy(&invalid.stderr).contains("SERVER_SHUTDOWN_TIMEOUT_SECONDS"));
    fs::write(root.join("config/server.toml"), "port = 99999").unwrap();
    assert!(!run(&root, "check").status.success());
    fs::write(
        root.join("config/server.toml"),
        "shutdown_timeout_seconds = 'nonsense'",
    )
    .unwrap();
    assert!(!run(&root, "check").status.success());
    assert!(
        !root.join("Cargo.lock").exists(),
        "Validation failed before any build"
    );
}

#[test]
fn package_manager_discovery_uses_the_platform_executable() {
    let temp = tempfile::tempdir().unwrap();
    let command = FrontendDriver::React.version_command(temp.path()).unwrap();
    assert_eq!(
        command.get_program(),
        if cfg!(windows) { "pnpm.cmd" } else { "pnpm" }
    );
    assert!(FrontendDriver::None.version_command(temp.path()).is_none());
    let executable = temp.path().join(if cfg!(windows) {
        "frontend/node_modules/.bin/vite.cmd"
    } else {
        "frontend/node_modules/.bin/vite"
    });
    fs::create_dir_all(executable.parent().unwrap()).unwrap();
    fs::write(executable, "fixture").unwrap();
    assert!(FrontendDriver::React.dependencies_installed(temp.path()));
    assert!(FrontendDriver::React.install_command(temp.path()).is_none());
}