Skip to content

Commit 20b5142

Browse files
authored
Fix and refactor SBT version logic (#710)
1 parent 117226c commit 20b5142

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/SbtBuildTool.scala

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ class SbtBuildTool(index: IndexCommand) extends BuildTool("sbt", index) {
6363
}
6464

6565
private def isSupportedSbtVersion(version: String): Boolean = {
66-
(!version.startsWith("0.13") || version.startsWith("0.13.17")) &&
67-
!version.startsWith("1.0") && !version.startsWith("1.1")
66+
SbtBuildTool.isSupportedSbtVersion(version) match {
67+
case Left(message) =>
68+
index.app.error(message)
69+
false
70+
case Right(value) =>
71+
value
72+
}
6873
}
6974

7075
private def sbtVersion(): Option[String] = {
@@ -96,3 +101,30 @@ class SbtBuildTool(index: IndexCommand) extends BuildTool("sbt", index) {
96101
)
97102
}
98103
}
104+
105+
object SbtBuildTool {
106+
def isSupportedSbtVersion(version: String): Either[String, Boolean] = {
107+
SbtVersionParser.versionSegments(version) match {
108+
case major :: minor :: patch :: _ =>
109+
Right {
110+
(major == 0 && minor == 13 && patch >= 17) ||
111+
(major == 1 && minor >= 2)
112+
}
113+
114+
case _ =>
115+
Left(
116+
s"Failed to parse SBT version: [$version]. Only SBT 0.13.17+ or SBT 1.2+ are supported"
117+
)
118+
119+
}
120+
}
121+
}
122+
123+
object SbtVersionParser {
124+
def versionSegments(raw: String) =
125+
raw
126+
.takeWhile(c => c.isDigit || c == '.')
127+
.split("\\.", 3)
128+
.toList
129+
.flatMap(_.toIntOption)
130+
}

tests/buildTools/src/test/scala/tests/SbtBuildToolSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ abstract class SbtBuildToolSuite(sbt: Tool.SBT) extends BaseBuildToolSuite {
6363
import Tool._
6464

6565
class Sbt_15_BuildToolSuite extends SbtBuildToolSuite(SBT15)
66-
class Sbt_19_BuildToolSuite extends SbtBuildToolSuite(SBT19)
66+
class Sbt_110_BuildToolSuite extends SbtBuildToolSuite(SBT110)

tests/buildTools/src/test/scala/tests/Tool.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object Tool {
4545
extends Tool("sbt", version, support)
4646
// See https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html#build-tool-compatibility-table
4747
case object SBT15 extends SBT("1.5.2", atMostJava(17))
48-
case object SBT19 extends SBT("1.9.9", noRestrictions)
48+
case object SBT110 extends SBT("1.10.0", noRestrictions)
4949

5050
sealed abstract class Scala(version: String, support: JVMSupport)
5151
extends Tool("scala", version, support)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tests
2+
3+
import com.sourcegraph.scip_java.buildtools.SbtBuildTool
4+
import com.sourcegraph.scip_java.buildtools.SbtVersionParser
5+
6+
class SbtVersionParserSuite extends munit.FunSuite {
7+
test("parsing sbt versions") {
8+
import SbtVersionParser.{versionSegments => parse}
9+
assertEquals(parse("1.9.7"), List(1, 9, 7))
10+
assertEquals(parse("1.10.0"), List(1, 10, 0))
11+
assertEquals(parse("1.10.0-RC1"), List(1, 10, 0))
12+
assertEquals(parse("0.13.17"), List(0, 13, 17))
13+
assertEquals(parse("0.13"), List(0, 13))
14+
}
15+
16+
test("supported sbt versions") {
17+
import SbtBuildTool.{isSupportedSbtVersion => check}
18+
19+
def checkSupported(version: String) = {
20+
assert(check(version).contains(true), check(version))
21+
}
22+
23+
def checkUnsupported(version: String) = {
24+
assert(check(version).contains(false), check(version))
25+
}
26+
27+
def checkFailed(version: String) = {
28+
assert(check(version).isLeft, check(version))
29+
}
30+
31+
checkSupported("1.10.0-RC1")
32+
checkSupported("0.13.17")
33+
checkSupported("1.5.6")
34+
checkSupported("1.9.7")
35+
36+
checkUnsupported("1.0.0-RC1")
37+
checkUnsupported("0.13.16")
38+
checkUnsupported("1.1.6")
39+
checkUnsupported("0.12.15")
40+
41+
checkFailed("1.0-RC1")
42+
checkFailed("0.13")
43+
checkFailed("BLA")
44+
checkFailed("")
45+
}
46+
47+
}

0 commit comments

Comments
 (0)