xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/util.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1"""Shared private utilities."""
2
3def _do_nothing_impl(ctx):
4    _ = ctx  # @unused
5    return []
6
7do_nothing = rule(implementation = _do_nothing_impl)
8
9def get_test_name_from_function(func):
10    """Derives a suitable test name from a function.
11
12    This can be used for better test feedback.
13
14    Args:
15      func: (callable) A test implementation or setup function.
16
17    Returns:
18      (str) The name of the given function, suitable as a test name.
19    """
20
21    # Starlark currently stringifies a function as "<function NAME>", so we use
22    # that knowledge to parse the "NAME" portion out. If this behavior ever
23    # changes, we'll need to update this.
24    # TODO(bazel-team): Expose a ._name field on functions to avoid this.
25    func_name = str(func)
26    func_name = func_name.partition("<function ")[-1]
27    func_name = func_name.rpartition(">")[0]
28    func_name = func_name.partition(" ")[0]
29
30    # Strip leading/trailing underscores so that test functions can
31    # have private names. This better allows unused tests to be flagged by
32    # buildifier (indicating a bug or code to delete)
33    return func_name.strip("_")
34
35get_function_name = get_test_name_from_function
36