Skip to content

Commit 4c5f429

Browse files
committed
Reporting test names
1 parent a3ef750 commit 4c5f429

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/IOUtils.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ object IOUtils {
7171
val isDebugReportFile = (file: File) => file.getName == Constants.XMLReportFilenameWithDebug
7272

7373
// loads all the invoked statement ids from the given files
74-
def invoked(files: Seq[File]): Set[Int] = {
75-
val acc = mutable.Set[Int]()
74+
def invoked(files: Seq[File]): Set[(Int, String)] = {
75+
val acc = mutable.Set[(Int, String)]()
7676
files.foreach { file =>
7777
val reader = Source.fromFile(file)
7878
for ( line <- reader.getLines() ) {
7979
if (!line.isEmpty) {
80-
acc += line.toInt
80+
acc += (line.split(" ").toList match {
81+
case List(idx, clazz) => (idx.toInt, clazz)
82+
case List(idx) => (idx.toInt, "")
83+
})
8184
}
8285
}
8386
reader.close()

scalac-scoverage-plugin/src/main/scala/scoverage/coverage.scala

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ case class Coverage()
3838
// returns the classes by least coverage
3939
def risks(limit: Int) = classes.toSeq.sortBy(_.statementCount).reverse.sortBy(_.statementCoverage).take(limit)
4040

41-
def apply(ids: Iterable[Int]): Unit = ids foreach invoked
42-
def invoked(id: Int): Unit = statementsById.get(id).foreach(_.invoked())
41+
def apply(ids: Iterable[(Int, String)]): Unit = ids foreach invoked
42+
def invoked(id: (Int, String)): Unit = statementsById.get(id._1).foreach(_.invoked(id._2))
4343
}
4444

4545
trait MethodBuilders {
@@ -119,9 +119,13 @@ case class Statement(location: Location,
119119
treeName: String,
120120
branch: Boolean,
121121
var count: Int = 0,
122-
ignored: Boolean = false) extends java.io.Serializable {
122+
ignored: Boolean = false,
123+
tests: mutable.Set[String] = mutable.Set[String]()) extends java.io.Serializable {
123124
def source = location.sourcePath
124-
def invoked(): Unit = count = count + 1
125+
def invoked(test: String): Unit = {
126+
count = count + 1
127+
if(test != "") tests += test
128+
}
125129
def isInvoked = count > 0
126130
}
127131

scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala

+14-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ScoveragePlugin(val global: Global) extends Plugin {
3535
options.dataDir = opt.substring("dataDir:".length)
3636
} else if (opt.startsWith("extraAfterPhase:") || opt.startsWith("extraBeforePhase:")) {
3737
// skip here, these flags are processed elsewhere
38+
} else if (opt == "reportTestName") {
39+
options.reportTestName = true
3840
} else {
3941
error("Unknown option: " + opt)
4042
}
@@ -82,6 +84,7 @@ class ScoverageOptions {
8284
var excludedFiles: Seq[String] = Nil
8385
var excludedSymbols: Seq[String] = Seq("scala.reflect.api.Exprs.Expr", "scala.reflect.api.Trees.Tree", "scala.reflect.macros.Universe.Tree")
8486
var dataDir: String = IOUtils.getTempPath
87+
var reportTestName: Boolean = false
8588
}
8689

8790
class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Option[String], extraBeforePhase: Option[String])
@@ -161,14 +164,17 @@ class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Opt
161164
),
162165
newTermName("invoked")
163166
),
164-
List(
165-
Literal(
166-
Constant(id)
167-
),
168-
Literal(
169-
Constant(options.dataDir)
170-
)
171-
)
167+
Literal(
168+
Constant(id)
169+
) ::
170+
Literal(
171+
Constant(options.dataDir)
172+
) ::
173+
(if(options.reportTestName)
174+
List(Literal(
175+
Constant(true)
176+
))
177+
else Nil)
172178
)
173179
}
174180

scalac-scoverage-plugin/src/main/scala/scoverage/report/StatementWriter.scala

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class StatementWriter(mFile: MeasuredFile) {
2424
<th>Pos</th>
2525
<th>Tree</th>
2626
<th>Symbol</th>
27+
<th>Tests</th>
2728
<th>Code</th>
2829
</tr>{mFile.statements.toSeq.sortBy(_.line).map(stmt => {
2930
<tr>
@@ -44,6 +45,9 @@ class StatementWriter(mFile: MeasuredFile) {
4445
<td>
4546
{stmt.symbolName}
4647
</td>
48+
<td>
49+
{stmt.tests.mkString(",")}
50+
</td>
4751
<td style={cellStyle(stmt.isInvoked)}>
4852
{stmt.desc}
4953
</td>

scalac-scoverage-plugin/src/test/scala/scoverage/IOUtilsTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class IOUtilsTest extends FreeSpec with OneInstancePerTest with Matchers {
1515
writer.write("1\n5\n9\n\n10\n")
1616
writer.close()
1717
val invoked = IOUtils.invoked(Seq(file))
18-
assert(invoked === Set(1, 5, 9, 10))
18+
assert(invoked === Set((1, ""), (5, ""), (9, ""), (10, "")))
1919

2020
file.delete()
2121
}
@@ -36,7 +36,7 @@ class IOUtilsTest extends FreeSpec with OneInstancePerTest with Matchers {
3636

3737
val files = IOUtils.findMeasurementFiles(file1.getParent)
3838
val invoked = IOUtils.invoked(files.toIndexedSeq)
39-
assert(invoked === Set(1, 2, 5, 7, 9, 10, 14))
39+
assert(invoked === Set((1, ""), (2, ""), (5, ""), (7, ""), (9, ""), (10, ""), (14, "")))
4040

4141
file1.delete()
4242
file2.delete()

scalac-scoverage-runtime/js/src/main/scala/scoverage/Platform.scala

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ object Platform {
1818

1919
lazy val Source = SupportSource
2020

21+
val isJvm = false
2122
}

scalac-scoverage-runtime/jvm/src/main/scala/scoverage/Platform.scala

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ object Platform {
1717
type FileFilter = SupportFileFilter
1818

1919
lazy val Source = SupportSource
20+
21+
val isJvm = true
2022
}

scalac-scoverage-runtime/shared/src/main/scala/scoverage/Invoker.scala

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object Invoker {
2929
* @param id the id of the statement that was invoked
3030
* @param dataDir the directory where the measurement data is held
3131
*/
32-
def invoked(id: Int, dataDir: String): Unit = {
32+
def invoked(id: Int, dataDir: String, reportTestName: Boolean = false): Unit = {
3333
// [sam] we can do this simple check to save writing out to a file.
3434
// This won't work across JVMs but since there's no harm in writing out the same id multiple
3535
// times since for coverage we only care about 1 or more, (it just slows things down to
@@ -53,12 +53,25 @@ object Invoker {
5353
threadFiles.set(files)
5454
}
5555
val writer = files.getOrElseUpdate(dataDir, new FileWriter(measurementFile(dataDir), true))
56-
writer.append(Integer.toString(id)).append("\n").flush()
5756

57+
// For some reason, the JS build does not print the output the correct way. I will look into this later.
58+
if(isJvm && reportTestName) writer.append(Integer.toString(id)).append(" ").append(getCallingScalaTest).append("\n").flush()
59+
else writer.append(Integer.toString(id)).append("\n").flush()
5860
ids.put(id, ())
5961
}
6062
}
6163

64+
def getCallingScalaTest: String = {
65+
val st = Thread.currentThread.getStackTrace
66+
val idx = st.indexWhere{
67+
ste => {
68+
val name = ste.getClassName.toLowerCase()
69+
name.endsWith("suite") || name.endsWith("spec") || name.endsWith("test")
70+
}
71+
}
72+
if(idx > 0) st(idx).getClassName else ""
73+
}
74+
6275
def measurementFile(dataDir: File): File = measurementFile(dataDir.getAbsolutePath)
6376
def measurementFile(dataDir: String): File = new File(dataDir, MeasurementsPrefix + Thread.currentThread.getId)
6477

0 commit comments

Comments
 (0)