Skip to content

Commit

Permalink
Fix Version.preReleaseIndex (#1552)
Browse files Browse the repository at this point in the history
* Fix Version.preReleaseIndex

... and recognize hashes which are only 6 characters long.

`preReleaseIndex` reported the wrong index for versions with components
with a length greater than one.

This is a follow-up #1549.

* Fix off-by-one

* Use foldMap
  • Loading branch information
fthomas authored Jul 28, 2020
1 parent ce9ad70 commit 144fc8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ final case class Version(value: String) {
.getOrElse(alnumComponents)

private[this] val preReleaseIndex: Option[NonNegInt] = {
val preReleaseIdentIndex = components.indexWhere {
val preReleaseIdentIndex = NonNegInt.unapply(components.indexWhere {
case a @ Version.Component.Alpha(_) => a.isPreReleaseIdent
case _ => false
}
NonNegInt.unapply(preReleaseIdentIndex).orElse(hashIndex)
})
preReleaseIdentIndex
.map(i => NonNegInt.unsafeFrom(components.take(i.value).foldMap(_.length)))
.orElse(hashIndex)
}

private[this] def hashIndex: Option[NonNegInt] =
"""[-+]\p{XDigit}{7,}""".r.findFirstMatchIn(value).flatMap(m => NonNegInt.unapply(m.start))
"""[-+]\p{XDigit}{6,}""".r.findFirstMatchIn(value).flatMap(m => NonNegInt.unapply(m.start))
}

object Version {
Expand All @@ -110,6 +112,14 @@ object Version {
case Component.Alpha(_) => true
case _ => false
}

final def length: Int =
this match {
case Component.Numeric(value) => value.length
case Component.Alpha(value) => value.length
case Component.Separator(_) => 1
case Component.Empty => 0
}
}
object Component {
final case class Numeric(value: String) extends Component {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class VersionTest
("3.0-RC3", List("3.0-RC4", "3.0-RC5", "3.0", "3.2"), Some("3.2")),
("1.3.0-RC5", List("1.3.0", "1.3.1", "1.3.2"), Some("1.3.2")),
("2.5", List("2.6", "3.6"), Some("2.6")),
("3.8", List("3.9.4"), Some("3.9.4")),
("1.3.0-RC5", List("1.3.0", "1.4.0"), Some("1.3.0")),
("1.1.2-1", List("2.0.0", "2.0.1-M3"), Some("2.0.0")),
("0.19.0-RC1", List("0.20.0-RC1", "0.20.0"), Some("0.20.0")),
Expand Down Expand Up @@ -185,6 +186,11 @@ class VersionTest
("2.1.4-11-307f3d8", List("2.1.4-13-fb16e4e"), Some("2.1.4-13-fb16e4e")),
("2.1.4-13-fb16e4e", List("2.2.0", "2.2.0-0-fe5ed67"), Some("2.2.0")),
("2.2.0", List("2.2.0-0-fe5ed67", "2.2.0-4-4bd225e"), None),
("0.116.0-alpha", List("0.118.1-alpha"), None),
("0.8.0", List("0.8.0-1-d81662"), None),
("0.6.3", List("289f9e3aa3f5014a5c64319da8e6ab993947ade2-0-289f9e"), None),
("0.1-58d9629", List("0.8.0"), Some("0.8.0")),
("0.9-a3bf234", List("0.14-9419610"), Some("0.14-9419610")),
("v2-rev374-1.23.0", List("v2-rev20190917-1.30.3"), Some("v2-rev20190917-1.30.3"))
)

Expand Down

0 comments on commit 144fc8a

Please sign in to comment.