Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and refactor SBT version logic #710

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ class SbtBuildTool(index: IndexCommand) extends BuildTool("sbt", index) {
}

private def isSupportedSbtVersion(version: String): Boolean = {
(!version.startsWith("0.13") || version.startsWith("0.13.17")) &&
!version.startsWith("1.0") && !version.startsWith("1.1")
SbtBuildTool.isSupportedSbtVersion(version) match {
case Left(message) =>
index.app.error(message)
false
case Right(value) =>
value
}
}

private def sbtVersion(): Option[String] = {
Expand Down Expand Up @@ -96,3 +101,30 @@ class SbtBuildTool(index: IndexCommand) extends BuildTool("sbt", index) {
)
}
}

object SbtBuildTool {
def isSupportedSbtVersion(version: String): Either[String, Boolean] = {
SbtVersionParser.versionSegments(version) match {
case major :: minor :: patch :: _ =>
Right {
(major == 0 && minor == 13 && patch >= 17) ||
(major == 1 && minor >= 2)
}

case _ =>
Left(
s"Failed to parse SBT version: [$version]. Only SBT 0.13.17+ or SBT 1.2+ are supported"
)

}
}
}

object SbtVersionParser {
def versionSegments(raw: String) =
raw
.takeWhile(c => c.isDigit || c == '.')
.split("\\.", 3)
.toList
.flatMap(_.toIntOption)
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ abstract class SbtBuildToolSuite(sbt: Tool.SBT) extends BaseBuildToolSuite {
import Tool._

class Sbt_15_BuildToolSuite extends SbtBuildToolSuite(SBT15)
class Sbt_19_BuildToolSuite extends SbtBuildToolSuite(SBT19)
class Sbt_110_BuildToolSuite extends SbtBuildToolSuite(SBT110)
2 changes: 1 addition & 1 deletion tests/buildTools/src/test/scala/tests/Tool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Tool {
extends Tool("sbt", version, support)
// See https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html#build-tool-compatibility-table
case object SBT15 extends SBT("1.5.2", atMostJava(17))
case object SBT19 extends SBT("1.9.9", noRestrictions)
case object SBT110 extends SBT("1.10.0", noRestrictions)

sealed abstract class Scala(version: String, support: JVMSupport)
extends Tool("scala", version, support)
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/src/test/scala/tests/SbtSupportedVersionsSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tests

import com.sourcegraph.scip_java.buildtools.SbtBuildTool
import com.sourcegraph.scip_java.buildtools.SbtVersionParser

class SbtVersionParserSuite extends munit.FunSuite {
test("parsing sbt versions") {
import SbtVersionParser.{versionSegments => parse}
assertEquals(parse("1.9.7"), List(1, 9, 7))
assertEquals(parse("1.10.0"), List(1, 10, 0))
assertEquals(parse("1.10.0-RC1"), List(1, 10, 0))
assertEquals(parse("0.13.17"), List(0, 13, 17))
assertEquals(parse("0.13"), List(0, 13))
}

test("supported sbt versions") {
import SbtBuildTool.{isSupportedSbtVersion => check}

def checkSupported(version: String) = {
assert(check(version).contains(true), check(version))
}

def checkUnsupported(version: String) = {
assert(check(version).contains(false), check(version))
}

def checkFailed(version: String) = {
assert(check(version).isLeft, check(version))
}

checkSupported("1.10.0-RC1")
checkSupported("0.13.17")
checkSupported("1.5.6")
checkSupported("1.9.7")

checkUnsupported("1.0.0-RC1")
checkUnsupported("0.13.16")
checkUnsupported("1.1.6")
checkUnsupported("0.12.15")

checkFailed("1.0-RC1")
checkFailed("0.13")
checkFailed("BLA")
checkFailed("")
}

}
Loading