|
| 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 | + print(script_content) |
| 25 | + |
| 26 | + script_name = "{}_runner.sh".format(ctx.attr.name) |
| 27 | + ctx.actions.write( |
| 28 | + output = ctx.outputs.executable, |
| 29 | + content = script_content, |
| 30 | + is_executable = True, |
| 31 | + ) |
| 32 | + |
| 33 | + # The runfiles need to include the actual test_runner binary |
| 34 | + runfiles = ctx.runfiles(files = [ctx.executable.test_runner_binary, ctx.executable.test_binary]) |
| 35 | + |
| 36 | + return [DefaultInfo( |
| 37 | + files = depset([ctx.outputs.executable]), |
| 38 | + runfiles = runfiles, |
| 39 | + )] |
| 40 | + |
| 41 | +_test_runner_sh_test = rule( |
| 42 | + implementation = _test_runner_sh_test_impl, |
| 43 | + attrs = { |
| 44 | + "test_runner_binary": attr.label( |
| 45 | + doc = "The test runner binary.", |
| 46 | + mandatory = True, |
| 47 | + executable = True, |
| 48 | + cfg = "exec", # Compile for execution platform |
| 49 | + ), |
| 50 | + "test_binary": attr.label( |
| 51 | + doc = "The binary target to be tested.", |
| 52 | + mandatory = True, |
| 53 | + executable = True, |
| 54 | + cfg = "exec", # Compile for execution platform |
| 55 | + ), |
| 56 | + "test_runner_args": attr.string_list( |
| 57 | + doc = "A list of arguments to pass to the test_runner.", |
| 58 | + default = [], |
| 59 | + ), |
| 60 | + "test_args": attr.string_list( |
| 61 | + doc = "A list of arguments to pass to the test.", |
| 62 | + default = [], |
| 63 | + ), |
| 64 | + }, |
| 65 | + executable = True, # Signifies that this rule outputs an executable (the script) |
| 66 | + test = True, # Signifies that this rule can be run as a test |
| 67 | +) |
| 68 | + |
| 69 | +def test_runner_sh_test(name, test_runner, test_binary, test_runner_args = [], test_args = [], **kwargs): |
| 70 | + """ |
| 71 | + Generates an sh_test that executes the given test_runner binary with specified arguments. |
| 72 | +
|
| 73 | + Args: |
| 74 | + name: The name of the sh_test. |
| 75 | + test_runner: The label of the cc_binary or other executable target. |
| 76 | + args: A list of strings representing the arguments to pass to the test_runner. |
| 77 | + **kwargs: Additional arguments to pass to the underlying sh_test rule. |
| 78 | + """ |
| 79 | + script_name = "{}_generated_script".format(name) |
| 80 | + |
| 81 | + # Using the internal rule to generate the script |
| 82 | + _test_runner_sh_test( |
| 83 | + name = script_name, |
| 84 | + test_runner_binary = test_runner, |
| 85 | + test_runner_args = test_runner_args, |
| 86 | + test_binary = test_binary, |
| 87 | + test_args = test_args, |
| 88 | + # Ensure the generated script itself is not the test directly, |
| 89 | + # but is used by the native.sh_test |
| 90 | + testonly = True, # Mark as testonly if the script itself isn't meant for release |
| 91 | + tags = kwargs.pop("tags", []) + ["manual"], # Usually you don't run this rule directly |
| 92 | + ) |
| 93 | + |
| 94 | + # Create the sh_test rule using the generated script |
| 95 | + native.sh_test( |
| 96 | + name = name, |
| 97 | + srcs = [":" + script_name], |
| 98 | + data = [test_runner, test_binary], |
| 99 | + ) |
| 100 | + |
| 101 | +def memory_test(name, test_binary, **kwargs): |
| 102 | + script_name = "{}_generated_script".format(name) |
| 103 | + |
| 104 | + memory_tools = Label("//:libmemory_tools_interpose.so") |
| 105 | + test_runner = Label("//:test_runner") |
| 106 | + test_runner_args = ["--env", "LD_PRELOAD=./libmemory_tools_interpose.so"] |
| 107 | + |
| 108 | + # Using the internal rule to generate the script |
| 109 | + _test_runner_sh_test( |
| 110 | + name = script_name, |
| 111 | + test_runner_binary = test_runner, |
| 112 | + test_runner_args = test_runner_args, |
| 113 | + test_binary = test_binary, |
| 114 | + # Ensure the generated script itself is not the test directly, |
| 115 | + # but is used by the native.sh_test |
| 116 | + testonly = True, # Mark as testonly if the script itself isn't meant for release |
| 117 | + tags = kwargs.pop("tags", []) + ["manual"], # Usually you don't run this rule directly |
| 118 | + ) |
| 119 | + |
| 120 | + # Create the sh_test rule using the generated script |
| 121 | + native.sh_test( |
| 122 | + name = name, |
| 123 | + srcs = [":" + script_name], |
| 124 | + data = [test_runner, test_binary, memory_tools], |
| 125 | + ) |
0 commit comments