Repository: scala-steward-org/scala-steward
Pull request #1660 (2020-10-12 19:00:44)

Test MillAlg.getDependencies


Patch:
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 63e7440f..3249c666 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
@@ -39,6 +39,9 @@ object MillAlg {
         |interp.load.ivy("${BuildInfo.organization}" %% "${BuildInfo.millPluginModuleName}" % "${BuildInfo.version}")
         |""".stripMargin
 
+  val extractDeps: String =
+    s"${BuildInfo.millPluginModuleRootPkg}.StewardPlugin/extractDeps"
+
   def create[F[_]](implicit
       fileAlg: FileAlg[F],
       processAlg: ProcessAlg[F],
@@ -53,25 +56,13 @@ object MillAlg {
         for {
           repoDir <- workspaceAlg.repoDir(repo)
           predef = repoDir / "scala-steward.sc"
-          _ <- fileAlg.writeFile(predef, content)
-          extracted <- processAlg.exec(
-            Nel(
-              "mill",
-              List(
-                "-i",
-                "-p",
-                predef.toString(),
-                "show",
-                s"${BuildInfo.millPluginModuleRootPkg}.StewardPlugin/extractDeps"
-              )
-            ),
-            repoDir
-          )
+          extracted <- fileAlg.createTemporarily(predef, content) {
+            val command = Nel("mill", List("-i", "-p", predef.toString, "show", extractDeps))
+            processAlg.exec(command, repoDir)
+          }
           parsed <- F.fromEither(
             parser.parseModules(extracted.dropWhile(!_.startsWith("{")).mkString("\n"))
           )
-          _ <- fileAlg.deleteForce(predef)
-
         } yield parsed.map(module => Scope(module.dependencies, module.repositories))
 
       override def runMigrations(repo: Repo, migrations: Nel[Migration]): F[Unit] = F.unit


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala
new file mode 100644
index 00000000..917032b8
--- /dev/null
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/mill/MillAlgTest.scala
@@ -0,0 +1,26 @@
+package org.scalasteward.core.buildtool.mill
+
+import org.scalasteward.core.buildtool.mill.MillAlg.extractDeps
+import org.scalasteward.core.mock.MockContext.{config, millAlg}
+import org.scalasteward.core.mock.MockState
+import org.scalasteward.core.vcs.data.Repo
+import org.scalatest.funsuite.AnyFunSuite
+import org.scalatest.matchers.should.Matchers
+
+class MillAlgTest extends AnyFunSuite with Matchers {
+  test("getDependencies") {
+    val repo = Repo("lihaoyi", "fastparse")
+    val repoDir = config.workspace / repo.show
+    val predef = s"$repoDir/scala-steward.sc"
+    val millCmd = List("mill", "-i", "-p", predef, "show", extractDeps)
+    val initial = MockState.empty.copy(commandOutputs = Map(millCmd -> List("""{"modules":[]}""")))
+    val state = millAlg.getDependencies(repo).runS(initial).unsafeRunSync()
+    state shouldBe initial.copy(
+      commands = Vector(
+        List("write", predef),
+        List("VAR1=val1", "VAR2=val2", repoDir.toString) ++ millCmd,
+        List("rm", "-rf", predef)
+      )
+    )
+  }
+}
diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala
index 5983e90c..ef131ec5 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/sbt/SbtAlgTest.scala
@@ -30,7 +30,7 @@ class SbtAlgTest extends AnyFunSuite with Matchers {
     )
   }
 
-  test("getDependenciesAndResolvers") {
+  test("getDependencies") {
     val repo = Repo("typelevel", "cats")
     val repoDir = config.workspace / repo.show
     val files = Map(repoDir / "project" / "build.properties" -> "sbt.version=1.2.6")
diff --git a/modules/core/src/test/scala/org/scalasteward/core/io/MockProcessAlg.scala b/modules/core/src/test/scala/org/scalasteward/core/io/MockProcessAlg.scala
index 20c44ae6..5a23ab53 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/io/MockProcessAlg.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/io/MockProcessAlg.scala
@@ -15,6 +15,9 @@ class MockProcessAlg(config: ProcessCfg) extends UsingFirejail[MockEff](config.s
       extraEnv: (String, String)*
   ): MockEff[List[String]] =
     applyPure { s =>
-      (s.exec(cwd.toString :: command.toList, extraEnv ++ envVars: _*), List.empty[String])
+      (
+        s.exec(cwd.toString :: command.toList, extraEnv ++ envVars: _*),
+        s.commandOutputs.getOrElse(command.toList, List.empty)
+      )
     }
 }
diff --git a/modules/core/src/test/scala/org/scalasteward/core/mock/MockState.scala b/modules/core/src/test/scala/org/scalasteward/core/mock/MockState.scala
index 77d760b0..569689f6 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/mock/MockState.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/mock/MockState.scala
@@ -5,6 +5,7 @@ import org.http4s.Uri
 
 final case class MockState(
     commands: Vector[List[String]],
+    commandOutputs: Map[List[String], List[String]],
     logs: Vector[(Option[Throwable], String)],
     files: Map[File, String],
     uris: Map[Uri, String]
@@ -30,5 +31,11 @@ final case class MockState(
 
 object MockState {
   def empty: MockState =
-    MockState(commands = Vector.empty, logs = Vector.empty, files = Map.empty, uris = Map.empty)
+    MockState(
+      commands = Vector.empty,
+      commandOutputs = Map.empty,
+      logs = Vector.empty,
+      files = Map.empty,
+      uris = Map.empty
+    )
 }