Repository: scala-steward-org/scala-steward
Pull request #1754 (2020-11-18 19:25:20)

Post-update hooks: do not run scalafmt in a sandbox
It is not necessary to run scalafmt in a sandbox.


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
index d1ba0451..859188cc 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
@@ -18,6 +18,7 @@ package org.scalasteward.core.edit.hooks
 
 import cats.Monad
 import cats.syntax.all._
+import io.chrisdavenport.log4cats.Logger
 import org.scalasteward.core.data.{ArtifactId, GroupId, Update}
 import org.scalasteward.core.git.{Commit, GitAlg}
 import org.scalasteward.core.io.{ProcessAlg, WorkspaceAlg}
@@ -28,6 +29,7 @@ import org.scalasteward.core.vcs.data.Repo
 
 final class HookExecutor[F[_]](implicit
     gitAlg: GitAlg[F],
+    logger: Logger[F],
     processAlg: ProcessAlg[F],
     workspaceAlg: WorkspaceAlg[F],
     F: Monad[F]
@@ -47,8 +49,11 @@ final class HookExecutor[F[_]](implicit
       hook: PostUpdateHook
   ): F[List[Commit]] =
     for {
+      _ <- logger.info(s"Executing post-update hook for ${hook.groupId}:${hook.artifactId.name}")
       repoDir <- workspaceAlg.repoDir(repo)
-      _ <- processAlg.execSandboxed(hook.command, repoDir)
+      _ <-
+        if (hook.useSandbox) processAlg.execSandboxed(hook.command, repoDir)
+        else processAlg.exec(hook.command, repoDir)
       maybeCommit <- gitAlg.commitAllIfDirty(repo, hook.commitMessage(update))
     } yield maybeCommit.toList
 }
@@ -60,6 +65,7 @@ object HookExecutor {
         groupId = scalafmtGroupId,
         artifactId = scalafmtArtifactId,
         command = Nel.of(scalafmtBinary, "--non-interactive"),
+        useSandbox = false,
         commitMessage = update => s"Reformat with scalafmt ${update.nextVersion}",
         enabledByConfig = _.scalafmt.runAfterUpgradingOrDefault
       ),
@@ -67,6 +73,7 @@ object HookExecutor {
         groupId = GroupId("com.codecommit"),
         artifactId = ArtifactId("sbt-github-actions"),
         command = Nel.of("sbt", "githubWorkflowGenerate"),
+        useSandbox = true,
         commitMessage =
           update => s"Regenerate workflow with sbt-github-actions ${update.nextVersion}",
         enabledByConfig = _ => true
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/PostUpdateHook.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/PostUpdateHook.scala
index 43170bb4..adb2740d 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/PostUpdateHook.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/PostUpdateHook.scala
@@ -24,6 +24,7 @@ final case class PostUpdateHook(
     groupId: GroupId,
     artifactId: ArtifactId,
     command: Nel[String],
+    useSandbox: Boolean,
     commitMessage: Update => String,
     enabledByConfig: RepoConfig => Boolean
 )


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/edit/hooks/HookExecutorTest.scala b/modules/core/src/test/scala/org/scalasteward/core/edit/hooks/HookExecutorTest.scala
index 5d4caec4..acdb4094 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/edit/hooks/HookExecutorTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/edit/hooks/HookExecutorTest.scala
@@ -40,14 +40,12 @@ class HookExecutorTest extends AnyFunSuite with Matchers {
       .runS(initial)
       .unsafeRunSync()
 
-    state shouldBe initial.copy(commands =
-      Vector(
+    state shouldBe initial.copy(
+      commands = Vector(
         List(
+          "VAR1=val1",
+          "VAR2=val2",
           repoDir.toString,
-          "firejail",
-          s"--whitelist=$repoDir",
-          "--env=VAR1=val1",
-          "--env=VAR2=val2",
           "scalafmt",
           "--non-interactive"
         ),
@@ -68,7 +66,8 @@ class HookExecutorTest extends AnyFunSuite with Matchers {
           "-m",
           "Reformat with scalafmt 2.7.5"
         )
-      )
+      ),
+      logs = Vector((None, "Executing post-update hook for org.scalameta:scalafmt-core"))
     )
 
   }
@@ -93,8 +92,8 @@ class HookExecutorTest extends AnyFunSuite with Matchers {
       .runS(MockState.empty)
       .unsafeRunSync()
 
-    state shouldBe MockState.empty.copy(commands =
-      Vector(
+    state shouldBe MockState.empty.copy(
+      commands = Vector(
         List(
           repoDir.toString,
           "firejail",
@@ -112,7 +111,8 @@ class HookExecutorTest extends AnyFunSuite with Matchers {
           "--untracked-files=no",
           "--ignore-submodules"
         )
-      )
+      ),
+      logs = Vector((None, "Executing post-update hook for com.codecommit:sbt-github-actions"))
     )
   }
 }