-
Notifications
You must be signed in to change notification settings - Fork 52
/
junit5.bzl
108 lines (90 loc) · 4.2 KB
/
junit5.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
load("@rules_jvm_external//:defs.bzl", "DEFAULT_REPOSITORY_NAME", "artifact")
load("//java/private:library.bzl", "java_test")
load("//java/private:package.bzl", "get_package_name")
def junit5_deps(repository_name = DEFAULT_REPOSITORY_NAME):
return [
artifact("org.junit.jupiter:junit-jupiter-engine", repository_name),
artifact("org.junit.platform:junit-platform-launcher", repository_name),
artifact("org.junit.platform:junit-platform-reporting", repository_name),
]
def junit5_vintage_deps(repository_name = DEFAULT_REPOSITORY_NAME):
return junit5_deps(repository_name) + [
artifact("org.junit.vintage:junit-vintage-engine", repository_name),
]
"""Dependencies typically required by JUnit 5 tests.
See `java_junit5_test` for more details.
"""
JUNIT5_DEPS = junit5_deps()
JUNIT5_VINTAGE_DEPS = junit5_vintage_deps()
def java_junit5_test(
name,
test_class = None,
runtime_deps = [],
package_prefixes = [],
jvm_flags = [],
include_tags = [],
exclude_tags = [],
include_engines = [],
exclude_engines = [],
**kwargs):
"""Run junit5 tests using Bazel.
This is designed to be a drop-in replacement for `java_test`, but
rather than using a JUnit4 runner it provides support for using
JUnit5 directly. The arguments are the same as used by `java_test`.
By default Bazel, and by extension this rule, assumes you want to
always run all of the tests in a class file. The `include_tags`
and `exclude_tags` allows for selectively running specific tests
within a single class file based on your use of the `@Tag` Junit5
annotations. Please see [the JUnit 5
docs](https://junit.org/junit5/docs/current/user-guide/#running-tests-tags)
for more information about using JUnit5 tag annotation to control
test execution.
The generated target does not include any JUnit5 dependencies. If
you are using the standard `@maven` namespace for your
`maven_install` you can add these to your `deps` using
`JUNIT5_DEPS` or `JUNIT5_VINTAGE_DEPS` loaded from
`//java:defs.bzl`
**Note**: The junit5 runner prevents `System.exit` being called
using a `SecurityManager`, which means that one test can't
prematurely cause an entire test run to finish unexpectedly.
While the `SecurityManager` has been deprecated in recent Java
releases, there's no replacement yet. JEP 411 has this as one of
its goals, but this is not complete or available yet.
Args:
name: The name of the test.
test_class: The Java class to be loaded by the test runner. If not
specified, the class name will be inferred from a combination of
the current bazel package and the `name` attribute.
include_tags: Junit5 tag expressions to include execution of tagged tests.
exclude_tags: Junit tag expressions to exclude execution of tagged tests.
include_engines: A list of JUnit Platform test engine IDs to include.
exclude_engines: A list of JUnit Platform test engine IDs to exclude.
"""
if test_class:
clazz = test_class
else:
clazz = get_package_name(package_prefixes) + name
if include_tags:
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_TAGS=" + ",".join(include_tags)]
if exclude_tags:
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_TAGS=" + ",".join(exclude_tags)]
if include_engines:
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_ENGINES=%s" % ",".join(include_engines)]
if exclude_engines:
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_ENGINES=%s" % ",".join(exclude_engines)]
security_manager_flag_seen = False
for flag in jvm_flags:
if flag.startswith("-Djava.security.manager="):
security_manager_flag_seen = True
if not security_manager_flag_seen:
jvm_flags = jvm_flags + ["-Djava.security.manager=allow"]
java_test(
name = name,
main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
test_class = clazz,
runtime_deps = runtime_deps + [
"@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
],
jvm_flags = jvm_flags,
**kwargs
)