Skip to content

Commit

Permalink
Redefine definedTests in terms of the task itself to work correctly…
Browse files Browse the repository at this point in the history
… with other plugins which may redefine the task
  • Loading branch information
vasilmkd committed Oct 17, 2024
1 parent 3ffe55c commit 0bc6987
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ object JupiterPlugin extends AutoPlugin {
* By default this is applied to the Test configuration only.
*/
def scopedSettings: Seq[Def.Setting[_]] = Seq(

definedTests := collectTests.value
definedTests := Def.taskDyn {
val otherTests = definedTests.value
Def.task {
val jupiterTests = collectTests.value
otherTests ++ jupiterTests
}
}.value
)

/*
Expand All @@ -93,14 +98,12 @@ object JupiterPlugin extends AutoPlugin {
)

/*
* Collects available tests through JUnit Jupiter's discovery mechanism and
* combines them with the result of sbt.Keys.definedTests.
* Collects available tests through JUnit Jupiter's discovery mechanism.
*/
private def collectTests = Def.task {
private def collectTests: Def.Initialize[Task[Seq[TestDefinition]]] = Def.task {

val classes = classDirectory.value
val classpath = dependencyClasspath.value.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL
val result = Defaults.detectTests.value

val collector = new JupiterTestCollector.Builder()
.withClassDirectory(classes)
Expand All @@ -118,7 +121,7 @@ object JupiterPlugin extends AutoPlugin {
}
}

result ++ discoveredTests
discoveredTests
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ libraryDependencies ++= Seq(
TaskKey[Unit]("checkDefinedTestsThrowsException") := {
(Test / definedTests).result.value match {
case Inc(cause:Incomplete) =>
val actualMessage = cause.directCause.map(c => c.getMessage).getOrElse("")
val actualMessage = cause.causes.headOption.flatMap(_.directCause).map(_.getMessage).getOrElse("")
val expectedMessage = "Found at least one JUnit 5 test"
assert(actualMessage.startsWith(expectedMessage),
s"Expected an exception containing a message starting with `$expectedMessage` (actual: `$actualMessage`)")
Expand Down

0 comments on commit 0bc6987

Please sign in to comment.