Skip to content

Commit 032ff50

Browse files
author
Igal Tabachnik
committed
Creating a filtered description tree from Specs2 utilities - keeps ordering and hashCodes intact
1 parent 5246cf9 commit 032ff50

File tree

6 files changed

+44
-24
lines changed

6 files changed

+44
-24
lines changed

src/java/io/bazel/rulesscala/specs2/Specs2RunnerBuilder.scala

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import org.junit.runners.Suite
1111
import org.junit.runners.model.RunnerBuilder
1212
import org.specs2.concurrent.ExecutionEnv
1313
import org.specs2.control.Action
14+
import org.specs2.data.Trees._
15+
import org.specs2.fp.TreeLoc
1416
import org.specs2.main.{Arguments, CommandLine, Select}
1517
import org.specs2.specification.core.{Env, Fragment, SpecStructure}
1618
import org.specs2.specification.process.Stats
@@ -60,10 +62,15 @@ class FilteredSpecs2ClassRunner(testClass: Class[_], testFilter: Pattern)
6062
}
6163

6264
private def createFilteredDescription(specStructure: SpecStructure, ee: ExecutionEnv): Description = {
63-
val tree = allFragmentDescriptions(ee)
64-
val desc = Description.createSuiteDescription(testClass)
65-
tree.values.filter(matchingFilter).foreach(desc.addChild)
66-
desc
65+
val descTree = createDescriptionTree(ee).map(_._2)
66+
descTree.toTree.bottomUp {
67+
(description: Description, children: Stream[Description]) =>
68+
children.filter(matchingFilter).foreach {
69+
child => description.addChild(child)
70+
}
71+
description
72+
}.rootLabel
73+
6774
}
6875

6976
def matchesFilter: Boolean = {
@@ -79,10 +86,12 @@ class FilteredSpecs2ClassRunner(testClass: Class[_], testFilter: Pattern)
7986
else sanitized
8087
}
8188

89+
private def createDescriptionTree(implicit ee: ExecutionEnv): TreeLoc[(Fragment, Description)] =
90+
Try(allDescriptions[specs2_v4].createDescriptionTree(specStructure)(ee))
91+
.getOrElse(allDescriptions[specs2_v3].createDescriptionTree(specStructure))
92+
8293
private def allFragmentDescriptions(implicit ee: ExecutionEnv): Map[Fragment, Description] =
83-
Try(allDescriptions[specs2_v4].fragmentDescriptions(specStructure)(ee))
84-
.orElse(Try(allDescriptions[specs2_v3].fragmentDescriptions(specStructure)))
85-
.getOrElse(Map.empty)
94+
createDescriptionTree(ee).toTree.flattenLeft.toMap
8695

8796
/**
8897
* Retrieves an original (un-sanitized) text of an example fragment,
@@ -129,8 +138,11 @@ class FilteredSpecs2ClassRunner(testClass: Class[_], testFilter: Pattern)
129138
}
130139

131140
private def matchingFilter(desc: Description): Boolean = {
132-
val testCase = desc.getClassName + "#" + desc.getMethodName
133-
testFilter.toString.r.findAllIn(testCase).nonEmpty
141+
if (desc.isSuite) true
142+
else {
143+
val testCase = desc.getClassName + "#" + Option(desc.getMethodName).mkString
144+
testFilter.toString.r.findFirstIn(testCase).nonEmpty
145+
}
134146
}
135147

136148
private def specs2Examples(implicit ee: ExecutionEnv): List[String] =

src/java/io/bazel/rulesscala/specs2/package.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package io.bazel.rulesscala
22

33
import org.junit.runner.Description
44
import org.specs2.concurrent.ExecutionEnv
5+
import org.specs2.fp.TreeLoc
56
import org.specs2.reporter.JUnitDescriptions
67
import org.specs2.specification.core.{Fragment, SpecStructure}
78

89
package object specs2 {
910
type specs2_v4 = {
1011
//noinspection ScalaUnusedSymbol
11-
def fragmentDescriptions(spec: SpecStructure)(ee: ExecutionEnv): Map[Fragment, Description]
12+
def createDescriptionTree(spec: SpecStructure)(ee: ExecutionEnv): TreeLoc[(Fragment, Description)]
1213
}
1314
type specs2_v3 = {
1415
//noinspection ScalaUnusedSymbol
15-
def fragmentDescriptions(spec: SpecStructure): Map[Fragment, Description]
16+
def createDescriptionTree(spec: SpecStructure): TreeLoc[(Fragment, Description)]
1617
}
1718

1819
def allDescriptions[T]: T = JUnitDescriptions.asInstanceOf[T]

test/src/main/scala/scalarules/test/junit/specs2/Specs2Tests.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ class JunitSpecs2Test extends SpecWithJUnit {
1717
}
1818
}
1919

20-
class FailingSpecs2Test extends SpecWithJUnit {
21-
22-
"specs2 tests" should {
23-
"succeed" >> success
24-
"fail" >> failure
25-
}
26-
}
27-
2820
class JunitSpecs2AnotherTest extends SpecWithJUnit {
2921

3022
"other specs2 tests" should {

test_expect_failure/scala_junit_test/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ scala_junit_test(
2323
scala_specs2_junit_test(
2424
name = "specs2_failing_test",
2525
size = "small",
26-
srcs = ["specs2/FailingTest.scala"],
26+
srcs = ["specs2/FailingTest.scala", "specs2/SuiteWithOneFailingTest.scala"],
2727
suffixes = ["Test"],
2828
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package scalarules.test.junit.specs2
2+
3+
import org.specs2.mutable.SpecWithJUnit
4+
5+
class SuiteWithOneFailingTest extends SpecWithJUnit {
6+
7+
"specs2 tests" should {
8+
"succeed" >> success
9+
"fail" >> failure("boom")
10+
}
11+
12+
"some other suite" should {
13+
"do stuff" >> success
14+
}
15+
}

test_rules_scala.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,13 @@ scala_specs2_only_failed_test_shows_in_the_xml(){
548548
bazel test \
549549
--nocache_test_results \
550550
--test_output=streamed \
551-
'--test_filter=scalarules.test.junit.specs2.FailingSpecs2Test#specs2 tests should::fail$' \
552-
test:Specs2Tests
553-
matches=$(grep -c -e "testcase name='specs2 tests should::fail'" -e "testcase name='specs2 tests should::succeed'" ./bazel-testlogs/test/Specs2Tests/test.xml)
551+
'--test_filter=scalarules.test.junit.specs2.SuiteWithOneFailingTest#specs2 tests should::fail$' \
552+
test_expect_failure/scala_junit_test:specs2_failing_test
553+
matches=$(grep -c -e "testcase name='specs2 tests should::fail'" -e "testcase name='specs2 tests should::succeed'" ./bazel-testlogs/test_expect_failure/scala_junit_test/specs2_failing_test/test.xml)
554554
if [ $matches -eq 1 ]; then
555555
return 0
556556
else
557-
echo "Expecting only one result, found more than one. Please check 'bazel-testlogs/test/Specs2Tests/test.xml'"
557+
echo "Expecting only one result, found more than one. Please check './bazel-testlogs/test_expect_failure/scala_junit_test/specs2_failing_test/test.xml'"
558558
return 1
559559
fi
560560
test -e

0 commit comments

Comments
 (0)