Skip to content

Commit

Permalink
Specs2 filtering runner now filters test cases according to filter.
Browse files Browse the repository at this point in the history
This allows the bazel test runner correctly generate the test log, based only on tests that actually did run.
  • Loading branch information
Igal Tabachnik committed May 27, 2019
1 parent 1cffc5f commit 0cead0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ object Specs2FilteringRunnerBuilder {
class FilteredSpecs2ClassRunner(testClass: Class[_], testFilter: Pattern)
extends org.specs2.runner.JUnitRunner(testClass) {

override def getDescription(env: Env): Description = {
val root = super.getDescription(env)
val filtered = flatten(root).filter(matchingFilter)

val flattenedRoot = root.childlessCopy()
filtered.foreach(flattenedRoot.addChild)
flattenedRoot
}

def matchesFilter: Boolean = {
val fqn = testClass.getName + "#"
val matcher = testFilter.matcher(fqn)
Expand Down Expand Up @@ -106,22 +115,24 @@ class FilteredSpecs2ClassRunner(testClass: Class[_], testFilter: Pattern)
*
* This function returns a flat list of the descriptions and their children, starting with the root.
*/
private def flattenDescription(description: Description): List[Description] =
description.getChildren.asScala.toList.flatMap(d => d :: flattenDescription(d))
def flatten(root: Description): List[Description] = {
def flatten0(desc: Description, xs: List[Description]): List[Description] =
desc.childlessCopy() :: desc.getChildren.asScala.foldLeft(xs)((acc, x) => flatten0(x, acc))

flatten0(root, Nil)
}

private def matching(testFilter: Pattern): Description => Boolean = { d =>
val testCase = d.getClassName + "#" + d.getMethodName
testFilter.matcher(testCase).matches
private def matchingFilter(desc: Description): Boolean = {
val testCase = desc.getClassName + "#" + desc.getMethodName
testFilter.toString.r.findAllIn(testCase).nonEmpty
}

private def specs2ExamplesMatching(testFilter: Pattern, junitDescription: Description)(implicit ee: ExecutionEnv): List[String] =
flattenDescription(junitDescription)
.filter(matching(testFilter))
.flatMap(toDisplayName(_))
private def specs2Examples(implicit ee: ExecutionEnv): List[String] =
flatten(getDescription).flatMap(toDisplayName(_))

override def runWithEnv(n: RunNotifier, env: Env): Action[Stats] = {
implicit val ee = env.executionEnv
val specs2MatchedExamplesRegex = specs2ExamplesMatching(testFilter, getDescription).toRegexAlternation
val specs2MatchedExamplesRegex = specs2Examples.toRegexAlternation

val newArgs = Arguments(select = Select(_ex = specs2MatchedExamplesRegex), commandLine = CommandLine.create(testClass.getName))
val newEnv = env.copy(arguments overrideWith newArgs)
Expand Down
17 changes: 17 additions & 0 deletions test_rules_scala.sh
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,22 @@ scala_specs2_junit_test_test_filter_one_test(){
fi
}

scala_specs2_only_filtered_test_shows_in_the_xml(){
bazel test \
--nocache_test_results \
--test_output=streamed \
'--test_filter=scalarules.test.junit.specs2.JunitSpecs2Test#specs2 tests should::run smoothly in bazel$' \
test:Specs2Tests
matches=$(grep -c -e "testcase name='specs2 tests should::run smoothly in bazel'" -e "testcase name='specs2 tests should::not run smoothly in bazel'" ./bazel-testlogs/test/Specs2Tests/test.xml)
if [ $matches -eq 1 ]; then
return 0
else
echo "Expecting only one result, found more than one. Please check 'bazel-testlogs/test/Specs2Tests/test.xml'"
return 1
fi
test -e
}

scala_specs2_junit_test_test_filter_exact_match(){
local output=$(bazel test \
--nocache_test_results \
Expand Down Expand Up @@ -983,6 +999,7 @@ $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 scala_specs2_exception_in_initializer_terminates_without_timeout
$runner scala_specs2_only_filtered_test_shows_in_the_xml
$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 0cead0f

Please sign in to comment.