Skip to content

Commit

Permalink
Add macro junit_tests
Browse files Browse the repository at this point in the history
Macro junit_tests compiles and runs JUnit 5 tests.
  • Loading branch information
joca-bt committed Dec 13, 2023
1 parent 334e2e7 commit 627ec8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Rules:

- [executable_jar](docs/executable_jar.md)
- [javadoc](docs/javadoc.md)
- [junit_tests](docs/junit_tests.md) (macro)

## Usage

Expand All @@ -14,6 +15,6 @@ http_archive(
name = "extra_rules_java",
sha256 = <sha>,
strip_prefix = "extra_rules_java-{}".format(<tag>),
url = "https://github.com/joca-bt/extra_rules_java/archive/{}.tar.gz".format(<tag>),
url = "https://github.com/joca-bt/extra_rules_java/releases/download/{0}/extra_rules_java-{0}.tar.gz".format(<tag>),
)
```
9 changes: 9 additions & 0 deletions docs/junit_tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# junit_tests

```bazel
junit_tests(name, **kwargs)
```

Compiles and runs JUnit 5 tests.

The arguments are the same as for [`java_test`](https://bazel.build/reference/be/java#java_test).
2 changes: 2 additions & 0 deletions java/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("//java/private:executable_jar.bzl", _executable_jar = "executable_jar")
load("//java/private:javadoc.bzl", _javadoc = "javadoc")
load("//java/private:junit_tests.bzl", _junit_tests = "junit_tests")

executable_jar = _executable_jar
javadoc = _javadoc
junit_tests = _junit_tests
28 changes: 28 additions & 0 deletions java/private/junit_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
_FORBIDDEN_ARGUMENTS = [
"args",
"main_class",
"use_testrunner",
]

def junit_tests(name, **kwargs):
for argument in _FORBIDDEN_ARGUMENTS:
if argument in kwargs:
fail("Forbidden argument: {}.".format(argument))

if native.package_name():
jar = "{}/{}.jar".format(native.package_name(), name)
else:
jar = "{}.jar".format(name)

native.java_test(
name = name,
main_class = "extrarulesjava.testrunner.TestRunner",
use_testrunner = False,
runtime_deps = kwargs.pop("runtime_deps", []) + [
"@extra_rules_java//test-runner",
],
args = [
jar,
],
**kwargs,
)

0 comments on commit 627ec8c

Please sign in to comment.