-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.zig
More file actions
185 lines (164 loc) · 6.77 KB
/
main.zig
File metadata and controls
185 lines (164 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const std = @import("std");
const clap = @import("clap");
const HttpParser = @import("./httpfile/parser.zig");
const Client = @import("./httpfile/http_client.zig");
const AssertionChecker = @import("./httpfile/assertion_checker.zig");
const TestReporter = @import("./reporters/test_reporter.zig");
pub fn main() !void {
// Use a debug allocator for leak detection.
var debug = std.heap.DebugAllocator(.{}){};
defer _ = debug.deinit();
const allocator = debug.allocator();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
defer stdout.flush() catch {};
var stderr_buffer: [1024]u8 = undefined;
var stderr_writer = std.fs.File.stderr().writer(&stderr_buffer);
const stderr = &stderr_writer.interface;
defer stderr.flush() catch {};
// Determine thread count from environment.
const threads = std.process.parseEnvVarInt("HTTP_THREAD_COUNT", usize, 10) catch 1;
// Parse CLI arguments.
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit
\\<str>... Executes the HTTP specs in the provided files (if omitted, all files in subdirectories will be ran instead)
);
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{
.diagnostic = &diag,
.allocator = allocator,
}) catch |err| {
diag.report(stderr, err) catch {};
return err;
};
defer res.deinit();
if (res.args.help != 0) {
std.debug.print("--help\n", .{});
return;
}
// Discover all HTTP spec files to run.
var files: std.ArrayList([]const u8) = .empty;
defer {
for (files.items) |file| allocator.free(file);
files.deinit(allocator);
}
try collectSpecFiles(allocator, &files, res);
// Set up thread pool and reporter.
var pool: std.Thread.Pool = undefined;
try pool.init(.{
.allocator = allocator,
.n_jobs = threads,
});
defer pool.deinit();
var wg: std.Thread.WaitGroup = .{};
var reporter = TestReporter.BasicReporter.init();
// Run all tests in parallel.
for (files.items) |path| {
pool.spawnWg(&wg, runTest, .{ allocator, &reporter, path, stderr });
}
wg.wait();
// Print summary.
reporter.report(stdout);
}
/// Collects all HTTP spec files to run, based on CLI args.
fn collectSpecFiles(
allocator: std.mem.Allocator,
files: *std.ArrayList([]const u8),
res: anytype,
) !void {
if (res.positionals[0].len == 0) {
// No args: find all .http/.httpspec files recursively from cwd.
const http_files = try listHttpFiles(allocator, ".");
defer allocator.free(http_files);
for (http_files) |file| try files.append(allocator, file);
} else {
// Args: treat as files or directories.
for (res.positionals[0]) |pos| {
if (std.ascii.eqlIgnoreCase(std.fs.path.extension(pos), ".http") or
std.ascii.eqlIgnoreCase(std.fs.path.extension(pos), ".httpspec"))
{
try files.append(allocator, try allocator.dupe(u8, pos));
} else {
const file_info = try std.fs.cwd().statFile(pos);
if (file_info.kind != .directory) return error.InvalidPositionalArgument;
const http_files = try listHttpFiles(allocator, pos);
defer allocator.free(http_files);
for (http_files) |file| try files.append(allocator, file);
}
}
}
}
/// Runs all requests in a spec file and updates the reporter.
fn runTest(
base_allocator: std.mem.Allocator,
reporter: *TestReporter.BasicReporter,
path: []const u8,
stderr: *std.io.Writer,
) void {
// Create arena allocator for this test to provide memory isolation
var arena = std.heap.ArenaAllocator.init(base_allocator);
defer arena.deinit(); // Automatically frees all test allocations
const allocator = arena.allocator();
var has_failure = false;
reporter.incTestCount();
var items = HttpParser.parseFile(allocator, path) catch |err| {
reporter.incTestInvalid();
std.debug.print("Failed to parse file {s}: {s}\n", .{ path, @errorName(err) });
return;
};
const owned_items = items.toOwnedSlice(allocator) catch |err| {
reporter.incTestInvalid();
std.debug.print("Failed to convert items to owned slice in file {s}: {s}\n", .{ path, @errorName(err) });
return;
};
// Note: No need to manually free owned_items since arena will handle it
var client = Client.HttpClient.init(allocator);
defer client.deinit();
for (owned_items) |*owned_item| {
// Note: No need to manually deinit owned_item since arena will handle it
var responses = client.execute(owned_item) catch |err| {
reporter.incTestInvalid();
std.debug.print("Failed to execute request in file {s}: {s}\n", .{ path, @errorName(err) });
return;
};
defer responses.deinit();
var diagnostic = AssertionChecker.AssertionDiagnostic.init(allocator);
defer diagnostic.deinit();
AssertionChecker.check(owned_item, responses, &diagnostic, path);
if (AssertionChecker.hasFailures(&diagnostic)) {
AssertionChecker.reportFailures(&diagnostic, stderr) catch {};
has_failure = true;
break;
}
}
if (!has_failure) {
reporter.incTestPass();
} else {
reporter.incTestFail();
}
}
/// Recursively finds all .http/.httpspec files in a directory.
fn listHttpFiles(allocator: std.mem.Allocator, dir: []const u8) ![][]const u8 {
var files: std.ArrayList([]const u8) = .empty;
defer files.deinit(allocator);
var dir_entry = try std.fs.cwd().openDir(dir, .{ .iterate = true });
defer dir_entry.close();
var it = dir_entry.iterate();
while (try it.next()) |entry| {
if (entry.kind == .directory) {
if (std.mem.eql(u8, entry.name, ".") or std.mem.eql(u8, entry.name, "..")) continue;
const subdir = try std.fs.path.join(allocator, &[_][]const u8{ dir, entry.name });
defer allocator.free(subdir);
const sub_files = try listHttpFiles(allocator, subdir);
defer allocator.free(sub_files);
for (sub_files) |file| try files.append(allocator, file);
} else if (std.mem.eql(u8, std.fs.path.extension(entry.name), ".http") or
std.mem.eql(u8, std.fs.path.extension(entry.name), ".httpspec"))
{
const file_path = try std.fs.path.join(allocator, &[_][]const u8{ dir, entry.name });
try files.append(allocator, file_path);
}
}
return files.toOwnedSlice(allocator);
}