Skip to content

Commit

Permalink
Add bats_test_suite rule
Browse files Browse the repository at this point in the history
The rule can be used to generate `bats_test` targets for each source
file and a `test_suite` which encapsulates all tests.
  • Loading branch information
fmorency authored and filmil committed Feb 27, 2023
1 parent 950091f commit 3166697
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
37 changes: 37 additions & 0 deletions rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,40 @@ def bats_test(uses_bats_assert = False, **kwargs):
_bats_test(**kwargs)
else:
_bats_with_bats_assert_test(**kwargs)


# Inspired from `rules_rust`
def bats_test_suite(name, srcs, **kwargs):
"""
A rule for creating a test suite for a set of `bats_test` targets.
The rule can be used to generate `bats_test` targets for each source file and a `test_suite`
which encapsulates all tests.
Args:
name (str): The name of the `test_suite`.
srcs (list): All test sources, typically `glob(["*.bats"])`.
**kwargs (dict): Additional keyword arguments for the underyling `bats_test` targets. The
`tags` argument is also passed to the generated `test_suite` target.
"""
tests = []

for src in srcs:
if not src.endswith(".bats"):
fail("srcs should have `.bats` extensions")

# Prefixed with `name` to allow parameterization with macros
# The test name should not end with `.bats`
test_name = name + "_" + src[:-5]
bats_test(
name = test_name,
srcs = [src],
**kwargs
)
tests.append(test_name)

native.test_suite(
name = name,
tests = tests,
tags = kwargs.get("tags", None),
)
10 changes: 9 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@bazel_bats//:rules.bzl", "bats_test")
load("@bazel_bats//:rules.bzl", "bats_test", "bats_test_suite")

sh_binary(
name = "exit_with_input_bin",
Expand Down Expand Up @@ -87,3 +87,11 @@ filegroup(
name = "dummy",
srcs = ["dummy.txt"],
)

bats_test_suite(
name = "test_suite",
srcs = [
"hello_world_1.bats",
"hello_world_2.bats",
]
)

0 comments on commit 3166697

Please sign in to comment.