Skip to content

Commit ceccd53

Browse files
authored
Add setup testing toolchain macro (#1451)
* Add testing toolchain setup macro * Add starlark annotation to code sample * Remove unused kwargs * Remove leftover args from previous definitions
1 parent 11a2dd5 commit ceccd53

File tree

5 files changed

+206
-254
lines changed

5 files changed

+206
-254
lines changed

docs/testing.md

Lines changed: 75 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -21,135 +21,105 @@ specs2_junit_repositories()
2121
specs2_junit_toolchain()
2222
```
2323

24-
### Configuring JUnit dependencies via toolchain
24+
### Configuring testing dependencies via toolchain
25+
26+
Default dependencies, which come preconfigured with Rules Scala repositories are mostly tailored
27+
towards supporting Rules Scala codebase, and may miss specific versions or libraries for your
28+
usecase. You should prefer configuring dependencies via toolchains.
29+
30+
Test framework dependencies are configured via testing toolchain. For convenience, macro
31+
`setup_scala_testing_toolchain` can be used to define such toolchains.
2532

26-
`BUILD` file content in your preferred package:
2733
```starlark
28-
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
29-
load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain")
34+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain")
35+
```
3036

31-
scala_testing_toolchain(
32-
name = "testing_toolchains_with_junit",
33-
dep_providers = [
34-
":junit_classpath_provider",
35-
],
36-
visibility = ["//visibility:public"],
37-
)
37+
Attributes
3838

39-
toolchain(
40-
name = "testing_toolchain",
41-
toolchain = ":testing_toolchains_with_junit",
42-
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
43-
visibility = ["//visibility:public"],
44-
)
39+
- `name` - toolchain name
40+
- `visibility` - optional, default value is `["//visibility:public"]`
41+
- `scalatest_classpath` - List of ScalaTest deps, can be omitted if ScalaTest rules won't be used.
42+
- `junit_classpath` - List of Junit 4 deps, required for Specs2 and JUnit rules support. Otherwise,
43+
can
44+
be omitted.
45+
- `specs2_classpath` - List of Specs2 deps, requires `specs2_junit_classpath` and `junit_classpath`
46+
to be provided alongside.
47+
- `specs2_junit_classpath` - Specs2 JUnit runner dep, required for Specs2 rules as the use JUnit
48+
runner.
49+
50+
Examples (assumes maven deps are managed with rules_jvm_external):
4551

46-
declare_deps_provider(
47-
name = "junit_classpath_provider",
48-
deps_id = "junit_classpath",
49-
visibility = ["//visibility:public"],
50-
deps = [
51-
"@my_hamcrest_core",
52-
"@my_junit",
52+
#### ScalaTest (flat spec with must matchers)
53+
54+
```starlark
55+
# BUILD
56+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain")
57+
58+
setup_scala_testing_toolchain(
59+
name = "scalatest_toolchain",
60+
scalatest_classpath = [
61+
"@maven//:org_scalactic_scalactic_2_13",
62+
"@maven//:org_scalatest_scalatest_2_13",
63+
"@maven//:org_scalatest_scalatest_compatible",
64+
"@maven//:org_scalatest_scalatest_core_2_13",
65+
"@maven//:org_scalatest_scalatest_flatspec_2_13",
66+
"@maven//:org_scalatest_scalatest_matchers_core_2_13",
67+
"@maven//:org_scalatest_scalatest_mustmatchers_2_13",
5368
],
5469
)
5570
```
56-
Register toolchain
71+
Register the toolchain
5772
```starlark
5873
# WORKSPACE
59-
register_toolchains('//my/package:testing_toolchains_with_junit')
74+
register_toolchains('//:scalatest_toolchain')
6075
```
61-
`junit_classpath_provider` (deps_id `junit_classpath`) is where classpath required for junit tests
62-
is defined.
63-
64-
### ScalaTest dependencies can be configured by declaring a provider with an id `scalatest_classpath`:
6576

77+
#### JUnit 4
6678
```starlark
67-
# my/package/BUILD
68-
scala_testing_toolchain(
69-
name = "testing_toolchains_with_scalatest",
70-
dep_providers = [
71-
":scalatest_classpath_provider",
72-
],
73-
visibility = ["//visibility:public"],
74-
)
75-
76-
toolchain(
77-
name = "testing_toolchain",
78-
toolchain = ":testing_toolchains_with_scalatest",
79-
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
80-
visibility = ["//visibility:public"],
81-
)
82-
83-
declare_deps_provider(
84-
name = "scalatest_classpath_provider",
85-
deps_id = "junit_classpath",
86-
visibility = ["//visibility:public"],
87-
deps = [
88-
"@scalactic",
89-
"@scalatest",
79+
# BUILD
80+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain")
81+
82+
setup_scala_testing_toolchain(
83+
name = "junit_toolchain",
84+
junit_classpath = [
85+
"@maven//:junit_junit",
86+
"@maven//:org_hamcrest_hamcrest_core",
9087
],
9188
)
9289
```
93-
Register toolchain
90+
Register the toolchain
9491
```starlark
9592
# WORKSPACE
96-
register_toolchains('//my/package:testing_toolchains_with_scalatest')
93+
register_toolchains('//:junit_toolchain')
9794
```
9895

99-
### Specs2 with Junit support can be configured by declaring providers with
100-
`junit_classpath_provider`, `specs2_classpath_provider`, `specs2_junit_classpath_provider` ids:
96+
#### Specs2
97+
For Specs2 rules to work, `junit_classpath`, `specs2_junit_classpath` and `specs2_classpath` must
98+
be configured.
10199
```starlark
102-
# my/package/BUILD
103-
scala_testing_toolchain(
104-
name = "testing_toolchains_with_specs2_junit_impl",
105-
dep_providers = [
106-
":junit_classpath_provider",
107-
":specs2_classpath_provider",
108-
":specs2_junit_classpath_provider",
100+
# BUILD
101+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain")
102+
103+
setup_scala_testing_toolchain(
104+
name = "specs2_toolchain",
105+
junit_classpath = [
106+
"@maven//:junit_junit",
107+
"@maven//:org_hamcrest_hamcrest_core",
109108
],
110-
visibility = ["//visibility:public"],
111-
)
112-
113-
toolchain(
114-
name = "testing_toolchains_with_specs2_junit",
115-
toolchain = ":testing_toolchains_with_specs2_junit_impl",
116-
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
117-
visibility = ["//visibility:public"],
118-
)
119-
120-
declare_deps_provider(
121-
name = "junit_classpath_provider",
122-
deps_id = "junit_classpath",
123-
visibility = ["//visibility:public"],
124-
deps = [
125-
"@my_hamcrest_core",
126-
"@my_junit",
109+
specs2_junit_classpath = [
110+
"@maven//:org_specs2_specs2_junit_2_12",
127111
],
128-
)
129-
130-
declare_deps_provider(
131-
name = "specs2_classpath_provider",
132-
deps_id = "specs2_classpath",
133-
visibility = ["//visibility:public"],
134-
deps = [
135-
"@my_specs2_common",
136-
"@my_specs2_core",
137-
"@my_specs2_fp",
138-
"@my_specs2_matcher",
139-
],
140-
)
141-
142-
declare_deps_provider(
143-
name = "specs2_junit_classpath_provider",
144-
deps_id = "specs2_junit_classpath",
145-
visibility = ["//visibility:public"],
146-
deps = [
147-
"@my_specs2_junit",
148-
],
149-
)
112+
specs2_classpath = [
113+
"@maven//:org_specs2_specs2_common_2_12",
114+
"@maven//:org_specs2_specs2_core_2_12",
115+
"@maven//:org_specs2_specs2_fp_2_12",
116+
"@maven//:org_specs2_specs2_junit_2_12",
117+
"@maven//:org_specs2_specs2_matcher_2_12",
118+
]
119+
)
150120
```
151-
Register toolchain
121+
Register the toolchain
152122
```starlark
153123
# WORKSPACE
154-
register_toolchains('//my/package:testing_toolchains_with_specs2_junit')
124+
register_toolchains('//:specs2_toolchain')
155125
```
Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
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")
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain")
32

4-
scala_testing_toolchain(
5-
name = "testing_toolchains_with_all_deps_impl",
6-
dep_providers = [
7-
":junit_classpath_provider",
8-
":scalatest_classpath_provider",
9-
":specs2_classpath_provider",
10-
":specs2_junit_classpath_provider",
11-
],
12-
visibility = ["//visibility:public"],
13-
)
14-
15-
toolchain(
3+
setup_scala_testing_toolchain(
164
name = "testing_toolchain",
17-
toolchain = ":testing_toolchains_with_all_deps_impl",
18-
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
19-
visibility = ["//visibility:public"],
20-
)
21-
22-
declare_deps_provider(
23-
name = "junit_classpath_provider",
24-
deps_id = "junit_classpath",
25-
visibility = ["//visibility:public"],
26-
deps = [
5+
junit_classpath = [
276
"@io_bazel_rules_scala_junit_junit",
287
"@io_bazel_rules_scala_org_hamcrest_hamcrest_core",
298
],
30-
)
31-
32-
declare_deps_provider(
33-
name = "scalatest_classpath_provider",
34-
deps_id = "scalatest_classpath",
35-
visibility = ["//visibility:public"],
36-
deps = [
9+
scalatest_classpath = [
3710
"@io_bazel_rules_scala_scalactic",
3811
"@io_bazel_rules_scala_scalatest",
3912
"@io_bazel_rules_scala_scalatest_compatible",
@@ -47,26 +20,13 @@ declare_deps_provider(
4720
"@io_bazel_rules_scala_scalatest_mustmatchers",
4821
"@io_bazel_rules_scala_scalatest_shouldmatchers",
4922
],
50-
)
51-
52-
declare_deps_provider(
53-
name = "specs2_classpath_provider",
54-
deps_id = "specs2_classpath",
55-
visibility = ["//visibility:public"],
56-
deps = [
23+
specs2_classpath = [
5724
"@io_bazel_rules_scala_org_specs2_specs2_common",
5825
"@io_bazel_rules_scala_org_specs2_specs2_core",
5926
"@io_bazel_rules_scala_org_specs2_specs2_fp",
6027
"@io_bazel_rules_scala_org_specs2_specs2_matcher",
6128
],
62-
)
63-
64-
declare_deps_provider(
65-
name = "specs2_junit_classpath_provider",
66-
deps_id = "specs2_junit_classpath",
67-
visibility = ["//visibility:public"],
68-
deps = [
69-
"@io_bazel_rules_scala//scala:bazel_test_runner_deploy",
29+
specs2_junit_classpath = [
7030
"@io_bazel_rules_scala_org_specs2_specs2_junit",
7131
],
7232
)

scala/scala.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ load(
4242
_scala_test = "scala_test",
4343
_scala_test_suite = "scala_test_suite",
4444
)
45+
load(
46+
"@io_bazel_rules_scala//testing:testing.bzl",
47+
_setup_scala_testing_toolchain = "setup_scala_testing_toolchain",
48+
)
4549

4650
def scala_specs2_junit_test(name, **kwargs):
4751
_scala_junit_test(
@@ -75,4 +79,5 @@ rules_scala_setup = _rules_scala_setup
7579
rules_scala_toolchain_deps_repositories = _rules_scala_toolchain_deps_repositories
7680
scala_test = _scala_test
7781
scala_test_suite = _scala_test_suite
82+
setup_scala_testing_toolchain = _setup_scala_testing_toolchain
7883
setup_scala_toolchain = _setup_scala_toolchain

0 commit comments

Comments
 (0)