Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: get zig build test working #18207

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8f34750
build: add `test` step
DonIsaac Mar 14, 2025
66ed5f1
wip
DonIsaac Mar 15, 2025
b53339f
its alive
DonIsaac Mar 15, 2025
5032f98
cmake setup
DonIsaac Mar 15, 2025
021f441
make it pretty
DonIsaac Mar 15, 2025
c9115a4
`bun run zig-format`
DonIsaac Mar 15, 2025
a1037be
fix zig build targets
DonIsaac Mar 16, 2025
e25a1b0
merge root_test and main_test
DonIsaac Mar 16, 2025
7e73de4
cleanup
DonIsaac Mar 16, 2025
000c884
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 16, 2025
8c3ad08
remove prints
DonIsaac Mar 17, 2025
602fac7
ci: get tests running in ci
DonIsaac Mar 17, 2025
1c5e621
fix syntax error
DonIsaac Mar 17, 2025
6f83166
ci: fix test-zig-bun dependsOn
DonIsaac Mar 17, 2025
5b1e19d
a
DonIsaac Mar 17, 2025
70ba94f
ReleaseSafe -> Debug
DonIsaac Mar 17, 2025
02350ff
fix: do not bundle ubsan runtime
DonIsaac Mar 17, 2025
957e384
add option to download ReleaseSafe zig compiler
DonIsaac Mar 18, 2025
06341cd
ci: comment out zig test steps for now
DonIsaac Mar 18, 2025
9fbb147
cmake fixes
DonIsaac Mar 18, 2025
0e2e639
Merge branch 'main' into don/test/zig-test-3
DonIsaac Mar 19, 2025
c2b677e
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 24, 2025
017be4c
add tests to shell/braces.zig
DonIsaac Mar 24, 2025
42fa492
cleanup
DonIsaac Mar 24, 2025
685c89f
Merge branch 'main' into don/test/zig-test-3
DonIsaac Mar 26, 2025
1dc2f7d
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 26, 2025
5cdbd28
Merge branch 'don/test/zig-test-3' of github.com:oven-sh/bun into don…
DonIsaac Mar 26, 2025
b75bd3d
cmake tweaks
DonIsaac Mar 26, 2025
58a1ab3
test: add SmolStr unit tests
DonIsaac Mar 26, 2025
9fb7bd6
add link to ziglang/zig issue over test build step
DonIsaac Mar 26, 2025
af8bcd5
update ban-words
DonIsaac Mar 26, 2025
bcd64dc
chore: remove old testing code
DonIsaac Mar 26, 2025
57eb039
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 27, 2025
816e804
pr feedback
DonIsaac Mar 27, 2025
249c4aa
`bun run zig-format`
DonIsaac Mar 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
DonIsaac committed Mar 15, 2025
commit 66ed5f18376d34d2e21dad820b3043c1e2e465b4
108 changes: 90 additions & 18 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const p = std.debug.print;

const Build = std.Build;
const Step = Build.Step;
@@ -36,6 +37,7 @@ comptime {

const zero_sha = "0000000000000000000000000000000000000000";


const BunBuildOptions = struct {
target: ResolvedTarget,
optimize: OptimizeMode,
@@ -287,28 +289,98 @@ pub fn build(b: *Build) !void {

// zig build test
{
const wants_obj = false;
var step = b.step("test", "Build Bun's unit test suite");
var o = build_options;
var unit_tests = b.addTest(.{
.name = "bun-test",
.optimize = build_options.optimize,
.root_source_file = b.path("unit_test.zig"),
.test_runner = .{
if (comptime wants_obj) {
var unit_tests = b.addObject(.{
.name = "bun-test",
.root_source_file = b.path("root_test.zig"),
.target = build_options.target,
.optimize = build_options.optimize,
.use_llvm = !build_options.no_llvm,
.use_lld = if (build_options.os == .mac) false else !build_options.no_llvm,
.pic = true,
.omit_frame_pointer = false,
.strip = false,
});
configureObj(b, &o, unit_tests);
unit_tests.test_runner = .{
.path = b.path("root_test.zig"),
.mode = .simple,
},
.target = build_options.target,
.use_llvm = !build_options.no_llvm,
.use_lld = if (build_options.os == .mac) false else !build_options.no_llvm,
.pic = true,
.omit_frame_pointer = false,
.strip = false,
});
_ = &unit_tests;
// unit_tests.kind =
configureObj(b, &o, unit_tests);
b.installArtifact(unit_tests);
step.dependOn(&unit_tests.step);
};
unit_tests.linker_allow_shlib_undefined = true;
unit_tests.import_symbols = true;

// const FakeStep = struct {
// fn makeItGoAway(wrapped_step: *Step, options: Step.MakeOptions) anyerror!void {
// const compile: *Compile = @fieldParentPtr("step", wrapped_step);
// compile.step.make(wrapped_step, options) catch {};
// return;
// }
// };

// var fake_step = Step.init(.{
// .name = "expect-errors",
// .makeFn = &FakeStep.makeItGoAway,
// .id = unit_tests.step.id,
// });
// foo
// unit_tests.kind = .@"test";
const bin = unit_tests.getEmittedBin();
std.debug.assert(unit_tests.generated_bin != null);
const install = b.addInstallFile(switch (obj_format) {
.obj => bin,
.bc => unit_tests.getEmittedLlvmBc(),
}, "bun-test.o");

step.dependOn(&unit_tests.step);
step.dependOn(&install.step);
} else {
var unit_tests = b.addTest(.{
.name = "bun-test",
.optimize = build_options.optimize,
.root_source_file = b.path("unit_test.zig"),
// .root_source_file = b.path("root_test.zig"),
.test_runner = .{
.path = b.path("root_test.zig"),
.mode = .simple,
},
.target = build_options.target,
.use_llvm = !build_options.no_llvm,
.use_lld = if (build_options.os == .mac) false else !build_options.no_llvm,
.pic = true,
.omit_frame_pointer = false,
.strip = false,
});
configureObj(b, &o, unit_tests);
// unit_tests.linkage
// unit_tests.kind = .obj;
unit_tests.export_table = true;
// unit_tests.out_filename = std.zig.binNameAlloc(b.allocator, .{
// .root_name = "bun-test",
// .target = build_options.target.result,
// .output_mode = .Obj,
// .link_mode = .static,
// // .version = options.version,
// }) catch @panic("OOM"); // b.installArtifact(unit_tests);

const bin = unit_tests.getEmittedBin();
// std.debug.assert(unit_tests.generated_bin != null);
// const install = b.addInstallFile(switch (obj_format) {
// .obj => bin,
// .bc => unit_tests.getEmittedLlvmBc(),
// }, "bun-test.o");
// const test_exe = b.addInstallArtifact(unit_tests, Step.InstallArtifact.Options{});

const cpy_obj = b.addObjCopy(bin, .{});

step.dependOn(&unit_tests.step);
step.dependOn(&cpy_obj.step);
// step.dependOn(&install.step);
}

// step.dependOn(addInstallObjectFile(b, unit_tests, "bun-test", obj_format));
// _ = &step;
}

2 changes: 2 additions & 0 deletions cmake/Options.cmake
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@ else()
setx(DEBUG OFF)
endif()

setx(TEST ON)

if(CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
setx(ENABLE_SMOL ON)
endif()
13 changes: 11 additions & 2 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if(DEBUG)
if(TEST)
set(bun bun-test)
elseif(DEBUG)
set(bun bun-debug)
# elseif(ENABLE_SMOL)
# set(bun bun-smol-profile)
@@ -529,6 +531,9 @@ file(GLOB_RECURSE BUN_ZIG_SOURCES ${CONFIGURE_DEPENDS}
list(APPEND BUN_ZIG_SOURCES
${CWD}/build.zig
${CWD}/src/main.zig
${CWD}/src/main_test.zig
${CWD}/root_test.zig
${CWD}/unit_test.zig
${BUN_BINDGEN_ZIG_OUTPUTS}
)

@@ -550,7 +555,11 @@ else()
list(APPEND BUN_ZIG_GENERATED_SOURCES ${BUN_BAKE_RUNTIME_OUTPUTS})
endif()

set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-zig.o)
if (TEST)
set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-test.o)
else()
set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-zig.o)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM|arm64|ARM64|aarch64|AARCH64")
if(APPLE)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@
"prettier:check": "bun run analysis:no-llvm --target prettier-check",
"prettier:extra": "bun run analysis:no-llvm --target prettier-extra",
"prettier:diff": "bun run analysis:no-llvm --target prettier-diff",
"node:test": "node ./scripts/runner.node.mjs --quiet --exec-path=$npm_execpath --node-tests "
"node:test": "node ./scripts/runner.node.mjs --quiet --exec-path=$npm_execpath --node-tests ",
"clean:zig": "rm -rf build/debug/cache/zig 'build/debug/*.o' .zig-cache zig-out || true"
}
}
8 changes: 8 additions & 0 deletions src/main_test.zig
Original file line number Diff line number Diff line change
@@ -52,6 +52,8 @@ pub fn main() void {
}

fn runTests() void {
var pass: u32 = 0;
var fail: u32 = 0;
for (builtin.test_functions) |t| {
const start = std.time.milliTimestamp();
std.testing.allocator_instance = .{};
@@ -65,10 +67,16 @@ fn runTests() void {

if (result) |_| {
Output.pretty("<green>pass</r> - {s} <i>({d}ms)</r>", .{ name, elapsed });
pass += 1;
} else |err| {
Output.pretty("<red>fail</r> - {s} <i>({d}ms)</r>\n{s}", .{ t.name, elapsed, @errorName(err) });
fail += 1;
}
}

const total = pass + fail;
bun.assert(total > 0);
Output.pretty("\n{d} tests, {d} passed, {d} failed\n", .{ total, pass, fail });
}

fn extractName(t: std.builtin.TestFn) []const u8 {
32 changes: 31 additions & 1 deletion unit_test.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
const std = @import("std");
// const bun = @import("root").bun;
const t = std.testing;

// comptime {
// // _ = bun;
// refAllDeclsRecursive(bun);

// }

// pub fn refAllDeclsRecursive(comptime T: type) void {
// inline for (comptime std.meta.declarations(T)) |decl| {
// if (@TypeOf(@field(T, decl.name)) == type) {
// switch (@typeInfo(@field(T, decl.name))) {
// .@"struct", .@"enum", .@"union", .@"opaque" => refAllDeclsRecursive(@field(T, decl.name)),
// else => {},
// }
// }
// _ = &@field(T, decl.name);
// }
// }
test {
try t.expectEqual(1, 1);
// std.mem.doNotOptimizeAway(bun);
// std.testing.refAllDeclsRecursive(bun);
// try t.expectEqual(1, 1);
// bun.assert(true);
// const _main = @import("root").main;
// @compileLog(_main);
// @compileError("foo");

try std.testing.expectEqual(2, add(1, 1));
}

fn add (a: i32, b: i32) i32 {
return a + b;
}