Improve TestFramework error reporting

It is possible for the test task to fail because of issues with the
layered ClassLoaders. When this occurs and is detectable, I try to
provide a useful error message that will help the user fix the issue.
This commit is contained in:
Ethan Atkins 2018-12-03 18:08:21 -08:00
parent f118a5e410
commit 8cba83aebb
1 changed files with 13 additions and 0 deletions

View File

@ -46,6 +46,17 @@ final class TestFramework(val implClassNames: String*) extends Serializable {
log: ManagedLogger,
frameworkClassNames: List[String]
): Option[Framework] = {
def logError(e: Throwable): Option[Framework] = {
log.error(
s"Error loading test framework ($e). This usually means that you are"
+ " using a layered class loader that cannot reach the sbt.testing.Framework class."
+ " The most likely cause is that your project has a runtime dependency on your"
+ " test framework, e.g. scalatest. To fix this, you can try to set\n"
+ "Test / layeringStrategy := new LayeringStrategy.Test(false, true)\nor\n"
+ "Test / layeringStrategy := LayeringStrategy.Flat"
)
None
}
frameworkClassNames match {
case head :: tail =>
try {
@ -54,6 +65,8 @@ final class TestFramework(val implClassNames: String*) extends Serializable {
case oldFramework: OldFramework => new FrameworkWrapper(oldFramework)
})
} catch {
case e: NoClassDefFoundError => logError(e)
case e: MatchError => logError(e)
case _: ClassNotFoundException =>
log.debug("Framework implementation '" + head + "' not present.")
createFramework(loader, log, tail)