Skip to content

Commit

Permalink
bugfix: escape special chars in test names
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Sep 27, 2023
1 parent 209a298 commit 4a8058e
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scala.util.control.NonFatal
import scala.util.matching.Regex

import scala.meta.internal.metals.BuildServerConnection
import scala.meta.internal.metals.BuildTargets
Expand Down Expand Up @@ -640,9 +641,13 @@ class DebugProvider(
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
testProvider.getFramework(buildTarget, suite) match {
case JUnit4 =>
suite.copy(tests = suite.tests.map(escapeTestNameJunit))
case MUnit =>
suite.copy(tests = suite.tests.map(escapeTestNameMUnit))
case _ => suite
}
})
new b.DebugSessionParams(
singletonList(buildTarget.getId),
Expand Down Expand Up @@ -924,4 +929,15 @@ object DebugProvider {
extends Exception(
"Build misconfiguration. No semanticdb can be found for you file, please check the doctor."
)

def escapeTestNameJunit(testName: String): String =
testName.replace("$", "\\$")

val specialCharsRegex: Regex = "([\\.\\+\\*\\?\\^\\(\\)\\[\\]\\{\\}\\|\\&])".r

def escapeTestNameMUnit(testName: String): String =
specialCharsRegex.replaceAllIn(
escapeTestNameJunit(testName),
matched => "\\\\" + matched.group(1),
)
}

0 comments on commit 4a8058e

Please sign in to comment.