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

- `std.fs.create_dir` reports an error when a path component is an existing regular file instead of silently succeeding. [#19](https://github.com/puddi-lang/puddi/issues/19)
- `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
5 changes: 4 additions & 1 deletion src/platform_wasi.pud
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ fn __fs_mkdir(path: Str) -> Result<(), Str> {
if part == "" { continue }
cur = if cur == "" { part } else if cur == "/" { "/{part}" } else { "{cur}/{part}" }
match __fs_stat(cur) {
Ok(_) -> {}
Ok(s) -> match s.type_ {
__w_wasi_filesystem_types.DescriptorType.Directory -> {}
_ -> return Err(__io_err(__w_wasi_filesystem_types.ErrorCode.NotDirectory))
}
Err(_) -> {
let (d, rel) = __resolve_path(cur)?
match d.create_directory_at(rel) {
Expand Down
34 changes: 34 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,37 @@ fn pud_add_name_requires_value() {
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("--name needs a value"), "{stderr}");
}

#[test]
fn create_dir_rejects_file_component() {
let dir = std::env::temp_dir().join(format!("puddi-create-dir-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let src = dir.join("create_dir.pud");
std::fs::write(&src, r#"
import std.fs

fn show(name: Str, r: Result<(), fs.IoErr>) {
match r {
Ok(_) -> print("{name}: Ok")
Err(_) -> print("{name}: Err")
}
}

fn main() {
fs.write("f", "hello").unwrap()
show("file", fs.create_dir("f"))
show("file-component", fs.create_dir("f/sub"))
show("dir-first", fs.create_dir("d"))
show("dir-again", fs.create_dir("d"))
}
"#).unwrap();
let out = pud().current_dir(&dir).arg("run").arg(&src).output().unwrap();
std::fs::remove_dir_all(&dir).unwrap();
let text = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
assert!(out.status.success(), "{text}");
assert!(text.contains("file: Err"), "{text}");
assert!(text.contains("file-component: Err"), "{text}");
assert!(text.contains("dir-first: Ok"), "{text}");
assert!(text.contains("dir-again: Ok"), "{text}");
}
Loading