Skip to content

Commit

Permalink
Merge pull request #18 from henryxparker/print-stacktrace-in-logs
Browse files Browse the repository at this point in the history
Print stacktrace in logs
  • Loading branch information
Baccata authored Apr 4, 2024
2 parents 196c6ce + bd6250e commit ee57b39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/core/shared/src/main/scala/weaver/Formatter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package weaver

import scala.concurrent.duration.FiniteDuration
import scala.util.Try

import cats.data.Chain
import cats.syntax.show._
Expand Down Expand Up @@ -58,6 +59,9 @@ object Formatter {
import outcome._
import TestOutcome.{ Verbose, Summary }

val maxStackFrames = sys.props.get("WEAVER_MAX_STACKFRAMES").flatMap(s =>
Try(s.trim.toInt).toOption).getOrElse(50)

val builder = new StringBuilder()
val newLine = '\n'
builder.append(formatResultStatus(name, result, outcome.duration))
Expand Down Expand Up @@ -100,6 +104,19 @@ object Formatter {
}
builder.append(newLine)

entry.cause.map { t =>
builder.append(TAB4)
builder.append(t.toString)
builder.append(newLine)

TestErrorFormatter.formatStackTrace(t, Some(maxStackFrames)).map {
line =>
builder.append(TAB4.prefix * 2)
builder.append(line)
builder.append(newLine)
}
}

()
}

Expand Down
24 changes: 24 additions & 0 deletions modules/framework-cats/shared/src/test/scala/DogFoodTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ object DogFoodTests extends IOSuite {
}
}

test("failures with exceptions in logs display them correctly") {
_.runSuite(Meta.SucceedsWithErrorInLogs).map {
case (logs, _) =>
val expected =
"""
|- failure 0ms
| expected (src/main/DogFoodTests.scala:5)
|
| [ERROR] 12:54:35 [DogFoodTests.scala:5] error
| weaver.framework.test.Meta$CustomException: surfaced error
| DogFoodTests.scala:15 my.package.MyClass#MyMethod
| DogFoodTests.scala:20 my.package.ClassOfDifferentLength#method$new$1
| <snipped> cats.effect.internals.<...>
| <snipped> java.util.concurrent.<...>
|""".stripMargin.trim

exists(extractLogEventAfterFailures(logs) {
case LoggedEvent.Error(msg) => msg
}) { actual =>
expect.same(actual, expected)
}
}
}

test("failures with multi-line test name are rendered correctly") {
_.runSuite(Meta.Rendering).map {
case (logs, _) =>
Expand Down
18 changes: 18 additions & 0 deletions modules/framework-cats/shared/src/test/scala/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ object Meta {
}
}

object SucceedsWithErrorInLogs extends SimpleIOSuite {
override implicit protected def effectCompat: UnsafeRun[IO] =
SetTimeUnsafeRun
implicit val sourceLocation: SourceLocation = TimeCop.sourceLocation

loggedTest("failure") { log =>
for {
_ <- log.error(
"error",
cause = CustomException(
"surfaced error",
withSnips = true
)
)
} yield failure("expected")
}
}

case class CustomException(
str: String,
causedBy: Exception = null,
Expand Down

0 comments on commit ee57b39

Please sign in to comment.