diff --git a/WORKSPACE b/WORKSPACE index aa10fc7cd..03edea1ae 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -95,6 +95,14 @@ scala_maven_import_external( jar_sha256 = "972139718abc8a4893fa78cba8cf7b2c903f35c97aaf44fa3031b0669948b480", ) +scala_maven_import_external( + name = "junit_4_11", + licenses = ["restricted"], # Eclipse Public License - v 1.0 + artifact = "junit:junit:4.11", + server_urls = ["http://central.maven.org/maven2"], + jar_sha256 = "90a8e1603eeca48e7e879f3afbc9560715322985f39a274f6f6070b43f9d06fe", +) + # bazel's java_import_external has been altered in rules_scala to be a macro based on jvm_import_external # in order to allow for other jvm-language imports (e.g. scala_import) # the 3rd-party dependency below is using the java_import_external macro @@ -108,3 +116,38 @@ java_import_external( neverlink = True, generated_linkable_rule_name="linkable_org_apache_commons_commons_lang_3_5_without_file", ) + +build_bazel_integration_testing_version="36ffe6fe0f4bb727c1fe34209a8d6fd33d8d0d8e" # update this as needed +http_archive( + name = "build_bazel_integration_testing", + url = "https://github.com/bazelbuild/bazel-integration-testing/archive/%s.zip"%build_bazel_integration_testing_version, + strip_prefix = "bazel-integration-testing-" + build_bazel_integration_testing_version, +) +load("@build_bazel_integration_testing//tools:repositories.bzl", "bazel_binaries") +bazel_binaries(versions = ["0.12.0"]) +load("@build_bazel_integration_testing//tools:bazel_java_integration_test.bzl", "bazel_java_integration_test_deps") +bazel_java_integration_test_deps() + +load("@build_bazel_integration_testing//tools:import.bzl", "bazel_external_dependency_archive") +BAZEL_JAVA_LAUNCHER_VERSION = "0.4.5" +java_stub_template_url = ("raw.githubusercontent.com/bazelbuild/bazel/" + + BAZEL_JAVA_LAUNCHER_VERSION + + "/src/main/java/com/google/devtools/build/lib/bazel/rules/java/" + + "java_stub_template.txt") + +rules_scala_version = "a8ef632b1b020cdf2c215ecd9fcfa84bc435efcb" +bazel_external_dependency_archive( + name = "integration_test_ext", + srcs = { + "fe61287087b471a74c81625d9d341224fb5ffc6c9358e51c7368182b7b6f112c": [ + "https://github.com/bazelbuild/rules_scala/archive/%s.zip" % rules_scala_version, + ], + "f09d06d55cd25168427a323eb29d32beca0ded43bec80d76fc6acd8199a24489": [ + "https://mirror.bazel.build/%s" % java_stub_template_url, + "https://%s" % java_stub_template_url + ], + "12037ca64c68468e717e950f47fc77d5ceae5e74e3bdca56f6d02fd5bfd6900b": [ + "https://downloads.lightbend.com/scala/2.11.11/scala-2.11.11.tgz" + ], + }, +) diff --git a/test/BUILD b/test/BUILD index dc533896e..dd4737a91 100644 --- a/test/BUILD +++ b/test/BUILD @@ -14,6 +14,8 @@ load("//scala:scala.bzl", load("//scala_proto:scala_proto.bzl", "scalapb_proto_library") +load("@build_bazel_integration_testing//tools:bazel_java_integration_test.bzl", "bazel_java_integration_test") + # The examples below show how to combine Scala and Java rules. # ScalaBinary is the Scala equivalent of JavaBinary. @@ -364,12 +366,29 @@ scala_junit_test( print_discovered_classes = True ) -scala_junit_test( - name = "JunitFiltersClassesWithoutTests", - srcs = ["src/main/scala/scala/test/junit/JunitNoTests.scala"], - size = "small", - suffixes = ["Test"], - print_discovered_classes = True +scala_library( + name = "rules_scala_it_junit", + srcs = ["src/main/scala/scala/test/junit/RulesScalaIntegrationTests.scala"], + + deps = [ + "@junit_4_11//jar", + "@build_bazel_integration_testing//java/build/bazel/tests/integration:workspace_driver", + "@build_bazel_integration_testing//java/build/bazel/tests/integration:integration", + ], + testonly = 1 +) + +bazel_java_integration_test ( + name = "RulesScalaIntegrationTests", + versions = ["0.12.0"], + test_class = "scala.test.junit.RulesScalaIntegrationTests", + srcs = [], + runtime_deps = [":rules_scala_it_junit"], + external_deps = [ + "@integration_test_ext", + ], + tags = ["IT"], + ) scala_junit_test( diff --git a/test/src/main/scala/scala/test/junit/JunitNoTests.scala b/test/src/main/scala/scala/test/junit/JunitNoTests.scala deleted file mode 100644 index 5f7acb277..000000000 --- a/test/src/main/scala/scala/test/junit/JunitNoTests.scala +++ /dev/null @@ -1,11 +0,0 @@ -package scala.test.junit - -import org.junit.Test - -class SomeHelpreForTest - -class SingleTestSoTargetWillNotFailDueToNoTestsTest { - @Test - def someTest: Unit = - println("passing") -} \ No newline at end of file diff --git a/test/src/main/scala/scala/test/junit/RulesScalaIntegrationTests.scala b/test/src/main/scala/scala/test/junit/RulesScalaIntegrationTests.scala new file mode 100644 index 000000000..a66cb2536 --- /dev/null +++ b/test/src/main/scala/scala/test/junit/RulesScalaIntegrationTests.scala @@ -0,0 +1,87 @@ +package scala.test.junit + +import build.bazel.tests.integration.BazelBaseTestCase +import org.junit.{Assert, Test} +import scala.collection.JavaConverters._ + +class RulesScalaIntegrationTests extends BazelBaseTestCase { + + private val NoTestsFoundExitCode = 3 + private val NoTestsFoundLabel = "no_tests_found" + + @Test + def singleTestSoTargetWillNotFailDueToNoTestsTest: Unit = { + givenRulesScalaWorkspaceFile() + givenBuildFile() + givenEmptyTestSuitFile() + + val cmd = driver.bazelCommand("test", "//...").build() + val exitCode = cmd.run() + val errorOutput = cmd.getErrorLines.asScala + + assertNoTestsFoundCorrectOutput(errorOutput, exitCode) + } + + private def assertNoTestsFoundCorrectOutput(output: Seq[String], exitCode: Int): Unit = { + Assert.assertEquals(exitCode, NoTestsFoundExitCode ) + val hasCorrectOutput = output.exists(line => { + line.contains("FAIL") && line.contains(NoTestsFoundLabel) + }) + + Assert.assertTrue(hasCorrectOutput) + } + + private def givenRulesScalaWorkspaceFile(): Unit = { + val workspace = + """ + |rules_scala_version="a8ef632b1b020cdf2c215ecd9fcfa84bc435efcb" # update this as needed + | + |http_archive( + | name = "io_bazel_rules_scala", + | url = "https://github.com/bazelbuild/rules_scala/archive/%s.zip"%rules_scala_version, + | type = "zip", + | strip_prefix= "rules_scala-%s" % rules_scala_version + |) + | + |load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains") + |scala_register_toolchains() + | + |load("@io_bazel_rules_scala//scala:scala.bzl", "scala_repositories") + |scala_repositories() + | + |load("@io_bazel_rules_scala//junit:junit.bzl", "junit_repositories") + |junit_repositories() + """.stripMargin + driver.scratchFile("WORKSPACE", workspace) + } + + private def givenBuildFile(): Unit = { + val build = + s""" + |load("@io_bazel_rules_scala//scala:scala.bzl", "scala_junit_test") + | + |scala_junit_test( + | name = "$NoTestsFoundLabel", + | srcs = ["JunitTest.scala"], + | suffixes = ["WrongTestSuffix"], + | size = "small" + |) + """.stripMargin + driver.scratchFile("BUILD", build) + } + + private def givenEmptyTestSuitFile(): Unit = { + val junitTest = + """ + |import org.junit.Test + |class JunitTest { + | @Test + | def running: Unit = { + | } + |} + """.stripMargin + + driver.scratchFile("JunitTest.scala", junitTest) + } + +} diff --git a/test_expect_failure/scala_junit_test/BUILD b/test_expect_failure/scala_junit_test/BUILD index 32bef98b4..0c87dbb1b 100644 --- a/test_expect_failure/scala_junit_test/BUILD +++ b/test_expect_failure/scala_junit_test/BUILD @@ -11,9 +11,3 @@ scala_junit_test( srcs = ["JunitTest.scala"], size = "small", ) -scala_junit_test( - name = "no_tests_found", - srcs = ["JunitTest.scala"], - suffixes = ["DoesNotMatch"], - size = "small", -) \ No newline at end of file diff --git a/test_expect_failure/scala_junit_test/JunitTest.scala b/test_expect_failure/scala_junit_test/JunitTest.scala deleted file mode 100644 index 8a95055b3..000000000 --- a/test_expect_failure/scala_junit_test/JunitTest.scala +++ /dev/null @@ -1,10 +0,0 @@ - -import org.junit.Test - -class JunitTest { - - @Test - def running: Unit = { - } - -} diff --git a/test_rules_scala.sh b/test_rules_scala.sh index 4e44cc3ce..34bae6a6b 100755 --- a/test_rules_scala.sh +++ b/test_rules_scala.sh @@ -301,10 +301,6 @@ test_junit_test_must_have_prefix_or_suffix() { action_should_fail test test_expect_failure/scala_junit_test:no_prefix_or_suffix } -test_junit_test_errors_when_no_tests_found() { - action_should_fail test test_expect_failure/scala_junit_test:no_tests_found -} - test_resources() { RESOURCE_NAME="resource.txt" TARGET=$1 @@ -750,7 +746,6 @@ $runner junit_generates_xml_logs $runner scala_library_jar_without_srcs_must_fail_on_mismatching_resource_strip_prefix $runner multiple_junit_patterns $runner test_junit_test_must_have_prefix_or_suffix -$runner test_junit_test_errors_when_no_tests_found $runner scala_library_jar_without_srcs_must_include_direct_file_resources $runner scala_library_jar_without_srcs_must_include_filegroup_resources $runner bazel run test/src/main/scala/scala/test/large_classpath:largeClasspath