Skip to content

Commit 30362f7

Browse files
committed
support directories and 0 positional args
1 parent a9cb9d9 commit 30362f7

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Httpspec is a tool that expands upon HTTP Files by adding assertions to them, to be used for integration tests. HTTPSpec is also intended
44
to be a test suite runner for these extended HTTP Files.
55

6-
Httpspec takes in an .http (or .httpspec) file, runs those requests in sequence with optional assertions that are checked along the way. Let's take the following specfile:
6+
Httpspec takes in an .http (or .httpspec) file (or a directory containing those files, defaulting to CWD if none is provided), runs those requests in sequence with optional assertions that are checked along the way. Let's take the following specfile:
77

88
```
99
### Make a simple 200 request to HTTP Bin
@@ -41,6 +41,5 @@ Fail: 1
4141
There are some gaps in this implementation at this point (these are things I plan to address):
4242

4343
1. There is not currently a way to pipe the response from a previous request into the next request. This is limiting as you often may need an ID from a previous request to make the next request.
44-
2. Currently, HTTPSpec only takes in a single file. I intend to also support directory paths, but this hasn't been implemented yet.
45-
3. Currently, everything runs serially. The plan is to run each file in isolation against a thread pool. This will happen eventually, so if you start writing tests today, write them with this in mind (dont try to get fancy with sequencing of test files).
46-
4. Not all of the assertion types specified in the [specification](./HTTPSpec.md) are implemented yet.
44+
2. Currently, everything runs serially. The plan is to run each file in isolation against a thread pool. This will happen eventually, so if you start writing tests today, write them with this in mind (dont try to get fancy with sequencing of test files).
45+
3. Not all of the assertion types specified in the [specification](./HTTPSpec.md) are implemented yet.

‎src/main.zig‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,47 @@ pub fn main() !void {
3030
std.debug.print("--help\n", .{});
3131
}
3232

33+
var files = std.ArrayList([]const u8).init(allocator);
34+
defer {
35+
for (files.items) |file| {
36+
allocator.free(file);
37+
}
38+
files.deinit();
39+
}
40+
41+
if (res.positionals[0].len == 0) {
42+
// If no positional arguments are provided, we assume we want to run all HTTP files in the current directory and subdirectories
43+
const http_files = try listHttpFiles(allocator, ".");
44+
defer allocator.free(http_files);
45+
for (http_files) |file| {
46+
try files.append(file);
47+
}
48+
}
49+
50+
for (res.positionals[0]) |pos| {
51+
if (std.ascii.eqlIgnoreCase(std.fs.path.extension(pos), ".http") or std.ascii.eqlIgnoreCase(std.fs.path.extension(pos), ".httpspec")) {
52+
// If a positional argument is provided, we assume it's a file
53+
try files.append(pos);
54+
} else {
55+
// if its NOT a directory we return an error
56+
const file_info = try std.fs.cwd().statFile(pos);
57+
if (file_info.kind != .directory) {
58+
return error.InvalidPositionalArgument;
59+
}
60+
// If it's a directory, we list all HTTP files in it
61+
const http_files = try listHttpFiles(allocator, pos);
62+
defer allocator.free(http_files);
63+
for (http_files) |file| {
64+
try files.append(file);
65+
}
66+
}
67+
}
68+
3369
var test_count: usize = 0;
3470
var test_pass: usize = 0;
3571
var test_fail: usize = 0;
3672
// TODO: This is simple, but completely serial. Ideally, we'd span this across multiple threads.
37-
// TODO: Need to make this handle directories as well.
38-
// TODO: Need to make this handle no positional inputs (i.e., run all files in subdirectories).
39-
for (res.positionals[0]) |pos| {
73+
for (files.items) |pos| {
4074
test_count += 1;
4175
var has_failure = false;
4276
// TODO: Each one gets its own areana?
@@ -78,3 +112,35 @@ pub fn main() !void {
78112
return err;
79113
};
80114
}
115+
116+
// List all HTTP files in the given directory and its subdirectories
117+
// This function returns a slice of file paths that end with .http or .httpspec
118+
fn listHttpFiles(allocator: std.mem.Allocator, dir: []const u8) ![][]const u8 {
119+
var files = std.ArrayList([]const u8).init(allocator);
120+
defer files.deinit();
121+
122+
var dir_entry = try std.fs.cwd().openDir(dir, .{});
123+
defer dir_entry.close();
124+
125+
var it = dir_entry.iterate();
126+
while (try it.next()) |entry| {
127+
if (entry.kind == .directory) {
128+
const paths = &[_][]const u8{ dir, entry.name };
129+
const subdir = std.fs.path.join(allocator, paths) catch return error.OutOfMemory;
130+
defer allocator.free(subdir);
131+
const sub_files = try listHttpFiles(allocator, subdir);
132+
defer allocator.free(sub_files);
133+
for (sub_files) |file| {
134+
try files.append(file);
135+
}
136+
} else if (std.mem.eql(u8, std.fs.path.extension(entry.name), ".http") or
137+
std.mem.eql(u8, std.fs.path.extension(entry.name), ".httpspec"))
138+
{
139+
const paths = &[_][]const u8{ dir, entry.name };
140+
const file_path = std.fs.path.join(allocator, paths) catch return error.OutOfMemory;
141+
try files.append(file_path);
142+
}
143+
}
144+
145+
return files.toOwnedSlice();
146+
}

0 commit comments

Comments
 (0)