authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:32:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:15-07:00
logcdda39559020b8d2c14a49d670738a3d265b496f
tree3e73e18b774cff98498f5243394fcbc6ca4052b2
parent5c6adbeb3931c209c651239cffb60831bcf02949

std lib tests: avoid cwd races by using std.testing.tmpDir


2 files changed, 86 insertions(+), 84 deletions(-)

lib/std/os/linux/io_uring.zig+71-66
...@@ -1728,10 +1728,12 @@ test "writev/fsync/readv" {...@@ -1728,10 +1728,12 @@ test "writev/fsync/readv" {
1728 };1728 };
1729 defer ring.deinit();1729 defer ring.deinit();
17301730
1731 var tmp = std.testing.tmpDir(.{});
1732 defer tmp.cleanup();
1733
1731 const path = "test_io_uring_writev_fsync_readv";1734 const path = "test_io_uring_writev_fsync_readv";
1732 const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });1735 const file = try tmp.dir.createFile(path, .{ .read = true, .truncate = true });
1733 defer file.close();1736 defer file.close();
1734 defer std.fs.cwd().deleteFile(path) catch {};
1735 const fd = file.handle;1737 const fd = file.handle;
17361738
1737 const buffer_write = [_]u8{42} ** 128;1739 const buffer_write = [_]u8{42} ** 128;
...@@ -1796,10 +1798,11 @@ test "write/read" {...@@ -1796,10 +1798,11 @@ test "write/read" {
1796 };1798 };
1797 defer ring.deinit();1799 defer ring.deinit();
17981800
1801 var tmp = std.testing.tmpDir(.{});
1802 defer tmp.cleanup();
1799 const path = "test_io_uring_write_read";1803 const path = "test_io_uring_write_read";
1800 const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });1804 const file = try tmp.dir.createFile(path, .{ .read = true, .truncate = true });
1801 defer file.close();1805 defer file.close();
1802 defer std.fs.cwd().deleteFile(path) catch {};
1803 const fd = file.handle;1806 const fd = file.handle;
18041807
1805 const buffer_write = [_]u8{97} ** 20;1808 const buffer_write = [_]u8{97} ** 20;
...@@ -1842,10 +1845,12 @@ test "write_fixed/read_fixed" {...@@ -1842,10 +1845,12 @@ test "write_fixed/read_fixed" {
1842 };1845 };
1843 defer ring.deinit();1846 defer ring.deinit();
18441847
1848 var tmp = std.testing.tmpDir(.{});
1849 defer tmp.cleanup();
1850
1845 const path = "test_io_uring_write_read_fixed";1851 const path = "test_io_uring_write_read_fixed";
1846 const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });1852 const file = try tmp.dir.createFile(path, .{ .read = true, .truncate = true });
1847 defer file.close();1853 defer file.close();
1848 defer std.fs.cwd().deleteFile(path) catch {};
1849 const fd = file.handle;1854 const fd = file.handle;
18501855
1851 var raw_buffers: [2][11]u8 = undefined;1856 var raw_buffers: [2][11]u8 = undefined;
...@@ -1899,8 +1904,10 @@ test "openat" {...@@ -1899,8 +1904,10 @@ test "openat" {
1899 };1904 };
1900 defer ring.deinit();1905 defer ring.deinit();
19011906
1907 var tmp = std.testing.tmpDir(.{});
1908 defer tmp.cleanup();
1909
1902 const path = "test_io_uring_openat";1910 const path = "test_io_uring_openat";
1903 defer std.fs.cwd().deleteFile(path) catch {};
19041911
1905 // Workaround for LLVM bug: https://github.com/ziglang/zig/issues/120141912 // Workaround for LLVM bug: https://github.com/ziglang/zig/issues/12014
1906 const path_addr = if (builtin.zig_backend == .stage2_llvm) p: {1913 const path_addr = if (builtin.zig_backend == .stage2_llvm) p: {
...@@ -1910,12 +1917,12 @@ test "openat" {...@@ -1910,12 +1917,12 @@ test "openat" {
19101917
1911 const flags: u32 = os.O.CLOEXEC | os.O.RDWR | os.O.CREAT;1918 const flags: u32 = os.O.CLOEXEC | os.O.RDWR | os.O.CREAT;
1912 const mode: os.mode_t = 0o666;1919 const mode: os.mode_t = 0o666;
1913 const sqe_openat = try ring.openat(0x33333333, linux.AT.FDCWD, path, flags, mode);1920 const sqe_openat = try ring.openat(0x33333333, tmp.dir.fd, path, flags, mode);
1914 try testing.expectEqual(linux.io_uring_sqe{1921 try testing.expectEqual(linux.io_uring_sqe{
1915 .opcode = .OPENAT,1922 .opcode = .OPENAT,
1916 .flags = 0,1923 .flags = 0,
1917 .ioprio = 0,1924 .ioprio = 0,
1918 .fd = linux.AT.FDCWD,1925 .fd = tmp.dir.fd,
1919 .off = 0,1926 .off = 0,
1920 .addr = path_addr,1927 .addr = path_addr,
1921 .len = mode,1928 .len = mode,
...@@ -1931,12 +1938,6 @@ test "openat" {...@@ -1931,12 +1938,6 @@ test "openat" {
1931 const cqe_openat = try ring.copy_cqe();1938 const cqe_openat = try ring.copy_cqe();
1932 try testing.expectEqual(@as(u64, 0x33333333), cqe_openat.user_data);1939 try testing.expectEqual(@as(u64, 0x33333333), cqe_openat.user_data);
1933 if (cqe_openat.err() == .INVAL) return error.SkipZigTest;1940 if (cqe_openat.err() == .INVAL) return error.SkipZigTest;
1934 // AT.FDCWD is not fully supported before kernel 5.6:
1935 // See https://lore.kernel.org/io-uring/20200207155039.12819-1-axboe@kernel.dk/T/
1936 // We use IORING_FEAT_RW_CUR_POS to know if we are pre-5.6 since that feature was added in 5.6.
1937 if (cqe_openat.err() == .BADF and (ring.features & linux.IORING_FEAT_RW_CUR_POS) == 0) {
1938 return error.SkipZigTest;
1939 }
1940 if (cqe_openat.res <= 0) std.debug.print("\ncqe_openat.res={}\n", .{cqe_openat.res});1941 if (cqe_openat.res <= 0) std.debug.print("\ncqe_openat.res={}\n", .{cqe_openat.res});
1941 try testing.expect(cqe_openat.res > 0);1942 try testing.expect(cqe_openat.res > 0);
1942 try testing.expectEqual(@as(u32, 0), cqe_openat.flags);1943 try testing.expectEqual(@as(u32, 0), cqe_openat.flags);
...@@ -1954,10 +1955,12 @@ test "close" {...@@ -1954,10 +1955,12 @@ test "close" {
1954 };1955 };
1955 defer ring.deinit();1956 defer ring.deinit();
19561957
1958 var tmp = std.testing.tmpDir(.{});
1959 defer tmp.cleanup();
1960
1957 const path = "test_io_uring_close";1961 const path = "test_io_uring_close";
1958 const file = try std.fs.cwd().createFile(path, .{});1962 const file = try tmp.dir.createFile(path, .{});
1959 errdefer file.close();1963 errdefer file.close();
1960 defer std.fs.cwd().deleteFile(path) catch {};
19611964
1962 const sqe_close = try ring.close(0x44444444, file.handle);1965 const sqe_close = try ring.close(0x44444444, file.handle);
1963 try testing.expectEqual(linux.IORING_OP.CLOSE, sqe_close.opcode);1966 try testing.expectEqual(linux.IORING_OP.CLOSE, sqe_close.opcode);
...@@ -2295,10 +2298,12 @@ test "fallocate" {...@@ -2295,10 +2298,12 @@ test "fallocate" {
2295 };2298 };
2296 defer ring.deinit();2299 defer ring.deinit();
22972300
2301 var tmp = std.testing.tmpDir(.{});
2302 defer tmp.cleanup();
2303
2298 const path = "test_io_uring_fallocate";2304 const path = "test_io_uring_fallocate";
2299 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });2305 const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
2300 defer file.close();2306 defer file.close();
2301 defer std.fs.cwd().deleteFile(path) catch {};
23022307
2303 try testing.expectEqual(@as(u64, 0), (try file.stat()).size);2308 try testing.expectEqual(@as(u64, 0), (try file.stat()).size);
23042309
...@@ -2339,10 +2344,11 @@ test "statx" {...@@ -2339,10 +2344,11 @@ test "statx" {
2339 };2344 };
2340 defer ring.deinit();2345 defer ring.deinit();
23412346
2347 var tmp = std.testing.tmpDir(.{});
2348 defer tmp.cleanup();
2342 const path = "test_io_uring_statx";2349 const path = "test_io_uring_statx";
2343 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });2350 const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
2344 defer file.close();2351 defer file.close();
2345 defer std.fs.cwd().deleteFile(path) catch {};
23462352
2347 try testing.expectEqual(@as(u64, 0), (try file.stat()).size);2353 try testing.expectEqual(@as(u64, 0), (try file.stat()).size);
23482354
...@@ -2351,14 +2357,14 @@ test "statx" {...@@ -2351,14 +2357,14 @@ test "statx" {
2351 var buf: linux.Statx = undefined;2357 var buf: linux.Statx = undefined;
2352 const sqe = try ring.statx(2358 const sqe = try ring.statx(
2353 0xaaaaaaaa,2359 0xaaaaaaaa,
2354 linux.AT.FDCWD,2360 tmp.dir.fd,
2355 path,2361 path,
2356 0,2362 0,
2357 linux.STATX_SIZE,2363 linux.STATX_SIZE,
2358 &buf,2364 &buf,
2359 );2365 );
2360 try testing.expectEqual(linux.IORING_OP.STATX, sqe.opcode);2366 try testing.expectEqual(linux.IORING_OP.STATX, sqe.opcode);
2361 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2367 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2362 try testing.expectEqual(@as(u32, 1), try ring.submit());2368 try testing.expectEqual(@as(u32, 1), try ring.submit());
23632369
2364 const cqe = try ring.copy_cqe();2370 const cqe = try ring.copy_cqe();
...@@ -2371,8 +2377,6 @@ test "statx" {...@@ -2371,8 +2377,6 @@ test "statx" {
2371 // The filesystem containing the file referred to by fd does not support this operation;2377 // The filesystem containing the file referred to by fd does not support this operation;
2372 // or the mode is not supported by the filesystem containing the file referred to by fd:2378 // or the mode is not supported by the filesystem containing the file referred to by fd:
2373 .OPNOTSUPP => return error.SkipZigTest,2379 .OPNOTSUPP => return error.SkipZigTest,
2374 // The kernel is too old to support FDCWD for dir_fd
2375 .BADF => return error.SkipZigTest,
2376 else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),2380 else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),
2377 }2381 }
2378 try testing.expectEqual(linux.io_uring_cqe{2382 try testing.expectEqual(linux.io_uring_cqe{
...@@ -2606,28 +2610,28 @@ test "renameat" {...@@ -2606,28 +2610,28 @@ test "renameat" {
2606 const old_path = "test_io_uring_renameat_old";2610 const old_path = "test_io_uring_renameat_old";
2607 const new_path = "test_io_uring_renameat_new";2611 const new_path = "test_io_uring_renameat_new";
26082612
2613 var tmp = std.testing.tmpDir(.{});
2614 defer tmp.cleanup();
2615
2609 // Write old file with data2616 // Write old file with data
26102617
2611 const old_file = try std.fs.cwd().createFile(old_path, .{ .truncate = true, .mode = 0o666 });2618 const old_file = try tmp.dir.createFile(old_path, .{ .truncate = true, .mode = 0o666 });
2612 defer {2619 defer old_file.close();
2613 old_file.close();
2614 std.fs.cwd().deleteFile(new_path) catch {};
2615 }
2616 try old_file.writeAll("hello");2620 try old_file.writeAll("hello");
26172621
2618 // Submit renameat2622 // Submit renameat
26192623
2620 var sqe = try ring.renameat(2624 var sqe = try ring.renameat(
2621 0x12121212,2625 0x12121212,
2622 linux.AT.FDCWD,2626 tmp.dir.fd,
2623 old_path,2627 old_path,
2624 linux.AT.FDCWD,2628 tmp.dir.fd,
2625 new_path,2629 new_path,
2626 0,2630 0,
2627 );2631 );
2628 try testing.expectEqual(linux.IORING_OP.RENAMEAT, sqe.opcode);2632 try testing.expectEqual(linux.IORING_OP.RENAMEAT, sqe.opcode);
2629 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2633 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2630 try testing.expectEqual(@as(i32, linux.AT.FDCWD), @bitCast(i32, sqe.len));2634 try testing.expectEqual(@as(i32, tmp.dir.fd), @bitCast(i32, sqe.len));
2631 try testing.expectEqual(@as(u32, 1), try ring.submit());2635 try testing.expectEqual(@as(u32, 1), try ring.submit());
26322636
2633 const cqe = try ring.copy_cqe();2637 const cqe = try ring.copy_cqe();
...@@ -2645,7 +2649,7 @@ test "renameat" {...@@ -2645,7 +2649,7 @@ test "renameat" {
26452649
2646 // Validate that the old file doesn't exist anymore2650 // Validate that the old file doesn't exist anymore
2647 {2651 {
2648 _ = std.fs.cwd().openFile(old_path, .{}) catch |err| switch (err) {2652 _ = tmp.dir.openFile(old_path, .{}) catch |err| switch (err) {
2649 error.FileNotFound => {},2653 error.FileNotFound => {},
2650 else => std.debug.panic("unexpected error: {}", .{err}),2654 else => std.debug.panic("unexpected error: {}", .{err}),
2651 };2655 };
...@@ -2653,7 +2657,7 @@ test "renameat" {...@@ -2653,7 +2657,7 @@ test "renameat" {
26532657
2654 // Validate that the new file exists with the proper content2658 // Validate that the new file exists with the proper content
2655 {2659 {
2656 const new_file = try std.fs.cwd().openFile(new_path, .{});2660 const new_file = try tmp.dir.openFile(new_path, .{});
2657 defer new_file.close();2661 defer new_file.close();
26582662
2659 var new_file_data: [16]u8 = undefined;2663 var new_file_data: [16]u8 = undefined;
...@@ -2674,22 +2678,24 @@ test "unlinkat" {...@@ -2674,22 +2678,24 @@ test "unlinkat" {
26742678
2675 const path = "test_io_uring_unlinkat";2679 const path = "test_io_uring_unlinkat";
26762680
2681 var tmp = std.testing.tmpDir(.{});
2682 defer tmp.cleanup();
2683
2677 // Write old file with data2684 // Write old file with data
26782685
2679 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });2686 const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
2680 defer file.close();2687 defer file.close();
2681 defer std.fs.cwd().deleteFile(path) catch {};
26822688
2683 // Submit unlinkat2689 // Submit unlinkat
26842690
2685 var sqe = try ring.unlinkat(2691 var sqe = try ring.unlinkat(
2686 0x12121212,2692 0x12121212,
2687 linux.AT.FDCWD,2693 tmp.dir.fd,
2688 path,2694 path,
2689 0,2695 0,
2690 );2696 );
2691 try testing.expectEqual(linux.IORING_OP.UNLINKAT, sqe.opcode);2697 try testing.expectEqual(linux.IORING_OP.UNLINKAT, sqe.opcode);
2692 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2698 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2693 try testing.expectEqual(@as(u32, 1), try ring.submit());2699 try testing.expectEqual(@as(u32, 1), try ring.submit());
26942700
2695 const cqe = try ring.copy_cqe();2701 const cqe = try ring.copy_cqe();
...@@ -2706,7 +2712,7 @@ test "unlinkat" {...@@ -2706,7 +2712,7 @@ test "unlinkat" {
2706 }, cqe);2712 }, cqe);
27072713
2708 // Validate that the file doesn't exist anymore2714 // Validate that the file doesn't exist anymore
2709 _ = std.fs.cwd().openFile(path, .{}) catch |err| switch (err) {2715 _ = tmp.dir.openFile(path, .{}) catch |err| switch (err) {
2710 error.FileNotFound => {},2716 error.FileNotFound => {},
2711 else => std.debug.panic("unexpected error: {}", .{err}),2717 else => std.debug.panic("unexpected error: {}", .{err}),
2712 };2718 };
...@@ -2722,20 +2728,21 @@ test "mkdirat" {...@@ -2722,20 +2728,21 @@ test "mkdirat" {
2722 };2728 };
2723 defer ring.deinit();2729 defer ring.deinit();
27242730
2725 const path = "test_io_uring_mkdirat";2731 var tmp = std.testing.tmpDir(.{});
2732 defer tmp.cleanup();
27262733
2727 defer std.fs.cwd().deleteDir(path) catch {};2734 const path = "test_io_uring_mkdirat";
27282735
2729 // Submit mkdirat2736 // Submit mkdirat
27302737
2731 var sqe = try ring.mkdirat(2738 var sqe = try ring.mkdirat(
2732 0x12121212,2739 0x12121212,
2733 linux.AT.FDCWD,2740 tmp.dir.fd,
2734 path,2741 path,
2735 0o0755,2742 0o0755,
2736 );2743 );
2737 try testing.expectEqual(linux.IORING_OP.MKDIRAT, sqe.opcode);2744 try testing.expectEqual(linux.IORING_OP.MKDIRAT, sqe.opcode);
2738 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2745 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2739 try testing.expectEqual(@as(u32, 1), try ring.submit());2746 try testing.expectEqual(@as(u32, 1), try ring.submit());
27402747
2741 const cqe = try ring.copy_cqe();2748 const cqe = try ring.copy_cqe();
...@@ -2752,7 +2759,7 @@ test "mkdirat" {...@@ -2752,7 +2759,7 @@ test "mkdirat" {
2752 }, cqe);2759 }, cqe);
27532760
2754 // Validate that the directory exist2761 // Validate that the directory exist
2755 _ = try std.fs.cwd().openDir(path, .{});2762 _ = try tmp.dir.openDir(path, .{});
2756}2763}
27572764
2758test "symlinkat" {2765test "symlinkat" {
...@@ -2765,26 +2772,25 @@ test "symlinkat" {...@@ -2765,26 +2772,25 @@ test "symlinkat" {
2765 };2772 };
2766 defer ring.deinit();2773 defer ring.deinit();
27672774
2775 var tmp = std.testing.tmpDir(.{});
2776 defer tmp.cleanup();
2777
2768 const path = "test_io_uring_symlinkat";2778 const path = "test_io_uring_symlinkat";
2769 const link_path = "test_io_uring_symlinkat_link";2779 const link_path = "test_io_uring_symlinkat_link";
27702780
2771 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });2781 const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
2772 defer {2782 defer file.close();
2773 file.close();
2774 std.fs.cwd().deleteFile(path) catch {};
2775 std.fs.cwd().deleteFile(link_path) catch {};
2776 }
27772783
2778 // Submit symlinkat2784 // Submit symlinkat
27792785
2780 var sqe = try ring.symlinkat(2786 var sqe = try ring.symlinkat(
2781 0x12121212,2787 0x12121212,
2782 path,2788 path,
2783 linux.AT.FDCWD,2789 tmp.dir.fd,
2784 link_path,2790 link_path,
2785 );2791 );
2786 try testing.expectEqual(linux.IORING_OP.SYMLINKAT, sqe.opcode);2792 try testing.expectEqual(linux.IORING_OP.SYMLINKAT, sqe.opcode);
2787 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2793 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2788 try testing.expectEqual(@as(u32, 1), try ring.submit());2794 try testing.expectEqual(@as(u32, 1), try ring.submit());
27892795
2790 const cqe = try ring.copy_cqe();2796 const cqe = try ring.copy_cqe();
...@@ -2801,7 +2807,7 @@ test "symlinkat" {...@@ -2801,7 +2807,7 @@ test "symlinkat" {
2801 }, cqe);2807 }, cqe);
28022808
2803 // Validate that the symlink exist2809 // Validate that the symlink exist
2804 _ = try std.fs.cwd().openFile(link_path, .{});2810 _ = try tmp.dir.openFile(link_path, .{});
2805}2811}
28062812
2807test "linkat" {2813test "linkat" {
...@@ -2814,32 +2820,31 @@ test "linkat" {...@@ -2814,32 +2820,31 @@ test "linkat" {
2814 };2820 };
2815 defer ring.deinit();2821 defer ring.deinit();
28162822
2823 var tmp = std.testing.tmpDir(.{});
2824 defer tmp.cleanup();
2825
2817 const first_path = "test_io_uring_linkat_first";2826 const first_path = "test_io_uring_linkat_first";
2818 const second_path = "test_io_uring_linkat_second";2827 const second_path = "test_io_uring_linkat_second";
28192828
2820 // Write file with data2829 // Write file with data
28212830
2822 const first_file = try std.fs.cwd().createFile(first_path, .{ .truncate = true, .mode = 0o666 });2831 const first_file = try tmp.dir.createFile(first_path, .{ .truncate = true, .mode = 0o666 });
2823 defer {2832 defer first_file.close();
2824 first_file.close();
2825 std.fs.cwd().deleteFile(first_path) catch {};
2826 std.fs.cwd().deleteFile(second_path) catch {};
2827 }
2828 try first_file.writeAll("hello");2833 try first_file.writeAll("hello");
28292834
2830 // Submit linkat2835 // Submit linkat
28312836
2832 var sqe = try ring.linkat(2837 var sqe = try ring.linkat(
2833 0x12121212,2838 0x12121212,
2834 linux.AT.FDCWD,2839 tmp.dir.fd,
2835 first_path,2840 first_path,
2836 linux.AT.FDCWD,2841 tmp.dir.fd,
2837 second_path,2842 second_path,
2838 0,2843 0,
2839 );2844 );
2840 try testing.expectEqual(linux.IORING_OP.LINKAT, sqe.opcode);2845 try testing.expectEqual(linux.IORING_OP.LINKAT, sqe.opcode);
2841 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);2846 try testing.expectEqual(@as(i32, tmp.dir.fd), sqe.fd);
2842 try testing.expectEqual(@as(i32, linux.AT.FDCWD), @bitCast(i32, sqe.len));2847 try testing.expectEqual(@as(i32, tmp.dir.fd), @bitCast(i32, sqe.len));
2843 try testing.expectEqual(@as(u32, 1), try ring.submit());2848 try testing.expectEqual(@as(u32, 1), try ring.submit());
28442849
2845 const cqe = try ring.copy_cqe();2850 const cqe = try ring.copy_cqe();
...@@ -2856,7 +2861,7 @@ test "linkat" {...@@ -2856,7 +2861,7 @@ test "linkat" {
2856 }, cqe);2861 }, cqe);
28572862
2858 // Validate the second file2863 // Validate the second file
2859 const second_file = try std.fs.cwd().openFile(second_path, .{});2864 const second_file = try tmp.dir.openFile(second_path, .{});
2860 defer second_file.close();2865 defer second_file.close();
28612866
2862 var second_file_data: [16]u8 = undefined;2867 var second_file_data: [16]u8 = undefined;
lib/std/os/linux/test.zig+15-18
...@@ -8,10 +8,12 @@ const expectEqual = std.testing.expectEqual;...@@ -8,10 +8,12 @@ const expectEqual = std.testing.expectEqual;
8const fs = std.fs;8const fs = std.fs;
99
10test "fallocate" {10test "fallocate" {
11 var tmp = std.testing.tmpDir(.{});
12 defer tmp.cleanup();
13
11 const path = "test_fallocate";14 const path = "test_fallocate";
12 const file = try fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });15 const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
13 defer file.close();16 defer file.close();
14 defer fs.cwd().deleteFile(path) catch {};
1517
16 try expect((try file.stat()).size == 0);18 try expect((try file.stat()).size == 0);
1719
...@@ -67,12 +69,12 @@ test "timer" {...@@ -67,12 +69,12 @@ test "timer" {
67}69}
6870
69test "statx" {71test "statx" {
72 var tmp = std.testing.tmpDir(.{});
73 defer tmp.cleanup();
74
70 const tmp_file_name = "just_a_temporary_file.txt";75 const tmp_file_name = "just_a_temporary_file.txt";
71 var file = try fs.cwd().createFile(tmp_file_name, .{});76 var file = try tmp.dir.createFile(tmp_file_name, .{});
72 defer {77 defer file.close();
73 file.close();
74 fs.cwd().deleteFile(tmp_file_name) catch {};
75 }
7678
77 var statx_buf: linux.Statx = undefined;79 var statx_buf: linux.Statx = undefined;
78 switch (linux.getErrno(linux.statx(file.handle, "", linux.AT.EMPTY_PATH, linux.STATX_BASIC_STATS, &statx_buf))) {80 switch (linux.getErrno(linux.statx(file.handle, "", linux.AT.EMPTY_PATH, linux.STATX_BASIC_STATS, &statx_buf))) {
...@@ -105,21 +107,16 @@ test "user and group ids" {...@@ -105,21 +107,16 @@ test "user and group ids" {
105}107}
106108
107test "fadvise" {109test "fadvise" {
110 var tmp = std.testing.tmpDir(.{});
111 defer tmp.cleanup();
112
108 const tmp_file_name = "temp_posix_fadvise.txt";113 const tmp_file_name = "temp_posix_fadvise.txt";
109 var file = try fs.cwd().createFile(tmp_file_name, .{});114 var file = try tmp.dir.createFile(tmp_file_name, .{});
110 defer {115 defer file.close();
111 file.close();
112 fs.cwd().deleteFile(tmp_file_name) catch {};
113 }
114116
115 var buf: [2048]u8 = undefined;117 var buf: [2048]u8 = undefined;
116 try file.writeAll(&buf);118 try file.writeAll(&buf);
117119
118 const ret = linux.fadvise(120 const ret = linux.fadvise(file.handle, 0, 0, linux.POSIX_FADV.SEQUENTIAL);
119 file.handle,
120 0,
121 0,
122 linux.POSIX_FADV.SEQUENTIAL,
123 );
124 try expectEqual(@as(usize, 0), ret);121 try expectEqual(@as(usize, 0), ret);
125}122}