Skip to content

Commit f0c8d07

Browse files
authored
Testing toolchain and Junit deps (#1102)
* Add testing toolchain with Junit deps * Add docs for junit * Update junit related aspect test deps
1 parent e0b4f1e commit f0c8d07

File tree

11 files changed

+131
-24
lines changed

11 files changed

+131
-24
lines changed

docs/testing.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Testing toolchain configuration
2+
3+
Toolchain type `testing_toolchain_type` is used to set up test dependencies.
4+
5+
### Example to set up JUnit dependencies
6+
7+
`BUILD` file content in your prefered package:
8+
```starlark
9+
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
10+
load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain")
11+
12+
scala_testing_toolchain(
13+
name = "testing_toolchains_with_junit",
14+
dep_providers = [
15+
":junit_classpath_provider",
16+
],
17+
visibility = ["//visibility:public"],
18+
)
19+
20+
toolchain(
21+
name = "testing_toolchain",
22+
toolchain = ":testing_toolchains_with_junit",
23+
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
24+
visibility = ["//visibility:public"],
25+
)
26+
27+
declare_deps_provider(
28+
name = "junit_classpath_provider",
29+
deps_id = "junit_classpath",
30+
visibility = ["//visibility:public"],
31+
deps = [
32+
"@my_hamcrest_core",
33+
"@my_junit",
34+
],
35+
)
36+
```
37+
38+
`junit_classpath_provider` (deps_id `junit_classpath`) is where classpath required for junit tests
39+
is defined.
40+
41+
Toolchain must be registerd in your `WORKSPACE` file:
42+
```starlark
43+
register_toolchains('//my/package:testing_toolchain')
44+
```
45+

junit/junit.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ def junit_repositories(maven_servers = _default_maven_server_urls()):
3131
name = "io_bazel_rules_scala/dependency/hamcrest/hamcrest_core",
3232
actual = "@io_bazel_rules_scala_org_hamcrest_hamcrest_core//jar",
3333
)
34+
35+
native.register_toolchains("@io_bazel_rules_scala//testing:testing_toolchain")

scala/private/phases/phase_collect_jars.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def phase_collect_jars_macro_library(ctx, p):
3333
def phase_collect_jars_junit_test(ctx, p):
3434
args = struct(
3535
extra_deps = [
36-
ctx.attr._junit,
37-
ctx.attr._hamcrest,
36+
ctx.attr._junit_classpath,
3837
ctx.attr.suite_label,
3938
ctx.attr._bazel_test_runner,
4039
],

scala/private/phases/phase_compile.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ def phase_compile_junit_test(ctx, p):
6262
args = struct(
6363
buildijar = False,
6464
implicit_junit_deps_needed_for_java_compilation = [
65-
ctx.attr._junit,
66-
ctx.attr._hamcrest,
65+
ctx.attr._junit_classpath,
6766
],
6867
unused_dependency_checker_ignored_targets = [
6968
target.label
7069
for target in p.scalac_provider.default_classpath +
7170
ctx.attr.unused_dependency_checker_ignored_targets
7271
] + [
73-
ctx.attr._junit.label,
74-
ctx.attr._hamcrest.label,
72+
ctx.attr._junit_classpath.label,
7573
ctx.attr.suite_label.label,
7674
ctx.attr._bazel_test_runner.label,
7775
],

scala/private/rules/scala_junit_test.bzl

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,8 @@ _scala_junit_test_attrs = {
6969
mandatory = False,
7070
),
7171
"jvm_flags": attr.string_list(),
72-
"_junit": attr.label(
73-
default = Label(
74-
"//external:io_bazel_rules_scala/dependency/junit/junit",
75-
),
76-
),
77-
"_hamcrest": attr.label(
78-
default = Label(
79-
"//external:io_bazel_rules_scala/dependency/hamcrest/hamcrest_core",
80-
),
72+
"_junit_classpath": attr.label(
73+
default = Label("@io_bazel_rules_scala//testing/toolchain:junit_classpath"),
8174
),
8275
"_bazel_test_runner": attr.label(
8376
default = Label(
@@ -93,10 +86,7 @@ _junit_resolve_deps = {
9386
Label(
9487
"@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath",
9588
),
96-
Label("//external:io_bazel_rules_scala/dependency/junit/junit"),
97-
Label(
98-
"//external:io_bazel_rules_scala/dependency/hamcrest/hamcrest_core",
99-
),
89+
Label("@io_bazel_rules_scala//testing/toolchain:junit_classpath"),
10090
],
10191
allow_files = False,
10292
),

src/java/io/bazel/rulesscala/test_discovery/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ scala_library(
77
"FilteredRunnerBuilder.scala",
88
],
99
visibility = ["//visibility:public"],
10-
deps = ["//external:io_bazel_rules_scala/dependency/junit/junit"],
10+
deps = ["@io_bazel_rules_scala//testing/toolchain:junit_classpath"],
1111
)

test/aspect/aspect.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ def _rule_impl(ctx):
3333
"scala_junit_test": [
3434
"//test/aspect:scala_junit_test",
3535
"//scala/private/toolchain_deps:scala_library_classpath",
36-
"@io_bazel_rules_scala_junit_junit//:io_bazel_rules_scala_junit_junit",
37-
"@io_bazel_rules_scala_org_hamcrest_hamcrest_core//:io_bazel_rules_scala_org_hamcrest_hamcrest_core",
36+
"//testing/toolchain:junit_classpath",
3837
],
3938
"scala_specs2_junit_test": [
4039
"//scala/private/toolchain_deps:scala_library_classpath",
4140
"//test/aspect:scala_specs2_junit_test",
42-
"@io_bazel_rules_scala_junit_junit//:io_bazel_rules_scala_junit_junit",
43-
"@io_bazel_rules_scala_org_hamcrest_hamcrest_core//:io_bazel_rules_scala_org_hamcrest_hamcrest_core",
41+
"//testing/toolchain:junit_classpath",
4442
# From specs2/specs2.bzl:specs2_dependencies()
4543
"@io_bazel_rules_scala//specs2:specs2",
4644
"@io_bazel_rules_scala_org_specs2_specs2_common//:io_bazel_rules_scala_org_specs2_specs2_common",

testing/BUILD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
2+
load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain")
3+
4+
scala_testing_toolchain(
5+
name = "testing_toolchains_with_all_deps_impl",
6+
dep_providers = [
7+
":junit_classpath_provider",
8+
],
9+
visibility = ["//visibility:public"],
10+
)
11+
12+
toolchain(
13+
name = "testing_toolchain",
14+
toolchain = ":testing_toolchains_with_all_deps_impl",
15+
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
16+
visibility = ["//visibility:public"],
17+
)
18+
19+
declare_deps_provider(
20+
name = "junit_classpath_provider",
21+
deps_id = "junit_classpath",
22+
visibility = ["//visibility:public"],
23+
deps = [
24+
"//external:io_bazel_rules_scala/dependency/hamcrest/hamcrest_core",
25+
"//external:io_bazel_rules_scala/dependency/junit/junit",
26+
],
27+
)

testing/toolchain/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@io_bazel_rules_scala//testing/toolchain:toolchain_deps.bzl", "testing_toolchain_deps")
2+
3+
toolchain_type(
4+
name = "testing_toolchain_type",
5+
visibility = ["//visibility:public"],
6+
)
7+
8+
testing_toolchain_deps(
9+
name = "junit_classpath",
10+
deps_id = "junit_classpath",
11+
visibility = ["//visibility:public"],
12+
)

testing/toolchain/toolchain.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("//scala:providers.bzl", _DepsInfo = "DepsInfo")
2+
3+
def _scala_toolchain_impl(ctx):
4+
toolchain = platform_common.ToolchainInfo(
5+
dep_providers = ctx.attr.dep_providers,
6+
)
7+
return [toolchain]
8+
9+
scala_testing_toolchain = rule(
10+
_scala_toolchain_impl,
11+
attrs = {
12+
"dep_providers": attr.label_list(
13+
default = [
14+
],
15+
providers = [_DepsInfo],
16+
),
17+
},
18+
)

testing/toolchain/toolchain_deps.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load(
2+
"//scala/private/toolchain_deps:toolchain_deps.bzl",
3+
"expose_toolchain_deps",
4+
"java_info_for_deps",
5+
)
6+
7+
_toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type"
8+
9+
def _testing_toolchain_deps(ctx):
10+
return expose_toolchain_deps(ctx, _toolchain_type)
11+
12+
testing_toolchain_deps = rule(
13+
implementation = _testing_toolchain_deps,
14+
attrs = {
15+
"deps_id": attr.string(mandatory = True),
16+
},
17+
toolchains = [_toolchain_type],
18+
)

0 commit comments

Comments
 (0)