capy-cli 0.7.2

The command-line interface for the Capy web framework.
Documentation
//! Installer safety and generated source validation.
use capy_cli::{make_auth, new_project_with_frontend, FrontendDriver};
use std::{
    collections::BTreeMap,
    fs,
    path::{Path, PathBuf},
};
fn snapshot(root: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
    fn visit(base: &Path, root: &Path, files: &mut BTreeMap<PathBuf, Vec<u8>>) {
        for entry in fs::read_dir(root).unwrap() {
            let path = entry.unwrap().path();
            if path.is_dir() {
                visit(base, &path, files);
            } else {
                files.insert(
                    path.strip_prefix(base).unwrap().to_path_buf(),
                    fs::read(path).unwrap(),
                );
            }
        }
    }
    let mut files = BTreeMap::new();
    visit(root, root, &mut files);
    files
}
#[test]
fn installer_handles_api_and_react_and_refuses_reinstallation() {
    for frontend in [FrontendDriver::None, FrontendDriver::React] {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("identity");
        new_project_with_frontend(&root, "identity", frontend).unwrap();
        make_auth(&root).unwrap();
        let files = snapshot(&root);
        assert!(String::from_utf8_lossy(&files[Path::new("Cargo.toml")]).contains("\"auth\""));
        assert!(String::from_utf8_lossy(&files[Path::new("src/main.rs")])
            .contains(".auth(application::app::auth::delivery())"));
        assert!(
            !String::from_utf8_lossy(&files[Path::new("app/models/user.rs")]).contains("Serialize")
        );
        assert_eq!(
            files.contains_key(Path::new("frontend/src/lib/auth.ts")),
            frontend == FrontendDriver::React
        );
        for (name, content) in &files {
            if name.extension().is_some_and(|extension| extension == "rs") {
                syn::parse_file(std::str::from_utf8(content).unwrap()).unwrap();
            }
        }
        let error = make_auth(&root).unwrap_err();
        assert!(error.to_string().contains("already exists"));
        assert_eq!(snapshot(&root), files);
    }
}
#[test]
fn collisions_and_invalid_registries_leave_all_files_unchanged() {
    for (path, contents) in [
        ("app/models/user.rs", "// existing user"),
        ("app/mod.rs", "use external::something as auth;"),
        ("app/mod.rs", "pub struct auth;"),
        ("app/models/mod.rs", "pub struct User;"),
        ("database/migrations/mod.rs", "// customized registry"),
        ("src/main.rs", "fn main() {}"),
        ("config/auth.toml", "# existing config"),
    ] {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("identity");
        new_project_with_frontend(&root, "identity", FrontendDriver::None).unwrap();
        fs::write(root.join(path), contents).unwrap();
        let before = snapshot(&root);
        assert!(make_auth(&root).is_err(), "{path}");
        assert_eq!(snapshot(&root), before, "{path}");
    }
}
#[cfg(unix)]
#[test]
fn installer_rejects_symlinked_registries() {
    let temp = tempfile::tempdir().unwrap();
    let root = temp.path().join("identity");
    new_project_with_frontend(&root, "identity", FrontendDriver::None).unwrap();
    let outside = temp.path().join("outside.rs");
    fs::write(&outside, "// outside").unwrap();
    fs::remove_file(root.join("app/models/mod.rs")).unwrap();
    std::os::unix::fs::symlink(&outside, root.join("app/models/mod.rs")).unwrap();
    assert!(make_auth(&root)
        .unwrap_err()
        .to_string()
        .contains("symlink"));
    assert_eq!(fs::read_to_string(outside).unwrap(), "// outside");
    assert!(!root.join("app/auth.rs").exists());
}

#[test]
fn legacy_conventional_helper_upgrades_but_customizations_are_preserved() {
    for customized in [false, true] {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("identity");
        new_project_with_frontend(&root, "identity", FrontendDriver::React).unwrap();
        let legacy = include_str!("../templates/auth/legacy-api.ts.txt");
        fs::write(
            root.join("frontend/src/lib/api.ts"),
            format!(
                "{legacy}{}",
                if customized {
                    "// my custom client\n"
                } else {
                    ""
                }
            ),
        )
        .unwrap();
        let before = snapshot(&root);
        if customized {
            assert!(make_auth(&root)
                .unwrap_err()
                .to_string()
                .contains("customized"));
            assert_eq!(snapshot(&root), before);
        } else {
            make_auth(&root).unwrap();
            assert!(fs::read_to_string(root.join("frontend/src/lib/api.ts"))
                .unwrap()
                .contains("setCsrfTokenProvider"));
        }
    }
}