Skip to content

Commit a6ff326

Browse files
authored
Merge pull request #3005 from scala-steward-org/topic/exec-commands-in-mockeff
Allow to execute commands in MockEff
2 parents bd75a8c + 4d0462d commit a6ff326

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

modules/core/src/main/scala/org/scalasteward/core/io/ProcessAlg.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import org.scalasteward.core.io.process.{Args, SlurpOption, SlurpOptions}
2424
import org.scalasteward.core.util.Nel
2525
import org.typelevel.log4cats.Logger
2626

27-
final class ProcessAlg[F[_]](config: ProcessCfg)(execImpl: Args => F[List[String]]) {
27+
final class ProcessAlg[F[_]](config: ProcessCfg)(
28+
private[io] val execImpl: Args => F[List[String]]
29+
) {
2830
def exec(
2931
command: Nel[String],
3032
workingDirectory: File,

modules/core/src/test/scala/org/scalasteward/core/io/MockProcessAlg.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ import org.scalasteward.core.mock.MockEff
88
object MockProcessAlg {
99
def create(config: ProcessCfg): ProcessAlg[MockEff] =
1010
new ProcessAlg(config)({ args =>
11-
Kleisli { x =>
11+
Kleisli { ctx =>
1212
for {
13-
state <- x.get
13+
state <- ctx.get
1414
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-
}
15+
_ <- ctx.set(state.exec(cmd, args.extraEnv: _*))
16+
res <- state.commandOutputs.get(args.command.toList) match {
17+
case Some(output) => IO.fromEither(output)
18+
case None if state.execCommands => ProcessAlgTest.ioProcessAlg.execImpl(args)
19+
case None => IO.pure(List.empty)
20+
}
2321
} yield res
2422
}
2523
})

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
1111
final case class MockState(
1212
trace: Vector[TraceEntry],
1313
commandOutputs: Map[List[String], Either[Throwable, List[String]]],
14+
execCommands: Boolean,
1415
files: Map[File, String],
1516
uris: Map[Uri, String],
1617
clientResponses: HttpApp[MockEff]
@@ -41,6 +42,7 @@ object MockState {
4142
MockState(
4243
trace = Vector.empty,
4344
commandOutputs = Map.empty,
45+
execCommands = false,
4446
files = Map.empty,
4547
uris = Map.empty,
4648
clientResponses = HttpApp.notFound

0 commit comments

Comments
 (0)