Skip to content
Merged
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 lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ pub const Dir = struct {
.OBJECT_NAME_INVALID => unreachable,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
.NOT_A_DIRECTORY => return error.NotDir,
.INVALID_PARAMETER => unreachable,
else => return w.unexpectedStatus(rc),
}
Expand Down
33 changes: 33 additions & 0 deletions lib/std/fs/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ test "readAllAlloc" {
testing.expectError(error.FileTooBig, file.readAllAlloc(testing.allocator, file_size, write_buf.len - 1));
}

test "directory operations on files" {
var tmp_dir = tmpDir(.{});
defer tmp_dir.cleanup();

const test_file_name = "test_file";

var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });
file.close();

testing.expectError(error.PathAlreadyExists, tmp_dir.dir.makeDir(test_file_name));
testing.expectError(error.NotDir, tmp_dir.dir.openDir(test_file_name, .{}));
testing.expectError(error.NotDir, tmp_dir.dir.deleteDir(test_file_name));

if (builtin.os.tag != .wasi) {
// TODO: use Dir's realpath function once that exists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps #5701 could be of use here (not necessarily saying to wait on it to land, just an FYI)

const absolute_path = blk: {
const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_file_name });
defer testing.allocator.free(relative_path);
break :blk try fs.realpathAlloc(testing.allocator, relative_path);
};
defer testing.allocator.free(absolute_path);

testing.expectError(error.PathAlreadyExists, fs.makeDirAbsolute(absolute_path));
testing.expectError(error.NotDir, fs.deleteDirAbsolute(absolute_path));
}

// ensure the file still exists and is a file as a sanity check
file = try tmp_dir.dir.openFile(test_file_name, .{});
const stat = try file.stat();
testing.expect(stat.kind == .File);
file.close();
}

test "openSelfExe" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;

Expand Down