地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
at main 61 lines 1.9 kB view raw
1const std = @import("std"); 2 3pub const TestEnv = struct { 4 allocator: std.mem.Allocator, 5 tmp_dir: std.testing.TmpDir, 6 tmp_path: []const u8, 7 8 pub fn init(allocator: std.mem.Allocator) !TestEnv { 9 var tmp_dir = std.testing.tmpDir(.{}); 10 const real_path = try tmp_dir.dir.realpathAlloc(allocator, "."); 11 12 return TestEnv{ 13 .allocator = allocator, 14 .tmp_dir = tmp_dir, 15 .tmp_path = real_path, 16 }; 17 } 18 19 pub fn deinit(self: *TestEnv) void { 20 self.allocator.free(self.tmp_path); 21 self.tmp_dir.cleanup(); 22 } 23 24 pub fn createFiles(self: *TestEnv, names: []const []const u8) !void { 25 for (names) |name| { 26 const file = try self.tmp_dir.dir.createFile(name, .{}); 27 file.close(); 28 } 29 } 30 31 pub const DirNode = struct { 32 name: []const u8, 33 children: ?[]const DirNode, 34 }; 35 36 pub fn createDirStructure(self: *TestEnv, nodes: []const DirNode) !void { 37 for (nodes) |node| { 38 if (node.children) |children| { 39 try self.tmp_dir.dir.makeDir(node.name); 40 var subdir = try self.tmp_dir.dir.openDir(node.name, .{}); 41 defer subdir.close(); 42 43 for (children) |child| { 44 if (child.children) |_| { 45 try subdir.makeDir(child.name); 46 } else { 47 const file = try subdir.createFile(child.name, .{}); 48 file.close(); 49 } 50 } 51 } else { 52 const file = try self.tmp_dir.dir.createFile(node.name, .{}); 53 file.close(); 54 } 55 } 56 } 57 58 pub fn path(self: *TestEnv, relative: []const u8) ![]const u8 { 59 return try std.fs.path.join(self.allocator, &.{ self.tmp_path, relative }); 60 } 61};