Repository: scala-steward-org/scala-steward
Pull request #1552 (2020-07-27 21:27:40)

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.

codecov[bot]: # [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=h1) Report
> Merging [#1552](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=desc) into [master](https://codecov.io/gh/scala-steward-org/scala-steward/commit/53c40dfea5d4ac492463b1ce384c7bc99c818166&el=desc) will **increase** coverage by `0.06%`.
> The diff coverage is `87.50%`.

[![Impacted file tree graph](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552/graphs/tree.svg?width=650&height=150&src=pr&token=udzMsc1Dne)](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1552      +/-   ##
==========================================
+ Coverage   70.19%   70.25%   +0.06%     
==========================================
  Files         113      112       -1     
  Lines        1778     1782       +4     
  Branches       59       48      -11     
==========================================
+ Hits         1248     1252       +4     
  Misses        530      530              
```


| [Impacted Files](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...ain/scala/org/scalasteward/core/data/Version.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552/diff?src=pr&el=tree#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9kYXRhL1ZlcnNpb24uc2NhbGE=) | `93.58% <87.50%> (-0.94%)` | :arrow_down: |
| [...la-2.12/org/scalasteward/mill/plugin/package.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552/diff?src=pr&el=tree#diff-bW9kdWxlcy9taWxsLXBsdWdpbi9zcmMvbWFpbi9zY2FsYS0yLjEyL29yZy9zY2FsYXN0ZXdhcmQvbWlsbC9wbHVnaW4vcGFja2FnZS5zY2FsYQ==) | | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=footer). Last update [53c40df...f1648db](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1552?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala b/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala
index 8136dffd..e5813012 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala
@@ -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 {
@@ -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 {


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala b/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala
index 037b8236..861a66ef 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala
@@ -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")),
@@ -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"))
     )