authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-23 17:41:41-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-25 03:49:58-07:00
logdcdbb7006ca63a20c003a9928d1cb5e3d0d1cbdb
tree9783d8a06cd6faa14a15e10652b425949778d10e
parentf50ed941746c7c5ca4643080fbd2228491df6f19

Add tests for using directory operations on files


1 files changed, 33 insertions(+), 0 deletions(-)

lib/std/fs/test.zig+33
......@@ -40,6 +40,39 @@ test "readAllAlloc" {
4040 testing.expectError(error.FileTooBig, file.readAllAlloc(testing.allocator, file_size, write_buf.len - 1));
4141}
4242
43test "directory operations on files" {
44 var tmp_dir = tmpDir(.{});
45 defer tmp_dir.cleanup();
46
47 const test_file_name = "test_file";
48
49 var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });
50 file.close();
51
52 testing.expectError(error.PathAlreadyExists, tmp_dir.dir.makeDir(test_file_name));
53 testing.expectError(error.NotDir, tmp_dir.dir.openDir(test_file_name, .{}));
54 testing.expectError(error.NotDir, tmp_dir.dir.deleteDir(test_file_name));
55
56 if (builtin.os.tag != .wasi) {
57 // TODO: use Dir's realpath function once that exists
58 const absolute_path = blk: {
59 const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_file_name });
60 defer testing.allocator.free(relative_path);
61 break :blk try fs.realpathAlloc(testing.allocator, relative_path);
62 };
63 defer testing.allocator.free(absolute_path);
64
65 testing.expectError(error.PathAlreadyExists, fs.makeDirAbsolute(absolute_path));
66 testing.expectError(error.NotDir, fs.deleteDirAbsolute(absolute_path));
67 }
68
69 // ensure the file still exists and is a file as a sanity check
70 file = try tmp_dir.dir.openFile(test_file_name, .{});
71 const stat = try file.stat();
72 testing.expect(stat.kind == .File);
73 file.close();
74}
75
4376test "openSelfExe" {
4477 if (builtin.os.tag == .wasi) return error.SkipZigTest;
4578