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 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
40 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
1d72423
build time wip
DonIsaac Mar 27, 2025
d122782
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 27, 2025
03d30ab
tweaks
DonIsaac Mar 27, 2025
39cf739
`bun run zig-format`
DonIsaac Mar 27, 2025
9a3afa2
Merge branch 'main' of github.com:oven-sh/bun into don/test/zig-test-3
DonIsaac Mar 28, 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
37 changes: 35 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,36 @@ pub fn build(b: *Build) !void {
step.dependOn(addInstallObjectFile(b, bun_obj, "bun-zig", obj_format));
}

// zig build test
{
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("src/unit_test.zig"),
.test_runner = .{ .path = b.path("src/main_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,
.omit_frame_pointer = false,
.strip = false,
});
configureObj(b, &o, unit_tests);
unit_tests.linker_allow_shlib_undefined = true;
unit_tests.link_function_sections = true;
unit_tests.link_data_sections = true;

const bin = unit_tests.getEmittedBin();
const obj = Build.LazyPath{ .generated = .{
.file = bin.generated.file,
.up = 1,
.sub_path = "bun-test.o",
} };
const cpy_obj = b.addInstallFile(obj, "bun-test.o");
step.dependOn(&cpy_obj.step);
}

// zig build windows-shim
{
var step = b.step("windows-shim", "Build the Windows shim (bun_shim_impl.exe + bun_shim_debug.exe)");
Expand Down Expand Up @@ -456,6 +486,11 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {
.omit_frame_pointer = false,
.strip = false, // stripped at the end
});
configureObj(b, opts, obj);
return obj;
}

fn configureObj(b: *Build, opts: *BunBuildOptions, obj: *Compile) void {
if (opts.enable_asan) {
if (@hasField(Build.Module, "sanitize_address")) {
obj.root_module.sanitize_address = true;
Expand Down Expand Up @@ -494,8 +529,6 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {

const translate_c = getTranslateC(b, opts.target, opts.optimize);
obj.root_module.addImport("translated-c-headers", translate_c.createModule());

return obj;
}

const ObjectFormat = enum {
Expand Down
9 changes: 9 additions & 0 deletions cmake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ else()
setx(DEBUG OFF)
endif()

optionx(BUN_TEST BOOL "Build Bun's unit test suite instead of the normal build" DEFAULT OFF)

if (BUN_TEST)
setx(TEST ON)
else()
setx(TEST OFF)
endif()


if(CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
setx(ENABLE_SMOL ON)
endif()
Expand Down
20 changes: 17 additions & 3 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ else()
set(bunStrip bun)
endif()

if(TEST)
set(bun ${bun}-test)
endif()

set(bunExe ${bun}${CMAKE_EXECUTABLE_SUFFIX})

if(bunStrip)
Expand Down Expand Up @@ -528,7 +532,6 @@ file(GLOB_RECURSE BUN_ZIG_SOURCES ${CONFIGURE_DEPENDS}

list(APPEND BUN_ZIG_SOURCES
${CWD}/build.zig
${CWD}/src/main.zig
${BUN_BINDGEN_ZIG_OUTPUTS}
)

Expand All @@ -550,7 +553,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)
Expand All @@ -573,6 +580,12 @@ if(NOT "${REVISION}" STREQUAL "")
set(ZIG_FLAGS_BUN ${ZIG_FLAGS_BUN} -Dsha=${REVISION})
endif()

if (TEST)
set(ZIG_STEPS test)
else()
set(ZIG_STEPS obj)
endif()

register_command(
TARGET
bun-zig
Expand All @@ -582,7 +595,7 @@ register_command(
"Building src/*.zig for ${ZIG_TARGET}"
COMMAND
${ZIG_EXECUTABLE}
build obj
build ${ZIG_STEPS}
${CMAKE_ZIG_FLAGS}
--prefix ${BUILD_PATH}
-Dobj_format=${ZIG_OBJECT_FORMAT}
Expand All @@ -596,6 +609,7 @@ register_command(
-Dcodegen_path=${CODEGEN_PATH}
-Dcodegen_embed=$<IF:$<BOOL:${CODEGEN_EMBED}>,true,false>
--prominent-compile-errors
--summary all
${ZIG_FLAGS_BUN}
ARTIFACTS
${BUN_ZIG_OUTPUT}
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"scripts": {
"build": "bun run build:debug",
"build:test": "bun run zig:test",
"build:debug": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -B build/debug",
"build:valgrind": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DENABLE_BASELINE=ON -ENABLE_VALGRIND=ON -B build/debug-valgrind",
"build:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -B build/release",
Expand All @@ -55,6 +56,8 @@
"test:release": "node scripts/runner.node.mjs --exec-path ./build/release/bun",
"banned": "bun test test/internal/ban-words.test.ts",
"zig": "vendor/zig/zig.exe",
"zig:test": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUN_TEST=ON -B build/debug",
"zig:test:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DBUNTEST=ON -B build/release",
"zig:fmt": "bun run zig-format",
"zig:check": "bun run zig build check --summary new",
"zig:check-all": "bun run zig build check-all --summary new",
Expand All @@ -74,6 +77,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/CMakeCache.txt 'build/debug/*.o' .zig-cache zig-out || true"
}
}
4 changes: 0 additions & 4 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,6 @@ pub const RefString = struct {
}
};

comptime {
std.testing.refAllDecls(RefString);
}

pub export fn MarkedArrayBuffer_deallocator(bytes_: *anyopaque, _: *anyopaque) void {
const mimalloc = @import("../allocators/mimalloc.zig");
// zig's memory allocator interface won't work here
Expand Down
4 changes: 0 additions & 4 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2181,10 +2181,6 @@ pub const PathOrBlob = union(enum) {
}
};

comptime {
std.testing.refAllDecls(Process);
}

/// StatFS and BigIntStatFS classes from node:fs
pub fn StatFSType(comptime big: bool) type {
const Int = if (big) i64 else i32;
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/node/util/parse_args_utils.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const std = @import("std");
const bun = @import("root").bun;
const testing = std.testing;
const String = if (@import("builtin").is_test) TestString else bun.String;
const JSValue = if (@import("builtin").is_test) usize else bun.JSC.JSValue;
// const String = if (@import("builtin").is_test) TestString else bun.String;
// const JSValue = if (@import("builtin").is_test) usize else bun.JSC.JSValue;
const String = bun.String;
const JSValue = bun.JSC.JSValue;

pub const OptionValueType = enum { boolean, string };

Expand Down
2 changes: 1 addition & 1 deletion src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ pub fn concat(comptime T: type, dest: []T, src: []const []const T) void {

pub const fast_debug_build_cmd = .None;
pub const fast_debug_build_mode = fast_debug_build_cmd != .None and
Environment.isDebug;
Environment.isDebug and !Environment.isTest;

pub const MultiArrayList = @import("./multi_array_list.zig").MultiArrayList;
pub const NullableAllocator = @import("./allocators/NullableAllocator.zig");
Expand Down
8 changes: 4 additions & 4 deletions src/cli/test_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ pub const TestCommand = struct {
strings.startsWith(arg, "./") or
strings.startsWith(arg, "../") or
(Environment.isWindows and (strings.startsWith(arg, ".\\") or
strings.startsWith(arg, "..\\")))) break true;
strings.startsWith(arg, "..\\")))) break true;
} else false) {
// One of the files is a filepath. Instead of treating the arguments as filters, treat them as filepaths
for (ctx.positionals[1..]) |arg| {
Expand Down Expand Up @@ -1453,9 +1453,9 @@ pub const TestCommand = struct {

if (has_file_like == null and
(strings.hasSuffixComptime(filter, ".ts") or
strings.hasSuffixComptime(filter, ".tsx") or
strings.hasSuffixComptime(filter, ".js") or
strings.hasSuffixComptime(filter, ".jsx")))
strings.hasSuffixComptime(filter, ".tsx") or
strings.hasSuffixComptime(filter, ".js") or
strings.hasSuffixComptime(filter, ".jsx")))
{
has_file_like = i;
}
Expand Down
63 changes: 11 additions & 52 deletions src/cli/upgrade_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,50 +116,9 @@ pub const Version = struct {
comptime {
_ = Bun__githubURL;
}
};

pub const UpgradeCheckerThread = struct {
pub fn spawn(env_loader: *DotEnv.Loader) void {
if (env_loader.map.get("BUN_DISABLE_UPGRADE_CHECK") != null or
env_loader.map.get("CI") != null or
strings.eqlComptime(env_loader.get("BUN_CANARY") orelse "0", "1"))
return;
var update_checker_thread = std.Thread.spawn(.{}, run, .{env_loader}) catch return;
update_checker_thread.detach();
}

fn _run(env_loader: *DotEnv.Loader) anyerror!void {
var rand = std.rand.DefaultPrng.init(@as(u64, @intCast(@max(std.time.milliTimestamp(), 0))));
const delay = rand.random().intRangeAtMost(u64, 100, 10000);
std.time.sleep(std.time.ns_per_ms * delay);

Output.Source.configureThread();
HTTP.HTTPThread.init(&.{});

defer {
js_ast.Expr.Data.Store.deinit();
js_ast.Stmt.Data.Store.deinit();
}

var version = (try UpgradeCommand.getLatestVersion(default_allocator, env_loader, null, null, false, true)) orelse return;

if (!version.isCurrent()) {
if (version.name()) |name| {
Output.prettyErrorln("\n<r><d>Bun v{s} is out. Run <b><cyan>bun upgrade<r> to upgrade.\n", .{name});
Output.flush();
}
}

version.buf.deinit();
}

fn run(env_loader: *DotEnv.Loader) void {
_run(env_loader) catch |err| {
if (Environment.isDebug) {
Output.prettyError("\n[UpgradeChecker] ERROR: {s}\n", .{@errorName(err)});
Output.flush();
}
};
test {
std.mem.doNotOptimizeAway(Bun__githubURL);
_ = Bun__githubURL;
}
};

Expand Down Expand Up @@ -656,14 +615,14 @@ pub const UpgradeCommand = struct {
const powershell_path =
bun.which(&buf, bun.getenvZ("PATH") orelse "", "", "powershell") orelse
hardcoded_system_powershell: {
const system_root = bun.getenvZ("SystemRoot") orelse "C:\\Windows";
const hardcoded_system_powershell = bun.path.joinAbsStringBuf(system_root, &buf, &.{ system_root, "System32\\WindowsPowerShell\\v1.0\\powershell.exe" }, .windows);
if (bun.sys.exists(hardcoded_system_powershell)) {
break :hardcoded_system_powershell hardcoded_system_powershell;
}
Output.prettyErrorln("<r><red>error:<r> Failed to unzip {s} due to PowerShell not being installed.", .{tmpname});
Global.exit(1);
};
const system_root = bun.getenvZ("SystemRoot") orelse "C:\\Windows";
const hardcoded_system_powershell = bun.path.joinAbsStringBuf(system_root, &buf, &.{ system_root, "System32\\WindowsPowerShell\\v1.0\\powershell.exe" }, .windows);
if (bun.sys.exists(hardcoded_system_powershell)) {
break :hardcoded_system_powershell hardcoded_system_powershell;
}
Output.prettyErrorln("<r><red>error:<r> Failed to unzip {s} due to PowerShell not being installed.", .{tmpname});
Global.exit(1);
};

var unzip_argv = [_]string{
powershell_path,
Expand Down
2 changes: 1 addition & 1 deletion src/env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const allow_assert = isDebug or isTest or std.builtin.Mode.ReleaseSafe == @i
/// All calls to `@export` should be gated behind this check, so that code
/// generators that compile Zig code know not to reference and compile a ton of
/// unused code.
pub const export_cpp_apis = @import("builtin").output_mode == .Obj;
pub const export_cpp_apis = @import("builtin").output_mode == .Obj or isTest;

pub const build_options = @import("build_options");

Expand Down
Loading