puerto 0.33.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, puerto-framework.
//! Fase 37.5: `puerto generar --simular …` runs the command on a copy of the project and says what
//! it would change, without writing anything in it.
use super::*;
use crate::commands::simulate;
use crate::tree::{self, Change};

#[test]
fn the_changes_between_two_trees_are_new_changed_with_their_lines_and_deleted() {
    let dir = temp_dir("simulate_trees");
    cleanup(&dir);
    let (before, after) = (dir.join("antes"), dir.join("despues"));
    for (root, files) in [
        (
            &before,
            &[
                ("igual.txt", "a\n"),
                ("cambia.txt", "a\nb\n"),
                ("se_va.txt", "x\n"),
            ][..],
        ),
        (
            &after,
            &[
                ("igual.txt", "a\n"),
                ("cambia.txt", "a\nc\nd\n"),
                ("sub/nuevo.txt", "y\n"),
            ],
        ),
    ] {
        for (path, content) in files {
            fs::create_dir_all(root.join(path).parent().unwrap()).unwrap();
            fs::write(root.join(path), content).unwrap();
        }
    }

    let changes = tree::changes(
        &tree::snapshot(&before).unwrap(),
        &tree::snapshot(&after).unwrap(),
    );

    assert_eq!(
        changes,
        vec![
            (
                PathBuf::from("cambia.txt"),
                Change::Modified {
                    added: 2,
                    removed: 1
                }
            ),
            (PathBuf::from("se_va.txt"), Change::Deleted),
            (PathBuf::from("sub/nuevo.txt"), Change::Created),
        ]
    );
    assert_eq!(
        simulate::report(&changes),
        "~ cambia.txt (+2 −1)\n- se_va.txt\n+ sub/nuevo.txt\n"
    );
    assert_eq!(simulate::report(&[]), "Nada cambiaría.\n");
    cleanup(&dir);
}

#[test]
fn a_simulated_scaffold_lists_what_it_writes_and_leaves_the_project_as_it_was() {
    let dir = temp_dir("simulate_scaffold");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("simular", &dir).unwrap();
    let before = tree::snapshot(&output).unwrap();

    let changes = simulate::simulate(&output, |copy| {
        scaffold::run_scaffold("Producto", copy, &[], OverwritePolicy::Ask)
    })
    .unwrap();

    assert!(
        changes.contains(&(
            PathBuf::from("contextos/producto/dominio/src/modelo.rs"),
            Change::Created
        )),
        "{changes:?}"
    );
    assert!(
        changes
            .iter()
            .any(|(path, change)| path == Path::new("puerto.toml")
                && matches!(change, Change::Modified { .. })),
        "{changes:?}"
    );
    assert!(
        tree::snapshot(&output).unwrap() == before,
        "the project changed"
    );
    assert!(!output.join(".puerto").exists());
    cleanup(&dir);
}

#[test]
fn a_simulated_command_that_fails_gives_its_error_and_writes_nothing() {
    let dir = temp_dir("simulate_fails");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("simular-falla", &dir).unwrap();
    let before = tree::snapshot(&output).unwrap();

    let result = simulate::simulate(&output, |copy| {
        fs::write(copy.join("a_medias.txt"), "x").unwrap();
        Err("el generador falla".into())
    });

    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("el generador falla")
    );
    assert!(
        tree::snapshot(&output).unwrap() == before,
        "the project changed"
    );
    cleanup(&dir);
}

#[test]
fn the_command_run_on_the_copy_is_the_same_without_simular() {
    let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();

    assert_eq!(
        simulate::without_simulate(args(&[
            "generar",
            "--simular",
            "campo",
            "X",
            "--",
            "--simular"
        ])),
        args(&["generar", "campo", "X", "--", "--simular"])
    );
    assert_eq!(
        simulate::without_simulate(args(&["generar", "campo", "X", "--simular"])),
        args(&["generar", "campo", "X"])
    );
}