puerto 0.19.1

Puerto — Rust full-stack DDD framework CLI. Scaffolds Clean Architecture projects with auto-wired DI, SQLx support, and zero manual wiring. Formerly puerto-framework.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Phase 12.0 (ADR-0017): what every project shares lives in `puerto-core`, not in its template.
use super::*;

fn read(dir: &Path, path: &str) -> String {
    fs::read_to_string(dir.join(path)).unwrap()
}

/// The `major.minor` of the CLI under test: what a new project asks of `puerto-core`.
fn cli_minor() -> String {
    let version = env!("CARGO_PKG_VERSION");
    let mut parts = version.split('.');
    format!("{}.{}", parts.next().unwrap(), parts.next().unwrap())
}

fn new_project(name: &str) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project(name, &dir).unwrap();
    (dir, output)
}

#[test]
fn new_project_depends_on_puerto_core_at_the_cli_minor() {
    let (dir, output) = new_project("runtime-deps");

    let root = read(&output, "Cargo.toml");
    assert!(root.contains("[workspace.dependencies]"));
    assert!(
        root.contains(&format!("puerto-core = \"{}\"", cli_minor())),
        "{root}"
    );
    assert!(!root.contains(crate::runtime::VERSION_MARKER));
    for member in ["business", "infrastructure", "presentation"] {
        let manifest = read(&output, &format!("{member}/Cargo.toml"));
        assert!(
            manifest.contains("puerto-core = { workspace = true }"),
            "{member}"
        );
    }

    cleanup(&dir);
}

#[test]
fn new_project_ships_no_copy_of_the_runtime_types() {
    let (dir, output) = new_project("runtime-no-shared");

    for path in [
        "business/src/domain/shared.rs",
        "business/src/domain/shared/pagination.rs",
        "business/src/domain/shared/repository_error.rs",
    ] {
        assert!(!output.join(path).exists(), "{path}");
    }
    assert!(!read(&output, "business/src/lib.rs").contains("pub mod shared;"));

    cleanup(&dir);
}

#[test]
fn no_demo_project_declares_no_shared_module() {
    let (dir, output) = new_project("runtime-no-demo");
    scaffold::apply_no_demo(&output).unwrap();

    assert_eq!(
        read(&output, "business/src/lib.rs"),
        "pub mod domain {\n}\npub mod application {\n}\n"
    );
    assert!(!output.join("business/src/domain/shared.rs").exists());

    cleanup(&dir);
}

#[test]
fn greeting_takes_the_repository_error_from_puerto_core() {
    let (dir, output) = new_project("runtime-greeting");

    for path in [
        "business/src/domain/greeting/errors.rs",
        "business/src/domain/greeting/repository.rs",
        "infrastructure/src/greeting/repository.rs",
    ] {
        let source = read(&output, path);
        assert!(
            source.contains("use puerto_core::RepositoryError;"),
            "{path}"
        );
        assert!(!source.contains("domain::shared"), "{path}");
    }

    cleanup(&dir);
}

#[test]
fn scaffold_imports_the_runtime_types_from_puerto_core() {
    for (name, db) in [
        ("runtime_scaffold_mem", false),
        ("runtime_scaffold_pg", true),
    ] {
        let dir = temp_dir(name);
        cleanup(&dir);
        setup_puerto_stubs(&dir);
        if db {
            fs::create_dir_all(dir.join("infrastructure/migrations")).unwrap();
        }
        scaffold::run("Product", &dir, db, true, &[], &[]).unwrap();

        for (path, import) in [
            (
                "business/src/domain/product/errors.rs",
                "use puerto_core::RepositoryError;",
            ),
            (
                "business/src/domain/product/repository.rs",
                "use puerto_core::{Page, PageRequest, RepositoryError};",
            ),
            (
                "business/src/domain/product/use_cases/list_products.rs",
                "use puerto_core::{Page, PageRequest};",
            ),
            (
                "business/src/application/product/list_products.rs",
                "use puerto_core::Page;",
            ),
            (
                "infrastructure/src/product/repository.rs",
                "use puerto_core::{Page, PageRequest, RepositoryError};",
            ),
            (
                "presentation/src/api/product/routes.rs",
                "use puerto_core::PageRequest;",
            ),
            (
                "presentation/src/api/product/dto.rs",
                "use puerto_core::Page;",
            ),
        ] {
            let source = read(&dir, path);
            assert!(source.contains(import), "{name}: {path}");
            assert!(!source.contains("shared::pagination"), "{name}: {path}");
            assert!(
                !source.contains("shared::repository_error"),
                "{name}: {path}"
            );
        }

        cleanup(&dir);
    }
}

#[test]
fn non_crud_scaffold_imports_the_repository_error_from_puerto_core() {
    for (name, db) in [("runtime_simple_mem", false), ("runtime_simple_pg", true)] {
        let dir = temp_dir(name);
        cleanup(&dir);
        setup_puerto_stubs(&dir);
        if db {
            fs::create_dir_all(dir.join("infrastructure/migrations")).unwrap();
        }
        scaffold::run("Product", &dir, db, false, &[], &[]).unwrap();

        for path in [
            "business/src/domain/product/repository.rs",
            "infrastructure/src/product/repository.rs",
        ] {
            let source = read(&dir, path);
            assert!(
                source.contains("use puerto_core::RepositoryError;"),
                "{name}: {path}"
            );
            assert!(!source.contains("domain::shared"), "{name}: {path}");
        }

        cleanup(&dir);
    }
}

#[test]
fn snippets_import_the_runtime_types_from_puerto_core() {
    let snippets = format!(
        "{}{}",
        crate::snippets::SNIPPETS_JSON,
        crate::snippets::SQL_SNIPPETS_JSON
    );
    assert!(!snippets.contains("shared::pagination"));
    assert!(!snippets.contains("shared::repository_error"));
    assert!(snippets.contains("use puerto_core::RepositoryError;"));
    assert!(snippets.contains("use puerto_core::{Page, PageRequest, RepositoryError};"));
}

// ── The rendered requirement and the version check ────────────────────────

#[test]
fn should_require_the_cli_minor_when_rendering_the_runtime_version() {
    assert_eq!(crate::runtime::requirement("0.14.0"), "0.14");
    assert_eq!(crate::runtime::requirement("0.14.3"), "0.14");
    assert_eq!(crate::runtime::requirement("1.2.0"), "1.2");
}

#[test]
fn should_replace_the_marker_with_the_requirement_when_rendering_the_manifest() {
    let manifest = format!(
        "[workspace.dependencies]\npuerto-core = \"{}\"\n",
        crate::runtime::VERSION_MARKER
    );
    assert_eq!(
        crate::runtime::render_manifest(&manifest, "0.14.2"),
        "[workspace.dependencies]\npuerto-core = \"0.14\"\n"
    );
}

#[test]
fn should_warn_when_a_runtime_crate_is_on_another_minor() {
    let manifest = "[workspace.dependencies]\npuerto-core = \"0.13\"\n";
    assert_eq!(
        crate::runtime::version_warnings(manifest, "0.14.0"),
        vec!["puerto-core 0.13 but puerto 0.14 — see the CHANGELOG migration guide".to_string()]
    );
}

#[test]
fn should_stay_silent_when_only_the_patch_differs() {
    let manifest = "[workspace.dependencies]\npuerto-core = \"0.14.1\"\n";
    assert!(crate::runtime::version_warnings(manifest, "0.14.3").is_empty());
}

#[test]
fn should_read_the_version_from_a_table_and_from_member_dependencies() {
    let manifest = "[dependencies]\npuerto-core = { version = \"0.12\" }\n";
    assert_eq!(
        crate::runtime::version_warnings(manifest, "0.14.0").len(),
        1
    );
}

#[test]
fn should_ignore_a_manifest_without_runtime_crates() {
    let manifest = "[workspace]\nmembers = [\"business\"]\n";
    assert!(crate::runtime::version_warnings(manifest, "0.14.0").is_empty());
}

#[test]
fn should_ignore_a_workspace_inherited_runtime_dependency() {
    let manifest = "[dependencies]\npuerto-core = { workspace = true }\n";
    assert!(crate::runtime::version_warnings(manifest, "0.14.0").is_empty());
}

#[test]
fn validate_passes_on_a_new_project() {
    let (dir, output) = new_project("runtime-validate");

    assert!(validate::run_validate(&output).is_ok());

    cleanup(&dir);
}

// ── Phase 12.1: `puerto-web` ──────────────────────────────────────────────

#[test]
fn new_project_depends_on_puerto_web_from_presentation_only() {
    let (dir, output) = new_project("runtime-web-deps");

    let root = read(&output, "Cargo.toml");
    assert!(
        root.contains(&format!("puerto-web = \"{}\"", cli_minor())),
        "{root}"
    );
    let presentation = read(&output, "presentation/Cargo.toml");
    assert!(presentation.contains("puerto-web = { workspace = true }"));
    // The server, `.env` and the subscriber are the runtime's now.
    assert!(!presentation.contains("dotenvy"), "{presentation}");
    assert!(
        !presentation.contains("tracing-subscriber"),
        "{presentation}"
    );
    for member in ["business", "infrastructure"] {
        assert!(!read(&output, &format!("{member}/Cargo.toml")).contains("puerto-web"));
    }

    cleanup(&dir);
}

#[test]
fn new_project_main_hands_the_server_to_puerto_web() {
    let (dir, output) = new_project("runtime-web-main");

    let main = read(&output, "presentation/src/main.rs");
    assert!(main.contains("puerto_web::Config::from_env()?"), "{main}");
    assert!(main.contains("puerto_web::App::new(config)"), "{main}");
    assert!(
        main.contains(".try_serve(generated::bootstrap::build_app)"),
        "{main}"
    );
    assert!(main.contains("mod app;"), "{main}");
    for gone in [
        "run_with_graceful_shutdown",
        "tracing_subscriber",
        "SERVICE_PORT",
        "mod health;",
    ] {
        assert!(!main.contains(gone), "{gone}: {main}");
    }

    cleanup(&dir);
}

#[test]
fn new_project_ships_no_copy_of_the_http_shell() {
    let (dir, output) = new_project("runtime-web-no-copy");

    assert!(!output.join("presentation/src/health.rs").exists());
    // `IntoErrorResponse` stays: an `error_mapper.rs` implements it for an error of `business`,
    // which the orphan rule only allows for a trait of `presentation` itself.
    let error = read(&output, "presentation/src/api/error.rs");
    assert!(error.contains("pub trait IntoErrorResponse"), "{error}");
    assert!(error.contains("use puerto_web::ErrorResponse;"), "{error}");
    assert!(!error.contains("struct ErrorResponse"), "{error}");
    let greeting = format!(
        "{}{}",
        read(&output, "presentation/src/api/greeting/error_mapper.rs"),
        read(&output, "presentation/src/api/greeting/responses.rs")
    );
    assert!(!greeting.contains("error::ErrorResponse"), "{greeting}");
    assert!(greeting.contains("use crate::api::error::IntoErrorResponse;"));
    assert!(greeting.contains("use puerto_web::ErrorResponse;"));

    cleanup(&dir);
}

#[test]
fn bootstrap_builds_the_routes_with_puerto_web_and_hands_them_to_the_app_hook() {
    let (dir, output) = new_project("runtime-web-bootstrap");

    let bootstrap = read(&output, "presentation/src/generated/bootstrap.rs");
    assert!(
        bootstrap.contains(
            "pub async fn build_app(config: puerto_web::Config) -> Result<impl Endpoint, String>"
        ),
        "{bootstrap}"
    );
    assert!(
        bootstrap.contains("Ok(crate::app::configure(puerto_web::routes("),
        "{bootstrap}"
    );
    assert!(bootstrap.contains("puerto_web::health::always_ready()"));
    for gone in ["Cors", "std::env::var(\"SERVICE_PORT\")", "crate::health"] {
        assert!(!bootstrap.contains(gone), "{gone}: {bootstrap}");
    }

    cleanup(&dir);
}

#[test]
fn db_bootstrap_probes_the_database_through_puerto_web() {
    let dir = temp_dir("runtime_web_db_bootstrap");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some("runtime-web-db".into()), true, &dir).unwrap();
    // The readiness check pings the database once an entity is stored in it.
    scaffold::run_scaffold("Product", &output, &[], OverwritePolicy::Ask).unwrap();

    let bootstrap = read(&output, "presentation/src/generated/bootstrap.rs");
    assert!(
        bootstrap.contains("puerto_web::health::probe(move ||"),
        "{bootstrap}"
    );
    assert!(
        bootstrap.contains("infrastructure::db::ping(&pool)"),
        "{bootstrap}"
    );
    assert!(!bootstrap.contains("always_ready"), "{bootstrap}");

    cleanup(&dir);
}

#[test]
fn new_project_has_a_hand_written_app_hook() {
    let (dir, output) = new_project("runtime-web-app-hook");

    let app = read(&output, "presentation/src/app.rs");
    assert!(
        app.contains("pub fn configure(routes: Route) -> impl Endpoint"),
        "{app}"
    );
    assert!(!app.contains("AUTO-GENERATED"), "{app}");

    cleanup(&dir);
}

#[test]
fn app_hook_survives_scaffold_force_and_bootstrap_byte_for_byte() {
    let (dir, output) = new_project("runtime-web-app-survives");
    let hook = output.join("presentation/src/app.rs");
    let custom = "use poem::{Endpoint, EndpointExt, Route, middleware::SetHeader};\n\npub fn configure(routes: Route) -> impl Endpoint {\n    routes.with(SetHeader::new().overriding(\"x-team\", \"orders\"))\n}\n";
    fs::write(&hook, custom).unwrap();

    scaffold::run_scaffold("Product", &output, &[], OverwritePolicy::Ask).unwrap();
    scaffold::run_scaffold("Product", &output, &[], OverwritePolicy::Force).unwrap();
    crate::generators::bootstrap::run_bootstrap_command(&output, false).unwrap();

    assert_eq!(fs::read_to_string(&hook).unwrap(), custom);

    cleanup(&dir);
}

#[test]
fn scaffold_takes_the_error_body_from_puerto_web() {
    for (name, crud) in [("runtime_web_crud", true), ("runtime_web_simple", false)] {
        let dir = temp_dir(name);
        cleanup(&dir);
        setup_puerto_stubs(&dir);
        scaffold::run("Product", &dir, false, crud, &[], &[]).unwrap();

        for (path, import) in [
            (
                "presentation/src/api/product/error_mapper.rs",
                "use puerto_web::ErrorResponse;",
            ),
            (
                "presentation/src/api/product/responses.rs",
                "use puerto_web::ErrorResponse;",
            ),
        ] {
            let source = read(&dir, path);
            assert!(source.contains(import), "{name}: {path}\n{source}");
            assert!(!source.contains("error::ErrorResponse"), "{name}: {path}");
        }

        cleanup(&dir);
    }
}

#[test]
fn snippets_take_the_error_body_from_puerto_web() {
    let snippets = crate::snippets::SNIPPETS_JSON;
    assert!(!snippets.contains("error::ErrorResponse"));
    assert!(!snippets.contains("error::{ErrorResponse"));
    assert!(snippets.contains("use puerto_web::ErrorResponse;"));
}

#[test]
fn every_runtime_crate_is_patched_in_the_compile_matrix() {
    assert_eq!(
        crate::runtime::RUNTIME_CRATES,
        [
            "puerto-core",
            "puerto-web",
            "puerto-sqlx",
            "puerto-events",
            "puerto-kafka",
            "puerto-mail",
            "puerto-auth"
        ]
    );
}

// ── Phase 12.2: `puerto-sqlx` ─────────────────────────────────────────────

fn new_db_project(name: &str) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = new_project_non_interactive(Some(name.into()), true, &dir).unwrap();
    (dir, output)
}

#[test]
fn db_project_depends_on_puerto_sqlx_from_infrastructure_only() {
    let (dir, output) = new_db_project("runtime-sqlx-deps");

    let root = read(&output, "Cargo.toml");
    assert!(
        root.contains(&format!("puerto-sqlx = \"{}\"", cli_minor())),
        "{root}"
    );
    let infrastructure = read(&output, "infrastructure/Cargo.toml");
    assert!(
        infrastructure.contains("puerto-sqlx = { workspace = true }"),
        "{infrastructure}"
    );
    for member in ["business", "presentation"] {
        assert!(!read(&output, &format!("{member}/Cargo.toml")).contains("puerto-sqlx"));
    }

    cleanup(&dir);
}

#[test]
fn project_without_db_does_not_depend_on_puerto_sqlx() {
    let (dir, output) = new_project("runtime-sqlx-no-db");

    for path in [
        "Cargo.toml",
        "business/Cargo.toml",
        "infrastructure/Cargo.toml",
        "presentation/Cargo.toml",
    ] {
        assert!(!read(&output, path).contains("puerto-sqlx"), "{path}");
    }

    cleanup(&dir);
}

#[test]
fn db_module_keeps_only_the_project_migrations_and_re_exports_puerto_sqlx() {
    let (dir, output) = new_db_project("runtime-sqlx-db-rs");

    let db = read(&output, "infrastructure/src/db.rs");
    assert!(db.contains("pub use puerto_sqlx::*;"), "{db}");
    assert!(
        db.contains(
            "pub static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!(\"./migrations\");"
        ),
        "{db}"
    );
    // The pool, the probe and the error mapping are the runtime's — no code of their own here.
    for gone in ["fn ", "23505", "PgPoolOptions", "DatabaseError"] {
        assert!(!db.contains(gone), "{gone}: {db}");
    }

    cleanup(&dir);
}

#[test]
fn db_bootstrap_runs_the_project_migrations_through_puerto_sqlx() {
    let (dir, output) = new_db_project("runtime-sqlx-bootstrap");
    scaffold::run_scaffold("Product", &output, &[], OverwritePolicy::Ask).unwrap();

    let bootstrap = read(&output, "presentation/src/generated/bootstrap.rs");
    assert!(
        bootstrap
            .contains("infrastructure::db::run_migrations(&pool, &infrastructure::db::MIGRATOR)"),
        "{bootstrap}"
    );
    assert!(
        bootstrap.contains("infrastructure::db::create_postgres_pool(&database_url)"),
        "{bootstrap}"
    );

    cleanup(&dir);
}

#[test]
fn compile_matrix_patches_only_the_runtime_crates_a_project_names() {
    let (dir, output) = new_project("runtime-sqlx-patch");
    super::compile_matrix::patch_runtime_crates(&output);

    let root = read(&output, "Cargo.toml");
    let patch = &root[root.find("[patch.crates-io]").unwrap()..];
    assert!(patch.contains("puerto-core = { path"), "{patch}");
    assert!(patch.contains("puerto-web = { path"), "{patch}");
    // An unused `[patch]` entry is a cargo warning in every build of a project without a database.
    assert!(!patch.contains("puerto-sqlx"), "{patch}");

    cleanup(&dir);
}

// Phase 13.2 (ADR-0018): the outbox tables `puerto new --events` writes are the ones
// `puerto-sqlx`'s own tests run against (`OUTBOX_MIGRATION` embeds that file).
#[test]
fn outbox_migration_template_is_exactly_the_one_puerto_sqlx_is_tested_with() {
    let template = include_str!("../templates/infrastructure/outbox.sql");
    let runtime = include_str!("../../../sqlx/migrations/outbox.sql");
    let runtime_lib = include_str!("../../../sqlx/src/lib.rs");

    assert_eq!(template, runtime);
    assert!(runtime_lib.contains(r#"include_str!("../migrations/outbox.sql")"#));
}

// Phase 14.2 (ADR-0020): the sent-mail log the first `generate mail --on` writes is the table
// `puerto-sqlx`'s own tests run against (`SENT_MAILS_MIGRATION` embeds that file).
#[test]
fn sent_mails_migration_template_is_exactly_the_one_puerto_sqlx_is_tested_with() {
    let template = include_str!("../templates/infrastructure/sent_mails.sql");
    let runtime = include_str!("../../../sqlx/migrations/sent_mails.sql");
    let runtime_lib = include_str!("../../../sqlx/src/lib.rs");

    assert_eq!(template, runtime);
    assert!(runtime_lib.contains(r#"include_str!("../migrations/sent_mails.sql")"#));
}

// Phase 16.6 (ADR-0021): `puerto-sqlx`'s `PgSessions` joins the `users` table `puerto new --auth`
// writes, so the two must be one schema (`USERS_MIGRATION` embeds that file).
#[test]
fn users_migration_template_is_exactly_the_one_puerto_sqlx_is_tested_with() {
    let template = include_str!("../templates/auth/users.sql");
    let runtime = include_str!("../../../sqlx/migrations/users.sql");
    let runtime_lib = include_str!("../../../sqlx/src/lib.rs");

    assert_eq!(template, runtime);
    assert!(runtime_lib.contains(r#"include_str!("../migrations/users.sql")"#));
}

// Phase 16.6 (ADR-0021): the credentials and sessions `puerto new --auth local` writes are the
// tables `puerto-sqlx`'s own tests run against (`AUTH_MIGRATION` embeds that file).
#[test]
fn auth_migration_template_is_exactly_the_one_puerto_sqlx_is_tested_with() {
    let template = include_str!("../templates/auth/local/auth.sql");
    let runtime = include_str!("../../../sqlx/migrations/auth.sql");
    let runtime_lib = include_str!("../../../sqlx/src/lib.rs");

    assert_eq!(template, runtime);
    assert!(runtime_lib.contains(r#"include_str!("../migrations/auth.sql")"#));
}