From 0cead0f77371be51dec78a7d2277b53ea6017819 Mon Sep 17 00:00:00 2001 From: Igal Tabachnik Date: Mon, 27 May 2019 13:57:44 +0300 Subject: [PATCH] Specs2 filtering runner now filters test cases according to filter. This allows the bazel test runner correctly generate the test log, based only on tests that actually did run. --- .../specs2/Specs2RunnerBuilder.scala | 31 +++++++++++++------ test_rules_scala.sh | 17 ++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala b/src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala index 9f4f8e03c..d783956ee 100644 --- a/src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala +++ b/src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala @@ -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) @@ -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) diff --git a/test_rules_scala.sh b/test_rules_scala.sh index 0164e9659..39eac9827 100755 --- a/test_rules_scala.sh +++ b/test_rules_scala.sh @@ -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 \ @@ -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