Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bats_test_suite rule #18

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
filmil marked this conversation as resolved.
Show resolved Hide resolved

# 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",
]
)