Skip to content

ScalaTest deps to toolchain #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ load("//specs2:specs2_junit.bzl", "specs2_junit_repositories")

specs2_junit_repositories()

register_toolchains("//testing:testing_toolchain")

load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories")

scalafmt_default_config()
Expand Down
50 changes: 47 additions & 3 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
## Testing toolchain configuration

Toolchain type `testing_toolchain_type` is used to set up test dependencies.
Toolchain type `testing_toolchain_type` is used to set up test dependencies. You can customize
test dependencies by defining a custom testing toolchain.

In your `WORKSPACE` default repositories and toolchains can be loaded via:
```starlark
# JUnit 4
load("@io_bazel_rules_scala//testing:junit.bzl", "junit_repositories", "junit_toolchain")
junit_repositories()
junit_toolchain()

# ScalaTest
load(""@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")
scalatest_repositories()
scalatest_toolchain()
```

### Example to set up JUnit dependencies

`BUILD` file content in your prefered package:
`BUILD` file content in your preferred package:
```starlark
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain")
Expand Down Expand Up @@ -38,8 +52,38 @@ declare_deps_provider(
`junit_classpath_provider` (deps_id `junit_classpath`) is where classpath required for junit tests
is defined.

Toolchain must be registerd in your `WORKSPACE` file:
ScalaTest support can be enabled by configuring a provider with an id `scalatest_classpath`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to this change but this doc seems to suggest that in order to use scalatest I need to configure it. Why doesn't it show the easy defaults way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understood your comment, there are no defaults when setting up custom toolchain

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is docs/testing.md only about custom testing toolchains?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, but I think it requires a lot more effort to rethink/rewrite whole setting up rules scala docs. I think it should be done outside this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point- configuring custom deps became more complicated (but more powerful and we're ok with it).
If the readme before caused people to think they need to do X to use it now it seems like one needs to do 5X.
Can you please add a note here clarifying that this is needed only for the custom case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reorganized doc and added a note about custom deps

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I appreciate it


```starlark
scala_testing_toolchain(
name = "testing_toolchains_with_scalatest",
dep_providers = [
":scalatest_classpath_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "testing_toolchain",
toolchain = ":testing_toolchains_with_scalatest",
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
visibility = ["//visibility:public"],
)

declare_deps_provider(
name = "scalatest_classpath_provider",
deps_id = "junit_classpath",
visibility = ["//visibility:public"],
deps = [
"@scalactic",
"@scalatest",
],
)
```

Toolchain must be registered in your `WORKSPACE` file:
```starlark
register_toolchains('//my/package:testing_toolchain')
```

Single toolchain can be used to configure multiple testing rules (JUnit 4, ScalaTest).
2 changes: 0 additions & 2 deletions junit/junit.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ def junit_repositories(maven_servers = _default_maven_server_urls()):
name = "io_bazel_rules_scala/dependency/hamcrest/hamcrest_core",
actual = "@io_bazel_rules_scala_org_hamcrest_hamcrest_core//jar",
)

native.register_toolchains("@io_bazel_rules_scala//testing:testing_toolchain")
4 changes: 2 additions & 2 deletions scala/private/rules/scala_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ _scala_test_attrs = {
"jvm_flags": attr.string_list(),
"_scalatest": attr.label(
default = Label(
"//external:io_bazel_rules_scala/dependency/scalatest/scalatest",
"@io_bazel_rules_scala//testing/toolchain:scalatest_classpath",
),
),
"_scalatest_runner": attr.label(
Expand All @@ -84,7 +84,7 @@ _test_resolve_deps = {
"@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath",
),
Label(
"//external:io_bazel_rules_scala/dependency/scalatest/scalatest",
"@io_bazel_rules_scala//testing/toolchain:scalatest_classpath",
),
],
allow_files = False,
Expand Down
8 changes: 2 additions & 6 deletions scala/scalatest/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package(default_visibility = ["//visibility:public"])

load("//scala:scala_import.bzl", "scala_import")

scala_import(
alias(
name = "scalatest",
jars = [],
exports = [
"//external:io_bazel_rules_scala/dependency/scala/scalactic/scalactic",
"//external:io_bazel_rules_scala/dependency/scala/scalatest/scalatest",
],
actual = "//testing/toolchain:scalatest_classpath",
)
2 changes: 1 addition & 1 deletion scala/support/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ scala_library(
],
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scalatest/scalatest",
"//scala/private/toolchain_deps:scala_xml",
"//testing/toolchain:scalatest_classpath",
],
)
2 changes: 1 addition & 1 deletion specs2/specs2_junit.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load(
"specs2_repositories",
"specs2_version",
)
load("//junit:junit.bzl", "junit_repositories")
load("//testing:junit.bzl", "junit_repositories")
load(
"//scala:scala_cross_version.bzl",
_default_maven_server_urls = "default_maven_server_urls",
Expand Down
2 changes: 1 addition & 1 deletion src/java/io/bazel/rulesscala/scala_test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ java_library(
srcs = ["Runner.java"],
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scalatest/scalatest",
"//testing/toolchain:scalatest_classpath",
"@bazel_tools//tools/java/runfiles",
],
)
2 changes: 1 addition & 1 deletion test/aspect/aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _rule_impl(ctx):
"scala_test": [
"//test/aspect:scala_test",
"//scala/private/toolchain_deps:scala_library_classpath",
"@io_bazel_rules_scala//scala/scalatest:scalatest",
"//testing/toolchain:scalatest_classpath",
],
"scala_junit_test": [
"//test/aspect:scala_junit_test",
Expand Down
2 changes: 2 additions & 0 deletions test_version/WORKSPACE.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ load("@io_bazel_rules_scala//specs2:specs2_junit.bzl", "specs2_junit_repositorie

specs2_junit_repositories(scala_version)

register_toolchains("@io_bazel_rules_scala//testing:testing_toolchain")

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")

scala_register_unused_deps_toolchains()
Expand Down
41 changes: 41 additions & 0 deletions testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ scala_testing_toolchain(
name = "testing_toolchains_with_all_deps_impl",
dep_providers = [
":junit_classpath_provider",
":scalatest_classpath_provider",
],
visibility = ["//visibility:public"],
)
Expand All @@ -16,6 +17,36 @@ toolchain(
visibility = ["//visibility:public"],
)

scala_testing_toolchain(
name = "scalatest_toolchain_impl",
dep_providers = [
":scalatest_classpath_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "scalatest_toolchain",
toolchain = ":scalatest_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
visibility = ["//visibility:public"],
)

scala_testing_toolchain(
name = "junit_toolchain_impl",
dep_providers = [
":junit_classpath_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "junit_toolchain",
toolchain = ":junit_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
visibility = ["//visibility:public"],
)

declare_deps_provider(
name = "junit_classpath_provider",
deps_id = "junit_classpath",
Expand All @@ -25,3 +56,13 @@ declare_deps_provider(
"//external:io_bazel_rules_scala/dependency/junit/junit",
],
)

declare_deps_provider(
name = "scalatest_classpath_provider",
deps_id = "scalatest_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scalactic/scalactic",
"//external:io_bazel_rules_scala/dependency/scala/scalatest/scalatest",
],
)
7 changes: 7 additions & 0 deletions testing/junit.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//junit:junit.bzl", _repositories = "junit_repositories")

def junit_repositories():
_repositories()

def junit_toolchain():
native.register_toolchains("@io_bazel_rules_scala//testing:junit_toolchain")
6 changes: 6 additions & 0 deletions testing/scalatest.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def scalatest_repositories():
# currently ScalaTest dependencies are already loaded via //scala:scala.bzl#scala_repositories()
pass

def scalatest_toolchain():
native.register_toolchain("//testing:scalatest_toolchain")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liucijus Is this supposed to be register_toolchains?
Added the following in my WORKSPACE file:

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_toolchain")

scalatest_toolchain()

and it errored out with:

Error: no native function or rule 'register_toolchain'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting, here's the fix: #1130

6 changes: 6 additions & 0 deletions testing/toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ testing_toolchain_deps(
deps_id = "junit_classpath",
visibility = ["//visibility:public"],
)

testing_toolchain_deps(
name = "scalatest_classpath",
deps_id = "scalatest_classpath",
visibility = ["//visibility:public"],
)