Skip to content

Commit 5446578

Browse files
committed
Use check ignore in HookExecutor
1 parent 502d5e5 commit 5446578

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,20 @@ final class HookExecutor[F[_]](implicit
9696
newContent = oldContent.fold(newLines)(_ + "\n" + newLines)
9797
_ <- fileAlg.writeFile(file, newContent)
9898
pathAsString = file.pathAsString
99-
_ <- gitAlg
99+
100+
addAndCommit = gitAlg.add(repo, pathAsString).flatMap { _ =>
101+
val blameIgnoreCommitMsg =
102+
CommitMsg(s"Add '${commitMsg.title}' to $gitBlameIgnoreRevsName")
103+
gitAlg.commitAllIfDirty(repo, blameIgnoreCommitMsg)
104+
}
105+
maybeBlameIgnoreCommit <- gitAlg
100106
.checkIgnore(repo, pathAsString)
101107
.ifM(
102-
logger.warn(s"Impossible to add '$pathAsString' because it is git ignored."),
103-
gitAlg.add(repo, pathAsString)
108+
logger
109+
.warn(s"Impossible to add '$pathAsString' because it is git ignored.")
110+
.as(Option.empty[Commit]),
111+
addAndCommit
104112
)
105-
blameIgnoreCommitMsg = CommitMsg(s"Add '${commitMsg.title}' to $gitBlameIgnoreRevsName")
106-
maybeBlameIgnoreCommit <- gitAlg.commitAllIfDirty(repo, blameIgnoreCommitMsg)
107113
} yield maybeBlameIgnoreCommit
108114
} else F.pure(None)
109115
}

modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class MillAlgTest extends FunSuite {
2828
"show",
2929
extractDeps
3030
)
31-
val initial = MockState.empty.copy(commandOutputs = Map(millCmd -> List("""{"modules":[]}""")))
31+
val initial =
32+
MockState.empty.copy(commandOutputs = Map(millCmd -> Right(List("""{"modules":[]}"""))))
3233
val state = millAlg.getDependencies(buildRoot).runS(initial).unsafeRunSync()
3334
val expected = initial.copy(
3435
trace = Vector(

modules/core/src/test/scala/org/scalasteward/core/edit/hooks/HookExecutorTest.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.scalasteward.core.edit.hooks
22

3+
import cats.data.NonEmptyList
34
import cats.syntax.all._
45
import munit.CatsEffectSuite
56
import org.scalasteward.core.TestInstances.{dummyRepoCache, dummySha1}
@@ -15,6 +16,9 @@ import org.scalasteward.core.repoconfig.{PostUpdateHookConfig, RepoConfig, Scala
1516
import org.scalasteward.core.scalafmt.ScalafmtAlg.opts
1617
import org.scalasteward.core.scalafmt.{scalafmtArtifactId, scalafmtBinary, scalafmtGroupId}
1718
import org.scalasteward.core.util.Nel
19+
import org.scalasteward.core.io.process.ProcessFailedException
20+
import org.scalasteward.core.io.process
21+
import scala.collection.mutable.ListBuffer
1822

1923
class HookExecutorTest extends CatsEffectSuite {
2024
private val repo = Repo("scala-steward-org", "scala-steward")
@@ -28,17 +32,19 @@ class HookExecutorTest extends CatsEffectSuite {
2832
}
2933

3034
test("scalafmt: enabled by config") {
35+
val gitBlameIgnoreRevs = repoDir / gitBlameIgnoreRevsName
3136
val update = (scalafmtGroupId % scalafmtArtifactId % "2.7.4" %> "2.7.5").single
3237
val initial = MockState.empty.copy(commandOutputs =
3338
Map(
3439
FileGitAlg.gitCmd.toList ++
3540
List("status", "--porcelain", "--untracked-files=no", "--ignore-submodules") ->
36-
List("build.sbt"),
41+
Right(List("build.sbt")),
3742
FileGitAlg.gitCmd.toList ++ List("rev-parse", "--verify", "HEAD") ->
38-
List(dummySha1.value.value)
43+
Right(List(dummySha1.value.value)),
44+
FileGitAlg.gitCmd.toList ++ List("check-ignore", gitBlameIgnoreRevs.pathAsString) ->
45+
Left(dummyProcessError)
3946
)
4047
)
41-
val gitBlameIgnoreRevs = repoDir / gitBlameIgnoreRevsName
4248
val state = FileAlgTest.ioFileAlg.deleteForce(gitBlameIgnoreRevs) >>
4349
hookExecutor.execPostUpdateHooks(data, update).runS(initial)
4450

@@ -70,6 +76,7 @@ class HookExecutorTest extends CatsEffectSuite {
7076
Cmd(gitCmd(repoDir), "rev-parse", "--verify", "HEAD"),
7177
Cmd("read", gitBlameIgnoreRevs.pathAsString),
7278
Cmd("write", gitBlameIgnoreRevs.pathAsString),
79+
Cmd(gitCmd(repoDir), "check-ignore", gitBlameIgnoreRevs.pathAsString),
7380
Cmd(gitCmd(repoDir), "add", gitBlameIgnoreRevs.pathAsString),
7481
Cmd(
7582
gitCmd(repoDir),
@@ -166,4 +173,7 @@ class HookExecutorTest extends CatsEffectSuite {
166173

167174
state.map(assertEquals(_, expected))
168175
}
176+
177+
private val dummyProcessError =
178+
new ProcessFailedException(process.Args(NonEmptyList.of("cmd")), ListBuffer.empty, 1)
169179
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package org.scalasteward.core.io
22

33
import cats.data.Kleisli
4+
import cats.effect.IO
45
import org.scalasteward.core.application.Config.ProcessCfg
56
import org.scalasteward.core.mock.MockEff
67

78
object MockProcessAlg {
89
def create(config: ProcessCfg): ProcessAlg[MockEff] =
910
new ProcessAlg(config)({ args =>
10-
Kleisli {
11-
_.modify { s =>
12-
val cmd = args.workingDirectory.map(_.toString).toList ++ args.command.toList
13-
val s1 = s.exec(cmd, args.extraEnv: _*)
14-
val a = s.commandOutputs.getOrElse(args.command.toList, List.empty)
15-
(s1, a)
16-
}
11+
Kleisli { x =>
12+
for {
13+
state <- x.get
14+
cmd = args.workingDirectory.map(_.toString).toList ++ args.command.toList
15+
newState = state.exec(cmd, args.extraEnv: _*)
16+
res <- x
17+
.set(newState)
18+
.flatMap { _ =>
19+
state.commandOutputs
20+
.getOrElse(args.command.toList, Right(List.empty))
21+
.fold(err => IO.raiseError(err), a => IO.pure(a))
22+
}
23+
} yield res
1724
}
1825
})
1926
}

modules/core/src/test/scala/org/scalasteward/core/mock/MockState.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
1010

1111
final case class MockState(
1212
trace: Vector[TraceEntry],
13-
commandOutputs: Map[List[String], List[String]],
13+
commandOutputs: Map[List[String], Either[Throwable, List[String]]],
1414
files: Map[File, String],
1515
uris: Map[Uri, String],
1616
clientResponses: HttpApp[MockEff]

0 commit comments

Comments
 (0)