Skip to content

improvement: Add stack trace to failed results #837

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

Merged
merged 2 commits into from
Feb 6, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ object TestSuiteEventHandler {
case Status.Failure =>
val failedMsg =
TestUtils.printThrowable(e.throwable()).getOrElse("")
val stackTrace = TestUtils.printStackTrace(e.throwable()).getOrElse("")
val formatted =
TestSuiteEventHandler.formatError(
name,
failedMsg,
indentSize = 0
)
SingleTestResult.Failed(name, e.duration, formatted)
SingleTestResult.Failed(name, e.duration, formatted, stackTrace, location = null)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized it's gson, so this needs to be null instead of Option

case _ =>
SingleTestResult.Skipped(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ final case class TestSuiteSummary(
tests: java.util.List[SingleTestSummary]
)

final class TestLocation(
val file: String,
val line: Int
)

/**
* Sealed hierarchy that models 3 possible outcomes of single test case.
* I wanted to model this a discriminated union type and due to gson serialization,
Expand Down Expand Up @@ -40,10 +45,18 @@ object SingleTestResult {
val kind: String,
val testName: String,
val duration: Long,
val error: String
val error: String,
val stackTrace: String,
val location: TestLocation
) extends SingleTestSummary
object Failed {
def apply(testName: String, duration: Long, error: String): Failed =
new Failed("failed", testName, duration, error)
def apply(
testName: String,
duration: Long,
error: String,
stackTrace: String,
location: TestLocation
): Failed =
new Failed("failed", testName, duration, error, stackTrace, location)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ch.epfl.scala.debugadapter.testing

import sbt.testing._
import sbt.testing.*

import java.io.ByteArrayOutputStream
import java.io.PrintStream

object TestUtils {
def printSelector(selector: Selector): Option[String] = selector match {
Expand All @@ -17,6 +20,18 @@ object TestUtils {
else Some(stripTestFrameworkSpecificInformation(opt.get().getMessage))
}

def printStackTrace(opt: OptionalThrowable): Option[String] = {
if (opt.isEmpty) None
else
Some {
val writer = new StringBuffer
val outputStream = new ByteArrayOutputStream()
val printStream = new PrintStream(outputStream)
opt.get().printStackTrace(printStream)
outputStream.toString
}
}

private val specs2Prefix = "java.lang.Exception: "
private val utestPrefix = "utest.AssertionError: "
private val scalaTestPrefix = "org.scalatest.exceptions.TestFailedException: "
Expand Down