Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 3 additions & 8 deletions examples/todo/src/main.pud
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,16 @@ fn load(path: Str) -> Result<List<Todo>, 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")

for t in pending {
print(" - {t.title}")
}
Ok(())
}

test "empty list has nothing pending" {
Expand Down
24 changes: 24 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Loading