const std = @import("std"); const expect = std.testing.expect; const eql = std.mem.eql; // Let's create and open a file in our current working directory, write to it, and then read from it. // Here we have to use `.seekTo` to go back to the start of the file before reading what we have written. // 현재 작업 디렉터리에서 파일을 생성하고 열어, 그 안에 내용을 기록한 후 다시 읽어 봅시다. // 여기서 기록한 내용을 읽기 전에 파일의 시작 부분으로 돌아가기 위해 `.seekTo`를 사용해야 합니다. test "createFile, write, seekTo, read" { const file = try std.fs.cwd().createFile( "junk_file.txt", .{ .read = true }, ); defer file.close(); try file.writeAll("Hello File!"); var buffer: [100]u8 = undefined; try file.seekTo(0); const bytes_read = try file.readAll(&buffer); try expect(eql(u8, buffer[0..bytes_read], "Hello File!")); } // The functions `std.fs.openFileAbsolute` and similar absolute functions exist, but we will not test them here. // We can get various information about files by using `.stat()` on them. // `Stat` also contains fields for `.inode` and `.mode`, but they are not tested here as they rely on the current OS' types. // When the Enum type is known from context, it can be omitted, so we can compare `stat.kind` to `.file` instead of `Kind.file`. // 함수 `std.fs.openFileAbsolute` 와 같은 절대 경로 함수가 존재하지만, 여기서는 테스트하지 않습니다. // 파일에 대한 다양한 정보를 얻기 위해 `.stat()` 함수를 사용할 수 있습니다. // `Stat` 에는 `.inode` 와 `.mode` 필드가 포함되어 있지만, 현재 OS의 타입에 의존하므로 여기서는 테스트하지 않습니다. // Enum 타입이 컨텍스트에서 알려져 있을 때, 타입을 생략할 수 있고, `stat.kind` 를 `.file` 과 비교하는 대신 `Kind.file` 과 비교할 수 있습니다. test "file stat" { const file = try std.fs.cwd().createFile( "junk_file2.txt", .{ .read = true }, ); defer file.close(); const stat = try file.stat(); try expect(stat.size == 0); try expect(stat.kind == .file); try expect(stat.ctime <= std.time.nanoTimestamp()); try expect(stat.mtime <= std.time.nanoTimestamp()); try expect(stat.atime <= std.time.nanoTimestamp()); } // We can make directories and iterate over their contents. // Here we will use an iterator (discussed later). // This directory (and its contents) will be deleted after this test finishes. // 디렉터리를 생성하고 그 내용을 반복할 수 있습니다. // 여기서는 이터레이터(나중에 설명)를 사용합니다. // 이 디렉터리(와 그 내용)는 이 테스트가 완료된 후 삭제됩니다. test "make dir" { try std.fs.cwd().makeDir("test-tmp"); var iter_dir = try std.fs.cwd().openDir( "test-tmp", .{ .iterate = true }, ); defer { iter_dir.close(); std.fs.cwd().deleteTree("test-tmp") catch unreachable; } _ = try iter_dir.createFile("x", .{}); _ = try iter_dir.createFile("y", .{}); _ = try iter_dir.createFile("z", .{}); var file_count: usize = 0; var iter = iter_dir.iterate(); while (try iter.next()) |entry| { if (entry.kind == .file) file_count += 1; } try expect(file_count == 3); }