<commit_before>const std = @import("../std.zig");
const testing = std.testing;
const builtin = std.builtin;
const fs = std.fs;
const mem = std.mem;

const File = std.fs.File;
const tmpDir = testing.tmpDir;

test "readAllAlloc" {
    var tmp_dir = tmpDir(.{});
    defer tmp_dir.cleanup();

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

    const buf1 = try file.readAllAlloc(testing.allocator, 0, 1024);
    defer testing.allocator.free(buf1);
    testing.expect(buf1.len == 0);

    const write_buf: []const u8 = "this is a test.\nthis is a test.\nthis is a test.\nthis is a test.\n";
    try file.writeAll(write_buf);
    try file.seekTo(0);
    const file_size = try file.getEndPos();

    // max_bytes > file_size
    const buf2 = try file.readAllAlloc(testing.allocator, file_size, 1024);
    defer testing.allocator.free(buf2);
    testing.expectEqual(write_buf.len, buf2.len);
    testing.expect(std.mem.eql(u8, write_buf, buf2));
    try file.seekTo(0);

    // max_bytes == file_size
    const buf3 = try file.readAllAlloc(testing.allocator, file_size, write_buf.len);
    defer testing.allocator.free(buf3);
    testing.expectEqual(write_buf.len, buf3.len);
    testing.expect(std.mem.eql(u8, write_buf, buf3));

    // max_bytes < file_size
    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 "file operations on directories" {
    var tmp_dir = tmpDir(.{});
    defer tmp_dir.cleanup();

    const test_dir_name = "test_dir";

    try tmp_dir.dir.makeDir(test_dir_name);

    testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
    testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
    testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
    // note: the `.write = true` is necessary to ensure the error occurs on all platforms
    testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));

    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_dir_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.IsDir, fs.createFileAbsolute(absolute_path, .{}));
        testing.expectError(error.IsDir, fs.deleteFileAbsolute(absolute_path));
    }

    // ensure the directory still exists as a sanity check
    var dir = try tmp_dir.dir.openDir(test_dir_name, .{});
    dir.close();
}

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

    const self_exe_file = try std.fs.openSelfExe(.{});
    self_exe_file.close();
}

const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.ns_per_ms;<commit_msg>Disable wasi 'readFileAlloc on a directory' assertion for now<commit_after>const std = @import("../std.zig");
const testing = std.testing;
const builtin = std.builtin;
const fs = std.fs;
const mem = std.mem;

const File = std.fs.File;
const tmpDir = testing.tmpDir;

test "readAllAlloc" {
    var tmp_dir = tmpDir(.{});
    defer tmp_dir.cleanup();

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

    const buf1 = try file.readAllAlloc(testing.allocator, 0, 1024);
    defer testing.allocator.free(buf1);
    testing.expect(buf1.len == 0);

    const write_buf: []const u8 = "this is a test.\nthis is a test.\nthis is a test.\nthis is a test.\n";
    try file.writeAll(write_buf);
    try file.seekTo(0);
    const file_size = try file.getEndPos();

    // max_bytes > file_size
    const buf2 = try file.readAllAlloc(testing.allocator, file_size, 1024);
    defer testing.allocator.free(buf2);
    testing.expectEqual(write_buf.len, buf2.len);
    testing.expect(std.mem.eql(u8, write_buf, buf2));
    try file.seekTo(0);

    // max_bytes == file_size
    const buf3 = try file.readAllAlloc(testing.allocator, file_size, write_buf.len);
    defer testing.allocator.free(buf3);
    testing.expectEqual(write_buf.len, buf3.len);
    testing.expect(std.mem.eql(u8, write_buf, buf3));

    // max_bytes < file_size
    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 "file operations on directories" {
    var tmp_dir = tmpDir(.{});
    defer tmp_dir.cleanup();

    const test_dir_name = "test_dir";

    try tmp_dir.dir.makeDir(test_dir_name);

    testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
    testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
    // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.
    // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.
    if (builtin.os.tag != .wasi) {
        testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
    }
    // Note: The `.write = true` is necessary to ensure the error occurs on all platforms.
    // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
    testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));

    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_dir_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.IsDir, fs.createFileAbsolute(absolute_path, .{}));
        testing.expectError(error.IsDir, fs.deleteFileAbsolute(absolute_path));
    }

    // ensure the directory still exists as a sanity check
    var dir = try tmp_dir.dir.openDir(test_dir_name, .{});
    dir.close();
}

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

    const self_exe_file = try std.fs.openSelfExe(.{});
    self_exe_file.close();
}

const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.ns_per_ms;
{'fraction_non_alphanumeric': 0.13376887651279853, 'fraction_numerical': 0.007711256292170933, 'mean_word_length': 3.476510067114094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5481382', 'n_tokens_mistral': 3185, 'n_tokens_neox': 3076, 'n_words': 649}