Skip to content

Commit

Permalink
Prevent exception in static initializers in specs2 tests taking down …
Browse files Browse the repository at this point in the history
…the test runner (#500)

* Adding main_class from —define, if exists

* Returning an empty description in case of exception

* Moving failing test to its proper location
  • Loading branch information
hmemcpy authored and johnynek committed May 30, 2018
1 parent f8891f4 commit 861d4fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Specs2PrefixSuffixTestDiscoveringSuite(suite: Class[Any], runnerBuilder: R

override def getName: String = "Aggregate Specs2 Test Suite"

override def getDescription: Description = {
lazy val emptySuiteDescription = {
val description = Description.createSuiteDescription(getName)
description.addChild(Description.EMPTY)
description
}
Try(super.getDescription).getOrElse(emptySuiteDescription)
}

override def getChildren: util.List[Runner] =
super.getChildren.asScala
.collect {
Expand Down
9 changes: 8 additions & 1 deletion test_expect_failure/scala_junit_test/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//scala:scala.bzl", "scala_junit_test")
load("//scala:scala.bzl", "scala_junit_test", "scala_specs2_junit_test")

scala_junit_test(
name = "failing_test",
Expand All @@ -16,4 +16,11 @@ scala_junit_test(
srcs = ["JunitTest.scala"],
suffixes = ["DoesNotMatch"],
size = "small",
)

scala_specs2_junit_test(
name = "specs2_failing_test",
srcs = ["specs2/FailingTest.scala"],
suffixes = ["Test"],
size = "small",
)
10 changes: 10 additions & 0 deletions test_expect_failure/scala_junit_test/specs2/FailingTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package scala.test.junit.specs2

import org.specs2.mutable.SpecWithJUnit

class FailingTest extends SpecWithJUnit {

val boom: String = { throw new Exception("Boom") }

"some test" >> { boom must beEmpty }
}
35 changes: 35 additions & 0 deletions test_rules_scala.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ test_scala_library_suite() {
action_should_fail build test_expect_failure/scala_library_suite:library_suite_dep_on_children
}

test_expect_failure_with_message() {
set +e

expected_message=$1
test_filter=$2
test_command=$3

command="bazel test --nocache_test_results --test_output=streamed ${test_filter} ${test_command}"
output=$(${command} 2>&1)

echo ${output} | grep "$expected_message"
if [ $? -ne 0 ]; then
echo "'bazel test ${test_command}' should have logged \"${expected_message}\"."
exit 1
fi
if [ "${additional_expected_message}" != "" ]; then
echo ${output} | grep "$additional_expected_message"
if [ $? -ne 0 ]; then
echo "'bazel test ${test_command}' should have logged \"${additional_expected_message}\"."
exit 1
fi
fi

set -e
}

test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message() {
set +e

Expand Down Expand Up @@ -571,6 +597,14 @@ scala_specs2_junit_test_test_filter_match_multiple_methods(){
done
}


scala_specs2_exception_in_initializer_without_filter(){
expected_message="org.specs2.control.UserException: cannot create an instance for class scala.test.junit.specs2.FailingTest"
test_command="test_expect_failure/scala_junit_test:specs2_failing_test"

test_expect_failure_with_message "$expected_message" $test_filter $test_command
}

scalac_jvm_flags_are_configured(){
action_should_fail build //test_expect_failure/compilers_jvm_flags:can_configure_jvm_flags_for_scalac
}
Expand Down Expand Up @@ -775,6 +809,7 @@ $runner scala_specs2_junit_test_test_filter_exact_match
$runner scala_specs2_junit_test_test_filter_exact_match_unsafe_characters
$runner scala_specs2_junit_test_test_filter_exact_match_escaped_and_sanitized
$runner scala_specs2_junit_test_test_filter_match_multiple_methods
$runner scala_specs2_exception_in_initializer_without_filter
$runner scalac_jvm_flags_are_configured
$runner javac_jvm_flags_are_configured
$runner javac_jvm_flags_via_javacopts_are_configured
Expand Down

0 comments on commit 861d4fa

Please sign in to comment.