From 7046828ed893cb7f0d67477b2a52501c7ffb7fb2 Mon Sep 17 00:00:00 2001 From: Krzysztof Romanowski Date: Wed, 30 Mar 2022 14:37:46 +0200 Subject: [PATCH 1/2] wip --- ...Tests.scala => DirectiveParsingTest.scala} | 49 +++++++++++++------ project/deps.sc | 2 +- 2 files changed, 36 insertions(+), 15 deletions(-) rename modules/build/src/test/scala/scala/build/tests/{ScalaPreprocessorTests.scala => DirectiveParsingTest.scala} (58%) diff --git a/modules/build/src/test/scala/scala/build/tests/ScalaPreprocessorTests.scala b/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala similarity index 58% rename from modules/build/src/test/scala/scala/build/tests/ScalaPreprocessorTests.scala rename to modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala index e914211315..106505d6c6 100644 --- a/modules/build/src/test/scala/scala/build/tests/ScalaPreprocessorTests.scala +++ b/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala @@ -6,20 +6,24 @@ import com.virtuslab.using_directives.custom.model.UsingDirectiveKind import scala.build.errors.Diagnostic import scala.build.preprocessing.{ExtractedDirectives, ScopePath} +import scala.meta.internal.semanticdb +import scala.build.errors.Severity -class ScalaPreprocessorTests extends munit.FunSuite { +class DirectiveParsingTest extends munit.FunSuite { + val path = os.pwd trait Check { def test(diags: Seq[Diagnostic]): Boolean } + case class Warn(messagePattern: String, line: Int, index: Int) extends Check { def test(diags: Seq[Diagnostic]): Boolean = { val matching = diags.filter(_.positions.exists { case Position.File(_, (`line`, `index`), _) => true case _ => false - }) + }).filter(_.severity == Severity.Warning) val Regex = s".*$messagePattern.*".r matching.map(_.message).exists { case Regex() => true @@ -36,8 +40,17 @@ class ScalaPreprocessorTests extends munit.FunSuite { matching.isEmpty } } + + case class Error(messagePattern: String) extends Check { + def test(diags: Seq[Diagnostic]): Boolean = { + val Regex = s".*$messagePattern.*".r + val matching = diags.filter(d => d.severity == Severity.Error && Regex.unapplySeq(d.message).nonEmpty) + expect(matching.nonEmpty) + matching.nonEmpty + } + } - private def testWarnings(lines: String*)(expectedWarnings: Check*): Unit = { + private def testDiagnostics(lines: String*)(expectedWarnings: Check*): List[Diagnostic] = { val persistentLogger = new PersistentDiagnosticLogger(Logger.nop) val code = lines.mkString("\n").toCharArray() val res = ExtractedDirectives.from( @@ -47,11 +60,13 @@ class ScalaPreprocessorTests extends munit.FunSuite { UsingDirectiveKind.values(), ScopePath.fromPath(path) ) - expect(res.isRight) + + if (!expectedWarnings.exists(_.isInstanceOf[Error])) expect(res.isRight) val diags = persistentLogger.diagnostics if (expectedWarnings.isEmpty) expect(diags.isEmpty) else expectedWarnings.foreach(warn => expect(warn.test(diags))) + diags } val commentDirective = """// using commentDirective "b" """ @@ -59,8 +74,8 @@ class ScalaPreprocessorTests extends munit.FunSuite { val directive = """using directive "b" """ test("Test deprecation warnings about comments") { - testWarnings(commentDirective)(Warn("deprecated", 0, 0)) - testWarnings( + testDiagnostics(commentDirective)(Warn("deprecated", 0, 0)) + testDiagnostics( commentDirective, specialCommentDirective, commentDirective @@ -71,21 +86,27 @@ class ScalaPreprocessorTests extends munit.FunSuite { } test("Test warnings about mixing syntax") { - testWarnings(directive, specialCommentDirective)(Warn("ignored", 1, 0)) - testWarnings(directive, commentDirective)(Warn("ignored", 1, 0)) - testWarnings(specialCommentDirective, commentDirective)(Warn("ignored", 1, 0)) - testWarnings(commentDirective, specialCommentDirective)(Warn("deprecated", 0, 0)) + testDiagnostics(directive, specialCommentDirective)(Warn("ignored", 1, 0)) + testDiagnostics(directive, commentDirective)(Warn("ignored", 1, 0)) + testDiagnostics(specialCommentDirective, commentDirective)(Warn("ignored", 1, 0)) + testDiagnostics(commentDirective, specialCommentDirective)(Warn("deprecated", 0, 0)) } test("Plain comment only result in no ignored warning") { - testWarnings(commentDirective)(NoWarn("ignored")) + testDiagnostics(commentDirective)(NoWarn("ignored")) } test("@using is deprecated") { def addAt(s: String) = s.replace("using ", "@using ") - testWarnings(addAt(commentDirective))(Warn("syntax", 0, 3), Warn("keyword", 0, 3)) - testWarnings(addAt(directive))(Warn("syntax", 0, 0), Warn("keyword", 0, 0)) - testWarnings(addAt(specialCommentDirective))(Warn("syntax", 0, 4), Warn("keyword", 0, 4)) + testDiagnostics(addAt(commentDirective))(Warn("syntax", 0, 3), Warn("keyword", 0, 3)) + testDiagnostics(addAt(directive))(Warn("syntax", 0, 0), Warn("keyword", 0, 0)) + testDiagnostics(addAt(specialCommentDirective))(Warn("syntax", 0, 4), Warn("keyword", 0, 4)) + } + + test("interpolator in dependency") { + val diags = testDiagnostics("""//> using lib ivy"org.scala-sbt::io:1.6.0"""")(Error("illegal")) + println(diags) + } } diff --git a/project/deps.sc b/project/deps.sc index 85b1dfad4c..da1d6f10a4 100644 --- a/project/deps.sc +++ b/project/deps.sc @@ -119,7 +119,7 @@ object Deps { def svm = ivy"org.graalvm.nativeimage:svm:$graalVmVersion" def swoval = ivy"com.swoval:file-tree-views:2.1.9" def testInterface = ivy"org.scala-sbt:test-interface:1.0" - def usingDirectives = ivy"org.virtuslab:using_directives:0.0.7-277bd4a-SNAPSHOT" + def usingDirectives = ivy"org.virtuslab:using_directives:0.0.7-d329f97-SNAPSHOT" val metaconfigTypesafe = ivy"com.geirsson::metaconfig-typesafe-config:0.10.0" } From a71680b8e17c0bce5dfa09e1c3efb87b11f5a8e1 Mon Sep 17 00:00:00 2001 From: Krzysztof Romanowski Date: Wed, 30 Mar 2022 15:25:19 +0200 Subject: [PATCH 2/2] Update using directives to 0.8 and add tests --- .../build/tests/DirectiveParsingTest.scala | 56 ++++++++++++++----- project/deps.sc | 2 +- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala b/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala index 106505d6c6..5f4d893c3b 100644 --- a/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala +++ b/modules/build/src/test/scala/scala/build/tests/DirectiveParsingTest.scala @@ -6,11 +6,11 @@ import com.virtuslab.using_directives.custom.model.UsingDirectiveKind import scala.build.errors.Diagnostic import scala.build.preprocessing.{ExtractedDirectives, ScopePath} -import scala.meta.internal.semanticdb import scala.build.errors.Severity +import scala.build.errors.CompositeBuildException +import scala.build.errors.BuildException class DirectiveParsingTest extends munit.FunSuite { - val path = os.pwd @@ -40,17 +40,21 @@ class DirectiveParsingTest extends munit.FunSuite { matching.isEmpty } } - + case class Error(messagePattern: String) extends Check { - def test(diags: Seq[Diagnostic]): Boolean = { - val Regex = s".*$messagePattern.*".r - val matching = diags.filter(d => d.severity == Severity.Error && Regex.unapplySeq(d.message).nonEmpty) + def test(diags: Seq[Diagnostic]): Boolean = + testMsgs(diags.filter(_.severity == Severity.Error).map(_.message)) + + def testMsgs(diags: Seq[String]): Boolean = { + val Regex = s".*$messagePattern.*".r + println("Errors: " + diags) + val matching = diags.filter(d => Regex.unapplySeq(d).nonEmpty) expect(matching.nonEmpty) matching.nonEmpty } } - private def testDiagnostics(lines: String*)(expectedWarnings: Check*): List[Diagnostic] = { + private def testDiagnostics(lines: String*)(checks: Check*): List[Diagnostic] = { val persistentLogger = new PersistentDiagnosticLogger(Logger.nop) val code = lines.mkString("\n").toCharArray() val res = ExtractedDirectives.from( @@ -61,12 +65,32 @@ class DirectiveParsingTest extends munit.FunSuite { ScopePath.fromPath(path) ) - if (!expectedWarnings.exists(_.isInstanceOf[Error])) expect(res.isRight) + def checkDiag(checks: Seq[Check]) = { + val diags = persistentLogger.diagnostics + if (checks.isEmpty) expect(diags.isEmpty) + else checks.foreach(warn => expect(warn.test(diags))) + diags + } + + val (expectedError, expectedWarnings) = + checks.partitionMap { case e: Error => Left(e); case o => Right(o) } + if (expectedError.isEmpty) expect(res.isRight) + else + res match { + case Left(exception) => + def flatten(e: BuildException): Seq[String] = e match { + case c: CompositeBuildException => + c.exceptions.flatMap(flatten) + case e => + Seq(e.getMessage()) + } + val msgs = flatten(exception) + expectedError.foreach(e => expect(e.testMsgs(msgs))) + case _ => + checkDiag(expectedError) + } + checkDiag(expectedWarnings) - val diags = persistentLogger.diagnostics - if (expectedWarnings.isEmpty) expect(diags.isEmpty) - else expectedWarnings.foreach(warn => expect(warn.test(diags))) - diags } val commentDirective = """// using commentDirective "b" """ @@ -104,9 +128,13 @@ class DirectiveParsingTest extends munit.FunSuite { } test("interpolator in dependency") { - val diags = testDiagnostics("""//> using lib ivy"org.scala-sbt::io:1.6.0"""")(Error("illegal")) + val diags = + testDiagnostics("""//> using lib ivy"org.scala-sbt::io:1.6.0"""")(Error("interpolator")) println(diags) - } + test("unicode in using directives") { + val diags = testDiagnostics("""using nativeMode “release-full”"""")(Error("illegal")) + println(diags) + } } diff --git a/project/deps.sc b/project/deps.sc index da1d6f10a4..c1624695ae 100644 --- a/project/deps.sc +++ b/project/deps.sc @@ -119,7 +119,7 @@ object Deps { def svm = ivy"org.graalvm.nativeimage:svm:$graalVmVersion" def swoval = ivy"com.swoval:file-tree-views:2.1.9" def testInterface = ivy"org.scala-sbt:test-interface:1.0" - def usingDirectives = ivy"org.virtuslab:using_directives:0.0.7-d329f97-SNAPSHOT" + def usingDirectives = ivy"org.virtuslab:using_directives:0.0.8" val metaconfigTypesafe = ivy"com.geirsson::metaconfig-typesafe-config:0.10.0" }