From f79a1d23211317021f2c8b8d8931254cb3a9b074 Mon Sep 17 00:00:00 2001 From: Randolf Jung Date: Wed, 23 Sep 2026 22:32:23 -0700 Subject: [PATCH] rust: build lint test runners for the test execution platform Use the test execution group for clippy and rustfmt runners, preserve the executable extension, and separate marker paths according to the runner platform. Assisted-by: OpenAI Codex --- rust/private/lint_test.bzl | 6 +-- test/unit/BUILD.bazel | 18 +++++++ test/unit/lint_tests/lint_tests.bzl | 79 +++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/rust/private/lint_test.bzl b/rust/private/lint_test.bzl index eef61a6a1f..2fd676e08c 100644 --- a/rust/private/lint_test.bzl +++ b/rust/private/lint_test.bzl @@ -55,7 +55,7 @@ LINT_TEST_COMMON_ATTRS = { ), "_runner": attr.label( doc = "The shared runner (prints/inspects collected marker paths).", - cfg = "exec", + cfg = config.exec("test"), executable = True, default = Label("//rust/private/lint_test_runner"), ), @@ -116,7 +116,7 @@ def lint_test_rule_impl(ctx, info_provider, output_group_names): list: `[DefaultInfo, RunEnvironmentInfo, OutputGroupInfo]` for the test target. """ - is_windows = ctx.executable._runner.extension == ".exe" + is_windows = ctx.executable._runner.extension == "exe" runner = ctx.actions.declare_file("{}{}".format( ctx.label.name, ".exe" if is_windows else "", @@ -140,7 +140,7 @@ def lint_test_rule_impl(ctx, info_provider, output_group_names): ) workspace_name = ctx.workspace_name - markers_env = ctx.configuration.host_path_separator.join([ + markers_env = (";" if is_windows else ":").join([ rlocationpath(f, workspace_name) for f in checks.to_list() ]) diff --git a/test/unit/BUILD.bazel b/test/unit/BUILD.bazel index 433344f5c0..a8560644c2 100644 --- a/test/unit/BUILD.bazel +++ b/test/unit/BUILD.bazel @@ -3,3 +3,21 @@ package(default_visibility = ["//visibility:public"]) exports_files([ "common.bzl", ]) + +# Distinct executable platforms let analysis tests verify the test execution +# group without running an x86_64 test binary on an arm64 host. +platform( + name = "macos_aarch64", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], +) + +platform( + name = "macos_x86_64", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], +) diff --git a/test/unit/lint_tests/lint_tests.bzl b/test/unit/lint_tests/lint_tests.bzl index 1a984cbc64..40a93ed998 100644 --- a/test/unit/lint_tests/lint_tests.bzl +++ b/test/unit/lint_tests/lint_tests.bzl @@ -76,6 +76,55 @@ def _make_transitive_count_test(marker_suffix, output_group_name): clippy_transitive_test = _make_transitive_count_test(".clippy.ok", "clippy_checks") rustfmt_transitive_test = _make_transitive_count_test(".rustfmt.ok", "rustfmt_checks") +_RunnerInfo = provider(fields = {"file": "Underlying lint runner executable"}) + +def _runner_aspect_impl(_target, ctx): + return [_RunnerInfo(file = ctx.rule.attr._runner[DefaultInfo].files_to_run.executable)] + +_runner_aspect = aspect(implementation = _runner_aspect_impl) + +def _runner_format_test_impl(ctx): + env = analysistest.begin(ctx) + target = analysistest.target_under_test(env) + runner = target[_RunnerInfo].file + executable = target[DefaultInfo].files_to_run.executable + + asserts.equals(env, runner.extension, executable.extension) + + markers = target[RunEnvironmentInfo].environment["RUST_LINT_TEST_MARKERS"] + separator = ";" if runner.extension == "exe" else ":" + asserts.equals(env, 1, markers.count(separator)) + + return analysistest.end(env) + +_runner_format_test = analysistest.make( + _runner_format_test_impl, + extra_target_under_test_aspects = [_runner_aspect], +) + +def _runner_platform_test_impl(ctx): + env = analysistest.begin(ctx) + target = analysistest.target_under_test(env) + runner = target[_RunnerInfo].file + asserts.true( + env, + runner.path.startswith("bazel-out/macos_x86_64-"), + "Expected the test-platform runner, got {}".format(runner.path), + ) + return analysistest.end(env) + +_runner_platform_test = analysistest.make( + _runner_platform_test_impl, + config_settings = { + "//command_line_option:extra_execution_platforms": [ + str(Label("//test/unit:macos_aarch64")), + str(Label("//test/unit:macos_x86_64")), + ], + "//command_line_option:platforms": str(Label("//test/unit:macos_x86_64")), + }, + extra_target_under_test_aspects = [_runner_aspect], +) + def lint_tests_suite(name): """Wire up the fixture graph and the two analysistests. @@ -131,10 +180,40 @@ def lint_tests_suite(name): target_under_test = ":rustfmt_fixture", ) + _runner_format_test( + name = "clippy_runner_format_test", + target_under_test = ":clippy_fixture", + ) + _runner_format_test( + name = "rustfmt_runner_format_test", + target_under_test = ":rustfmt_fixture", + ) + + _runner_platform_test( + name = "clippy_runner_platform_test", + target_under_test = ":clippy_fixture", + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], + ) + _runner_platform_test( + name = "rustfmt_runner_platform_test", + target_under_test = ":rustfmt_fixture", + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], + ) + native.test_suite( name = name, tests = [ ":clippy_transitive_test", ":rustfmt_transitive_test", + ":clippy_runner_format_test", + ":rustfmt_runner_format_test", + ":clippy_runner_platform_test", + ":rustfmt_runner_platform_test", ], )