Skip to content

Commit bdeda6f

Browse files
committed
Fixed the issue that is throwing StringIndexOutOfBoundsException when duplicated replacements are used to edit the file. In other words, trying to replace single dependency more than once which is not valid.
1 parent f1f2e59 commit bdeda6f

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ final class EditAlg[F[_]](implicit
106106
repoDir <- workspaceAlg.repoDir(data.repo)
107107
replacementsByPath = updateReplacements.groupBy(_.position.path).toList
108108
_ <- replacementsByPath.traverse { case (path, replacements) =>
109-
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements))
109+
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements.toSet))
110110
}
111111
_ <- reformatChangedFiles(data)
112112
msgTemplate = data.config.commits.messageOrDefault

modules/core/src/main/scala/org/scalasteward/core/edit/update/data/Substring.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ object Substring {
4141
final case class Replacement(position: Position, replacement: String)
4242

4343
object Replacement {
44-
def applyAll(replacements: List[Replacement])(source: String): String = {
44+
def applyAll(replacements: Set[Replacement])(source: String): String = {
4545
var start = 0
4646
val sb = new java.lang.StringBuilder(source.length)
47-
replacements.sortBy(_.position.start).foreach { r =>
47+
replacements.toSeq.sortBy(_.position.start).foreach { r =>
4848
val before = source.substring(start, r.position.start)
4949
start = r.position.start + r.position.value.length
5050
sb.append(before).append(r.replacement)

modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.scalasteward.core.edit
22

3+
import cats.data.NonEmptyList
34
import cats.effect.unsafe.implicits.global
45
import munit.FunSuite
56
import org.scalasteward.core.TestInstances.dummyRepoCache
67
import org.scalasteward.core.TestSyntax._
7-
import org.scalasteward.core.data.{Repo, RepoData, Update}
8+
import org.scalasteward.core.data.{CrossDependency, GroupId, Repo, RepoData, Update, Version}
89
import org.scalasteward.core.mock.MockContext.context._
910
import org.scalasteward.core.mock.MockState
1011
import org.scalasteward.core.repoconfig.RepoConfig
@@ -931,6 +932,37 @@ class RewriteTest extends FunSuite {
931932
runApplyUpdate(update, original, expected)
932933
}
933934

935+
test("duplicate updates should be successful") {
936+
val dependency = "com.pauldijou".g % "jwt-play-json".a % "5.0.0"
937+
938+
val artifactId = Update.ForArtifactId(
939+
CrossDependency(dependency),
940+
newerVersions = NonEmptyList.of(Version("9.2.0")),
941+
newerGroupId = Some(GroupId("com.github.jwt-scala")),
942+
newerArtifactId = Some("jwt-play-json")
943+
)
944+
val duplicatedUpdates = Update.ForGroupId(NonEmptyList.of(artifactId, artifactId))
945+
val buildSbtContent =
946+
"""
947+
| lazy val root = (project in file("."))
948+
| .settings(
949+
| scalafmtOnCompile := true,
950+
| scalaVersion := scala213,
951+
| libraryDependencies ++= Seq(
952+
| "com.pauldijou" %% "jwt-play-json" % "5.0.0", // JWT parsing
953+
| "org.scalatestplus" %% "mockito-3-4" % "3.2.10.0" % Test
954+
| ),
955+
| crossScalaVersions := supportedScalaVersions
956+
| )
957+
|""".stripMargin
958+
val original = Map("build.sbt" -> buildSbtContent)
959+
val expectedSbtContent = buildSbtContent
960+
.replaceAll("com.pauldijou", "com.github.jwt-scala")
961+
.replaceAll("5.0.0", "9.2.0")
962+
val expected = Map("build.sbt" -> expectedSbtContent)
963+
runApplyUpdate(duplicatedUpdates, original, expected)
964+
}
965+
934966
private def runApplyUpdate(
935967
update: Update.Single,
936968
files: Map[String, String],

0 commit comments

Comments
 (0)