Skip to content

Return unresolved when tests not found during discovery #28

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 21 additions & 9 deletions src/main/scala/org/scalatestplus/junit5/ScalaTestEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
val engineDesc = new EngineDescriptor(uniqueId, "ScalaTest EngineDescriptor")

if (System.getProperty("org.scalatestplus.junit5.ScalaTestEngine.disabled") != "true") {
logger.info("Starting test discovery...")
logger.fine("Starting test discovery...")

val alwaysTruePredicate =
new java.util.function.Predicate[String]() {
Expand Down Expand Up @@ -107,7 +107,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
.stream()
.flatMap(addToParentFunction(context))
.collect(Collectors.toSet())
Resolution.matches(matches)
if (matches.isEmpty) {
Resolution.unresolved()
} else {
Resolution.matches(matches)
}
}

override def resolve(selector: PackageSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
Expand All @@ -116,7 +120,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
.stream()
.flatMap(addToParentFunction(context))
.collect(Collectors.toSet())
Resolution.matches(matches)
if (matches.isEmpty) {
Resolution.unresolved()
} else {
Resolution.matches(matches)
}
}

override def resolve(selector: ModuleSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
Expand All @@ -125,7 +133,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
.stream()
.flatMap(addToParentFunction(context))
.collect(Collectors.toSet())
Resolution.matches(matches)
if (matches.isEmpty) {
Resolution.unresolved()
} else {
Resolution.matches(matches)
}
}

override def resolve(selector: ClassSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
Expand Down Expand Up @@ -234,7 +246,7 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {

resolver.resolve(discoveryRequest, engineDesc)

logger.info("Completed test discovery, discovered suite count: " + engineDesc.getChildren.size())
logger.config("Completed test discovery, discovered suite count: " + engineDesc.getChildren.size())
}

engineDesc
Expand All @@ -245,15 +257,15 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
*/
def execute(request: ExecutionRequest): Unit = {
if (System.getProperty("org.scalatestplus.junit5.ScalaTestEngine.disabled") != "true") {
logger.info("Start tests execution...")
logger.fine("Start tests execution...")
val engineDesc = request.getRootTestDescriptor
val listener = request.getEngineExecutionListener

listener.executionStarted(engineDesc)
engineDesc.getChildren.asScala.foreach { testDesc =>
testDesc match {
case clzDesc: ScalaTestClassDescriptor =>
logger.info("Start execution of suite class " + clzDesc.suiteClass.getName + "...")
logger.fine("Start execution of suite class " + clzDesc.suiteClass.getName + "...")
listener.executionStarted(clzDesc)
val suiteClass = clzDesc.suiteClass
val canInstantiate = JUnitHelper.checkForPublicNoArgConstructor(suiteClass) && classOf[org.scalatest.Suite].isAssignableFrom(suiteClass)
Expand Down Expand Up @@ -327,15 +339,15 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {

listener.executionFinished(clzDesc, TestExecutionResult.successful())

logger.info("Completed execution of suite class " + clzDesc.suiteClass.getName + ".")
logger.config("Completed execution of suite class " + clzDesc.suiteClass.getName + ".")

case otherDesc =>
// Do nothing for other descriptor, just log it.
logger.warning("Found test descriptor " + otherDesc.toString + " that is not supported, skipping.")
}
}
listener.executionFinished(engineDesc, TestExecutionResult.successful())
logger.info("Completed tests execution.")
logger.fine("Completed tests execution.")
}
}
}
58 changes: 58 additions & 0 deletions src/test/scala/org/scalatestplus/junit5/ScalaTestEngineSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.scalatestplus.junit5

import org.junit.platform.engine.UniqueId
import org.junit.platform.engine.discovery.ClasspathRootSelector
import org.junit.platform.engine.discovery.DiscoverySelectors.{selectClass, selectClasspathRoots, selectPackage}
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request
import org.scalatest.funspec
import org.scalatestplus.junit5.helpers.HappySuite

import java.nio.file.{Files, Path, Paths}
import scala.jdk.CollectionConverters.SetHasAsScala

class ScalaTestEngineSpec extends funspec.AnyFunSpec {
val engine = new ScalaTestEngine

describe("ScalaTestEngine") {
describe("discover method") {
it("should discover suites on classpath") {
val classPathRoot = classOf[ScalaTestEngineSpec].getProtectionDomain.getCodeSource.getLocation
val discoveryRequest = request.selectors(
selectClasspathRoots(java.util.Set.of(Paths.get(classPathRoot.toURI)))
).build()

val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
assert(engineDescriptor.getChildren.asScala.exists(td => td.asInstanceOf[ScalaTestClassDescriptor].suiteClass == classOf[HappySuite]))
}

it("should return unresolved for classpath without any tests") {
val emptyPath = Files.createTempDirectory(null)
val discoveryRequest = request.selectors(
selectClasspathRoots(java.util.Set.of(emptyPath))
).build()

val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
assert(engineDescriptor.getChildren.asScala.isEmpty)
}

it("should discover suites in package") {
val classPathRoot = classOf[ScalaTestEngineSpec].getProtectionDomain.getCodeSource.getLocation
val discoveryRequest = request.selectors(
selectPackage("org.scalatestplus.junit5.helpers")
).build()

val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
assert(engineDescriptor.getChildren.asScala.exists(td => td.asInstanceOf[ScalaTestClassDescriptor].suiteClass == classOf[HappySuite]))
}

it("should return unresolved for package without any tests") {
val discoveryRequest = request.selectors(
selectPackage("org.scalatestplus.junit5.nonexistant")
).build()

val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
assert(engineDescriptor.getChildren.asScala.isEmpty)
}
}
}
}