Compare commits

...

5 Commits

Author SHA1 Message Date
Don Isaac
ac7ff8cba5 Merge branch 'main' of github.com:oven-sh/bun into don/build/zig-test 2024-12-30 13:21:44 -05:00
DonIsaac
d98c847027 bun run zig-format 2024-12-21 00:13:30 +00:00
Don Isaac
b09430c11c build: support zig build test 2024-12-20 16:09:28 -08:00
DonIsaac
30371a69a7 bun run zig-format 2024-12-20 23:07:17 +00:00
Don Isaac
8c1a4973e8 refactor: organize native glob code 2024-12-20 14:20:47 -08:00
4 changed files with 59 additions and 5 deletions

View File

@@ -346,6 +346,20 @@ pub fn build(b: *Build) !void {
// const run = b.addRunArtifact(exe);
// step.dependOn(&run.step);
}
// zig build test
{
const unit_tests = b.addTest(.{
.name = "zig-unit-tests",
.root_source_file = b.path("src/unit_test.zig"),
.optimize = build_options.optimize,
.target = build_options.target,
.use_llvm = if (build_options.no_llvm) false else null,
.use_lld = if (build_options.os == .mac) false else !build_options.no_llvm,
});
const test_step = b.step("test", "Run Zig-only unit tests");
test_step.dependOn(&unit_tests.step);
}
}
pub fn addMultiCheck(

View File

@@ -24,7 +24,6 @@ const std = @import("std");
const bun = @import("root").bun;
const eqlComptime = @import("../string_immutable.zig").eqlComptime;
const expect = std.testing.expect;
const isAllAscii = @import("../string_immutable.zig").isAllASCII;
const math = std.math;
const mem = std.mem;

View File

@@ -24,7 +24,6 @@
const std = @import("std");
const math = std.math;
const mem = std.mem;
const expect = std.testing.expect;
// These store character indices into the glob and path strings.
path_index: usize = 0,
@@ -118,13 +117,13 @@ const BraceIndex = struct {
pub fn preprocess_glob(glob: []const u8, brace_indices: *[10]BraceIndex, brace_indices_len: *u8, search_count: *u8, i: *u32) ?u32 {
while (i.* < glob.len) {
const c = glob[i];
const c = glob[i.*];
switch (c) {
'{' => {
if (brace_indices_len.* == brace_indices.len) continue;
const stack_idx = brace_indices_len.*;
if (i == glob.len - 1) continue;
const matching_idx = preprocess_glob(glob[i + 1 ..], brace_indices, brace_indices_len, search_count + 1);
if (@as(usize, @intCast(i.*)) == glob.len - 1) continue;
const matching_idx = preprocess_glob(glob[i + 1 ..], brace_indices, brace_indices_len, search_count + 1, i);
if (matching_idx) |idx| {
if (brace_indices_len.* == brace_indices.len) continue;
brace_indices[stack_idx].start = @intCast(i);
@@ -513,3 +512,32 @@ pub fn detectGlobSyntax(potential_pattern: []const u8) bool {
return false;
}
const t = std.testing;
const expect = t.expect;
const expectEqual = t.expect;
// TODO: too many compilation errors in preprocess_glob
// test {
// std.testing.refAllDecls(@This());
// }
// test match {
// try expect(match("*", "foo.txt"));
// }
test skipGlobstars {
const TestCase = std.meta.Tuple(&[_]type{ []const u8, []const u8 });
const cases = [_]TestCase{
.{ "*.js", "*.js" },
.{ "**/*.js", "*.js" },
};
for (cases) |case| {
var end_of_stars: usize = 0;
const glob = case[0];
const expected = case[1];
const ret = skipGlobstars(glob, &end_of_stars);
try t.expectEqual(end_of_stars, ret);
try t.expectEqualStrings(expected, glob[end_of_stars..]);
}
}

13
src/unit_test.zig Normal file
View File

@@ -0,0 +1,13 @@
//! Unit tests for Zig-only code.
//!
//! To register a new test suite, add it to the bottom of the `_ = @import(...)`
//! list below.
//!
//! ## IMPORTANT NOTE
//!
//! You cannot register files that import `bun` (that is, `@import("root").bun`)
//! or rely on externally linked C code. These tests are built and run in isolation,
//! meaning C libraries won't be linked + available.
test {
_ = @import("./glob/ascii.zig");
}