aboutsummaryrefslogtreecommitdiff
path: root/lib/private/util.bzl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/util.bzl')
-rw-r--r--lib/private/util.bzl35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/private/util.bzl b/lib/private/util.bzl
new file mode 100644
index 0000000..fc003f9
--- /dev/null
+++ b/lib/private/util.bzl
@@ -0,0 +1,35 @@
+"""Shared private utilities."""
+
+def _do_nothing_impl(ctx):
+ _ = ctx # @unused
+ return []
+
+do_nothing = rule(implementation = _do_nothing_impl)
+
+def get_test_name_from_function(func):
+ """Derives a suitable test name from a function.
+
+ This can be used for better test feedback.
+
+ Args:
+ func: (callable) A test implementation or setup function.
+
+ Returns:
+ (str) The name of the given function, suitable as a test name.
+ """
+
+ # Starlark currently stringifies a function as "<function NAME>", so we use
+ # that knowledge to parse the "NAME" portion out. If this behavior ever
+ # changes, we'll need to update this.
+ # TODO(bazel-team): Expose a ._name field on functions to avoid this.
+ func_name = str(func)
+ func_name = func_name.partition("<function ")[-1]
+ func_name = func_name.rpartition(">")[0]
+ func_name = func_name.partition(" ")[0]
+
+ # Strip leading/trailing underscores so that test functions can
+ # have private names. This better allows unused tests to be flagged by
+ # buildifier (indicating a bug or code to delete)
+ return func_name.strip("_")
+
+get_function_name = get_test_name_from_function