Add tests for using file operations on directories#5717
Add tests for using file operations on directories#5717andrewrk merged 5 commits intoziglang:masterfrom
Conversation
lib/std/fs/test.zig
Outdated
| // note: the `.write = true` is necessary to ensure the error occurs on all platforms | ||
| testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true })); |
There was a problem hiding this comment.
I didn't include a read-only test because I was unsure about how to handle the differing errors per-platform, as mentioned here:
On Linux, when opening a file with read-only permissions, there is no way to determine whether the file descriptor is a directory handle without an additional syscall.
That is, open on Linux only returns EISDIR when "pathname refers to a directory and the access requested involved writing".
Seems like it'd be good to test the read-only case, but I'm not sure if that should involve changing the implementation of openFile to behave consistently across platforms (i.e. do a stat on Linux and return error.IsDir) or if the test case should have a per-platform implementation and the differing IsDir behavior be documented. This might be worth opening a new issue for, though.
EDIT: Opened an issue for this: #5732
|
CI failures:
|
|
So I can immediately help with macOS (or even BSD perhaps?). In macOS, If you want, I could either supply a patch directly to your branch, or could work with you at getting it right. For reference, here's how we do it in In terms of WASI failure, I'll need to spend some time to debug it, but I won't be able to do it until tomorrow. I'm happy to help you with this if you want to wait until tomorrow? |
|
Really cool you've added this testcase BTW! We need a lot more of those, so thanks a bunch! |
|
Info dump for POSIX says only
No worries, sounds good. |
…tems Linux deviates from POSIX and returns EISDIR while other POSIX systems return EPERM. To make all platforms consistent in their errors when calling deleteFile on a directory, we have to do a stat to translate EPERM (AccessDenied) to EISDIR (IsDir).
e6aa0fa to
14c3c47
Compare
Reduces duplicate code, consistent with other fn/fnZ/fnW implementations
|
@kubkon did you have a chance to look into the |
|
@squeek502 just a heads up, I've found the source of the problem. In WASI, when you want to call This isn't a bug in Zig's libstd, rather a subtlety in WASI implementation/spec. I've now submitted an issue about this in Oh, and I should probably add that some errno remapping as was the case with |
Makes sense. When investigating earlier I found bytecodealliance/wasmtime@0302f1a which seems like a similar situation.
Seems reasonable. |
andrewrk
left a comment
There was a problem hiding this comment.
We're probably going to need to adjust the API in the future so that the callsite can issue a deleteFile operation which is willing to tolerate the ambiguity of error.AccessDenied vs error.IsDir, in order to save a syscall. But that can be a separate issue - this is definitely an improvement over status quo. Thanks for working on better test coverage!
The inverse of #5684. Contributes towards #5653