Skip to content

Commit

Permalink
single file test
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 24, 2023
1 parent c511174 commit b859a4a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class FallbackMetalsLspService(
folderVisibleName,
headDoctor,
) {

buildServerPromise.success(())
indexingPromise.success(())

private val files: AtomicReference[Set[AbsolutePath]] = new AtomicReference(
Set.empty
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2152,10 +2152,12 @@ class MetalsLspService(
}

val scalaCliServers = scalaCli.servers
val scalaCliBuildChanges = otherChanges.groupBy { change =>
val groupedByServer = otherChanges.groupBy { change =>
val connOpt = buildTargets.buildServerOf(change.getTarget)
connOpt.flatMap(conn => scalaCliServers.find(_ == conn))
}
val scalaCliAffectedServers = groupedByServer.collect { case (Some(server), _) => server }
val mainConnectionChanges = groupedByServer.get(None)

if (ammoniteChanges.nonEmpty)
ammonite.importBuild().onComplete {
Expand All @@ -2164,9 +2166,8 @@ class MetalsLspService(
scribe.error("Error re-importing Ammonite build", exception)
}

scalaCliBuildChanges.collect { case (Some(server), _) =>
server
.importBuild()
scalaCliAffectedServers.map { server =>
server.importBuild()
.onComplete {
case Success(()) =>
case Failure(exception) =>
Expand All @@ -2178,7 +2179,7 @@ class MetalsLspService(
}
}

if (scalaCliBuildChanges.exists(_._1.isEmpty)) {
if (mainConnectionChanges.nonEmpty) {
bspSession match {
case None => scribe.warn("No build server connected")
case Some(session) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class WorkspaceFolders(

private val folderServices: AtomicReference[WorkspaceFoldersServices] = {
val (scalaProjects, nonScalaProjects) =
initialFolders.partition(_.isMetalsProject) match {
case (Nil, nonScala) => (List(nonScala.head), nonScala.tail)
case t => t
}
initialFolders.partition(_.isMetalsProject)
val services = scalaProjects.map(createService(_))
new AtomicReference(WorkspaceFoldersServices(services, nonScalaProjects))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class ScalaCliServers(
buildTargets: BuildTargets,
)(implicit ec: ExecutionContextExecutorService)
extends Cancelable {
val buildTargetsData = new TargetData
private val serversRef: AtomicReference[Set[ScalaCli]] = new AtomicReference(
Set.empty
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ class MetalsLanguageServer(
if (folderPathsWithScala.nonEmpty) folderPathsWithScala
else List(fallbackServicePath)

folderPathsWithScala.foreach(folder =>
new StdReportContext(folder.toNIO, _ => None).cleanUpOldReports()
)
folderPathsWithScala.foreach(folder =>
new StdReportContext(folder.toNIO, _ => None).cleanUpOldReports()
)

MetalsLogger.setupLspLogger(
logPaths,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/main/scala/tests/BaseLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ abstract class BaseLspSuite(
server,
client,
expectError,
layout.keys.toList,
Some(layout.keys.toList),
)
}

Expand Down
23 changes: 13 additions & 10 deletions tests/unit/src/main/scala/tests/BuildServerInitializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sealed trait BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
workspaceFolders: List[String] = Nil,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult]
}

Expand All @@ -38,11 +38,13 @@ object QuickBuildInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
workspaceFolders: List[String] = Nil,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
val foldersToInit =
if (workspaceFolders.nonEmpty) workspaceFolders.map(workspace.resolve)
else List(workspace)
workspaceFolders match {
case Some(workspaceFolders) => workspaceFolders.map(workspace.resolve)
case None => List(workspace)
}
foldersToInit.foreach(QuickBuild.bloopInstall)
for {
initializeResult <- server.initialize(workspaceFolders)
Expand All @@ -67,7 +69,7 @@ object BloopImportInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
workspaceFolders: List[String] = Nil,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
for {
initializeResult <- server.initialize()
Expand Down Expand Up @@ -95,11 +97,12 @@ object SbtServerInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
workspaceFolders: List[String] = Nil,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
val paths =
if (workspaceFolders.isEmpty) List(workspace)
else workspaceFolders.map(workspace.resolve)
val paths = workspaceFolders match {
case Some(workspaceFolders) => workspaceFolders.map(workspace.resolve)
case None => List(workspace)
}
paths.foreach { path =>
val sbtVersion =
SbtBuildTool
Expand Down Expand Up @@ -174,7 +177,7 @@ object MillServerInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
workspaceFolders: List[String] = Nil,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
for {
initializeResult <- server.initialize()
Expand Down
20 changes: 13 additions & 7 deletions tests/unit/src/main/scala/tests/TestingServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ final case class TestingServer(
}

def initialize(
workspaceFolders: List[String] = Nil
workspaceFolders: Option[List[String]] = None
): Future[l.InitializeResult] = {
val params = new InitializeParams
val workspaceCapabilities = new WorkspaceClientCapabilities()
Expand Down Expand Up @@ -578,12 +578,18 @@ final case class TestingServer(
Map.empty.asJava.toJson,
)
)
params.setWorkspaceFolders(
workspaceFolders
.map(file => new WorkspaceFolder(toPath(file).toURI.toString))
.asJava
)
params.setRootUri(workspace.toURI.toString)

workspaceFolders match {
case Some(workspaceFolders) =>
params.setWorkspaceFolders(
workspaceFolders
.map(file => new WorkspaceFolder(toPath(file).toURI.toString))
.asJava
)
case None =>
params.setRootUri(workspace.toURI.toString)
}

languageServer.initialize(params).asScala
}

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/src/test/scala/tests/SingleFileSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tests

class SingleFileSuite extends BaseCompletionLspSuite("workspaceFolderSuite") {

test("basic") {
cleanWorkspace()
writeLayout(
"""|/A.scala
|case class MyObjectA() {
| val i: Int = "aaa"
|}
|""".stripMargin
)
for {
_ <- initialize(Map.empty[String, String], expectError = false)
_ <- server.didOpen("A.scala")
_ = assertNoDiff(
server.client.workspaceDiagnostics,
"""|A.scala:2:16: error: Found: ("aaa" : String)
|Required: Int
| val i: Int = "aaa"
| ^^^^^
|""".stripMargin,
)
} yield ()
}

}

0 comments on commit b859a4a

Please sign in to comment.