Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed throwing StringIndexOutOfBoundsException #3016

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ final class EditAlg[F[_]](implicit
repoDir <- workspaceAlg.repoDir(data.repo)
replacementsByPath = updateReplacements.groupBy(_.position.path).toList
_ <- replacementsByPath.traverse { case (path, replacements) =>
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements.toSet))
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements))
}
_ <- reformatChangedFiles(data)
msgTemplate = data.config.commits.messageOrDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ object Substring {
final case class Replacement(position: Position, replacement: String)

object Replacement {
def applyAll(replacements: Set[Replacement])(source: String): String = {
def applyAll(replacements: List[Replacement])(source: String): String = {
var start = 0
val sb = new java.lang.StringBuilder(source.length)
replacements.toSeq.sortBy(_.position.start).foreach { r =>
replacements.distinctBy(_.position.start).sortBy(_.position.start).foreach { r =>
val before = source.substring(start, r.position.start)
start = r.position.start + r.position.value.length
sb.append(before).append(r.replacement)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.scalasteward.core.edit

import cats.data.NonEmptyList
import cats.effect.unsafe.implicits.global
import munit.FunSuite
import org.scalasteward.core.TestInstances.dummyRepoCache
import org.scalasteward.core.TestSyntax._
import org.scalasteward.core.data.{CrossDependency, GroupId, Repo, RepoData, Update, Version}
import org.scalasteward.core.data.{Repo, RepoData, Update}
import org.scalasteward.core.mock.MockContext.context._
import org.scalasteward.core.mock.MockState
import org.scalasteward.core.repoconfig.RepoConfig
Expand Down Expand Up @@ -432,6 +431,17 @@ class RewriteTest extends FunSuite {
runApplyUpdate(update, original, expected)
}

// https://github.com/scala-steward-org/scala-steward/pull/3016
test("artifact change with multiple artifactId cross names") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

val update = ("com.pauldijou".g % Nel.of(
("jwt-core", "jwt-core_2.12").a,
("jwt-core", "jwt-core_2.13").a
) % "5.0.0" %> "9.2.0").single.copy(newerGroupId = Some("com.github.jwt-scala".g))
val original = Map("build.sbt" -> """ "com.pauldijou" %% "jwt-core" % "5.0.0" """)
val expected = Map("build.sbt" -> """ "com.github.jwt-scala" %% "jwt-core" % "9.2.0" """)
runApplyUpdate(update, original, expected)
}

// https://github.com/scala-steward-org/scala-steward/pull/566
test("prevent exception: named capturing group is missing trailing '}'") {
val update =
Expand Down Expand Up @@ -932,37 +942,6 @@ class RewriteTest extends FunSuite {
runApplyUpdate(update, original, expected)
}

test("duplicate updates should be successful") {
val dependency = "com.pauldijou".g % "jwt-play-json".a % "5.0.0"

val artifactId = Update.ForArtifactId(
CrossDependency(dependency),
newerVersions = NonEmptyList.of(Version("9.2.0")),
newerGroupId = Some(GroupId("com.github.jwt-scala")),
newerArtifactId = Some("jwt-play-json")
)
val duplicatedUpdates = Update.ForGroupId(NonEmptyList.of(artifactId, artifactId))
val buildSbtContent =
"""
| lazy val root = (project in file("."))
| .settings(
| scalafmtOnCompile := true,
| scalaVersion := scala213,
| libraryDependencies ++= Seq(
| "com.pauldijou" %% "jwt-play-json" % "5.0.0", // JWT parsing
| "org.scalatestplus" %% "mockito-3-4" % "3.2.10.0" % Test
| ),
| crossScalaVersions := supportedScalaVersions
| )
|""".stripMargin
val original = Map("build.sbt" -> buildSbtContent)
val expectedSbtContent = buildSbtContent
.replaceAll("com.pauldijou", "com.github.jwt-scala")
.replaceAll("5.0.0", "9.2.0")
val expected = Map("build.sbt" -> expectedSbtContent)
runApplyUpdate(duplicatedUpdates, original, expected)
}

private def runApplyUpdate(
update: Update.Single,
files: Map[String, String],
Expand Down