Skip to content

Commit 40945c5

Browse files
committed
Return unresolved when tests not found during discovery
1 parent a7d7c5d commit 40945c5

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/main/scala/org/scalatestplus/junit5/ScalaTestEngine.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import org.junit.platform.engine.support.descriptor.EngineDescriptor
2121
import org.junit.platform.engine.support.discovery.SelectorResolver.{Match, Resolution}
2222
import org.junit.platform.engine.support.discovery.{EngineDiscoveryRequestResolver, SelectorResolver}
2323
import org.junit.platform.engine.{EngineDiscoveryRequest, ExecutionRequest, TestDescriptor, TestExecutionResult, UniqueId}
24-
import org.scalatest.{Args, ConfigMap, DynaTags, Filter, ParallelTestExecution, Stopper, Suite, Tracker}
24+
import org.scalatest.{Args, ConfigMap, DynaTags, Filter, ParallelTestExecution, Stopper, Tracker}
2525

2626
import java.lang.reflect.Modifier
2727
import java.util.concurrent.atomic.AtomicInteger
28-
import java.util.{Optional, UUID}
28+
import java.util.Optional
2929
import java.util.concurrent.{ExecutorService, Executors, ThreadFactory}
3030
import java.util.logging.Logger
3131
import java.util.stream.Collectors
@@ -107,7 +107,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
107107
.stream()
108108
.flatMap(addToParentFunction(context))
109109
.collect(Collectors.toSet())
110-
Resolution.matches(matches)
110+
if (matches.isEmpty) {
111+
Resolution.unresolved()
112+
} else {
113+
Resolution.matches(matches)
114+
}
111115
}
112116

113117
override def resolve(selector: PackageSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
@@ -116,7 +120,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
116120
.stream()
117121
.flatMap(addToParentFunction(context))
118122
.collect(Collectors.toSet())
119-
Resolution.matches(matches)
123+
if (matches.isEmpty) {
124+
Resolution.unresolved()
125+
} else {
126+
Resolution.matches(matches)
127+
}
120128
}
121129

122130
override def resolve(selector: ModuleSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
@@ -125,7 +133,11 @@ class ScalaTestEngine extends org.junit.platform.engine.TestEngine {
125133
.stream()
126134
.flatMap(addToParentFunction(context))
127135
.collect(Collectors.toSet())
128-
Resolution.matches(matches)
136+
if (matches.isEmpty) {
137+
Resolution.unresolved()
138+
} else {
139+
Resolution.matches(matches)
140+
}
129141
}
130142

131143
override def resolve(selector: ClassSelector, context: SelectorResolver.Context): SelectorResolver.Resolution = {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.scalatestplus.junit5
2+
3+
import org.junit.platform.engine.UniqueId
4+
import org.junit.platform.engine.discovery.ClasspathRootSelector
5+
import org.junit.platform.engine.discovery.DiscoverySelectors.{selectClasspathRoots, selectPackage}
6+
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request
7+
import org.scalatest.{BeforeAndAfterAll, funspec}
8+
import org.scalatestplus.junit5.helpers.HappySuite
9+
10+
import java.nio.file.{Files, Paths}
11+
import scala.collection.JavaConverters._
12+
13+
class ScalaTestEngineSpec extends funspec.AnyFunSpec with BeforeAndAfterAll {
14+
val engine = new ScalaTestEngine
15+
var scalaTestEngineProperty: Option[String] = None
16+
17+
override def beforeAll(): Unit = {
18+
scalaTestEngineProperty = Option(System.clearProperty("org.scalatestplus.junit5.ScalaTestEngine.disabled"))
19+
}
20+
21+
override def afterAll(): Unit = {
22+
scalaTestEngineProperty.foreach(System.setProperty("org.scalatestplus.junit5.ScalaTestEngine.disabled", _))
23+
}
24+
25+
describe("ScalaTestEngine") {
26+
describe("discover method") {
27+
it("should discover suites on classpath") {
28+
val classPathRoot = classOf[ScalaTestEngineSpec].getProtectionDomain.getCodeSource.getLocation
29+
val discoveryRequest = request.selectors(
30+
selectClasspathRoots(java.util.Collections.singleton(Paths.get(classPathRoot.toURI)))
31+
).build()
32+
val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
33+
assert(engineDescriptor.getChildren.asScala.exists(td => td.asInstanceOf[ScalaTestClassDescriptor].suiteClass == classOf[HappySuite]))
34+
}
35+
36+
it("should return unresolved for classpath without any tests") {
37+
val emptyPath = Files.createTempDirectory(null)
38+
val discoveryRequest = request.selectors(
39+
selectClasspathRoots(java.util.Collections.singleton(emptyPath))
40+
).build()
41+
42+
val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
43+
assert(engineDescriptor.getChildren.asScala.isEmpty)
44+
}
45+
46+
it("should discover suites in package") {
47+
val discoveryRequest = request.selectors(
48+
selectPackage("org.scalatestplus.junit5.helpers")
49+
).build()
50+
51+
val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
52+
assert(engineDescriptor.getChildren.asScala.exists(td => td.asInstanceOf[ScalaTestClassDescriptor].suiteClass == classOf[HappySuite]))
53+
}
54+
55+
it("should return unresolved for package without any tests") {
56+
val discoveryRequest = request.selectors(
57+
selectPackage("org.scalatestplus.junit5.nonexistant")
58+
).build()
59+
60+
val engineDescriptor = engine.discover(discoveryRequest, UniqueId.forEngine(engine.getId()))
61+
assert(engineDescriptor.getChildren.asScala.isEmpty)
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)