forked from JetBrains/bazel-bsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunit5.bzl
152 lines (135 loc) · 5.05 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
load("@rules_java//java:defs.bzl", "java_test")
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test")
# External dependencies & java_junit5_test rule
JUNIT_JUPITER_GROUP_ID = "org.junit.jupiter"
JUNIT_JUPITER_ARTIFACT_ID_LIST = [
"junit-jupiter-api",
"junit-jupiter-engine",
"junit-jupiter-params",
]
JUNIT_PLATFORM_GROUP_ID = "org.junit.platform"
JUNIT_PLATFORM_ARTIFACT_ID_LIST = [
"junit-platform-commons",
"junit-platform-console",
"junit-platform-engine",
"junit-platform-launcher",
"junit-platform-suite-api",
]
JUNIT_EXTRA_DEPENDENCIES = [
("org.apiguardian", "apiguardian-api", "1.0.0"),
("org.opentest4j", "opentest4j", "1.1.1"),
("org.assertj", "assertj-core", "3.22.0"),
("io.kotest", "kotest-assertions-api-jvm", "5.2.2"),
("io.kotest", "kotest-assertions-core-jvm", "5.2.2"),
("io.kotest", "kotest-assertions-shared-jvm", "5.2.2"),
("io.kotest", "kotest-common-jvm", "5.2.2"),
]
def junit_jupiter_java_repositories(
version = "5.8.2"):
"""Imports dependencies for JUnit Jupiter"""
for artifact_id in JUNIT_JUPITER_ARTIFACT_ID_LIST:
jvm_maven_import_external(
name = _format_maven_jar_name(JUNIT_JUPITER_GROUP_ID, artifact_id),
artifact = "%s:%s:%s" % (
JUNIT_JUPITER_GROUP_ID,
artifact_id,
version,
),
server_urls = ["https://repo1.maven.org/maven2"],
licenses = ["notice"], # EPL 2.0 License
)
for t in JUNIT_EXTRA_DEPENDENCIES:
jvm_maven_import_external(
name = _format_maven_jar_name(t[0], t[1]),
artifact = "%s:%s:%s" % t,
server_urls = ["https://repo1.maven.org/maven2"],
licenses = ["notice"], # EPL 2.0 License
)
def junit_platform_java_repositories(
version = "1.8.2"):
"""Imports dependencies for JUnit Platform"""
for artifact_id in JUNIT_PLATFORM_ARTIFACT_ID_LIST:
jvm_maven_import_external(
name = _format_maven_jar_name(JUNIT_PLATFORM_GROUP_ID, artifact_id),
artifact = "%s:%s:%s" % (
JUNIT_PLATFORM_GROUP_ID,
artifact_id,
version,
),
server_urls = ["https://repo1.maven.org/maven2"],
licenses = ["notice"], # EPL 2.0 License
)
def java_junit5_test(name, srcs, test_package, deps = [], runtime_deps = [], **kwargs):
FILTER_KWARGS = [
"main_class",
"use_testrunner",
"args",
]
for arg in FILTER_KWARGS:
if arg in kwargs.keys():
kwargs.pop(arg)
junit_console_args = []
if test_package:
junit_console_args += ["--select-package", test_package]
else:
fail("must specify 'test_package'")
java_test(
name = name,
srcs = srcs,
use_testrunner = False,
main_class = "org.junit.platform.console.ConsoleLauncher",
args = junit_console_args,
deps = deps + [
_format_maven_jar_dep_name(JUNIT_JUPITER_GROUP_ID, artifact_id)
for artifact_id in JUNIT_JUPITER_ARTIFACT_ID_LIST
] + [
_format_maven_jar_dep_name(JUNIT_PLATFORM_GROUP_ID, "junit-platform-suite-api"),
] + [
_format_maven_jar_dep_name(t[0], t[1])
for t in JUNIT_EXTRA_DEPENDENCIES
],
runtime_deps = runtime_deps + [
_format_maven_jar_dep_name(JUNIT_PLATFORM_GROUP_ID, artifact_id)
for artifact_id in JUNIT_PLATFORM_ARTIFACT_ID_LIST
],
**kwargs
)
def kt_junit5_test(name, srcs, test_package, deps = [], runtime_deps = [], **kwargs):
FILTER_KWARGS = [
"main_class",
"use_testrunner",
"args",
]
for arg in FILTER_KWARGS:
if arg in kwargs.keys():
kwargs.pop(arg)
junit_console_args = []
if test_package:
junit_console_args += ["--select-package", test_package]
else:
fail("must specify 'test_package'")
kt_jvm_test(
name = name,
srcs = srcs,
main_class = "org.junit.platform.console.ConsoleLauncher",
args = junit_console_args,
deps = deps + [
_format_maven_jar_dep_name(JUNIT_JUPITER_GROUP_ID, artifact_id)
for artifact_id in JUNIT_JUPITER_ARTIFACT_ID_LIST
] + [
_format_maven_jar_dep_name(JUNIT_PLATFORM_GROUP_ID, "junit-platform-suite-api"),
] + [
_format_maven_jar_dep_name(t[0], t[1])
for t in JUNIT_EXTRA_DEPENDENCIES
],
runtime_deps = runtime_deps + [
_format_maven_jar_dep_name(JUNIT_PLATFORM_GROUP_ID, artifact_id)
for artifact_id in JUNIT_PLATFORM_ARTIFACT_ID_LIST
],
**kwargs
)
def _format_maven_jar_name(group_id, artifact_id):
return ("%s_%s" % (group_id, artifact_id)).replace(".", "_").replace("-", "_")
def _format_maven_jar_dep_name(group_id, artifact_id):
return "@%s//jar" % _format_maven_jar_name(group_id, artifact_id)