diff --git a/mtags/src/main/scala/scala/meta/internal/metals/RecursivelyDelete.scala b/mtags/src/main/scala/scala/meta/internal/metals/RecursivelyDelete.scala index 7d627078044..b1e5a8c3566 100644 --- a/mtags/src/main/scala/scala/meta/internal/metals/RecursivelyDelete.scala +++ b/mtags/src/main/scala/scala/meta/internal/metals/RecursivelyDelete.scala @@ -10,8 +10,8 @@ import java.nio.file.attribute.BasicFileAttributes import scala.meta.io.AbsolutePath object RecursivelyDelete { - def apply(root: AbsolutePath): Int = { - if (root.isFile) { + def apply(root: AbsolutePath, excludedNames: Set[String] = Set.empty): Int = { + if (root.isFile && !excludedNames(root.toNIO.getFileName.toString)) { Files.delete(root.toNIO) 1 } else if (root.isDirectory) { @@ -22,10 +22,12 @@ object RecursivelyDelete { override def visitFile( file: Path, attrs: BasicFileAttributes - ): FileVisitResult = { + ): FileVisitResult = if (!excludedNames(file.getFileName.toString)) { count += 1 Files.delete(file) super.visitFile(file, attrs) + } else { + super.visitFile(file, attrs) } override def postVisitDirectory( dir: Path, diff --git a/tests/slow/src/main/scala/tests/scalacli/BaseScalaCliSuite.scala b/tests/slow/src/main/scala/tests/scalacli/BaseScalaCliSuite.scala index 37f19ae4ff4..32af2b132a3 100644 --- a/tests/slow/src/main/scala/tests/scalacli/BaseScalaCliSuite.scala +++ b/tests/slow/src/main/scala/tests/scalacli/BaseScalaCliSuite.scala @@ -9,6 +9,8 @@ import scala.concurrent.Promise import scala.concurrent.duration._ import scala.meta.internal.metals.Messages +import scala.meta.internal.metals.MetalsEnrichments._ +import scala.meta.internal.metals.MetalsServerConfig import scala.meta.internal.metals.scalacli.ScalaCli import ch.epfl.scala.bsp4j.MessageType @@ -21,6 +23,16 @@ abstract class BaseScalaCliSuite(protected val scalaVersion: String) extends BaseLspSuite(s"scala-cli-$scalaVersion") with ScriptsAssertions { + override def serverConfig: MetalsServerConfig = + super.serverConfig.copy(loglevel = "debug") + + override def beforeEach(context: BeforeEach): Unit = { + super.beforeEach(context) + dapClient.touch() + dapServer.touch() + bspTrace.touch() + } + private val scheduler = Executors.newSingleThreadScheduledExecutor() private def timeout( diff --git a/tests/slow/src/test/scala/tests/mill/MillServerSuite.scala b/tests/slow/src/test/scala/tests/mill/MillServerSuite.scala index c0d62627182..0c015c1492a 100644 --- a/tests/slow/src/test/scala/tests/mill/MillServerSuite.scala +++ b/tests/slow/src/test/scala/tests/mill/MillServerSuite.scala @@ -28,8 +28,6 @@ class MillServerSuite val supportedBspVersion = V.millVersion val scalaVersion = V.scala213 def buildTool: MillBuildTool = MillBuildTool(() => userConfig, workspace) - def bspTrace: AbsolutePath = - workspace.resolve(".metals").resolve("bsp.trace.json") override def currentDigest( workspace: AbsolutePath @@ -38,32 +36,9 @@ class MillServerSuite override def beforeEach(context: BeforeEach): Unit = { super.beforeEach(context) cleanWorkspace() - bspTrace.writeText("") + bspTrace.touch() } - private def logBspTraces(): Option[Unit] = - bspTrace.readTextOpt.map(trace => scribe.warn(s"""|BSP trace: - |$trace - |""".stripMargin)) - - override def munitTestTransforms: List[TestTransform] = - super.munitTestTransforms :+ - new TestTransform( - "Print BSP traces", - { test => - test.withBody(() => - test - .body() - .andThen { - case Failure(exception) => - logBspTraces() - exception - case Success(value) => value - }(munitExecutionContext) - ) - }, - ) - test("too-old") { writeLayout(MillBuildLayout("", V.scala213, testDep = None, preBspVersion)) for { diff --git a/tests/unit/src/main/scala/tests/BaseCodeLensLspSuite.scala b/tests/unit/src/main/scala/tests/BaseCodeLensLspSuite.scala index 9b15a5574eb..7f09c349dfa 100644 --- a/tests/unit/src/main/scala/tests/BaseCodeLensLspSuite.scala +++ b/tests/unit/src/main/scala/tests/BaseCodeLensLspSuite.scala @@ -4,6 +4,7 @@ import scala.concurrent.Future import scala.meta.internal.builds.ShellRunner import scala.meta.internal.metals.MetalsEnrichments._ +import scala.meta.internal.metals.MetalsServerConfig import ch.epfl.scala.bsp4j.DebugSessionParams import com.google.gson.JsonObject @@ -16,6 +17,16 @@ abstract class BaseCodeLensLspSuite( initializer: BuildServerInitializer = QuickBuildInitializer, ) extends BaseLspSuite(name, initializer) { + override def serverConfig: MetalsServerConfig = + super.serverConfig.copy(loglevel = "debug") + + override def beforeEach(context: BeforeEach): Unit = { + super.beforeEach(context) + dapClient.touch() + dapServer.touch() + bspTrace.touch() + } + protected def runFromCommand( cmd: Command, javaHome: Option[String], diff --git a/tests/unit/src/main/scala/tests/BaseDapSuite.scala b/tests/unit/src/main/scala/tests/BaseDapSuite.scala index 2a19c739489..5a30bf2bd02 100644 --- a/tests/unit/src/main/scala/tests/BaseDapSuite.scala +++ b/tests/unit/src/main/scala/tests/BaseDapSuite.scala @@ -3,12 +3,9 @@ package tests import java.util.Collections.emptyList import scala.concurrent.Future -import scala.util.Failure -import scala.util.Success import scala.meta.internal.metals.MetalsEnrichments._ -import scala.meta.internal.metals.Trace -import scala.meta.internal.metals.debug.DebugProtocol +import scala.meta.internal.metals.MetalsServerConfig import scala.meta.internal.metals.debug.DebugStep._ import scala.meta.internal.metals.debug.DebugWorkspaceLayout import scala.meta.internal.metals.debug.StepNavigator @@ -26,42 +23,16 @@ abstract class BaseDapSuite( initializer: BuildServerInitializer, buildToolLayout: BuildToolLayout, ) extends BaseLspSuite(suiteName, initializer) { - - private val dapClient = Trace.protocolTracePath(DebugProtocol.clientName) - private val dapServer = Trace.protocolTracePath(DebugProtocol.serverName) + override def serverConfig: MetalsServerConfig = + super.serverConfig.copy(loglevel = "debug") override def beforeEach(context: BeforeEach): Unit = { super.beforeEach(context) dapClient.touch() dapServer.touch() + bspTrace.touch() } - protected def logDapTraces(): Unit = { - if (isCI) { - scribe.warn("The DAP test failed, printing the traces") - scribe.warn(dapClient.toString() + ":\n" + dapClient.readText) - scribe.warn(dapServer.toString() + ":\n" + dapServer.readText) - } - } - - override def munitTestTransforms: List[TestTransform] = - super.munitTestTransforms :+ - new TestTransform( - "Print DAP traces", - { test => - test.withBody(() => - test - .body() - .andThen { - case Failure(exception) => - logDapTraces() - exception - case Success(value) => value - }(munitExecutionContext) - ) - }, - ) - def debugMain( buildTarget: String, main: String, diff --git a/tests/unit/src/main/scala/tests/BaseLspSuite.scala b/tests/unit/src/main/scala/tests/BaseLspSuite.scala index f8ba9ee1d37..bfbed72d229 100644 --- a/tests/unit/src/main/scala/tests/BaseLspSuite.scala +++ b/tests/unit/src/main/scala/tests/BaseLspSuite.scala @@ -6,6 +6,8 @@ import java.util.concurrent.Executors import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContextExecutorService import scala.concurrent.Future +import scala.util.Failure +import scala.util.Success import scala.util.control.NonFatal import scala.meta.internal.io.PathIO @@ -14,11 +16,14 @@ import scala.meta.internal.metals.Debug import scala.meta.internal.metals.ExecuteClientCommandConfig import scala.meta.internal.metals.Icons import scala.meta.internal.metals.InitializationOptions +import scala.meta.internal.metals.MetalsEnrichments._ import scala.meta.internal.metals.MetalsServerConfig import scala.meta.internal.metals.MtagsResolver import scala.meta.internal.metals.RecursivelyDelete import scala.meta.internal.metals.Time +import scala.meta.internal.metals.Trace import scala.meta.internal.metals.UserConfiguration +import scala.meta.internal.metals.debug.DebugProtocol import scala.meta.internal.metals.logging.MetalsLogger import scala.meta.io.AbsolutePath @@ -49,6 +54,14 @@ abstract class BaseLspSuite( var workspace: AbsolutePath = _ var onStartCompilation: () => Unit = () => () + protected def metalsDotDir: AbsolutePath = workspace.resolve(".metals") + protected def dapClient: AbsolutePath = + Trace.protocolTracePath(DebugProtocol.clientName, metalsDotDir) + protected def dapServer: AbsolutePath = + Trace.protocolTracePath(DebugProtocol.serverName, metalsDotDir) + protected def bspTrace: AbsolutePath = + Trace.protocolTracePath("bsp", metalsDotDir) + protected def initializationOptions: Option[InitializationOptions] = None private var useVirtualDocs = false @@ -65,6 +78,35 @@ abstract class BaseLspSuite( } } + override def munitTestTransforms: List[TestTransform] = + super.munitTestTransforms :+ + new TestTransform( + "Print DAP traces", + { test => + test.withBody(() => + test + .body() + .andThen { + case Failure(exception) => + logDapTraces() + exception + case Success(value) => value + }(munitExecutionContext) + ) + }, + ) + + protected def logDapTraces(): Unit = { + if (isCI) { + scribe.warn("The test failed, printing the traces:") + if (dapClient.exists) + scribe.warn(dapClient.toString() + ":\n" + dapClient.readText) + if (dapServer.exists) + scribe.warn(dapServer.toString() + ":\n" + dapServer.readText) + if (bspTrace.exists) + scribe.warn(bspTrace.toString() + ":\n" + bspTrace.readText) + } + } def writeLayout(layout: String): Unit = { FileLayout.fromString(layout, workspace) } @@ -213,7 +255,14 @@ abstract class BaseLspSuite( def cleanWorkspace(): Unit = { if (workspace.isDirectory) { try { - RecursivelyDelete(workspace) + RecursivelyDelete( + root = workspace, + excludedNames = Set( + "bsp.trace.json", + "dap-client.trace.json", + "dap-server.trace.json", + ), + ) Files.createDirectories(workspace.toNIO) } catch { case NonFatal(_) =>