/
DirectiveParsingTest.scala
136 lines (116 loc) · 4.37 KB
/
DirectiveParsingTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package scala.build
package tests
import com.eed3si9n.expecty.Expecty.{assert => expect}
import scala.build.errors.Diagnostic
import scala.build.preprocessing.{ExtractedDirectives, ScopePath}
import scala.build.errors.Severity
import scala.build.errors.CompositeBuildException
import scala.build.errors.BuildException
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
case _ => false
}
}
}
case class NoWarn(messagePattern: String) extends Check {
def test(diags: Seq[Diagnostic]): Boolean = {
val Regex = s".*$messagePattern.*".r
val matching = diags.filter(d => Regex.unapplySeq(d.message).nonEmpty)
expect(matching.isEmpty)
matching.isEmpty
}
}
case class Error(messagePattern: String) extends Check {
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*)(checks: Check*): List[Diagnostic] = {
val persistentLogger = new PersistentDiagnosticLogger(Logger.nop)
val code = lines.mkString("\n").toCharArray()
val res = ExtractedDirectives.from(
code,
Right(path),
persistentLogger,
maybeRecoverOnError = e => Some(e)
)
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 commentDirective = """// using commentDirective "b" """
val specialCommentDirective = """//> using specialCommentDirective "b" """
val directive = """using directive "b" """
test("Test deprecation warnings about comments") {
testDiagnostics(commentDirective)(Warn("deprecated", 0, 0))
testDiagnostics(
commentDirective,
specialCommentDirective,
commentDirective
)(
Warn("deprecated", 0, 0),
Warn("deprecated", 2, 0)
)
}
test("Test warnings about mixing syntax") {
testDiagnostics(directive, specialCommentDirective)(Warn("ignored", 0, 0))
testDiagnostics(directive, commentDirective)(Warn("ignored", 0, 0))
testDiagnostics(specialCommentDirective, commentDirective)(Warn("ignored", 1, 0))
testDiagnostics(commentDirective, specialCommentDirective)(Warn("deprecated", 0, 0))
}
test("Plain comment result in ignored warning") {
testDiagnostics(commentDirective)(Warn("ignored", 0, 0))
}
test("@using is deprecated") {
def addAt(s: String) = s.replace("using ", "@using ")
testDiagnostics(addAt(specialCommentDirective))(Warn("syntax", 0, 4), Warn("keyword", 0, 4))
}
test("interpolator in dependency") {
val diags =
testDiagnostics("""//> using dep 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)
}
}