Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed May 21, 2024
1 parent a641cf6 commit e0f1288
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 82 deletions.
22 changes: 12 additions & 10 deletions metals/src/main/scala/scala/meta/internal/metals/TargetData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ final class TargetData {
def targetJarClasspath(
id: BuildTargetIdentifier
): Option[List[AbsolutePath]] = {
buildTargetDependencyModules.get(id) match {
case None =>
jvmTarget(id).flatMap { target =>
target.jarClasspath
}
case Some(value) =>
Some(value.flatMap(_.getArtifacts().asScala).collect {
val fromDepModules =
for {
module <- buildTargetDependencyModules.getOrElse(id, Nil)
artifact <- module.getArtifacts().asScala
path <- artifact match {
case artifact: MavenDependencyModuleArtifact
if artifact.getClassifier() == null =>
artifact.getUri().toAbsolutePath
})
}
Some(artifact.getUri().toAbsolutePath)
case _ => None
}
} yield path

if (fromDepModules.isEmpty) jvmTarget(id).flatMap(_.jarClasspath)
else Some(fromDepModules)
}

def targetClasspath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class DebugProvider(
params: DebugSessionParams,
cancelPromise: Promise[Unit],
) =
if (buildServer.isDebuggingProvider) {
if (buildServer.isDebuggingProvider || buildServer.isSbt) {
buildServer.startDebugSession(params, cancelPromise)
} else {
def getDebugee: Future[MetalsDebuggee] = params.getDataKind() match {
Expand All @@ -350,7 +350,10 @@ class DebugProvider(
case _ => throw new RuntimeException(s"Can't resolve debugee")
}

for (debuggee <- getDebugee) yield {
for {
_ <- compilations.compileTargets(params.getTargets().asScala.toSeq)
debuggee <- getDebugee
} yield {
val dapLogger =
new scala.meta.internal.metals.debug.server.DebugLogger()
val resolver = new MetalsDebugToolsResolver()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package scala.meta.internal.metals.debug.server

import java.nio.file.Path

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise
Expand All @@ -18,6 +16,7 @@ import ch.epfl.scala.debugadapter.Module
import ch.epfl.scala.debugadapter.ScalaVersion
import ch.epfl.scala.debugadapter.SourceDirectory
import ch.epfl.scala.debugadapter.SourceJar
import ch.epfl.scala.debugadapter.StandaloneSourceFile
import ch.epfl.scala.debugadapter.UnmanagedEntry

class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
Expand All @@ -38,24 +37,21 @@ class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
.filter(_.nonEmpty)
.getOrElse(Nil)
val debugLibs = libraries.flatMap(createLibrary(_))
val includedInLibs = debugLibs
.flatMap(_.sourceEntries.flatMap {
case SourceJar(jar) => Some(jar)
case _ => None
})
.toSet
val includedInLibs = debugLibs.map(_.absolutePath).toSet

val cancelPromise: Promise[Unit] = Promise()

for (
classpath <- buildTargets
.targetClasspath(id, cancelPromise)
.getOrElse(Future.successful(Nil))
.map(_.toAbsoluteClasspath.map(_.toNIO).toSeq)
.map(
_.filter(_.endsWith(".jar")).toAbsoluteClasspath.map(_.toNIO).toSeq
)
) yield {

val filteredClassPath = classpath.collect {
case path if includedInLibs(path) => UnmanagedEntry(path)
case path if !includedInLibs(path) => UnmanagedEntry(path)
}.toList

val modules = buildTargets
Expand All @@ -71,9 +67,8 @@ class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
buildTargets.scalaTarget(id).map(_.scalaVersion),
name,
modules,
libraries.flatMap(createLibrary(_)),
debugLibs,
filteredClassPath,
classpath,
)
}
}
Expand Down Expand Up @@ -118,8 +113,15 @@ class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
private def sources(id: BuildTargetIdentifier) =
buildTargets.sourceItemsToBuildTargets
.filter(_._2.iterator.asScala.contains(id))
.collect { case (path, _) =>
SourceDirectory(path.toNIO)
.collect { case (sourcePath, _) =>
if (sourcePath.isDirectory) {
SourceDirectory(sourcePath.toNIO)
} else {
StandaloneSourceFile(
sourcePath.toNIO,
sourcePath.toNIO.getFileName.toString,
)
}
}
.toSeq
}
Expand All @@ -130,5 +132,4 @@ case class DebugeeProject(
modules: Seq[Module],
libraries: Seq[Library],
unmanagedEntries: Seq[UnmanagedEntry],
classpath: Seq[Path],
)
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MainClassDebugAdapter(
val jvmOptions =
mainClass.getJvmOptions.asScala.toList :+ enableDebugInterface
val fullClasspathStr =
project.classpath.map(_.toString()).mkString(File.pathSeparator)
classPath.map(_.toString()).mkString(File.pathSeparator)
val java = JavaBinary(userJavaHome).toString()
val classpathOption = "-cp" :: fullClasspathStr :: Nil
val appOptions =
Expand Down
12 changes: 12 additions & 0 deletions tests/slow/src/test/scala/tests/mill/MillBreakpointDapSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tests.mill

import tests.MillBuildLayout
import tests.MillServerInitializer
import tests.debug.BaseBreakpointDapSuite

class MillBreakpointDapSuite
extends BaseBreakpointDapSuite(
"mill-debug-breakpoint",
MillServerInitializer,
MillBuildLayout,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class MillServerCodeLensSuite
cleanWorkspace()
writeLayout(
MillBuildLayout(
"""|/MillMinimal/src/Main.scala
"""|/a/src/Main.scala
|package foo
|
|object Main {
| def main(args: Array[String]): Unit = {
| println("Hello java!")
| }
|}
|/MillMinimal/test/src/Foo.scala
|/a/test/src/Foo.scala
|// no test lense as debug is not supported
|class Foo extends munit.FunSuite {}
|""".stripMargin,
Expand All @@ -40,27 +40,27 @@ class MillServerCodeLensSuite
_ <- server.initialize()
_ <- server.initialized()
_ <- server.executeCommand(ServerCommands.GenerateBspConfig)
_ <- server.didOpen("MillMinimal/src/Main.scala")
_ <- server.didSave("MillMinimal/src/Main.scala")(identity)
_ <- server.didOpen("a/src/Main.scala")
_ <- server.didSave("a/src/Main.scala")(identity)
_ = assertNoDiagnostics()
_ <- assertCodeLenses(
"MillMinimal/src/Main.scala",
"a/src/Main.scala",
"""|package foo
|
|<<run>>
|<<run>><<debug>>
|object Main {
| def main(args: Array[String]): Unit = {
| println("Hello java!")
| }
|}""".stripMargin,
)
_ <- assertCodeLenses(
"MillMinimal/test/src/Foo.scala",
"a/test/src/Foo.scala",
"""|// no test lense as debug is not supported
|class Foo extends munit.FunSuite {}
|""".stripMargin,
)
lenses <- server.codeLenses("MillMinimal/src/Main.scala")
lenses <- server.codeLenses("a/src/Main.scala")
_ = assert(lenses.size > 0, "No lenses were generated!")
command = lenses.head.getCommand()
_ = assertEquals(runFromCommand(command, None), Some("Hello java!"))
Expand Down
19 changes: 19 additions & 0 deletions tests/slow/src/test/scala/tests/mill/MillStepDapSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests.mill

import scala.meta.internal.metals.BuildInfo

import tests.MillBuildLayout
import tests.MillServerInitializer
import tests.debug.BaseStepDapSuite

class MillStepDapSuite
extends BaseStepDapSuite(
"mill-debug-step",
MillServerInitializer,
MillBuildLayout,
) {

// otherwise we get both Scala 2.12 and 2.13 dependencies, which is more tricky for the tests
override def scalaVersion: String = BuildInfo.scala212

}
2 changes: 1 addition & 1 deletion tests/unit/src/main/scala/tests/BuildServerLayout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object MillBuildLayout extends BuildToolLayout {
s"""|/build.sc
|import mill._, scalalib._
|
|object MillMinimal extends ScalaModule {
|object a extends ScalaModule {
| def scalaVersion = "${scalaVersion}"
| $munitModule
|}
Expand Down
93 changes: 48 additions & 45 deletions tests/unit/src/main/scala/tests/debug/BaseStepDapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,52 +92,54 @@ abstract class BaseStepDapSuite(
focusFile = "a/src/main/scala/a/ScalaMain.scala",
)

assertSteps("step-into-scala-lib", withoutVirtualDocs = true)(
sources = """|/a/src/main/scala/Main.scala
|package a
|
|object Main {
| def main(args: Array[String]): Unit = {
|>> println("foo")
| System.exit(0)
| }
|}
|""".stripMargin,
main = "a.Main",
instrument = steps => {
steps
.at("a/src/main/scala/Main.scala", line = 5)(StepIn)
.atDependency(
server.toPathFromSymbol("scala.Predef", "scala/Predef.scala"),
line = if (scalaVersion.startsWith("2.13")) 427 else 405,
)(Continue)
},
)
if (suiteName != "mill-debug-step") { // TODO: delete condition after https://github.com/com-lihaoyi/mill/issues/3148
assertSteps("step-into-scala-lib", withoutVirtualDocs = true)(
sources = """|/a/src/main/scala/Main.scala
|package a
|
|object Main {
| def main(args: Array[String]): Unit = {
|>> println("foo")
| System.exit(0)
| }
|}
|""".stripMargin,
main = "a.Main",
instrument = steps => {
steps
.at("a/src/main/scala/Main.scala", line = 5)(StepIn)
.atDependency(
server.toPathFromSymbol("scala.Predef", "scala/Predef.scala"),
line = if (scalaVersion.startsWith("2.13")) 427 else 405,
)(Continue)
},
)

assertSteps("step-into-java-lib", withoutVirtualDocs = true)(
sources = """|/a/src/main/scala/Main.scala
|package a
|
|object Main {
| def main(args: Array[String]): Unit = {
|>> System.out.println("foo")
| System.exit(0)
| }
|}
|""".stripMargin,
main = "a.Main",
instrument = steps => {
val (javaLibFile, javaLibLine) =
if (isJava17) ("java.base/java/io/PrintStream.java", 1027)
else ("java.base/java/io/PrintStream.java", 881)
steps
.at("a/src/main/scala/Main.scala", line = 5)(StepIn)
.atDependency(
server.toPathFromSymbol("java.io.PrintStream", javaLibFile),
javaLibLine,
)(Continue)
},
)
assertSteps("step-into-java-lib", withoutVirtualDocs = true)(
sources = """|/a/src/main/scala/Main.scala
|package a
|
|object Main {
| def main(args: Array[String]): Unit = {
|>> System.out.println("foo")
| System.exit(0)
| }
|}
|""".stripMargin,
main = "a.Main",
instrument = steps => {
val (javaLibFile, javaLibLine) =
if (isJava17) ("java.base/java/io/PrintStream.java", 1027)
else ("java.base/java/io/PrintStream.java", 881)
steps
.at("a/src/main/scala/Main.scala", line = 5)(StepIn)
.atDependency(
server.toPathFromSymbol("java.io.PrintStream", javaLibFile),
javaLibLine,
)(Continue)
},
)
}

assertSteps("stops-on-different-class-in-same-file")(
sources = """|/a/src/main/scala/a/Main.scala
Expand Down Expand Up @@ -178,6 +180,7 @@ abstract class BaseStepDapSuite(

for {
_ <- initialize(workspaceLayout)
_ <- server.server.indexingPromise.future
_ <- server.didFocus(focusFile)
navigator = instrument(StepNavigator(workspace))
debugger <- debugMain("a", main, navigator)
Expand Down

0 comments on commit e0f1288

Please sign in to comment.