|
| 1 | +def _test_runner_sh_test_impl(ctx): |
| 2 | + script_content = """#!/bin/sh |
| 3 | +# Script to execute {runner_label} with arguments. |
| 4 | +
|
| 5 | +set -e # Exit immediately if a command exits with a non-zero status. |
| 6 | +
|
| 7 | +RUNNER_PATH="{runner_path_prefix}/{runner_short_path}" |
| 8 | +TEST_PATH="{test_path_prefix}/{test_short_path}" |
| 9 | +RUNNER_ARGS=({test_runner_args_string}) |
| 10 | +TEST_ARGS=({test_args_string}) |
| 11 | +
|
| 12 | +echo "Executing: $RUNNER_PATH" "${{RUNNER_ARGS[@]}}" "-- $TEST_PATH ${{TEST_ARGS[@]}}" |
| 13 | +"$RUNNER_PATH" "${{RUNNER_ARGS[@]}}" -- $TEST_PATH "${{TEST_ARGS[@]}}" |
| 14 | +""".format( |
| 15 | + runner_label = ctx.attr.test_runner_binary, |
| 16 | + runner_path_prefix = ctx.attr.test_runner_binary.label.package if ctx.attr.test_runner_binary.label.package else ".", |
| 17 | + runner_short_path = ctx.attr.test_runner_binary.label.name, |
| 18 | + test_path_prefix = ctx.attr.test_binary.label.package if ctx.attr.test_binary.label.package else ".", |
| 19 | + test_short_path = ctx.attr.test_binary.label.name, |
| 20 | + test_runner_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_runner_args]), |
| 21 | + test_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_args]), |
| 22 | + ) |
| 23 | + |
| 24 | + script_name = "{}_runner.sh".format(ctx.attr.name) |
| 25 | + ctx.actions.write( |
| 26 | + output = ctx.outputs.executable, |
| 27 | + content = script_content, |
| 28 | + is_executable = True, |
| 29 | + ) |
| 30 | + |
| 31 | + # The runfiles need to include the actual test_runner binary |
| 32 | + runfiles = ctx.runfiles(files = [ctx.executable.test_runner_binary, ctx.executable.test_binary]) |
| 33 | + |
| 34 | + return [DefaultInfo( |
| 35 | + files = depset([ctx.outputs.executable]), |
| 36 | + runfiles = runfiles, |
| 37 | + )] |
| 38 | + |
| 39 | +_test_runner_sh_test = rule( |
| 40 | + implementation = _test_runner_sh_test_impl, |
| 41 | + attrs = { |
| 42 | + "test_runner_binary": attr.label( |
| 43 | + doc = "The test runner binary.", |
| 44 | + mandatory = True, |
| 45 | + executable = True, |
| 46 | + cfg = "exec", # Compile for execution platform |
| 47 | + ), |
| 48 | + "test_binary": attr.label( |
| 49 | + doc = "The binary target to be tested.", |
| 50 | + mandatory = True, |
| 51 | + executable = True, |
| 52 | + cfg = "exec", # Compile for execution platform |
| 53 | + ), |
| 54 | + "test_runner_args": attr.string_list( |
| 55 | + doc = "A list of arguments to pass to the test_runner.", |
| 56 | + default = [], |
| 57 | + ), |
| 58 | + "test_args": attr.string_list( |
| 59 | + doc = "A list of arguments to pass to the test.", |
| 60 | + default = [], |
| 61 | + ), |
| 62 | + }, |
| 63 | + executable = True, # Signifies that this rule outputs an executable (the script) |
| 64 | + test = True, # Signifies that this rule can be run as a test |
| 65 | +) |
| 66 | + |
| 67 | +def test_runner_sh_test(name, test_runner, test_binary, test_runner_args = [], test_args = [], **kwargs): |
| 68 | + """ |
| 69 | + Generates an sh_test that executes the given test_runner binary with specified arguments. |
| 70 | +
|
| 71 | + Args: |
| 72 | + name: The name of the sh_test. |
| 73 | + test_runner: The label of the cc_binary or other executable target. |
| 74 | + args: A list of strings representing the arguments to pass to the test_runner. |
| 75 | + **kwargs: Additional arguments to pass to the underlying sh_test rule. |
| 76 | + """ |
| 77 | + script_name = "{}_generated_script".format(name) |
| 78 | + |
| 79 | + # Using the internal rule to generate the script |
| 80 | + _test_runner_sh_test( |
| 81 | + name = script_name, |
| 82 | + test_runner_binary = test_runner, |
| 83 | + test_runner_args = test_runner_args, |
| 84 | + test_binary = test_binary, |
| 85 | + test_args = test_args, |
| 86 | + # Ensure the generated script itself is not the test directly, |
| 87 | + # but is used by the native.sh_test |
| 88 | + testonly = True, # Mark as testonly if the script itself isn't meant for release |
| 89 | + tags = kwargs.pop("tags", []) + ["manual"], # Usually you don't run this rule directly |
| 90 | + ) |
| 91 | + |
| 92 | + # Create the sh_test rule using the generated script |
| 93 | + native.sh_test( |
| 94 | + name = name, |
| 95 | + srcs = [":" + script_name], |
| 96 | + data = [test_runner, test_binary], |
| 97 | + ) |
0 commit comments