puerto 0.14.0

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
use super::*;

// ── Phase 5: logger ───────────────────────────────────────────────────────

#[test]
fn new_project_bootstrap_injects_no_logger() {
    // Logging goes through `tracing` directly; the subscriber is set up once, by
    // `puerto_web::App::serve` (ADR-0017).
    let dir = temp_dir("logger_bootstrap");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();

    let output = generate_project("logger-test", &dir).unwrap();

    let content =
        fs::read_to_string(output.join("presentation/src/generated/bootstrap.rs")).unwrap();
    assert!(!content.contains("logger"), "{content}");
    let main = fs::read_to_string(output.join("presentation/src/main.rs")).unwrap();
    assert!(main.contains("puerto_web::App::new(config)"), "{main}");

    cleanup(&dir);
}

#[test]
fn scaffold_use_case_impl_logs_with_tracing() {
    let dir = temp_dir("logger_scaffold_impl");
    cleanup(&dir);
    setup_puerto_stubs(&dir);
    scaffold::run("Product", &dir, false, false, &[], &[]).unwrap();

    let content =
        fs::read_to_string(dir.join("business/src/application/product/create_product.rs")).unwrap();
    assert!(content.contains("tracing::info!"), "{content}");
    assert!(!content.contains("logger"), "{content}");

    cleanup(&dir);
}

#[test]
fn use_case_generator_impl_logs_with_tracing() {
    let dir = temp_dir("logger_uc_impl");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    setup_use_case_stubs(&dir);
    scaffold::run_use_case("Product", "delete_product", &dir).unwrap();

    let content =
        fs::read_to_string(dir.join("business/src/application/product/delete_product.rs")).unwrap();
    assert!(content.contains("tracing::info!"), "{content}");
    assert!(!content.contains("logger"), "{content}");

    cleanup(&dir);
}

#[test]
fn scaffold_bootstrap_wires_use_cases_without_a_logger() {
    let dir = temp_dir("logger_bootstrap_scaffold");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    setup_puerto_stubs(&dir);
    scaffold::run("Product", &dir, false, false, &[], &[]).unwrap();

    let content = fs::read_to_string(dir.join("presentation/src/generated/bootstrap.rs")).unwrap();
    assert!(content.contains(
        "let create_product = Arc::new(CreateProductUseCaseImpl { repository: Arc::new(InMemoryProductRepository::new()) as Arc<dyn ProductRepositoryTrait> });"
    ), "{content}");
    assert!(!content.contains("logger"), "{content}");

    cleanup(&dir);
}

// ── T-14: document-api conventions (T-31…T-38, logger removal) ────────────

/// Every file under `base` (skipping build output), as `(relative path, content)`.
fn project_files(base: &Path) -> Vec<(String, String)> {
    fn walk(dir: &Path, base: &Path, out: &mut Vec<(String, String)>) {
        for entry in fs::read_dir(dir).unwrap().flatten() {
            let path = entry.path();
            let name = entry.file_name().to_string_lossy().to_string();
            if name == "target" || name == ".git" {
                continue;
            }
            if path.is_dir() {
                walk(&path, base, out);
            } else if let Ok(content) = fs::read_to_string(&path) {
                let rel = path
                    .strip_prefix(base)
                    .unwrap()
                    .to_string_lossy()
                    .to_string();
                out.push((rel, content));
            }
        }
    }
    let mut out = vec![];
    walk(base, base, &mut out);
    out
}

/// A generated project with the Greeting demo plus a scaffolded entity carrying a VO.
fn project_with_product(name: &str, db: bool) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project(&name.replace('_', "-"), &dir).unwrap();
    crate::generators::project::apply_base_env(&output).unwrap();
    if db {
        crate::generators::project::apply_db_to_new_project(&output).unwrap();
    }
    let fields = [
        scaffold_test_field("title", "String"),
        puerto_toml::parse_field_arg("status:Status:enum:Active/Archived").unwrap(),
    ];
    scaffold::run_scaffold("Product", &output, &fields, OverwritePolicy::Ask).unwrap();
    (dir, output)
}

#[test]
fn generated_project_logs_with_tracing_and_has_no_logger_port() {
    // document-api's convention: `tracing` is already a facade, so an injected `LoggerTrait` only
    // added a field to every use case and repository and a mock helper to every test module.
    let (dir, output) = project_with_product("no_logger_port", true);
    scaffold::run_use_case("Product", "archive_product", &output).unwrap();

    for (path, content) in project_files(&output) {
        if !path.ends_with(".rs") {
            continue;
        }
        assert!(
            !content.contains("LoggerTrait"),
            "{path} still names LoggerTrait"
        );
        assert!(
            !content.contains("MockLogger"),
            "{path} still names MockLogger"
        );
        assert!(
            !content.contains("logger:"),
            "{path} still has a logger field"
        );
    }
    assert!(!output.join("business/src/domain/logger.rs").exists());
    assert!(!output.join("infrastructure/src/logger.rs").exists());
    let create =
        fs::read_to_string(output.join("business/src/application/product/create_product.rs"))
            .unwrap();
    assert!(create.contains("tracing::info!"), "{create}");
    let business_toml = fs::read_to_string(output.join("business/Cargo.toml")).unwrap();
    assert!(business_toml.contains("tracing"), "{business_toml}");

    cleanup(&dir);
}

#[test]
fn no_demo_project_has_no_logger_module() {
    let dir = temp_dir("no_demo_no_logger");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("no-demo-no-logger", &dir).unwrap();

    scaffold::apply_no_demo(&output).unwrap();

    let lib = fs::read_to_string(output.join("business/src/lib.rs")).unwrap();
    assert!(!lib.contains("logger"), "{lib}");
    let infra = fs::read_to_string(output.join("infrastructure/src/lib.rs")).unwrap();
    assert!(!infra.contains("logger"), "{infra}");
    cleanup(&dir);
}

// ── T-37: error responses ─────────────────────────────────────────────────

#[test]
fn error_mapper_names_the_code_and_maps_validation_to_422() {
    let fields = [
        scaffold_test_field("title", "String"),
        puerto_toml::parse_field_arg("status:Status:enum:Active/Archived").unwrap(),
    ];
    let ctx =
        crate::generators::presentation::build_error_mapper_ctx("Product", "product", &fields);

    let mapper = crate::generators::render::render("presentation/error_mapper.tera", &ctx).unwrap();

    assert!(mapper.contains("ErrorResponse::from_code"), "{mapper}");
    assert!(!mapper.contains("\"product_error\""), "{mapper}");
    assert!(
        mapper.contains("ProductError::ValidationError(_) => StatusCode::UNPROCESSABLE_ENTITY")
    );
    assert!(mapper.contains("ProductError::InvalidStatus => StatusCode::UNPROCESSABLE_ENTITY"));
    // One test per arm, so a status can't drift unnoticed.
    for test in [
        "should_map_validation_error_to_422_with_its_code",
        "should_map_not_found_to_404_with_its_code",
        "should_map_duplicate_to_409_with_its_code",
        "should_map_repository_error_to_500_with_its_code",
        "should_map_invalid_status_to_422_with_its_code",
    ] {
        assert!(mapper.contains(test), "missing {test}:\n{mapper}");
    }
}

#[test]
fn responses_offer_422_and_no_longer_map_400() {
    let ctx = crate::generators::presentation::build_simple_ctx("Product", "product");

    let responses =
        crate::generators::render::render("presentation/responses_crud.tera", &ctx).unwrap();

    assert!(responses.contains("#[oai(status = 422)]"));
    assert!(
        responses.contains("StatusCode::UNPROCESSABLE_ENTITY => Self::UnprocessableEntity(error)")
    );
    assert!(!responses.contains("BAD_REQUEST"), "{responses}");
}

// ── T-38: health probes + graceful shutdown ───────────────────────────────
// The probes, the server and the shutdown are `puerto-web`'s (ADR-0017) and tested there; the
// project hands its readiness check to `puerto_web::routes` (tests/runtime_crates.rs).

#[test]
fn readiness_pings_the_database_in_a_db_project() {
    let (dir, output) = project_with_product("health_db", true);

    let bootstrap =
        fs::read_to_string(output.join("presentation/src/generated/bootstrap.rs")).unwrap();
    let db_rs = fs::read_to_string(output.join("infrastructure/src/db.rs")).unwrap();

    assert!(
        bootstrap.contains("infrastructure::db::ping(&pool)"),
        "{bootstrap}"
    );
    // `ping` is `puerto-sqlx`'s, re-exported by the project's `db` module.
    assert!(db_rs.contains("pub use puerto_sqlx::*;"), "{db_rs}");
    cleanup(&dir);
}

// ── T-35: toolchain + edition ─────────────────────────────────────────────

#[test]
fn generated_project_pins_its_toolchain_and_uses_edition_2024() {
    // The clippy lints the generated `make lint` enforces change between Rust versions: without a
    // pin, whether a fresh project passes its own lint depends on the machine.
    let dir = temp_dir("toolchain_pin");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("toolchain-pin", &dir).unwrap();

    let toolchain = fs::read_to_string(output.join("rust-toolchain.toml")).unwrap();
    assert!(toolchain.contains("channel = \"1.95.0\""), "{toolchain}");
    assert!(toolchain.contains("clippy") && toolchain.contains("rustfmt"));
    for krate in ["business", "infrastructure", "presentation"] {
        let manifest = fs::read_to_string(output.join(format!("{krate}/Cargo.toml"))).unwrap();
        assert!(
            manifest.contains("edition = \"2024\""),
            "{krate}: {manifest}"
        );
    }
    let workspace = fs::read_to_string(output.join("Cargo.toml")).unwrap();
    assert!(workspace.contains("resolver = \"3\""), "{workspace}");
    cleanup(&dir);
}

// ── T-31 / T-32 / T-33: AI rules shipped to generated projects ────────────

#[test]
fn generated_agent_rules_never_describe_mod_rs_files() {
    // The per-crate AGENTS.md drew `mod.rs` trees — the one thing the module convention forbids —
    // and nothing tested them. An agent in a generated project was told to create them.
    let dir = temp_dir("agents_no_mod_rs");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("agents-no-mod-rs", &dir).unwrap();

    for (path, content) in project_files(&output) {
        if !path.ends_with("AGENTS.md") {
            continue;
        }
        for line in content.lines() {
            if line.contains("mod.rs") {
                assert!(
                    line.to_lowercase().contains("no `mod.rs`"),
                    "{path} describes a mod.rs file: {line}"
                );
            }
        }
        assert!(
            !content.contains("LoggerTrait"),
            "{path} documents the removed logger port"
        );
    }
    cleanup(&dir);
}

#[test]
fn generated_project_has_a_root_entry_point_for_agents() {
    let dir = temp_dir("agents_root");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("agents-root", &dir).unwrap();

    let claude = fs::read_to_string(output.join("CLAUDE.md")).unwrap();
    let agents = fs::read_to_string(output.join("AGENTS.md")).unwrap();

    assert_eq!(claude.trim(), "@AGENTS.md");
    for must in [
        "puerto generate",
        "generated/bootstrap.rs",
        "Tier 1",
        "make lint",
        "one commit",
        "business/AGENTS.md",
        // ADR-0017: an agent must know which code is the project's and which is the runtime's.
        "puerto-core",
        "puerto-web",
        "puerto-sqlx",
    ] {
        assert!(agents.contains(must), "root AGENTS.md misses '{must}'");
    }
    cleanup(&dir);
}

#[test]
fn template_bootstrap_is_exactly_what_the_generator_writes() {
    // `puerto new` copies the template's bootstrap.rs verbatim — it is not regenerated. When the
    // generator changed (the logger removal), the copy kept wiring a `TracingLogger` that no longer
    // existed, and every fresh project failed to compile.
    let dir = temp_dir("template_bootstrap_drift");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("template-bootstrap-drift", &dir).unwrap();
    let config = puerto_toml::read(&output).unwrap();

    let shipped =
        fs::read_to_string(output.join("presentation/src/generated/bootstrap.rs")).unwrap();
    let tags = fs::read_to_string(output.join("presentation/src/api/tags.rs")).unwrap();

    assert_eq!(
        shipped,
        crate::generators::bootstrap::generate_bootstrap_content(
            &config.entity,
            config.project.web
        )
        .unwrap(),
        "template/presentation/src/generated/bootstrap.rs is stale — regenerate it"
    );
    assert_eq!(
        tags,
        crate::generators::bootstrap::generate_tags_content(&config.entity).unwrap(),
        "template/presentation/src/api/tags.rs is stale — regenerate it"
    );
    cleanup(&dir);
}

// ── T-26: presentation holds ports, routes are tested ──────────────────────

fn scaffolded_product(name: &str) -> (PathBuf, PathBuf) {
    let dir = temp_dir(name);
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project(name, &dir).unwrap();
    scaffold::run_scaffold(
        "Product",
        &output,
        &[scaffold_test_field("title", "String")],
        OverwritePolicy::Ask,
    )
    .unwrap();
    (dir, output)
}

#[test]
fn scaffold_routes_hold_use_case_ports_not_impls() {
    let (dir, output) = scaffolded_product("t26_routes_ports");

    let routes = fs::read_to_string(output.join("presentation/src/api/product/routes.rs")).unwrap();
    for uc in [
        "CreateProduct",
        "GetProduct",
        "ListProducts",
        "UpdateProduct",
        "DeleteProduct",
    ] {
        assert!(
            routes.contains(&format!("Arc<dyn {uc}UseCaseTrait>")),
            "{routes}"
        );
    }
    assert!(!routes.contains("UseCaseImpl"), "{routes}");
    assert!(!routes.contains("application::"), "{routes}");

    cleanup(&dir);
}

#[test]
fn scaffold_use_case_traits_ship_a_mock() {
    let (dir, output) = scaffolded_product("t26_trait_mocks");

    for (module, uc) in [
        ("create_product", "CreateProduct"),
        ("get_product", "GetProduct"),
        ("list_products", "ListProducts"),
        ("update_product", "UpdateProduct"),
        ("delete_product", "DeleteProduct"),
    ] {
        let src = fs::read_to_string(
            output.join(format!("business/src/domain/product/use_cases/{module}.rs")),
        )
        .unwrap();
        assert!(
            src.contains("#[cfg(any(test, feature = \"test-utils\"))]\npub mod mocks {"),
            "{src}"
        );
        assert!(src.contains(&format!("pub {uc}UseCase {{}}")), "{src}");
    }

    cleanup(&dir);
}

#[test]
fn object_mothers_are_available_to_other_crates_tests() {
    let (dir, output) = scaffolded_product("t26_mothers_feature");

    let lib = fs::read_to_string(output.join("business/src/lib.rs")).unwrap();
    assert!(
        lib.contains("#[cfg(any(test, feature = \"test-utils\"))]\npub mod tests {"),
        "{lib}"
    );

    cleanup(&dir);
}

#[test]
fn scaffold_routes_test_every_status_path_with_mocked_use_cases() {
    let (dir, output) = scaffolded_product("t26_routes_tests");

    let routes = fs::read_to_string(output.join("presentation/src/api/product/routes.rs")).unwrap();
    assert!(routes.contains("#[cfg(test)]\nmod tests {"), "{routes}");
    assert!(routes.contains("poem::test::TestClient") || routes.contains("test::TestClient"));
    for status in [
        "CREATED",
        "UNPROCESSABLE_ENTITY",
        "OK",
        "NOT_FOUND",
        "INTERNAL_SERVER_ERROR",
        "NO_CONTENT",
    ] {
        assert!(
            routes.contains(&format!("StatusCode::{status}")),
            "{status}: {routes}"
        );
    }
    assert!(routes.contains("MockCreateProductUseCase"), "{routes}");
    assert!(routes.contains("ProductMother::random()"), "{routes}");

    cleanup(&dir);
}

#[test]
fn template_greeting_api_holds_a_port_and_is_tested() {
    let dir = temp_dir("t26_greeting");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("t26-greeting", &dir).unwrap();

    let routes =
        fs::read_to_string(output.join("presentation/src/api/greeting/routes.rs")).unwrap();
    assert!(
        routes.contains("Arc<dyn GetGreetingUseCaseTrait>"),
        "{routes}"
    );
    assert!(!routes.contains("UseCaseImpl"), "{routes}");
    assert!(routes.contains("MockGetGreetingUseCase"), "{routes}");
    let uc =
        fs::read_to_string(output.join("business/src/domain/greeting/use_cases/get_greeting.rs"))
            .unwrap();
    assert!(uc.contains("pub GetGreetingUseCase {}"), "{uc}");

    cleanup(&dir);
}

#[test]
fn presentation_tests_get_business_test_utils_and_the_poem_test_client() {
    let dir = temp_dir("t26_presentation_deps");
    cleanup(&dir);
    fs::create_dir_all(&dir).unwrap();
    let output = generate_project("t26-presentation-deps", &dir).unwrap();

    let manifest = fs::read_to_string(output.join("presentation/Cargo.toml")).unwrap();
    let dev = manifest
        .split("[dev-dependencies]")
        .nth(1)
        .expect("presentation has [dev-dependencies]");
    assert!(
        dev.contains(r#"business = { path = "../business", features = ["test-utils"] }"#),
        "{manifest}"
    );
    assert!(
        dev.contains(r#"poem = { version = "3", features = ["test"] }"#),
        "{manifest}"
    );

    cleanup(&dir);
}