Skip to content

Commit

Permalink
chore: Add tests for Bazel 7
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Sep 3, 2024
1 parent 8ba2b4f commit 7572848
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package scala.meta.internal.builds

import scala.meta.internal.metals.BuildInfo
import scala.meta.internal.metals.JavaBinary
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.UserConfiguration
import scala.meta.io.AbsolutePath

import coursierapi.Dependency
import coursierapi.Fetch
import scala.meta.internal.metals.BuildInfo

case class BazelBuildTool(
userConfig: () => UserConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ final class Compilations(
val originId = "METALS-$" + UUID.randomUUID().toString
val params = new b.CompileParams(targets.asJava)
params.setOriginId(originId)
if (
userConfiguration().verboseCompilation && (connection.isBloop || connection.isScalaCLI)
) {
params.setArguments(List("--verbose", "--best-effort").asJava)
} else { params.setArguments(List("--best-effort").asJava) }
if (connection.isBloop || connection.isScalaCLI) {
if (userConfiguration().verboseCompilation)
params.setArguments(List("--verbose", "--best-effort").asJava)
else params.setArguments(List("--best-effort").asJava)
}
targets.foreach { target =>
isCompiling(target) = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ object MetalsEnrichments
val ld = new l.Diagnostic(
diag.getRange.toLsp,
fansi.Str(diag.getMessage, ErrorMode.Strip).plainText,
diag.getSeverity.toLsp,
if (diag.getSeverity == null) l.DiagnosticSeverity.Error
else diag.getSeverity.toLsp,
if (diag.getSource == null) "scalac" else diag.getSource,
)
Option(diag.getCode()).foreach { code =>
Expand Down
103 changes: 54 additions & 49 deletions tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BazelLspSuite
val buildTool: BazelBuildTool = BazelBuildTool(() => userConfig, workspace)

val bazelVersion = "6.4.0"
val bazel7Version = "7.3.0"

def bazelBspConfig: AbsolutePath = workspace.resolve(".bsp/bazelbsp.json")

Expand All @@ -38,55 +39,59 @@ class BazelLspSuite
val importMessage: String =
GenerateBspAndConnect.params("bazel", "bazelbsp").getMessage()

test("basic") {
cleanWorkspace()
for {
_ <- initialize(
BazelBuildLayout(workspaceLayout, V.bazelScalaVersion, bazelVersion)
)
_ = assertNoDiff(
client.workspaceMessageRequests,
List(
importMessage
).mkString("\n"),
)
_ = assert(bazelBspConfig.exists)
_ = client.messageRequests.clear() // restart
_ = assertStatus(_.isInstalled)
_ = assertNoDiff(client.workspaceDiagnostics, "")
_ <- server.didChange("WORKSPACE")(_ + "\n# comment")
_ <- server.didSave("WORKSPACE")(identity)
// Comment changes do not trigger "re-import project" request
_ = assertNoDiff(client.workspaceMessageRequests, "")
_ <- server.didChange("Hello.scala") { text =>
text.replace("def hello: String", "def hello: Int")
}
_ <- server.didSave("Hello.scala")(identity)
_ = assertNoDiff(
client.workspaceDiagnostics,
"""|Hello.scala:4:20: error: type mismatch;
| found : String("Hello")
| required: Int
| def hello: Int = "Hello"
| ^
| def hello: Int = "Hello"
| ^
|""".stripMargin,
)
_ <- server.didChange(s"BUILD") { text =>
text.replace("\"hello\"", "\"hello1\"")
for (bazelVersion <- List(bazelVersion, bazel7Version)) {
test(s"basic-$bazelVersion") {
cleanWorkspace()
for {
_ <- initialize(
BazelBuildLayout(workspaceLayout, V.bazelScalaVersion, bazelVersion)
)
_ = assertNoDiff(
client.workspaceMessageRequests,
List(
importMessage
).mkString("\n"),
)
_ = assert(bazelBspConfig.exists)
_ = client.messageRequests.clear() // restart
_ = assertStatus(_.isInstalled)
_ = assertNoDiff(client.workspaceDiagnostics, "")
_ <- server.didChange("WORKSPACE")(_ + "\n# comment")
_ <- server.didSave("WORKSPACE")(identity)
// Comment changes do not trigger "re-import project" request
_ = assertNoDiff(client.workspaceMessageRequests, "")
_ <- server.didChange("Hello.scala") { text =>
text.replace("def hello: String", "def hello: Int")
}
_ <- server.didSave("Hello.scala")(identity)
_ = assertNoDiff(
client.workspaceDiagnostics { diag =>
diag.getSeverity() != null
},
"""|Hello.scala:4:20: error: type mismatch;
| found : String("Hello")
| required: Int
| def hello: Int = "Hello"
| ^
| def hello: Int = "Hello"
| ^
|""".stripMargin,
)
_ <- server.didChange(s"BUILD") { text =>
text.replace("\"hello\"", "\"hello1\"")
}
_ = assertNoDiff(client.workspaceMessageRequests, "")
_ = client.generateBspAndConnect = GenerateBspAndConnect.yes
_ <- server.didSave(s"BUILD")(identity)
} yield {
assertNoDiff(
client.workspaceMessageRequests,
List(
importBuildChangesMessage
).mkString("\n"),
)
server.assertBuildServerConnection()
}
_ = assertNoDiff(client.workspaceMessageRequests, "")
_ = client.generateBspAndConnect = GenerateBspAndConnect.yes
_ <- server.didSave(s"BUILD")(identity)
} yield {
assertNoDiff(
client.workspaceMessageRequests,
List(
importBuildChangesMessage
).mkString("\n"),
)
server.assertBuildServerConnection()
}
}

Expand Down Expand Up @@ -330,7 +335,7 @@ class BazelLspSuite
}

test("update-projectview") {
cleanWorkspace()
// cleanWorkspace()
writeLayout(
BazelBuildLayout(workspaceLayout, V.bazelScalaVersion, bazelVersion)
)
Expand Down
17 changes: 14 additions & 3 deletions tests/unit/src/main/scala/tests/TestingClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,13 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers)
def pathDiagnostics(filename: String): String = {
pathDiagnostics(toPath(filename))
}
def pathDiagnostics(path: AbsolutePath): String = {
def pathDiagnostics(
path: AbsolutePath,
filter: Diagnostic => Boolean = _ => true,
): String = {
val isDeleted = !path.isFile
val diags = diagnostics.getOrElse(path, Nil).sortBy(_.getRange)
val diags =
diagnostics.getOrElse(path, Nil).filter(filter).sortBy(_.getRange)
val relpath =
if (path.isJarFileSystem)
path.toString.stripPrefix("/")
Expand Down Expand Up @@ -275,7 +279,14 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers)
}
def workspaceDiagnostics: String = {
val paths = diagnostics.keys.toList.sortBy(_.toURI.toString)
paths.map(pathDiagnostics).mkString
paths.map(pathDiagnostics(_)).mkString
}

def workspaceDiagnostics(
filter: Diagnostic => Boolean = _ => true
): String = {
val paths = diagnostics.keys.toList.sortBy(_.toURI.toString)
paths.map(pathDiagnostics(_, filter)).mkString
}

override def registerCapability(
Expand Down

0 comments on commit 7572848

Please sign in to comment.