Skip to content

Commit

Permalink
bugfix: accommodate junit encoded test names (#5485)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored Aug 7, 2023
1 parent f276070 commit c58fc59
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ class MetalsLspService(
statusBar,
sourceMapper,
userConfig,
testProvider,
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
import scala.meta.internal.metals.clients.language.MetalsStatusParams
import scala.meta.internal.metals.config.RunType
import scala.meta.internal.metals.config.RunType._
import scala.meta.internal.metals.testProvider.TestSuitesProvider
import scala.meta.internal.mtags.DefinitionAlternatives.GlobalSymbol
import scala.meta.internal.mtags.OnDemandSymbolIndex
import scala.meta.internal.mtags.Semanticdbs
Expand Down Expand Up @@ -84,6 +85,7 @@ class DebugProvider(
statusBar: StatusBar,
sourceMapper: SourceMapper,
userConfig: () => UserConfiguration,
testProvider: TestSuitesProvider,
) extends Cancelable {

import DebugProvider._
Expand Down Expand Up @@ -634,13 +636,20 @@ class DebugProvider(
)(implicit ec: ExecutionContext): Future[DebugSessionParams] = {
def makeDebugSession() = {
val debugSession =
if (supportsTestSelection(request.target))
if (supportsTestSelection(request.target)) {
val testSuites =
request.requestData.copy(suites = request.requestData.suites.map {
suite =>
if (testProvider.getFramework(buildTarget, suite) == JUnit4)
suite.copy(tests = suite.tests.map(_.replace("$", "\\$")))
else suite
})
new b.DebugSessionParams(
singletonList(buildTarget.getId),
DebugProvider.ScalaTestSelection,
request.requestData.toJson,
testSuites.toJson,
)
else
} else
new b.DebugSessionParams(
singletonList(buildTarget.getId),
b.DebugSessionParamsDataKind.SCALA_TEST_SUITES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,13 @@ object TestExplorerEvent {
// Represents a single test within a test suite
final case class TestCaseEntry(
name: String,
displayName: String,
location: l.Location,
)

object TestCaseEntry {
def apply(
name: String,
location: l.Location,
): TestCaseEntry = TestCaseEntry(name, name, location)
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ private[testProvider] final class TestSuitesIndex {
): Set[FullyQualifiedName] =
cachedTestSuites.get(buildTarget).map(_.keySet.toSet).getOrElse(Set.empty)

def get(target: BuildTarget, name: FullyQualifiedName): Option[TestEntry] =
cachedTestSuites.get(target).flatMap(_.get(name))

def remove(
buildTarget: BuildTarget,
suiteName: FullyQualifiedName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import scala.meta.internal.metals.debug.BuildTargetClasses
import scala.meta.internal.metals.debug.JUnit4
import scala.meta.internal.metals.debug.MUnit
import scala.meta.internal.metals.debug.Scalatest
import scala.meta.internal.metals.debug.TestFramework
import scala.meta.internal.metals.debug.Unknown
import scala.meta.internal.metals.testProvider.TestExplorerEvent._
import scala.meta.internal.metals.testProvider.frameworks.JunitTestFinder
Expand Down Expand Up @@ -535,4 +536,15 @@ final class TestSuitesProvider(
events,
)

def getFramework(
target: BuildTarget,
selection: ScalaTestSuiteSelection,
): TestFramework = {
val framework =
for {
testEntry <- index.get(target, FullyQualifiedName(selection.className))
} yield testEntry.suiteDetails.framework
framework.getOrElse(Unknown)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scala.meta.internal.metals.testProvider.frameworks

import scala.reflect.NameTransformer

import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.testProvider.TestCaseEntry
import scala.meta.internal.mtags
Expand Down Expand Up @@ -37,7 +39,14 @@ class JunitTestFinder {
case symbol if isValid(symbol) =>
doc
.toLocation(uri, symbol.symbol)
.map(location => TestCaseEntry(symbol.displayName, location))
.map { location =>
val encodedName = NameTransformer.encode(symbol.displayName)
TestCaseEntry(
encodedName,
symbol.displayName,
location,
)
}
}
.flatten
.toVector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,52 @@ class TestSuitesProviderSuite extends BaseLspSuite("testSuitesFinderSuite") {
},
)

checkEvents(
"check-backtick",
List("junit:junit:4.13.2", "com.github.sbt:junit-interface:0.13.3"),
s"""|
|/app/src/main/scala/JunitTestSuite.scala
|import org.junit.Test
|class JunitTestSuite {
| @Test
| def `test-backtick` = ()
|}
|""".stripMargin,
file = "app/src/main/scala/JunitTestSuite.scala",
expected = () => {
val fcqn = "JunitTestSuite"
val className = "JunitTestSuite"
val symbol = "_empty_/JunitTestSuite#"
val file = "app/src/main/scala/JunitTestSuite.scala"
List(
rootBuildTargetUpdate(
"app",
targetUri,
List[TestExplorerEvent](
AddTestSuite(
fcqn,
className,
symbol,
QuickLocation(classUriFor(file), (1, 6, 1, 20)).toLsp,
canResolveChildren = true,
),
AddTestCases(
fcqn,
className,
List(
TestCaseEntry(
"test$minusbacktick",
"test-backtick",
QuickLocation(classUriFor(file), (3, 6, 3, 21)).toLsp,
)
).asJava,
),
).asJava,
)
)
},
)

checkEvents(
"scalatest-any-fun-suite",
List("org.scalatest::scalatest:3.2.13"),
Expand Down

0 comments on commit c58fc59

Please sign in to comment.