Repository: scala-steward-org/scala-steward
Pull request #2857 (2022-12-25 07:23:57)

Extract small refactorings from #2816
... to reduce its diff.


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/MavenAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/MavenAlg.scala
index 8eb995f0..d05ed104 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/MavenAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/MavenAlg.scala
@@ -38,7 +38,7 @@ final class MavenAlg[F[_]](config: Config)(implicit
   override def containsBuild(buildRoot: BuildRoot): F[Boolean] =
     workspaceAlg
       .buildRootDir(buildRoot)
-      .flatMap(buildRootDir => fileAlg.isRegularFile(buildRootDir / "pom.xml"))
+      .flatMap(buildRootDir => fileAlg.isRegularFile(buildRootDir / pomXmlName))
 
   override def getDependencies(buildRoot: BuildRoot): F[List[Scope.Dependencies]] =
     for {
@@ -64,7 +64,7 @@ final class MavenAlg[F[_]](config: Config)(implicit
     maybeIgnoreOptsFiles(repoDir).surround(processAlg.execSandboxed(command, repoDir))
 
   private def mvnCmd(commands: String*): Nel[String] =
-    Nel("mvn", "--batch-mode" :: commands.toList)
+    Nel("mvn", args.batchMode :: commands.toList)
 
   private def maybeIgnoreOptsFiles(dir: File): Resource[F, Unit] =
     if (config.ignoreOptsFiles) ignoreOptsFiles(dir) else Resource.unit[F]
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/command.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/command.scala
index c0b84de4..a405bf88 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/command.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/command.scala
@@ -22,5 +22,6 @@ object command {
 }
 
 object args {
+  val batchMode = "--batch-mode"
   val excludeTransitive = "-DexcludeTransitive=true"
 }
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/package.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/package.scala
new file mode 100644
index 00000000..13611f4c
--- /dev/null
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/maven/package.scala
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2018-2022 Scala Steward contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.scalasteward.core.buildtool
+
+package object maven {
+  val pomXmlName = "pom.xml"
+}
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
index 510af015..5bdb4d2e 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/mill/MillAlg.scala
@@ -70,7 +70,7 @@ final class MillAlg[F[_]](implicit
 
   private def getMillVersion(buildRootDir: File): F[Option[Version]] =
     for {
-      millVersionFileContent <- fileAlg.readFile(buildRootDir / ".mill-version")
+      millVersionFileContent <- fileAlg.readFile(buildRootDir / millVersionName)
       version = millVersionFileContent.flatMap(parser.parseMillVersion)
     } yield version
 
@@ -115,7 +115,8 @@ object MillAlg {
     Dependency(millMainGroupId, millMainArtifactId, version)
 
   def isMillMainUpdate(update: Update.Single): Boolean =
-    update.groupId === millMainGroupId && update.artifactIds.exists(
-      _.name === millMainArtifactId.name
-    )
+    update.groupId === millMainGroupId &&
+      update.artifactIds.exists(_.name === millMainArtifactId.name)
+
+  val millVersionName = ".mill-version"
 }
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala
index 244d371c..c97c5c22 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/SbtAlg.scala
@@ -50,7 +50,7 @@ final class SbtAlg[F[_]](config: Config)(implicit
   private def getSbtVersion(buildRoot: BuildRoot): F[Option[SbtVersion]] =
     for {
       buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
-      maybeProperties <- fileAlg.readFile(buildRootDir / project / "build.properties")
+      maybeProperties <- fileAlg.readFile(buildRootDir / project / buildPropertiesName)
       version = maybeProperties.flatMap(parser.parseBuildProperties)
     } yield version
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala
index 7a15c3d5..fb35f241 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/sbt/package.scala
@@ -19,7 +19,7 @@ package org.scalasteward.core.buildtool
 import cats.Functor
 import cats.syntax.all._
 import org.scalasteward.core.buildtool.sbt.data.{SbtVersion, ScalaVersion}
-import org.scalasteward.core.data.{ArtifactId, Dependency, GroupId, Version}
+import org.scalasteward.core.data._
 import org.scalasteward.core.io.{FileAlg, FileData}
 
 package object sbt {
@@ -30,6 +30,12 @@ package object sbt {
 
   val sbtArtifactId: ArtifactId = ArtifactId("sbt")
 
+  val buildPropertiesName = "build.properties"
+
+  def isSbtUpdate(update: Update.Single): Boolean =
+    update.groupId === sbtGroupId &&
+      update.artifactIds.exists(_.name === sbtArtifactId.name)
+
   def sbtDependency(sbtVersion: SbtVersion): Option[Dependency] = {
     val version = sbtVersion.toVersion
     Option.when(version >= Version("1.0.0"))(Dependency(sbtGroupId, sbtArtifactId, version))
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
index 2121e103..a08efc3c 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
@@ -99,9 +99,9 @@ final class EditAlg[F[_]](implicit
   ): F[EditAttempt] =
     for {
       _ <- logger.info(s"Running migration $migration")
-      result <- logger.attemptWarn.log("Scalafix migration failed")(
+      result <- logger.attemptWarn.log("Scalafix migration failed") {
         buildToolDispatcher.runMigration(repo, config, migration)
-      )
+      }
       maybeCommit <- gitAlg.commitAllIfDirty(repo, migration.commitMessage(result))
     } yield ScalafixEdit(migration, result, maybeCommit)
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/io/FileAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/io/FileAlg.scala
index 8d228f1b..4109b11e 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/io/FileAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/io/FileAlg.scala
@@ -59,7 +59,7 @@ trait FileAlg[F[_]] {
   final def createTemporarily[E](dir: File, data: FileData)(implicit
       F: ApplicativeError[F, E]
   ): Resource[F, Unit] =
-    createTemporarily(dir / data.name, data.content)
+    createTemporarily(dir / data.path, data.content)
 
   final def editFile(file: File, edit: String => Option[String])(implicit
       F: MonadThrow[F]
diff --git a/modules/core/src/main/scala/org/scalasteward/core/io/FileData.scala b/modules/core/src/main/scala/org/scalasteward/core/io/FileData.scala
index 4de10d3b..76385750 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/io/FileData.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/io/FileData.scala
@@ -17,6 +17,6 @@
 package org.scalasteward.core.io
 
 final case class FileData(
-    name: String,
+    path: String,
     content: String
 )
diff --git a/modules/core/src/main/scala/org/scalasteward/core/io/package.scala b/modules/core/src/main/scala/org/scalasteward/core/io/package.scala
index 6103a374..f0eee9df 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/io/package.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/io/package.scala
@@ -19,9 +19,10 @@ package org.scalasteward.core
 import better.files.File
 import cats.syntax.all._
 import org.scalasteward.core.buildtool.mill.MillAlg
-import org.scalasteward.core.buildtool.sbt.{sbtArtifactId, sbtGroupId}
+import org.scalasteward.core.buildtool.mill.MillAlg.millVersionName
+import org.scalasteward.core.buildtool.sbt.{buildPropertiesName, isSbtUpdate}
 import org.scalasteward.core.data.Update
-import org.scalasteward.core.scalafmt.{scalafmtArtifactId, scalafmtConfName, scalafmtGroupId}
+import org.scalasteward.core.scalafmt.{isScalafmtCoreUpdate, scalafmtConfName}
 
 package object io {
   def isSourceFile(update: Update.Single, fileExtensions: Set[String])(file: File): Boolean = {
@@ -33,20 +34,12 @@ package object io {
       file: File
   ): Boolean =
     () match {
-      case _ if isSbtUpdate(update)              => file.name === "build.properties"
+      case _ if isSbtUpdate(update)              => file.name === buildPropertiesName
       case _ if isScalafmtCoreUpdate(update)     => file.name === scalafmtConfName
-      case _ if MillAlg.isMillMainUpdate(update) => file.name === ".mill-version"
+      case _ if MillAlg.isMillMainUpdate(update) => file.name === millVersionName
       case _                                     => isGenericSourceFile(file, fileExtensions)
     }
 
   private def isGenericSourceFile(file: File, fileExtensions: Set[String]): Boolean =
     fileExtensions.exists(file.name.endsWith)
-
-  private def isSbtUpdate(update: Update.Single): Boolean =
-    update.groupId === sbtGroupId &&
-      update.artifactIds.exists(_.name === sbtArtifactId.name)
-
-  private def isScalafmtCoreUpdate(update: Update.Single): Boolean =
-    update.groupId === scalafmtGroupId &&
-      update.artifactIds.exists(_.name === scalafmtArtifactId.name)
 }
diff --git a/modules/core/src/main/scala/org/scalasteward/core/repoconfig/UpdatesConfig.scala b/modules/core/src/main/scala/org/scalasteward/core/repoconfig/UpdatesConfig.scala
index 94cec9ce..afb67930 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/repoconfig/UpdatesConfig.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/repoconfig/UpdatesConfig.scala
@@ -23,6 +23,7 @@ import io.circe.generic.extras.Configuration
 import io.circe.generic.extras.semiauto._
 import io.circe.refined._
 import io.circe.{Codec, Decoder}
+import org.scalasteward.core.buildtool.maven.pomXmlName
 import org.scalasteward.core.data.{GroupId, Update}
 import org.scalasteward.core.update.FilterAlg.{
   FilterResult,
@@ -85,7 +86,14 @@ final case class UpdatesConfig(
 
 object UpdatesConfig {
   val defaultFileExtensions: Set[String] =
-    Set(".scala", ".sbt", ".sbt.shared", ".sc", ".yml", "pom.xml")
+    Set(
+      ".scala",
+      ".sbt",
+      ".sbt.shared",
+      ".sc",
+      ".yml",
+      pomXmlName
+    )
 
   implicit val updatesConfigEq: Eq[UpdatesConfig] =
     Eq.fromUniversalEquals
diff --git a/modules/core/src/main/scala/org/scalasteward/core/scalafmt/package.scala b/modules/core/src/main/scala/org/scalasteward/core/scalafmt/package.scala
index 6e4af90b..bddc3e81 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/scalafmt/package.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/scalafmt/package.scala
@@ -35,6 +35,10 @@ package object scalafmt {
   def isScalafmtUpdate(update: Update.ForArtifactId): Boolean =
     update.groupId === scalafmtGroupId && update.artifactId.name === scalafmtArtifactId.name
 
+  def isScalafmtCoreUpdate(update: Update.Single): Boolean =
+    update.groupId === scalafmtGroupId &&
+      update.artifactIds.exists(_.name === scalafmtArtifactId.name)
+
   private def scalafmtGroupIdBy(version: Version): GroupId =
     if (version > Version("2.0.0-RC1")) scalafmtGroupId else GroupId("com.geirsson")
 
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index ee4e58e8..57ca24b6 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -3,7 +3,7 @@ import sbt._
 object Dependencies {
   val bcprovJdk15to18 = "org.bouncycastle" % "bcprov-jdk15to18" % "1.72"
   val betterFiles = "com.github.pathikrit" %% "better-files" % "3.9.1"
-  val catsEffect = "org.typelevel" %% "cats-effect" % "3.4.3"
+  val catsEffect = "org.typelevel" %% "cats-effect" % "3.4.2"
   val catsCore = "org.typelevel" %% "cats-core" % "2.9.0"
   val catsLaws = "org.typelevel" %% "cats-laws" % catsCore.revision
   val catsParse = "org.typelevel" %% "cats-parse" % "0.3.8"


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/maven/MavenAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/maven/MavenAlgTest.scala
index e0a3c3ec..c04af2c7 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/maven/MavenAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/maven/MavenAlgTest.scala
@@ -24,7 +24,7 @@ class MavenAlgTest extends FunSuite {
           "--env=VAR1=val1",
           "--env=VAR2=val2",
           "mvn",
-          "--batch-mode",
+          args.batchMode,
           command.listDependencies,
           args.excludeTransitive
         ),
@@ -36,7 +36,7 @@ class MavenAlgTest extends FunSuite {
           "--env=VAR1=val1",
           "--env=VAR2=val2",
           "mvn",
-          "--batch-mode",
+          args.batchMode,
           command.listRepositories
         )
       )
diff --git a/modules/core/src/test/scala/org/scalasteward/core/persistence/JsonKeyValueStoreTest.scala b/modules/core/src/test/scala/org/scalasteward/core/persistence/JsonKeyValueStoreTest.scala
index 31781840..5ce16091 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/persistence/JsonKeyValueStoreTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/persistence/JsonKeyValueStoreTest.scala
@@ -80,16 +80,4 @@ class JsonKeyValueStoreTest extends FunSuite {
     )
     assertEquals(state, expected)
   }
-
-  test("get with malformed JSON") {
-    val p = for {
-      kvStore <- JsonKeyValueStore.create[MockEff, String, String]("test-4", "0")
-      v1 <- kvStore.get("k1")
-    } yield v1
-
-    val k1File = config.workspace / "store" / "test-4" / "v0" / "k1" / "test-4.json"
-    val k1Mapping = k1File -> """ "v1 """
-    val value = MockState.empty.addFiles(k1Mapping).flatMap(p.runA).unsafeRunSync()
-    assertEquals(value, None)
-  }
 }