From 9d32489993177b3584676b6715192252872123e3 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 4 Mar 2026 03:19:28 +0000 Subject: [PATCH] feat: add phase 4 bun_test bootstrap --- README.md | 6 +++ bun/defs.bzl | 2 + internal/bun_test.bzl | 59 ++++++++++++++++++++++++++++- tests/bun_test_test/BUILD.bazel | 19 ++++++++++ tests/bun_test_test/failing.test.ts | 5 +++ tests/bun_test_test/passing.test.ts | 5 +++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 tests/bun_test_test/BUILD.bazel create mode 100644 tests/bun_test_test/failing.test.ts create mode 100644 tests/bun_test_test/passing.test.ts diff --git a/README.md b/README.md index 67d7abf..e81d03f 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,9 @@ Phase 3 bootstrap is in place: - Executable `bun_binary` rule (`/internal/bun_binary.bzl`) - Public export via `bun/defs.bzl` - Focused JS/TS runnable tests (`//tests/binary_test:all`) + +Phase 4 bootstrap is in place: + +- Test rule `bun_test` (`/internal/bun_test.bzl`) +- Public export via `bun/defs.bzl` +- Focused passing/failing test targets (`//tests/bun_test_test:all`) diff --git a/bun/defs.bzl b/bun/defs.bzl index 48cd3f7..0664176 100644 --- a/bun/defs.bzl +++ b/bun/defs.bzl @@ -1,5 +1,6 @@ load("//internal:bun_binary.bzl", "bun_binary") load("//internal:bun_install.bzl", "bun_install") +load("//internal:bun_test.bzl", "bun_test") load(":repositories.bzl", "bun_register_toolchains", "bun_repositories") load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain") @@ -7,6 +8,7 @@ __all__ = [ "BunToolchainInfo", "bun_binary", "bun_install", + "bun_test", "bun_register_toolchains", "bun_repositories", "bun_toolchain", diff --git a/internal/bun_test.bzl b/internal/bun_test.bzl index f013b9e..6f4a5a4 100644 --- a/internal/bun_test.bzl +++ b/internal/bun_test.bzl @@ -1,2 +1,57 @@ -def bun_test(**_kwargs): - fail("bun_test is not implemented yet") +"""Rule for running test suites with Bun.""" + + +def _bun_test_impl(ctx): + toolchain = ctx.toolchains["//bun:toolchain_type"] + bun_bin = toolchain.bun.bun_bin + + src_args = " ".join(["\"{}\"".format(src.path) for src in ctx.files.srcs]) + launcher = ctx.actions.declare_file(ctx.label.name) + ctx.actions.write( + output = launcher, + is_executable = True, + content = """#!/usr/bin/env bash +set -euo pipefail + +extra_args=() +if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]; then + extra_args+=("--test-name-pattern" "${{TESTBRIDGE_TEST_ONLY}}") +fi +if [[ -n "${{COVERAGE_DIR:-}}" ]]; then + extra_args+=("--coverage") +fi + +exec "{}" test {} "${{extra_args[@]}}" "$@" +""".format(bun_bin.path, src_args), + ) + + transitive_files = [] + if ctx.attr.node_modules: + transitive_files.append(ctx.attr.node_modules[DefaultInfo].files) + + runfiles = ctx.runfiles( + files = [bun_bin] + ctx.files.srcs + ctx.files.data, + transitive_files = depset(transitive = transitive_files), + ) + + return [ + DefaultInfo( + executable = launcher, + runfiles = runfiles, + ), + ] + + +bun_test = rule( + implementation = _bun_test_impl, + attrs = { + "srcs": attr.label_list( + mandatory = True, + allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], + ), + "node_modules": attr.label(), + "data": attr.label_list(allow_files = True), + }, + test = True, + toolchains = ["//bun:toolchain_type"], +) diff --git a/tests/bun_test_test/BUILD.bazel b/tests/bun_test_test/BUILD.bazel new file mode 100644 index 0000000..9b27da4 --- /dev/null +++ b/tests/bun_test_test/BUILD.bazel @@ -0,0 +1,19 @@ +load("//bun:defs.bzl", "bun_test") + +bun_test( + name = "passing_suite", + srcs = ["passing.test.ts"], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) + +bun_test( + name = "failing_suite", + srcs = ["failing.test.ts"], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) diff --git a/tests/bun_test_test/failing.test.ts b/tests/bun_test_test/failing.test.ts new file mode 100644 index 0000000..cb462d0 --- /dev/null +++ b/tests/bun_test_test/failing.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "bun:test"; + +test("failing suite", () => { + expect(1 + 1).toBe(3); +}); diff --git a/tests/bun_test_test/passing.test.ts b/tests/bun_test_test/passing.test.ts new file mode 100644 index 0000000..b5e066a --- /dev/null +++ b/tests/bun_test_test/passing.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "bun:test"; + +test("passing suite", () => { + expect(1 + 1).toBe(2); +});