Skip to content
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

Prevent showing filtered-out specs2 tests in the test.xml #759

Merged
merged 1 commit into from
May 27, 2019
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
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