From 33d8501ddcfb03ff3c8f1072a61e3ae87779e1ca Mon Sep 17 00:00:00 2001 From: Maciej Gajek Date: Thu, 3 Aug 2023 15:07:45 +0200 Subject: [PATCH] Add test for actionable diagnostics from compiler --- .../cli/integration/BspTestDefinitions.scala | 91 ++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala index a84c67b0b9..25bb7c99b7 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala @@ -1523,6 +1523,95 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String]) ) } + if (actualScalaVersion.startsWith("3.")) + test("actionable diagnostics from compiler") { + val inputs = TestInputs( + os.rel / "test.sc" -> + """//> using scala 3.3.2-RC1-bin-20230723-5afe621-NIGHTLY + |def foo(): Int = 23 + | + |def test: Int = foo + |// ^^^ error: missing parentheses + |""".stripMargin + ) + + withBsp(inputs, Seq(".")) { (root, localClient, remoteServer) => + async { + val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala) + val target = { + val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq + expect(targets.length == 2) + extractMainTargets(targets) + } + + val targetUri = TestUtil.normalizeUri(target.getUri) + checkTargetUri(root, targetUri) + + val targets = List(target).asJava + + val compileResp = await { + remoteServer + .buildTargetCompile(new b.CompileParams(targets)) + .asScala + } + expect(compileResp.getStatusCode == b.StatusCode.ERROR) + + val diagnosticsParams = { + val diagnostics = localClient.diagnostics() + val params = diagnostics(2) + expect(params.getBuildTarget.getUri == targetUri) + expect( + TestUtil.normalizeUri(params.getTextDocument.getUri) == + TestUtil.normalizeUri((root / "test.sc").toNIO.toUri.toASCIIString) + ) + params + } + + val diagnostics = diagnosticsParams.getDiagnostics.asScala + expect(diagnostics.size == 1) + + val theDiagnostic = diagnostics.head + + checkDiagnostic( + diagnostic = theDiagnostic, + expectedMessage = + "method foo in class test$_ must be called with () argument", + expectedSeverity = b.DiagnosticSeverity.ERROR, + expectedStartLine = 3, + expectedStartCharacter = 16, + expectedEndLine = 3, + expectedEndCharacter = 19 + ) + + // Shouldn't dataKind be set to "scala"? expect(theDiagnostic.getDataKind == "scala") + + val gson = new com.google.gson.Gson() + + val scalaDiagnostic: b.ScalaDiagnostic = gson.fromJson( + theDiagnostic.getData.toString, + classOf[b.ScalaDiagnostic] + ) + + val actions = scalaDiagnostic.getActions.asScala + expect(actions.size == 1) + + val action = actions.head + expect(action.getTitle == "Insert ()") + + val edit = action.getEdit + expect(edit.getChanges.asScala.size == 1) + val change = edit.getChanges.asScala.head + + val expectedRange = new b.Range( + new b.Position(11, 19), + new b.Position(11, 19) + ) + expect(change.getRange == expectedRange) + expect(change.getNewText == "()") + } + } + } + private def checkIfBloopProjectIsInitialised( root: os.Path, buildTargetsResp: b.WorkspaceBuildTargetsResult @@ -1577,7 +1666,7 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String]) expect(diagnostic.getRange.getEnd.getLine == expectedEndLine) expect(diagnostic.getRange.getEnd.getCharacter == expectedEndCharacter) if (strictlyCheckMessage) - expect(diagnostic.getMessage == expectedMessage) + assertNoDiff(diagnostic.getMessage, expectedMessage) else expect(diagnostic.getMessage.contains(expectedMessage)) for (es <- expectedSource)