feat: new bun_build and bun_compile, extend bun_install
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
"""Rule for running test suites with Bun."""
|
||||
|
||||
load("//internal:bun_command.bzl", "append_shell_flag", "append_shell_flag_files", "append_shell_flag_value", "append_shell_flag_values", "append_shell_install_mode", "append_shell_raw_flags", "render_shell_array", "shell_quote")
|
||||
load("//internal:js_library.bzl", "collect_js_runfiles")
|
||||
load("//internal:workspace.bzl", "create_bun_workspace_info", "render_workspace_setup", "workspace_runfiles")
|
||||
|
||||
|
||||
def _shell_quote(value):
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
|
||||
def _bun_test_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
@@ -15,33 +12,68 @@ def _bun_test_impl(ctx):
|
||||
dep_runfiles = [collect_js_runfiles(dep) for dep in ctx.attr.deps]
|
||||
workspace_info = create_bun_workspace_info(
|
||||
ctx,
|
||||
extra_files = ctx.files.srcs + ctx.files.data + [bun_bin],
|
||||
extra_files = ctx.files.srcs + ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin],
|
||||
primary_file = primary_file,
|
||||
)
|
||||
|
||||
src_args = " ".join([_shell_quote(src.short_path) for src in ctx.files.srcs])
|
||||
launcher_lines = [render_shell_array("bun_args", ["--bun", "test"])]
|
||||
append_shell_install_mode(launcher_lines, "bun_args", ctx.attr.install_mode)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--preload", ctx.files.preload)
|
||||
append_shell_flag_files(launcher_lines, "bun_args", "--env-file", ctx.files.env_files)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--no-env-file", ctx.attr.no_env_file)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--smol", ctx.attr.smol)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--timeout", str(ctx.attr.timeout_ms) if ctx.attr.timeout_ms > 0 else None)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--update-snapshots", ctx.attr.update_snapshots)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--rerun-each", str(ctx.attr.rerun_each) if ctx.attr.rerun_each > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--retry", str(ctx.attr.retry) if ctx.attr.retry > 0 else None)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--todo", ctx.attr.todo)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--only", ctx.attr.only)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--pass-with-no-tests", ctx.attr.pass_with_no_tests)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--concurrent", ctx.attr.concurrent)
|
||||
append_shell_flag(launcher_lines, "bun_args", "--randomize", ctx.attr.randomize)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--seed", str(ctx.attr.seed) if ctx.attr.seed > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--bail", str(ctx.attr.bail) if ctx.attr.bail > 0 else None)
|
||||
append_shell_flag_value(launcher_lines, "bun_args", "--max-concurrency", str(ctx.attr.max_concurrency) if ctx.attr.max_concurrency > 0 else None)
|
||||
append_shell_raw_flags(launcher_lines, "bun_args", ctx.attr.test_flags)
|
||||
launcher_lines.append('coverage_requested="0"')
|
||||
launcher_lines.append('coverage_dir=""')
|
||||
launcher_lines.append('if [[ "${COVERAGE_DIR:-}" != "" ]]; then')
|
||||
launcher_lines.append(' coverage_requested="1"')
|
||||
launcher_lines.append(' coverage_dir="${COVERAGE_DIR}"')
|
||||
launcher_lines.append('elif [[ "%s" == "1" ]]; then' % ("1" if ctx.attr.coverage else "0"))
|
||||
launcher_lines.append(' coverage_requested="1"')
|
||||
launcher_lines.append(' coverage_dir="${TEST_UNDECLARED_OUTPUTS_DIR:-${runtime_workspace}/coverage}"')
|
||||
launcher_lines.append('fi')
|
||||
launcher_lines.append('if [[ "${coverage_requested}" == "1" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--coverage")')
|
||||
launcher_lines.append(' bun_args+=("--coverage-dir" "${coverage_dir}")')
|
||||
if ctx.attr.coverage_reporters:
|
||||
for reporter in ctx.attr.coverage_reporters:
|
||||
launcher_lines.append(' bun_args+=("--coverage-reporter" %s)' % shell_quote(reporter))
|
||||
else:
|
||||
launcher_lines.append(' if [[ "${COVERAGE_DIR:-}" != "" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--coverage-reporter" "lcov")')
|
||||
launcher_lines.append(' fi')
|
||||
launcher_lines.append('fi')
|
||||
launcher_lines.append('if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" ]]; then')
|
||||
launcher_lines.append(' bun_args+=("--test-name-pattern" "${TESTBRIDGE_TEST_ONLY}")')
|
||||
launcher_lines.append('fi')
|
||||
if ctx.attr.reporter == "junit":
|
||||
launcher_lines.append('reporter_out="${XML_OUTPUT_FILE:-${runtime_workspace}/junit.xml}"')
|
||||
launcher_lines.append('bun_args+=("--reporter" "junit" "--reporter-outfile" "${reporter_out}")')
|
||||
elif ctx.attr.reporter == "dots":
|
||||
launcher_lines.append('bun_args+=("--reporter" "dots")')
|
||||
for src in ctx.files.srcs:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(src.short_path))
|
||||
for arg in ctx.attr.args:
|
||||
launcher_lines.append("bun_args+=(%s)" % shell_quote(arg))
|
||||
|
||||
command = """
|
||||
trap cleanup_runtime_workspace EXIT
|
||||
cd "${runtime_workspace}"
|
||||
test_args=(__SRC_ARGS__)
|
||||
|
||||
if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" && -n "${COVERAGE_DIR:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --test-name-pattern "${TESTBRIDGE_TEST_ONLY}" --coverage "$@"
|
||||
fi
|
||||
if [[ -n "${TESTBRIDGE_TEST_ONLY:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --test-name-pattern "${TESTBRIDGE_TEST_ONLY}" "$@"
|
||||
fi
|
||||
if [[ -n "${COVERAGE_DIR:-}" ]]; then
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" --coverage "$@"
|
||||
fi
|
||||
exec "${bun_bin}" --bun test "${test_args[@]}" "$@"
|
||||
""".replace("__SRC_ARGS__", src_args)
|
||||
if ctx.attr.args:
|
||||
default_args = "\n".join(['test_args+=({})'.format(_shell_quote(arg)) for arg in ctx.attr.args])
|
||||
command = command.replace(
|
||||
'test_args=(__SRC_ARGS__)',
|
||||
'test_args=(__SRC_ARGS__)\n' + default_args,
|
||||
)
|
||||
__BUN_ARGS__
|
||||
exec "${bun_bin}" "${bun_args[@]}" "$@"
|
||||
""".replace("__BUN_ARGS__", "\n".join(launcher_lines))
|
||||
|
||||
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(
|
||||
@@ -89,6 +121,90 @@ Supports Bazel test filtering (`--test_filter`) and coverage integration.
|
||||
allow_files = True,
|
||||
doc = "Additional runtime files needed by tests.",
|
||||
),
|
||||
"preload": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Modules to preload with `--preload` before running tests.",
|
||||
),
|
||||
"env_files": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "Additional environment files loaded with `--env-file`.",
|
||||
),
|
||||
"no_env_file": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, disables Bun's automatic `.env` loading.",
|
||||
),
|
||||
"smol": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, enables Bun's lower-memory runtime mode.",
|
||||
),
|
||||
"install_mode": attr.string(
|
||||
default = "disable",
|
||||
values = ["disable", "auto", "fallback", "force"],
|
||||
doc = "Whether Bun may auto-install missing packages while testing.",
|
||||
),
|
||||
"timeout_ms": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional per-test timeout in milliseconds.",
|
||||
),
|
||||
"update_snapshots": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, updates Bun snapshot files.",
|
||||
),
|
||||
"rerun_each": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional number of times to rerun each test file.",
|
||||
),
|
||||
"retry": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional default retry count for all tests.",
|
||||
),
|
||||
"todo": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, includes tests marked with `test.todo()`.",
|
||||
),
|
||||
"only": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, runs only tests marked with `test.only()` or `describe.only()`.",
|
||||
),
|
||||
"pass_with_no_tests": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, exits successfully when no tests are found.",
|
||||
),
|
||||
"concurrent": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, treats all tests as concurrent tests.",
|
||||
),
|
||||
"randomize": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, runs tests in random order.",
|
||||
),
|
||||
"seed": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional randomization seed.",
|
||||
),
|
||||
"bail": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional failure count after which Bun exits the test run.",
|
||||
),
|
||||
"reporter": attr.string(
|
||||
default = "console",
|
||||
values = ["console", "dots", "junit"],
|
||||
doc = "Test reporter format.",
|
||||
),
|
||||
"max_concurrency": attr.int(
|
||||
default = 0,
|
||||
doc = "Optional maximum number of concurrent tests.",
|
||||
),
|
||||
"coverage": attr.bool(
|
||||
default = False,
|
||||
doc = "If true, always enables Bun coverage output.",
|
||||
),
|
||||
"coverage_reporters": attr.string_list(
|
||||
doc = "Repeated Bun coverage reporters such as `text` or `lcov`.",
|
||||
),
|
||||
"test_flags": attr.string_list(
|
||||
doc = "Additional raw flags forwarded to `bun test` before the test source list.",
|
||||
),
|
||||
},
|
||||
test = True,
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
|
||||
Reference in New Issue
Block a user