Skip to content

Commit 323ba26

Browse files
committed
Produced a meaningful error message when toolkit is invoked without passing a version
1 parent 915aa4e commit 323ba26

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

modules/build/src/test/scala/scala/build/tests/DirectiveTests.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import scala.build.tests.util.BloopServer
99
import build.Ops.EitherThrowOps
1010
import dependency.AnyDependency
1111

12+
import scala.build.errors.ToolkitDirectiveMissingVersionError
13+
1214
class DirectiveTests extends munit.FunSuite {
1315

1416
val buildThreads = BuildThreads.create()
@@ -87,11 +89,11 @@ class DirectiveTests extends munit.FunSuite {
8789
}
8890
}
8991

90-
test("resolve toolkit & toolkit-test dependency") {
92+
test(s"resolve toolkit & toolkit-test dependency with version passed") {
9193
val testInputs = TestInputs(
9294
os.rel / "simple.sc" ->
93-
"""//> using toolkit latest
94-
|""".stripMargin
95+
s"""//> using toolkit latest
96+
|""".stripMargin
9597
)
9698
testInputs.withBuilds(baseOptions, buildThreads, bloopConfigOpt) {
9799
(_, _, maybeBuilds) =>
@@ -111,6 +113,21 @@ class DirectiveTests extends munit.FunSuite {
111113
expect(toolkitTestDep.version == expectedVersion)
112114
}
113115
}
116+
117+
for (toolkitDirectiveKey <- Seq("toolkit", "test.toolkit"))
118+
test(s"missing $toolkitDirectiveKey version produces an informative error message") {
119+
val testInputs = TestInputs(
120+
os.rel / "simple.sc" ->
121+
s"""//> using $toolkitDirectiveKey
122+
|""".stripMargin
123+
)
124+
testInputs.withBuilds(baseOptions, buildThreads, bloopConfigOpt) {
125+
(_, _, maybeBuilds) =>
126+
maybeBuilds match
127+
case Left(ToolkitDirectiveMissingVersionError(_, errorKey)) =>
128+
expect(errorKey == toolkitDirectiveKey)
129+
}
130+
}
114131
for (scope <- Scope.all) {
115132
def withProjectFile[T](projectFileContent: String)(f: (Build, Boolean) => T): T = TestInputs(
116133
os.rel / "project.scala" -> projectFileContent,

modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import scala.build.errors.{
77
BuildException,
88
CompositeBuildException,
99
MalformedDirectiveError,
10+
ToolkitDirectiveMissingVersionError,
1011
UsingDirectiveValueNumError,
1112
UsingDirectiveWrongValueTypeError
1213
}
@@ -53,15 +54,18 @@ object DirectiveValueParser {
5354
scopePath: ScopePath,
5455
path: Either[String, os.Path]
5556
): Either[BuildException, T] =
56-
values.filter(!_.isEmpty) match {
57-
case Seq(value) => parseValue(key, value, scopePath, path)
57+
values match {
58+
case Seq(value) if !value.isEmpty => parseValue(key, value, scopePath, path)
59+
case Seq(value) if value.isEmpty && (key == "toolkit" || key == "test.toolkit") =>
60+
// FIXME: handle similar parsing errors in the directive declaration instead of hacks like this one
61+
Left(ToolkitDirectiveMissingVersionError(maybePath = path, key = key))
5862
case resultValues @ _ =>
5963
Left(
6064
new UsingDirectiveValueNumError(
6165
maybePath = path,
6266
key = key,
6367
expectedValueNum = 1,
64-
providedValueNum = resultValues.length
68+
providedValueNum = resultValues.count(!_.isEmpty)
6569
)
6670
)
6771
}

modules/directives/src/main/scala/scala/build/errors/UsingDirectiveExpectationError.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ final class UsingDirectiveValueNumError(
2828
s"""Encountered an error when parsing the `$key` using directive$pathString.
2929
|Expected $expectedValueNum values, but got $providedValueNum values instead.""".stripMargin
3030
})
31+
32+
final class ToolkitDirectiveMissingVersionError(
33+
val maybePath: Either[String, os.Path],
34+
val key: String
35+
) extends UsingDirectiveExpectationError({
36+
val pathString = maybePath.map(p => s" at $p").getOrElse("")
37+
s"""Encountered an error when parsing the `$key` using directive$pathString.
38+
|Expected a version to be passed.
39+
|Example: `//> using $key latest`
40+
|""".stripMargin
41+
})
42+
object ToolkitDirectiveMissingVersionError {
43+
def apply(maybePath: Either[String, os.Path], key: String): ToolkitDirectiveMissingVersionError =
44+
new ToolkitDirectiveMissingVersionError(maybePath, key)
45+
def unapply(arg: ToolkitDirectiveMissingVersionError): (Either[String, os.Path], String) =
46+
arg.maybePath -> arg.key
47+
}

0 commit comments

Comments
 (0)