@@ -6,32 +6,27 @@ const Client = @import("./httpfile/http_client.zig");
66const AssertionChecker = @import ("./httpfile/assertion_checker.zig" );
77const TestReporter = @import ("./reporters/test_reporter.zig" );
88
9- pub fn main () ! void {
9+ pub fn main (init : std.process.Init ) ! void {
1010 // Use a debug allocator for leak detection.
11- var debug = std .heap .DebugAllocator (.{}){};
12- defer _ = debug .deinit ();
13- const allocator = debug .allocator ();
11+ const allocator = init .gpa ;
1412
1513 var stdout_buffer : [1024 ]u8 = undefined ;
16- var stdout_writer = std .fs .File .stdout ().writer (& stdout_buffer );
14+ var stdout_writer = std .Io .File .stdout ().writer (init . io , & stdout_buffer );
1715 const stdout = & stdout_writer .interface ;
1816 defer stdout .flush () catch {};
1917
2018 var stderr_buffer : [1024 ]u8 = undefined ;
21- var stderr_writer = std .fs .File .stderr ().writer (& stderr_buffer );
19+ var stderr_writer = std .Io .File .stderr ().writer (init . io , & stderr_buffer );
2220 const stderr = & stderr_writer .interface ;
2321 defer stderr .flush () catch {};
2422
25- // Determine thread count from environment.
26- const threads = std .process .parseEnvVarInt ("HTTP_THREAD_COUNT" , usize , 10 ) catch 1 ;
27-
2823 // Parse CLI arguments.
2924 const params = comptime clap .parseParamsComptime (
3025 \\-h, --help Display this help and exit
3126 \\<str>... Executes the HTTP specs in the provided files (if omitted, all files in subdirectories will be ran instead)
3227 );
3328 var diag = clap.Diagnostic {};
34- var res = clap .parse (clap .Help , & params , clap .parsers .default , .{
29+ var res = clap .parse (clap .Help , & params , clap .parsers .default , init . minimal . args , .{
3530 .diagnostic = & diag ,
3631 .allocator = allocator ,
3732 }) catch | err | {
@@ -51,38 +46,33 @@ pub fn main() !void {
5146 for (files .items ) | file | allocator .free (file );
5247 files .deinit (allocator );
5348 }
54- try collectSpecFiles (allocator , & files , res );
49+ try collectSpecFiles (init . io , allocator , & files , res );
5550
56- // Set up thread pool and reporter.
57- var pool : std.Thread.Pool = undefined ;
58- try pool .init (.{
59- .allocator = allocator ,
60- .n_jobs = threads ,
61- });
62- defer pool .deinit ();
51+ var group : std.Io.Group = .init ;
52+ errdefer group .cancel (init .io );
6353
64- var wg : std.Thread.WaitGroup = .{};
6554 var reporter = TestReporter .BasicReporter .init ();
6655
6756 // Run all tests in parallel.
6857 for (files .items ) | path | {
69- pool . spawnWg ( & wg , runTest , .{ allocator , & reporter , path , stderr });
58+ group . async ( init . io , runTest , .{ init . io , allocator , & reporter , path , stderr });
7059 }
71- wg . wait ( );
60+ try group . await ( init . io );
7261
7362 // Print summary.
7463 reporter .report (stdout );
7564}
7665
7766/// Collects all HTTP spec files to run, based on CLI args.
7867fn collectSpecFiles (
68+ io : std.Io ,
7969 allocator : std.mem.Allocator ,
8070 files : * std .ArrayList ([]const u8 ),
8171 res : anytype ,
8272) ! void {
8373 if (res .positionals [0 ].len == 0 ) {
8474 // No args: find all .http/.httpspec files recursively from cwd.
85- const http_files = try listHttpFiles (allocator , "." );
75+ const http_files = try listHttpFiles (io , allocator , "." );
8676 defer allocator .free (http_files );
8777 for (http_files ) | file | try files .append (allocator , file );
8878 } else {
@@ -93,9 +83,10 @@ fn collectSpecFiles(
9383 {
9484 try files .append (allocator , try allocator .dupe (u8 , pos ));
9585 } else {
96- const file_info = try std .fs .cwd ().statFile (pos );
86+ const cwd = std .Io .Dir .cwd ();
87+ const file_info = try cwd .statFile (io , pos , .{});
9788 if (file_info .kind != .directory ) return error .InvalidPositionalArgument ;
98- const http_files = try listHttpFiles (allocator , pos );
89+ const http_files = try listHttpFiles (io , allocator , pos );
9990 defer allocator .free (http_files );
10091 for (http_files ) | file | try files .append (allocator , file );
10192 }
@@ -105,38 +96,39 @@ fn collectSpecFiles(
10596
10697/// Runs all requests in a spec file and updates the reporter.
10798fn runTest (
99+ io : std.Io ,
108100 base_allocator : std.mem.Allocator ,
109101 reporter : * TestReporter.BasicReporter ,
110102 path : []const u8 ,
111- stderr : * std.io .Writer ,
112- ) void {
103+ stderr : * std.Io .Writer ,
104+ ) ! void {
113105 // Create arena allocator for this test to provide memory isolation
114106 var arena = std .heap .ArenaAllocator .init (base_allocator );
115107 defer arena .deinit (); // Automatically frees all test allocations
116108 const allocator = arena .allocator ();
117109
118110 var has_failure = false ;
119- reporter .incTestCount ();
111+ try reporter .incTestCount (io );
120112
121- var items = HttpParser .parseFile (allocator , path ) catch | err | {
122- reporter .incTestInvalid ();
113+ var items = HttpParser .parseFile (io , allocator , path ) catch | err | {
114+ try reporter .incTestInvalid (io );
123115 std .debug .print ("Failed to parse file {s}: {s}\n " , .{ path , @errorName (err ) });
124116 return ;
125117 };
126118 const owned_items = items .toOwnedSlice (allocator ) catch | err | {
127- reporter .incTestInvalid ();
119+ try reporter .incTestInvalid (io );
128120 std .debug .print ("Failed to convert items to owned slice in file {s}: {s}\n " , .{ path , @errorName (err ) });
129121 return ;
130122 };
131123 // Note: No need to manually free owned_items since arena will handle it
132124
133- var client = Client .HttpClient .init (allocator );
125+ var client = Client .HttpClient .init (io , allocator );
134126 defer client .deinit ();
135127
136128 for (owned_items ) | * owned_item | {
137129 // Note: No need to manually deinit owned_item since arena will handle it
138130 var responses = client .execute (owned_item ) catch | err | {
139- reporter .incTestInvalid ();
131+ try reporter .incTestInvalid (io );
140132 std .debug .print ("Failed to execute request in file {s}: {s}\n " , .{ path , @errorName (err ) });
141133 return ;
142134 };
@@ -151,33 +143,34 @@ fn runTest(
151143 }
152144 }
153145 if (! has_failure ) {
154- reporter .incTestPass ();
146+ try reporter .incTestPass (io );
155147 } else {
156- reporter .incTestFail ();
148+ try reporter .incTestFail (io );
157149 }
158150}
159151
160152/// Recursively finds all .http/.httpspec files in a directory.
161- fn listHttpFiles (allocator : std.mem.Allocator , dir : []const u8 ) ! [][]const u8 {
153+ fn listHttpFiles (io : std.Io , allocator : std.mem.Allocator , dir_path : []const u8 ) ! [][]const u8 {
162154 var files : std .ArrayList ([]const u8 ) = .empty ;
163155 defer files .deinit (allocator );
164156
165- var dir_entry = try std .fs .cwd ().openDir (dir , .{ .iterate = true });
166- defer dir_entry .close ();
157+ const cwd = std .Io .Dir .cwd ();
158+ var dir_entry = try std .Io .Dir .openDir (cwd , io , dir_path , .{ .iterate = true });
159+ defer dir_entry .close (io );
167160
168161 var it = dir_entry .iterate ();
169- while (try it .next ()) | entry | {
162+ while (try it .next (io )) | entry | {
170163 if (entry .kind == .directory ) {
171164 if (std .mem .eql (u8 , entry .name , "." ) or std .mem .eql (u8 , entry .name , ".." )) continue ;
172- const subdir = try std .fs .path .join (allocator , &[_ ][]const u8 { dir , entry .name });
165+ const subdir = try std .fs .path .join (allocator , &[_ ][]const u8 { dir_path , entry .name });
173166 defer allocator .free (subdir );
174- const sub_files = try listHttpFiles (allocator , subdir );
167+ const sub_files = try listHttpFiles (io , allocator , subdir );
175168 defer allocator .free (sub_files );
176169 for (sub_files ) | file | try files .append (allocator , file );
177170 } else if (std .mem .eql (u8 , std .fs .path .extension (entry .name ), ".http" ) or
178171 std .mem .eql (u8 , std .fs .path .extension (entry .name ), ".httpspec" ))
179172 {
180- const file_path = try std .fs .path .join (allocator , &[_ ][]const u8 { dir , entry .name });
173+ const file_path = try std .fs .path .join (allocator , &[_ ][]const u8 { dir_path , entry .name });
181174 try files .append (allocator , file_path );
182175 }
183176 }
0 commit comments