Skip to content

Commit

Permalink
Merge pull request #244 from rtimush/invalid-versions
Browse files Browse the repository at this point in the history
Parse versions prefixed with "v"
  • Loading branch information
rtimush authored Jun 27, 2021
2 parents 5529508 + 7341f9d commit d1f32ea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/main/scala/com/timushev/sbt/updates/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ object Reporter {
Seq(
Some(formatModule(m)),
Some(m.revision),
patchUpdate(c, vs).map(_.toString),
minorUpdate(c, vs).map(_.toString),
majorUpdate(c, vs).map(_.toString)
patchUpdate(c, vs).map(_.text),
minorUpdate(c, vs).map(_.text),
majorUpdate(c, vs).map(_.text)
)
}
.toSeq
Expand Down Expand Up @@ -151,10 +151,10 @@ object Reporter {
def pad(s: String, w: Int): String = s.padTo(w, ' ')

def include(included: ModuleFilter)(module: ModuleID, versions: SortedSet[Version]): SortedSet[Version] =
versions.filter(version => included.apply(module.withRevision0(version.toString)))
versions.filter(version => included.apply(module.withRevision0(version.text)))

def exclude(excluded: ModuleFilter)(module: ModuleID, versions: SortedSet[Version]): SortedSet[Version] =
versions.filterNot(version => excluded.apply(module.withRevision0(version.toString)))
versions.filterNot(version => excluded.apply(module.withRevision0(version.text)))

def excludeDependenciesFromPlugins(
dependencies: Seq[ModuleID],
Expand Down
25 changes: 14 additions & 11 deletions src/main/scala/com/timushev/sbt/updates/versions/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ sealed trait Version {
def major: Long
def minor: Long
def patch: Long
def text: String
}

case class ValidVersion(text: String, releasePart: List[Long], preReleasePart: List[String], buildPart: List[String])
extends Version {
def major: Long = releasePart.headOption.getOrElse(0)
def minor: Long = releasePart.drop(1).headOption.getOrElse(1)
def patch: Long = releasePart.drop(2).headOption.getOrElse(1)
override def toString: String = text
def major: Long = releasePart.headOption.getOrElse(0)
def minor: Long = releasePart.drop(1).headOption.getOrElse(1)
def patch: Long = releasePart.drop(2).headOption.getOrElse(1)
}

case class InvalidVersion(text: String) extends Version {
Expand Down Expand Up @@ -84,17 +84,20 @@ object Version {
}

object VersionParser extends RegexParsers {
private val token = """[^-+.]+""".r
private val number = """\d{1,18}(?=[-+.]|$)""".r ^^ (_.toLong)
private val plusAsPatchValue = """\+""".r ^^ (_ => Long.MaxValue)
private val token = """[^-+.]+""".r
private val number = """\d{1,18}(?=[-+.]|$)""".r ^^ (_.toLong)
private val plusAsNumber = """\+""".r ^^ (_ => Long.MaxValue)

private val numericPart: Parser[List[Long]] =
number ~ ("." ~> (plusAsNumber ^^ (List(_)) | numericPart)).? ^^ {
case h ~ Some(t) => h :: t
case h ~ None => List(h)
}

private val numericPart: Parser[List[Long]] = number ~ ("." ~> number) ~ ("." ~> (number | plusAsPatchValue)).* ^^ {
case h ~ m ~ t => h :: m :: t
}
private val part: Parser[List[String]] = token ~ (("." | "-") ~> token).* ^^ { case h ~ t => h :: t }

private val version: Parser[(List[Long], List[String], List[String])] =
numericPart ~ (("." | "-") ~> part).? ~ ("+" ~> part).? ^^ { case a ~ b ~ c =>
"v".? ~> numericPart ~ (("." | "-") ~> part).? ~ ("+" ~> part).? ^^ { case a ~ b ~ c =>
(a, b.getOrElse(Nil), c.getOrElse(Nil))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UpdatesFinderSpec extends AnyFreeSpec with Matchers {
.findUpdates(Seq(new FixedMetadataLoader(available)), allowPreRelease)(ModuleID("a", "b", current)),
1.minute
)
.map(_.toString())
.map(_.text)

val available = Seq(
"0.9.9-SNAPSHOT",
Expand Down
24 changes: 7 additions & 17 deletions src/test/scala/com/timushev/sbt/updates/versions/VersionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ class VersionSpec extends AnyFreeSpec with Matchers {
b should equal("build" :: "10" :: Nil)
case _ => fail("not a build version")
}

Version("2.0.2+9-4e5b95f4-SNAPSHOT") match { // Sbt-Dynver style snapshot version
case SnapshotVersion(r, p, b) =>
r should equal(2 :: 0 :: 2 :: Nil)
case _ => fail("not a snaphshot version")
case _ => fail("not a snapshot version")
}
}
"should be ordered according to the semantic versioning spec" in {
Expand Down Expand Up @@ -88,21 +87,6 @@ class VersionSpec extends AnyFreeSpec with Matchers {
case other => fail(other.toString)
}
}

"should reject versions like 1.+.+, 1.+.0, +.0.0" in {
VersionParser.parse("1.+.+") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
VersionParser.parse("1.+.0") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
VersionParser.parse("+.0.0") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
}
"should parse versions like 1.0.3m" in {
VersionParser.parse("1.0.3m") match {
case VersionParser.Success((1 :: 0 :: Nil, "3m" :: Nil, Nil), _) =>
Expand All @@ -119,6 +103,12 @@ class VersionSpec extends AnyFreeSpec with Matchers {
case other => fail(other.toString)
}
}
"should parse versions like v3-rev411-1.25.0" in {
VersionParser.parse("v3-rev411-1.25.0") match {
case VersionParser.Success((3 :: Nil, "rev411" :: "1" :: "25" :: "0" :: Nil, Nil), _) =>
case other => fail(other.toString)
}
}
}
}

Expand Down

0 comments on commit d1f32ea

Please sign in to comment.