Skip to content

Specs2 toolchain #1136

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 9 commits into from
Nov 13, 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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ jobs:
- name: "[linux] Dry test rules_scala + latest bazel version"
<<: *linux
env: TEST_SCRIPT=test_rules_scala XDG_CACHE_HOME=~/xdg_cache USE_BAZEL_VERSION=latest
- name: "[linux] Examples"
<<: *linux
env: TEST_SCRIPT=test_examples XDG_CACHE_HOME=~/xdg_cache


allow_failures:
env: TEST_SCRIPT=test_rules_scala XDG_CACHE_HOME=~/xdg_cache USE_BAZEL_VERSION=latest
Expand Down
82 changes: 74 additions & 8 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ junit_repositories()
junit_toolchain()

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

# Specs2 with Junit
load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories", "specs2_junit_toolchain")
specs2_junit_repositories()
specs2_junit_toolchain()
```

### Example to set up JUnit dependencies
### Configuring JUnit dependencies via toolchain

`BUILD` file content in your preferred package:
```starlark
Expand Down Expand Up @@ -48,13 +53,18 @@ declare_deps_provider(
],
)
```

Register toolchain
```starlark
# WORKSPACE
register_toolchains('//my/package:testing_toolchains_with_junit')
```
`junit_classpath_provider` (deps_id `junit_classpath`) is where classpath required for junit tests
is defined.

ScalaTest support can be enabled by configuring a provider with an id `scalatest_classpath`:
### ScalaTest dependencies can be configured by decalring a provider with an id `scalatest_classpath`:

```starlark
# my/package/BUILD
scala_testing_toolchain(
name = "testing_toolchains_with_scalatest",
dep_providers = [
Expand All @@ -80,10 +90,66 @@ declare_deps_provider(
],
)
```

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

Single toolchain can be used to configure multiple testing rules (JUnit 4, ScalaTest).
### Specs2 with Junit support can be configured by declaring providers with
`junit_classpath_provider`, `specs2_classpath_provider`, `specs2_junit_classpath_provider` ids:
```starlark
# my/package/BUILD
scala_testing_toolchain(
name = "testing_toolchains_with_specs2_junit_impl",
dep_providers = [
":junit_classpath_provider",
":specs2_classpath_provider",
":specs2_junit_classpath_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "testing_toolchains_with_specs2_junit",
toolchain = ":testing_toolchains_with_specs2_junit_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",
visibility = ["//visibility:public"],
deps = [
"@my_hamcrest_core",
"@my_junit",
],
)

declare_deps_provider(
name = "specs2_classpath_provider",
deps_id = "specs2_classpath",
visibility = ["//visibility:public"],
deps = [
"@my_specs2_common",
"@my_specs2_core",
"@my_specs2_fp",
"@my_specs2_matcher",
],
)

declare_deps_provider(
name = "specs2_junit_classpath_provider",
deps_id = "specs2_junit_classpath",
visibility = ["//visibility:public"],
deps = [
"@my_specs2_junit",
],
)
```
Register toolchain
```starlark
# WORKSPACE
register_toolchains('//my/package:testing_toolchains_with_specs2_junit')
```
Empty file.
35 changes: 35 additions & 0 deletions examples/testing/specs2_junit_repositories/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
workspace(name = "specs2_junit_repositories")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

skylib_version = "1.0.3"

http_archive(
name = "bazel_skylib",
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
type = "tar.gz",
url = "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{}/bazel-skylib-{}.tar.gz".format(skylib_version, skylib_version),
)

local_repository(
name = "io_bazel_rules_scala",
path = "../../..",
)

load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config()

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

scala_repositories()

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

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories", "specs2_junit_toolchain")

specs2_junit_repositories()

specs2_junit_toolchain()
7 changes: 7 additions & 0 deletions examples/testing/specs2_junit_repositories/example/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_specs2_junit_test")

scala_specs2_junit_test(
name = "example",
srcs = ["Specs2ExampleTest.scala"],
suffixes = ["Test"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example

import org.specs2.mutable.SpecWithJUnit

class Specs2ExampleTest extends SpecWithJUnit {
"works" in {
1 mustEqual 1
}
}
5 changes: 1 addition & 4 deletions specs2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ java_import(
name = "specs2",
jars = [],
exports = [
"@io_bazel_rules_scala_org_specs2_specs2_common",
"@io_bazel_rules_scala_org_specs2_specs2_core",
"@io_bazel_rules_scala_org_specs2_specs2_fp",
"@io_bazel_rules_scala_org_specs2_specs2_matcher",
"//testing/toolchain:specs2_classapath",
],
deps = [
"//scala/private/toolchain_deps:scala_library_classpath",
Expand Down
5 changes: 5 additions & 0 deletions test/shell/test_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ function scalatest_repositories_example() {
(cd examples/testing/scalatest_repositories; bazel test //...)
}

function specs2_junit_repositories_example() {
(cd examples/testing/specs2_junit_repositories; bazel test //...)
}

$runner scalatest_repositories_example
$runner specs2_junit_repositories_example
7 changes: 7 additions & 0 deletions test_examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

test_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/test/shell

. "${test_dir}"/test_examples.sh
1 change: 0 additions & 1 deletion test_rules_scala.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ $runner bazel test //test/... --extra_toolchains="//test_expect_failure/plus_one
. "${test_dir}"/test_toolchain.sh
. "${test_dir}"/test_unused_dependency.sh
. "${test_dir}"/test_twitter_scrooge.sh
. "${test_dir}"/test_examples.sh
40 changes: 40 additions & 0 deletions testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ scala_testing_toolchain(
dep_providers = [
":junit_classpath_provider",
":scalatest_classpath_provider",
":specs2_classpath_provider",
":specs2_junit_classpath_provider",
],
visibility = ["//visibility:public"],
)
Expand All @@ -32,6 +34,23 @@ toolchain(
visibility = ["//visibility:public"],
)

scala_testing_toolchain(
name = "specs2_junit_toolchain_impl",
dep_providers = [
":junit_classpath_provider",
":specs2_classpath_provider",
":specs2_junit_classpath_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "specs2_junit_toolchain",
toolchain = ":specs2_junit_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 = [
Expand Down Expand Up @@ -66,3 +85,24 @@ declare_deps_provider(
"//external:io_bazel_rules_scala/dependency/scala/scalatest/scalatest",
],
)

declare_deps_provider(
name = "specs2_classpath_provider",
deps_id = "specs2_classpath",
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_scala_org_specs2_specs2_common",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why all other deps are in form //external:io_bazel_rules_scala/dependency/... and these are not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It comes from the existing code: https://github.com/bazelbuild/rules_scala/blob/master/specs2/BUILD#L9

It's binded later in https://github.com/bazelbuild/rules_scala/blob/master/specs2/specs2.bzl#L26

Existing binds are subject to be removed later. Currently the goal is to give a way to not use binds.

"@io_bazel_rules_scala_org_specs2_specs2_core",
"@io_bazel_rules_scala_org_specs2_specs2_fp",
"@io_bazel_rules_scala_org_specs2_specs2_matcher",
],
)

declare_deps_provider(
name = "specs2_junit_classpath_provider",
deps_id = "specs2_junit_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/specs2/specs2_junit",
],
)
7 changes: 7 additions & 0 deletions testing/specs2_junit.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//specs2:specs2_junit.bzl", _repositories = "specs2_junit_repositories")

def specs2_junit_repositories():
_repositories()

def specs2_junit_toolchain():
native.register_toolchains("@io_bazel_rules_scala//testing:specs2_junit_toolchain")
12 changes: 12 additions & 0 deletions testing/toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ testing_toolchain_deps(
deps_id = "scalatest_classpath",
visibility = ["//visibility:public"],
)

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

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