Skip to content

Commit a53477e

Browse files
refactor(util): update utils
- add doc comments - update debug mode parameter name - fix subprocess util function
1 parent 63c98a1 commit a53477e

1 file changed

Lines changed: 42 additions & 20 deletions

File tree

src/util.zig

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ const BUILTIN = @import("builtin");
55

66
pub const NATIVE_OS = BUILTIN.target.os.tag;
77

8+
/// tree node
9+
///
10+
/// examples
11+
/// --------
12+
/// const TREE_NODE_INSTANCE: *TREE_NODE = try TREE_NODE.init(
13+
/// allocator,
14+
/// .{ .node_contents = dir_path },
15+
/// );
16+
/// defer TREE_NODE_INSTANCE.deinit(allocator);
817
const TREE_NODE = struct {
918
const Self = @This();
1019

@@ -38,6 +47,26 @@ const TREE_NODE = struct {
3847
}
3948
};
4049

50+
/// create file tree
51+
///
52+
/// examples
53+
/// --------
54+
/// const DIR_TREE_INSTANCE: *DIR_TREE = try DIR_TREE.init(allocator, TEST_DIR);
55+
/// defer DIR_TREE_INSTANCE.deinit(allocator);
56+
///
57+
/// var returned_dir_contents: STD.ArrayList([]const u8) = .empty;
58+
/// defer returned_dir_contents.deinit(allocator);
59+
///
60+
/// try DIR_TREE_INSTANCE.iterateAndFilterTree(allocator, .{
61+
/// .is_debug_enabled = false,
62+
/// .include_filter = ".txt",
63+
/// .is_move_semantics_enabled = true,
64+
/// .external_list = &returned_dir_contents,
65+
/// });
66+
///
67+
/// try STD.testing.expect(
68+
/// STD.mem.eql(u8, returned_dir_contents.items[0], "tests/test_dir/chapter/test.txt"),
69+
/// );
4170
pub const DIR_TREE = struct {
4271
const Self = @This();
4372

@@ -214,17 +243,19 @@ pub const DIR_TREE = struct {
214243
/// --------
215244
/// var process_output: STD.ArrayList(u8) = .empty;
216245
/// defer process_output.deinit(STD.testing.allocator);
246+
///
217247
/// try runSubProcess(STD.testing.allocator, &process_output, .{ .args = "echo hello world" });
248+
///
218249
/// try STD.testing.expect(STD.mem.eql(u8, process_output.items, "hello world\n"));
219250
pub fn runSubProcess(
220251
allocator: STD.mem.Allocator,
221252
extra_options: struct {
222253
allow_move_semantics: bool = false,
223254
args: []const u8 = "echo this is a sub-process",
224-
is_debug_mode: bool = false,
255+
is_debug_enabled: bool = false,
225256
move_process_output_to: *STD.ArrayList(u8),
226257
},
227-
) !u8 {
258+
) !void {
228259
const BUFFER_SIZE = comptime 1 << 16;
229260

230261
var process_args: STD.ArrayList([]const u8) = .empty;
@@ -257,29 +288,22 @@ pub fn runSubProcess(
257288

258289
try process.collectOutput(allocator, &process_stdout_buffer, &process_stderr_buffer, BUFFER_SIZE);
259290

291+
if (extra_options.is_debug_enabled) {
292+
STD.debug.print("STDERR ->\n{s}\n", .{process_stderr_buffer.items});
293+
STD.debug.print("STDOUT ->\n{s}\n", .{process_stdout_buffer.items});
294+
}
295+
260296
const PROCESS_STATUS = try process.wait();
261297

262298
if (PROCESS_STATUS.Exited != 0) {
263-
if (extra_options.is_debug_mode) {
264-
STD.debug.print("{s}\n", .{process_stderr_buffer.items});
265-
}
266-
267299
if (extra_options.allow_move_semantics) {
268300
try extra_options.move_process_output_to.appendSlice(allocator, process_stderr_buffer.items);
269301
}
270-
271-
return PROCESS_STATUS.Exited;
272-
}
273-
274-
if (extra_options.is_debug_mode) {
275-
STD.debug.print("{s}\n", .{process_stdout_buffer.items});
276302
}
277303

278304
if (extra_options.allow_move_semantics) {
279305
try extra_options.move_process_output_to.appendSlice(allocator, process_stdout_buffer.items);
280306
}
281-
282-
return PROCESS_STATUS.Exited;
283307
}
284308

285309
// tests
@@ -306,6 +330,8 @@ fn initTests() !void {
306330
}
307331

308332
test "build and search directory tree" {
333+
try initTests();
334+
309335
var mem_arena: STD.heap.ArenaAllocator = STD.heap.ArenaAllocator.init(STD.testing.allocator);
310336
defer mem_arena.deinit();
311337

@@ -353,16 +379,14 @@ test "run sub process with default arguments" {
353379
var process_output: STD.ArrayList(u8) = .empty;
354380
defer process_output.deinit(ALLOCATOR);
355381

356-
const PROCESS_EXIT_STATUS = try runSubProcess(
382+
try runSubProcess(
357383
ALLOCATOR,
358384
.{
359385
.allow_move_semantics = true,
360386
.move_process_output_to = &process_output,
361387
},
362388
);
363389

364-
try STD.testing.expect(PROCESS_EXIT_STATUS == 0);
365-
366390
try STD.testing.expect(
367391
STD.mem.eql(u8, process_output.items, "this is a sub-process\n"),
368392
);
@@ -377,7 +401,7 @@ test "run hello world sub process" {
377401
var process_output: STD.ArrayList(u8) = .empty;
378402
defer process_output.deinit(ALLOCATOR);
379403

380-
const PROCESS_EXIT_STATUS = try runSubProcess(
404+
try runSubProcess(
381405
ALLOCATOR,
382406
.{
383407
.args = "echo hello world",
@@ -386,8 +410,6 @@ test "run hello world sub process" {
386410
},
387411
);
388412

389-
try STD.testing.expect(PROCESS_EXIT_STATUS == 0);
390-
391413
try STD.testing.expect(
392414
STD.mem.eql(u8, process_output.items, "hello world\n"),
393415
);

0 commit comments

Comments
 (0)