Skip to content

Commit

Permalink
fix widcard version checking fix #39 #37
Browse files Browse the repository at this point in the history
  • Loading branch information
mutcianm committed Oct 15, 2019
1 parent 1a15013 commit b1e825d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.sbtidea.download.LocalPluginRegistry._
import org.jetbrains.sbtidea.download.api.{IdeaInstaller, PluginMetadata}

trait IdeaPluginInstaller extends IdeaInstaller {
import IdeaPluginInstaller._

private val localPluginRegistry = new LocalPluginRegistry(getInstallDir, log)

Expand Down Expand Up @@ -86,33 +87,38 @@ trait IdeaPluginInstaller extends IdeaInstaller {
lowerValid && upperValid
}




private def isPluginJar(artifact: Path): Boolean = {
val detector = new PluginXmlDetector
detector.test(artifact)
}

protected def pluginsDir: Path = getInstallDir.resolve("plugins")
}

object IdeaPluginInstaller {

// sort of copied from com.intellij.openapi.util.BuildNumber#compareTo
private def compareVersions(a: String, b: String): Int = {
def compareVersions(a: String, b: String): Int = {
val SNAPSHOT = "SNAPSHOT"
val SNAPSHOT_VALUE = Int.MaxValue

val c1 = a.replaceAll(SNAPSHOT, SNAPSHOT_VALUE.toString).split('.')
val c2 = b.replaceAll(SNAPSHOT, SNAPSHOT_VALUE.toString).split('.')
val c1 = a.replaceAll(SNAPSHOT, SNAPSHOT_VALUE.toString).replaceAll("\\*", SNAPSHOT_VALUE.toString).split('.')
val c2 = b.replaceAll(SNAPSHOT, SNAPSHOT_VALUE.toString).replaceAll("\\*", SNAPSHOT_VALUE.toString).split('.')
val pairs = c1
.zipAll(c2, "0", "0")
.map(x => x._1.toInt -> x._2.toInt)

for ((a_i, b_i) <- pairs) {
if ((a_i == b_i) && (a_i.toInt == SNAPSHOT_VALUE)) return 0
if (a_i == SNAPSHOT_VALUE) return -1
if (b_i == SNAPSHOT_VALUE) return 1
if (a_i == SNAPSHOT_VALUE) return 1
if (b_i == SNAPSHOT_VALUE) return -1
val res = a_i - b_i
if (res != 0) return res
}
c1.length - c2.length
}



private def isPluginJar(artifact: Path): Boolean = {
val detector = new PluginXmlDetector
detector.test(artifact)
}

protected def pluginsDir: Path = getInstallDir.resolve("plugins")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ final class IntellijPluginInstallerTest extends IntellijPluginInstallerTestBase
NioUtils.delete(ideaRoot / "plugins.idx")
}

test("Plugin installer checks IDEA compatibility using wildcards") {
val capturingLogger = new CapturingLogger
val pluginMetadata = PluginMetadata("org.intellij.scala", "Scala", "2019.2.423", "192.123", "193.*")
val mockPluginDist = createPluginJarMock(pluginMetadata)
val installer = createInstaller(capturingLogger)
val installedPluginRoot = installer.installIdeaPlugin(pluginMetadata.toPluginId, mockPluginDist)
installer.isPluginAlreadyInstalledAndUpdated(pluginMetadata.toPluginId) shouldBe true
capturingLogger.messages should not contain (
"Plugin org.intellij.scala is incompatible with current ideaVersion(192.5728.12): PluginMetadata(org.intellij.scala,Scala,2019.2.423,193.123,193.*)"
)
NioUtils.delete(installedPluginRoot)
NioUtils.delete(ideaRoot / "plugins.idx")
}


test("Plugin installer checks for newer plugin version") {
val capturingLogger = new CapturingLogger
val pluginMetadata = PluginMetadata("org.intellij.scala", "Scala", "2019.2.1", "192.123", "193.4")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jetbrains.sbtidea.download

import org.scalatest.{FunSuite, Matchers}

class VersionCompareTest extends FunSuite with Matchers {

test("versions greater") {
val validCombos = Seq(
"1" -> "0",
"1.0" -> "1",
"1.1" -> "0",
"1.1" -> "1.0",
"1.2.3" -> "1.1.9999"
)

for ((a, b) <- validCombos)
withClue(s"$a > $b: ") {
IdeaPluginInstaller.compareVersions(a, b) shouldBe > (0)
}
}

test("versions equal") {
val validCombos = Seq(
"0" -> "0",
"1.0" -> "1.0",
"1.1" -> "1.1",
"1.1.1" -> "1.1.1"
)

for ((a, b) <- validCombos)
withClue(s"$a == $b: ") {
IdeaPluginInstaller.compareVersions(a, b) shouldBe 0
}
}

test("version wildcards greater") {
val validCombos = Seq(
"*" -> "0",
"*" -> "999",
"1.*" -> "1.999",
"999.*" -> "1.1",
"999.1" -> "1.*",
"1.1.*" -> "1.1.999"
)

for ((a, b) <- validCombos)
withClue(s"$a > $b: ") {
IdeaPluginInstaller.compareVersions(a, b) shouldBe > (0)
}
}

}

0 comments on commit b1e825d

Please sign in to comment.