@@ -320,14 +320,8 @@ test "accessAbsolute" {
320
320
var tmp = tmpDir (.{});
321
321
defer tmp .cleanup ();
322
322
323
- var arena = ArenaAllocator .init (testing .allocator );
324
- defer arena .deinit ();
325
- const allocator = arena .allocator ();
326
-
327
- const base_path = blk : {
328
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
329
- break :blk try fs .realpathAlloc (allocator , relative_path );
330
- };
323
+ const base_path = try tmp .dir .realpathAlloc (testing .allocator , "." );
324
+ defer testing .allocator .free (base_path );
331
325
332
326
try fs .accessAbsolute (base_path , .{});
333
327
}
@@ -338,25 +332,52 @@ test "openDirAbsolute" {
338
332
var tmp = tmpDir (.{});
339
333
defer tmp .cleanup ();
340
334
335
+ const tmp_ino = (try tmp .dir .stat ()).inode ;
336
+
341
337
try tmp .dir .makeDir ("subdir" );
342
- var arena = ArenaAllocator .init (testing .allocator );
343
- defer arena .deinit ();
344
- const allocator = arena .allocator ();
338
+ const sub_path = try tmp .dir .realpathAlloc (testing .allocator , "subdir" );
339
+ defer testing .allocator .free (sub_path );
345
340
346
- const base_path = blk : {
347
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ], "subdir" });
348
- break :blk try fs .realpathAlloc (allocator , relative_path );
349
- };
341
+ // Can open sub_path
342
+ var tmp_sub = try fs .openDirAbsolute (sub_path , .{});
343
+ defer tmp_sub .close ();
344
+
345
+ const sub_ino = (try tmp_sub .stat ()).inode ;
350
346
351
347
{
352
- var dir = try fs .openDirAbsolute (base_path , .{});
348
+ // Can open sub_path + ".."
349
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , ".." });
350
+ defer testing .allocator .free (dir_path );
351
+
352
+ var dir = try fs .openDirAbsolute (dir_path , .{});
353
353
defer dir .close ();
354
+
355
+ const ino = (try dir .stat ()).inode ;
356
+ try testing .expectEqual (tmp_ino , ino );
354
357
}
355
358
356
- for ([_ ][]const u8 { "." , ".." }) | sub_path | {
357
- const dir_path = try fs .path .join (allocator , &.{ base_path , sub_path });
359
+ {
360
+ // Can open sub_path + "."
361
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , "." });
362
+ defer testing .allocator .free (dir_path );
363
+
358
364
var dir = try fs .openDirAbsolute (dir_path , .{});
359
365
defer dir .close ();
366
+
367
+ const ino = (try dir .stat ()).inode ;
368
+ try testing .expectEqual (sub_ino , ino );
369
+ }
370
+
371
+ {
372
+ // Can open subdir + "..", with some extra "."
373
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , "." , ".." , "." });
374
+ defer testing .allocator .free (dir_path );
375
+
376
+ var dir = try fs .openDirAbsolute (dir_path , .{});
377
+ defer dir .close ();
378
+
379
+ const ino = (try dir .stat ()).inode ;
380
+ try testing .expectEqual (tmp_ino , ino );
360
381
}
361
382
}
362
383
@@ -409,10 +430,7 @@ test "readLinkAbsolute" {
409
430
defer arena .deinit ();
410
431
const allocator = arena .allocator ();
411
432
412
- const base_path = blk : {
413
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
414
- break :blk try fs .realpathAlloc (allocator , relative_path );
415
- };
433
+ const base_path = try tmp .dir .realpathAlloc (allocator , "." );
416
434
417
435
{
418
436
const target_path = try fs .path .join (allocator , &.{ base_path , "file.txt" });
@@ -748,7 +766,6 @@ test "directory operations on files" {
748
766
test "file operations on directories" {
749
767
// TODO: fix this test on FreeBSD. https://github.com/ziglang/zig/issues/1759
750
768
if (native_os == .freebsd ) return error .SkipZigTest ;
751
- if (native_os == .wasi and builtin .link_libc ) return error .SkipZigTest ; // https://github.com/ziglang/zig/issues/20747
752
769
753
770
try testWithAllSupportedPathTypes (struct {
754
771
fn impl (ctx : * TestContext ) ! void {
@@ -759,18 +776,30 @@ test "file operations on directories" {
759
776
try testing .expectError (error .IsDir , ctx .dir .createFile (test_dir_name , .{}));
760
777
try testing .expectError (error .IsDir , ctx .dir .deleteFile (test_dir_name ));
761
778
switch (native_os ) {
762
- // no error when reading a directory.
763
- .dragonfly , .netbsd = > {},
764
- // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.
765
- // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.
766
- .wasi = > {},
779
+ .dragonfly , .netbsd = > {
780
+ // no error when reading a directory. See https://github.com/ziglang/zig/issues/5732
781
+ const buf = try ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize ));
782
+ testing .allocator .free (buf );
783
+ },
784
+ .wasi = > {
785
+ // WASI return EBADF, which gets mapped to NotOpenForReading.
786
+ // See https://github.com/bytecodealliance/wasmtime/issues/1935
787
+ try testing .expectError (error .NotOpenForReading , ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize )));
788
+ },
767
789
else = > {
768
790
try testing .expectError (error .IsDir , ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize )));
769
791
},
770
792
}
771
- // Note: The `.mode = .read_write` is necessary to ensure the error occurs on all platforms.
772
- // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
773
- try testing .expectError (error .IsDir , ctx .dir .openFile (test_dir_name , .{ .mode = .read_write }));
793
+
794
+ if (native_os == .wasi and builtin .link_libc ) {
795
+ // wasmtime unexpectedly succeeds here, see https://github.com/ziglang/zig/issues/20747
796
+ const handle = try ctx .dir .openFile (test_dir_name , .{ .mode = .read_write });
797
+ handle .close ();
798
+ } else {
799
+ // Note: The `.mode = .read_write` is necessary to ensure the error occurs on all platforms.
800
+ // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
801
+ try testing .expectError (error .IsDir , ctx .dir .openFile (test_dir_name , .{ .mode = .read_write }));
802
+ }
774
803
775
804
if (ctx .path_type == .absolute and comptime PathType .absolute .isSupported (builtin .os )) {
776
805
try testing .expectError (error .IsDir , fs .createFileAbsolute (test_dir_name , .{}));
@@ -993,10 +1022,7 @@ test "renameAbsolute" {
993
1022
defer arena .deinit ();
994
1023
const allocator = arena .allocator ();
995
1024
996
- const base_path = blk : {
997
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp_dir .sub_path [0.. ] });
998
- break :blk try fs .realpathAlloc (allocator , relative_path );
999
- };
1025
+ const base_path = try tmp_dir .dir .realpathAlloc (allocator , "." );
1000
1026
1001
1027
try testing .expectError (error .FileNotFound , fs .renameAbsolute (
1002
1028
try fs .path .join (allocator , &.{ base_path , "missing_file_name" }),
@@ -1386,7 +1412,6 @@ test "sendfile" {
1386
1412
defer tmp .cleanup ();
1387
1413
1388
1414
try tmp .dir .makePath ("os_test_tmp" );
1389
- defer tmp .dir .deleteTree ("os_test_tmp" ) catch {};
1390
1415
1391
1416
var dir = try tmp .dir .openDir ("os_test_tmp" , .{});
1392
1417
defer dir .close ();
@@ -1451,7 +1476,6 @@ test "copyRangeAll" {
1451
1476
defer tmp .cleanup ();
1452
1477
1453
1478
try tmp .dir .makePath ("os_test_tmp" );
1454
- defer tmp .dir .deleteTree ("os_test_tmp" ) catch {};
1455
1479
1456
1480
var dir = try tmp .dir .openDir ("os_test_tmp" , .{});
1457
1481
defer dir .close ();
@@ -1800,10 +1824,7 @@ test "'.' and '..' in absolute functions" {
1800
1824
defer arena .deinit ();
1801
1825
const allocator = arena .allocator ();
1802
1826
1803
- const base_path = blk : {
1804
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
1805
- break :blk try fs .realpathAlloc (allocator , relative_path );
1806
- };
1827
+ const base_path = try tmp .dir .realpathAlloc (allocator , "." );
1807
1828
1808
1829
const subdir_path = try fs .path .join (allocator , &.{ base_path , "./subdir" });
1809
1830
try fs .makeDirAbsolute (subdir_path );
0 commit comments