Add CrossVersionExtra.isBinaryCompatible utility

This commit is contained in:
Rui Gonçalves 2022-06-25 21:41:10 +01:00
parent c896641f1c
commit 2318269dc5
No known key found for this signature in database
GPG Key ID: A9499DC4ED3A91A9
4 changed files with 78 additions and 1 deletions

5
.gitignore vendored
View File

@ -4,4 +4,9 @@ __pycache__
scripted-test/src/sbt-test/*/*/project/build.properties
scripted-test/src/sbt-test/*/*/project/plugins.sbt
.idea
.bloop
.metals
.bsp/
metals.sbt

View File

@ -84,6 +84,23 @@ object CrossVersionUtil {
case _ => full
}
// Uses the following rules:
//
// - Forwards and backwards compatibility is guaranteed for Scala 2.N.x (https://docs.scala-lang.org/overviews/core/binary-compatibility-of-scala-releases.html)
//
// - A Scala compiler in version 3.x1.y1 is able to read TASTy files produced by another compiler in version 3.x2.y2 if x1 >= x2 (https://docs.scala-lang.org/scala3/reference/language-versions/binary-compatibility.html)
//
// - For non-stable Scala 3 versions, compiler versions can read TASTy in an older stable format but their TASTY versions are not compatible between each other even if the compilers have the same minor version (https://docs.scala-lang.org/scala3/reference/language-versions/binary-compatibility.html)
//
private[sbt] def isBinaryCompatibleWith(newVersion: String, origVersion: String): Boolean = {
(newVersion, origVersion) match {
case (NonReleaseV_n("2", nMin, _, _), NonReleaseV_n("2", oMin, _, _)) => nMin == oMin
case (ReleaseV("3", nMin, _, _), ReleaseV("3", oMin, _, _)) => nMin.toInt >= oMin.toInt
case (NonReleaseV_1("3", nMin, _, _), ReleaseV("3", oMin, _, _)) => nMin.toInt > oMin.toInt
case _ => newVersion == origVersion
}
}
def binaryScalaVersion(full: String): String = {
if (ScalaArtifacts.isScala3(full)) binaryScala3Version(full)
else

View File

@ -9,7 +9,7 @@ private[librarymanagement] abstract class CrossVersionFunctions {
/** Compatibility with 0.13 */
@deprecated(
"use CrossVersion.disabled instead. prior to sbt 1.3.0, Diabled did not work without apply(). sbt/sbt#4977",
"use CrossVersion.disabled instead. prior to sbt 1.3.0, Disabled did not work without apply(). sbt/sbt#4977",
"1.3.0"
)
final val Disabled = sbt.librarymanagement.Disabled
@ -235,4 +235,10 @@ private[librarymanagement] abstract class CrossVersionFunctions {
* Full sbt versions earlier than [[sbt.librarymanagement.CrossVersion.TransitionSbtVersion]] are returned as is.
*/
def binarySbtVersion(full: String): String = CrossVersionUtil.binarySbtVersion(full)
/**
* Returns `true` if a project targeting version `origVersion` can run with version `newVersion`.
*/
def isBinaryCompatibleWith(newVersion: String, origVersion: String): Boolean =
CrossVersionUtil.isBinaryCompatibleWith(newVersion, origVersion)
}

View File

@ -284,6 +284,55 @@ class CrossVersionTest extends UnitSpec {
patchVersion("2.11.8-X1.5-bin-extra") shouldBe Some("artefact_2.11.8-X1.5")
}
"isBinaryCompatibleWith" should "for (2.10.4, 2.10.5) return true" in {
isBinaryCompatibleWith("2.10.4", "2.10.5") shouldBe true
}
it should "for (2.10.6, 2.10.5) return true" in {
isBinaryCompatibleWith("2.10.6", "2.10.5") shouldBe true
}
it should "for (2.11.0, 2.10.5) return false" in {
isBinaryCompatibleWith("2.11.0", "2.10.5") shouldBe false
}
it should "for (3.0.0, 2.10.5) return false" in {
isBinaryCompatibleWith("3.0.0", "2.10.5") shouldBe false
}
it should "for (3.0.0, 3.1.0) return false" in {
isBinaryCompatibleWith("3.0.0", "3.1.0") shouldBe false
}
it should "for (3.1.0, 3.0.0) return true" in {
isBinaryCompatibleWith("3.1.0", "3.0.0") shouldBe true
}
it should "for (3.1.0, 3.1.1) return true" in {
isBinaryCompatibleWith("3.1.0", "3.1.1") shouldBe true
}
it should "for (3.1.1, 3.1.0) return true" in {
isBinaryCompatibleWith("3.1.1", "3.1.0") shouldBe true
}
it should "for (2.10.0-M1, 2.10.5) return true" in {
isBinaryCompatibleWith("2.10.0-M1", "2.10.5") shouldBe true
}
it should "for (2.10.5, 2.10.0-M1) return true" in {
isBinaryCompatibleWith("2.10.5", "2.10.0-M1") shouldBe true
}
it should "for (2.10.0-M1, 2.10.0-M2) return true" in {
isBinaryCompatibleWith("2.10.0-M1", "2.10.0-M2") shouldBe true
}
it should "for (2.10.0-M1, 2.11.0-M1) return false" in {
isBinaryCompatibleWith("2.10.0-M1", "2.11.0-M1") shouldBe false
}
it should "for (3.1.0-M1, 3.0.0) return true" in {
isBinaryCompatibleWith("3.1.0-M1", "3.0.0") shouldBe true
}
it should "for (3.1.0-M1, 3.1.0) return false" in {
isBinaryCompatibleWith("3.1.0-M1", "3.1.0") shouldBe false
}
it should "for (3.1.0-M1, 3.1.0-M2) return false" in {
isBinaryCompatibleWith("3.1.0-M1", "3.1.0-M2") shouldBe false
}
it should "for (3.1.0-M2, 3.1.0-M1) return false" in {
isBinaryCompatibleWith("3.1.0-M2", "3.1.0-M1") shouldBe false
}
private def constantVersion(value: String) =
CrossVersion(CrossVersion.constant(value), "dummy1", "dummy2") map (fn => fn("artefact"))