Skip to content

Commit ddaeb04

Browse files
authored
Expose portions of the java_junit5_test macro logic via //java:defs.bzl (#382)
# Summary Take portions of the `java_junit5_test` macro and expose it as a method and constant via `//java:defs.bzl`. ## Problem The `java_junit5_test` macro has some internal logic for preparing `java_test` to work with `JUnit5Runner`. Creating a test runner that wraps `JUnit5Runner` would have to duplicate that logic in order to expose the correct functionality. ## Solution Take the logic out of the macro and place it into a function that can be called by any macro when creating the `java_test` target. ## Testing Added test cases to ensure the logic is available to be imported and nominally returns the correct value. The test does not cover all branches of the jvm_flags logic for `java_junit5_test`.
1 parent 33472ef commit ddaeb04

File tree

5 files changed

+150
-21
lines changed

5 files changed

+150
-21
lines changed

examples/tests_and_lints/MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ use_repo(
4747
maven,
4848
"maven",
4949
)
50+
51+
register_toolchains(
52+
"@@bazel_skylib//toolchains/unittest:cmd_toolchain",
53+
"@@bazel_skylib//toolchains/unittest:bash_toolchain",
54+
)

examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_test", "java_junit5_test")
22
load("@rules_jvm_external//:defs.bzl", "artifact")
3+
load(":java_junit5_test_test_suite.bzl", "java_junit5_test_test_suite")
34

45
java_junit5_test(
56
name = "ExampleTest",
@@ -16,6 +17,10 @@ java_junit5_test(
1617
],
1718
)
1819

20+
java_junit5_test_test_suite(
21+
name = "java_junit5_test_test",
22+
)
23+
1924
checkstyle_test(
2025
name = "example_test_checkstyle",
2126
srcs = ["ExampleTest.java"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
2+
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test", "junit5_jvm_flags")
3+
load("@rules_java//java:java_test.bzl", "java_test")
4+
5+
TargetInfo = provider(
6+
doc = "Information relating to the target under test.",
7+
fields = ["attr"],
8+
)
9+
10+
def _target_info_aspect_impl(target, ctx):
11+
return TargetInfo(
12+
attr = ctx.rule.attr,
13+
)
14+
15+
target_info_aspect = aspect(
16+
implementation = _target_info_aspect_impl,
17+
)
18+
19+
def _attr_string_value_test_impl(ctx):
20+
env = analysistest.begin(ctx)
21+
target_under_test = analysistest.target_under_test(env)
22+
23+
asserts.equals(env, ctx.attr.check_value, getattr(target_under_test[TargetInfo].attr, ctx.attr.check_name))
24+
25+
return analysistest.end(env)
26+
27+
attr_string_value_test = analysistest.make(
28+
_attr_string_value_test_impl,
29+
extra_target_under_test_aspects = [target_info_aspect],
30+
attrs = {
31+
"check_name": attr.string(mandatory = True),
32+
"check_value": attr.string(mandatory = True),
33+
},
34+
)
35+
36+
def _attr_string_list_value_test_impl(ctx):
37+
env = analysistest.begin(ctx)
38+
target_under_test = analysistest.target_under_test(env)
39+
40+
asserts.equals(env, ctx.attr.check_value, getattr(target_under_test[TargetInfo].attr, ctx.attr.check_name))
41+
42+
return analysistest.end(env)
43+
44+
attr_string_list_value_test = analysistest.make(
45+
_attr_string_list_value_test_impl,
46+
extra_target_under_test_aspects = [target_info_aspect],
47+
attrs = {
48+
"check_name": attr.string(mandatory = True),
49+
"check_value": attr.string_list(mandatory = True),
50+
},
51+
)
52+
53+
def custom_junit5_test(name, **kwargs):
54+
jvm_flags = junit5_jvm_flags(
55+
jvm_flags = kwargs.pop("jvm_flags", []),
56+
include_tags = kwargs.pop("include_tags", []),
57+
exclude_tags = kwargs.pop("exclude_tags", []),
58+
include_engines = kwargs.pop("include_engines", []),
59+
exclude_engines = kwargs.pop("exclude_engines", []),
60+
)
61+
62+
java_test(
63+
name = name,
64+
main_class = "com.example.CustomMainClass",
65+
jvm_flags = jvm_flags,
66+
**kwargs
67+
)
68+
69+
def java_junit5_test_test_suite(name):
70+
java_junit5_test(
71+
name = "StandardMainClassTest",
72+
tags = ["manual"],
73+
)
74+
75+
custom_junit5_test(
76+
name = "CustomMainClassTest",
77+
include_tags = ["include_junit5_test"],
78+
tags = ["manual"],
79+
)
80+
81+
attr_string_value_test(
82+
name = "custom_main_class_test",
83+
target_under_test = ":CustomMainClassTest",
84+
check_name = "main_class",
85+
check_value = "com.example.CustomMainClass",
86+
)
87+
88+
attr_string_list_value_test(
89+
name = "custom_jvm_flags_test",
90+
target_under_test = ":CustomMainClassTest",
91+
check_name = "jvm_flags",
92+
check_value = ["-DJUNIT5_INCLUDE_TAGS=include_junit5_test", "-Djava.security.manager=allow"],
93+
)
94+
95+
attr_string_value_test(
96+
name = "standard_main_class_test",
97+
target_under_test = ":StandardMainClassTest",
98+
check_name = "main_class",
99+
check_value = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
100+
)
101+
102+
native.test_suite(
103+
name = name,
104+
tests = [
105+
":custom_main_class_test",
106+
":standard_main_class_test",
107+
],
108+
)

java/defs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ load("//java/private:java_test_suite.bzl", _java_test_suite = "java_test_suite")
99
load(
1010
"//java/private:junit5.bzl",
1111
_JUNIT5_DEPS = "JUNIT5_DEPS",
12+
_JUNIT5_RUNTIME_DEPS = "JUNIT5_RUNTIME_DEPS",
1213
_JUNIT5_VINTAGE_DEPS = "JUNIT5_VINTAGE_DEPS",
1314
_java_junit5_test = "java_junit5_test",
1415
_junit5_deps = "junit5_deps",
16+
_junit5_jvm_flags = "junit5_jvm_flags",
1517
_junit5_vintage_deps = "junit5_vintage_deps",
1618
)
1719
load(
@@ -41,8 +43,10 @@ java_junit5_test = _java_junit5_test
4143
java_test = _java_test
4244
java_test_suite = _java_test_suite
4345
junit5_deps = _junit5_deps
46+
junit5_jvm_flags = _junit5_jvm_flags
4447
junit5_vintage_deps = _junit5_vintage_deps
4548
JUNIT5_DEPS = _JUNIT5_DEPS
49+
JUNIT5_RUNTIME_DEPS = _JUNIT5_RUNTIME_DEPS
4650
JUNIT5_VINTAGE_DEPS = _JUNIT5_VINTAGE_DEPS
4751
pmd_binary = _pmd_binary
4852
pmd_ruleset = _pmd_ruleset

java/private/junit5.bzl

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ load("@rules_jvm_external//:defs.bzl", "DEFAULT_REPOSITORY_NAME", "artifact")
22
load("//java/private:library.bzl", "java_test")
33
load("//java/private:package.bzl", "get_package_name")
44

5+
JUNIT5_RUNTIME_DEPS = [
6+
"@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
7+
]
8+
59
def junit5_deps(repository_name = DEFAULT_REPOSITORY_NAME):
610
return [
711
artifact("org.junit.jupiter:junit-jupiter-engine", repository_name),
@@ -14,6 +18,28 @@ def junit5_vintage_deps(repository_name = DEFAULT_REPOSITORY_NAME):
1418
artifact("org.junit.vintage:junit-vintage-engine", repository_name),
1519
]
1620

21+
def junit5_jvm_flags(jvm_flags, include_tags = [], exclude_tags = [], include_engines = [], exclude_engines = []):
22+
if include_tags:
23+
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_TAGS=" + ",".join(include_tags)]
24+
25+
if exclude_tags:
26+
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_TAGS=" + ",".join(exclude_tags)]
27+
28+
if include_engines:
29+
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_ENGINES=%s" % ",".join(include_engines)]
30+
31+
if exclude_engines:
32+
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_ENGINES=%s" % ",".join(exclude_engines)]
33+
34+
security_manager_flag_seen = False
35+
for flag in jvm_flags:
36+
if flag.startswith("-Djava.security.manager="):
37+
security_manager_flag_seen = True
38+
if not security_manager_flag_seen:
39+
jvm_flags = jvm_flags + ["-Djava.security.manager=allow"]
40+
41+
return jvm_flags
42+
1743
"""Dependencies typically required by JUnit 5 tests.
1844
1945
See `java_junit5_test` for more details.
@@ -79,32 +105,13 @@ def java_junit5_test(
79105
else:
80106
clazz = get_package_name(package_prefixes) + name
81107

82-
if include_tags:
83-
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_TAGS=" + ",".join(include_tags)]
84-
85-
if exclude_tags:
86-
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_TAGS=" + ",".join(exclude_tags)]
87-
88-
if include_engines:
89-
jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_ENGINES=%s" % ",".join(include_engines)]
90-
91-
if exclude_engines:
92-
jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_ENGINES=%s" % ",".join(exclude_engines)]
93-
94-
security_manager_flag_seen = False
95-
for flag in jvm_flags:
96-
if flag.startswith("-Djava.security.manager="):
97-
security_manager_flag_seen = True
98-
if not security_manager_flag_seen:
99-
jvm_flags = jvm_flags + ["-Djava.security.manager=allow"]
108+
jvm_flags = junit5_jvm_flags(jvm_flags, include_tags, exclude_tags, include_engines, exclude_engines)
100109

101110
java_test(
102111
name = name,
103112
main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
104113
test_class = clazz,
105-
runtime_deps = runtime_deps + [
106-
"@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
107-
],
114+
runtime_deps = runtime_deps + JUNIT5_RUNTIME_DEPS,
108115
jvm_flags = jvm_flags,
109116
**kwargs
110117
)

0 commit comments

Comments
 (0)