diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-06-25 19:08:30 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-25 19:08:30 -0400 |
| commit | 061c8be049508368fba8f9c466fad38ae826c77e (patch) | |
| tree | 522ed379334a0afbd34a7c0dfcfcf03e3cd756ea /lib | |
| parent | 77bb2dc094bfe9fff9208a8af94d0da617bdae13 (diff) | |
| parent | dcdbb7006ca63a20c003a9928d1cb5e3d0d1cbdb (diff) | |
| download | zig-061c8be049508368fba8f9c466fad38ae826c77e.tar.gz zig-061c8be049508368fba8f9c466fad38ae826c77e.zip | |
Merge pull request #5684 from squeek502/fs-file-dir-ops
Add tests for using directory operations on files
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/fs.zig | 1 | ||||
| -rw-r--r-- | lib/std/fs/test.zig | 33 |
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 10422b9d54..6cb7d478b2 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -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), } diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 23a9c96ff5..b024f0f4f6 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -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 + 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; |
