From c256250b6ecc6e8a25943f7f3bce1371d1ed01f3 Mon Sep 17 00:00:00 2001 From: faizaan Date: Mon, 30 Nov 2026 20:35:37 +0530 Subject: [PATCH] Return a failing status when the todo example cannot load --- CHANGELOG.md | 1 + examples/todo/src/main.pud | 11 +++-------- tests/run.rs | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2fdf21..a178c94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to Puddi are recorded here. The format follows [Keep a Chang ### Fixed +- The todo example returns a failing exit status when `todos.json` cannot be loaded. [#20](https://github.com/puddi-lang/puddi/issues/20) - `pud add --name` requires a value instead of silently defaulting. - `pud doc` preserves paragraph breaks in module documentation. - Integer literals too large for the compiler's integer representation report an error instead of wrapping. diff --git a/examples/todo/src/main.pud b/examples/todo/src/main.pud index debf53f..3fc5488 100644 --- a/examples/todo/src/main.pud +++ b/examples/todo/src/main.pud @@ -50,14 +50,8 @@ fn load(path: Str) -> Result, AppErr> { decode(json.parse(text)?) } -fn main() { - let todos = match load("todos.json") { - Ok(t) -> t - Err(e) -> { - print("could not load: {e}") - return - } - } +fn main() -> Result<(), AppErr> { + let todos = load("todos.json")? let pending = todos.filter(|t| !t.done) print("{pending.len()} of {todos.len()} left") @@ -65,6 +59,7 @@ fn main() { for t in pending { print(" - {t.title}") } + Ok(()) } test "empty list has nothing pending" { diff --git a/tests/run.rs b/tests/run.rs index b1d6e08..f7cced9 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -140,3 +140,27 @@ fn pud_add_name_requires_value() { let stderr = String::from_utf8_lossy(&out.stderr); assert!(stderr.contains("--name needs a value"), "{stderr}"); } + +#[test] +fn todo_example_reports_load_failure() { + let example = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/todo"); + + // The bundled example still succeeds and prints exactly the same output. + let ok = pud().arg("run").current_dir(&example).output().unwrap(); + assert!(ok.status.success(), "{:?}", ok); + let ok_text = format!("{}{}", String::from_utf8_lossy(&ok.stdout), String::from_utf8_lossy(&ok.stderr)); + assert_eq!(ok_text, "2 of 4 left\n - add async\n - publish packages\n"); + + // A copy without todos.json must fail with an error instead of exiting 0. + let dir = std::env::temp_dir().join(format!("puddi-todo-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("src")).unwrap(); + std::fs::copy(example.join("pud.toml"), dir.join("pud.toml")).unwrap(); + std::fs::copy(example.join("src/main.pud"), dir.join("src/main.pud")).unwrap(); + let out = pud().arg("run").current_dir(&dir).output().unwrap(); + let text = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!out.status.success(), "todo example exited successfully on load failure: {text}"); + assert!(text.contains("error:"), "{text}"); + assert!(text.contains("not found"), "{text}"); +}