~jan0sch/smederee

Showing details for patch 2b88891eae7f9518ea860727961c836c8baacf8f.
2022-08-29 (Mon), 7:06 AM - Jens Grassel - 2b88891eae7f9518ea860727961c836c8baacf8f

SSH: Fix compilation error in integration test.

Summary of changes
2 files modified with 49 lines added and 4 lines removed
  • modules/hub/src/it/scala/de/smederee/hub/BaseSpec.scala with 38 added and 0 removed lines
  • modules/hub/src/it/scala/de/smederee/ssh/SshServerProviderTest.scala with 11 added and 4 removed lines
diff -rN -u old-smederee/modules/hub/src/it/scala/de/smederee/hub/BaseSpec.scala new-smederee/modules/hub/src/it/scala/de/smederee/hub/BaseSpec.scala
--- old-smederee/modules/hub/src/it/scala/de/smederee/hub/BaseSpec.scala	2025-02-02 14:37:43.514052493 +0000
+++ new-smederee/modules/hub/src/it/scala/de/smederee/hub/BaseSpec.scala	2025-02-02 14:37:43.514052493 +0000
@@ -9,9 +9,13 @@
 
 package de.smederee.hub
 
+import java.io.IOException
 import java.net.ServerSocket
+import java.nio.file._
+import java.nio.file.attribute.BasicFileAttributes
 
 import cats.effect._
+import cats.syntax.all._
 import com.comcast.ip4s._
 import com.typesafe.config.ConfigFactory
 import de.smederee.hub.config._
@@ -212,4 +216,38 @@
         _ <- IO(statement.close())
       } yield account
     }
+
+  /** Delete the given directory recursively.
+    *
+    * @param path
+    *   The path on the filesystem to the directory that shall be deleted.
+    * @return
+    *   `true` if the directory was deleted.
+    */
+  protected def deleteDirectory(path: Path): IO[Boolean] =
+    IO.delay {
+      if (path.toString.trim =!= "/") {
+        Files.walkFileTree(
+          path,
+          new FileVisitor[Path] {
+            override def visitFileFailed(file: Path, exc: IOException): FileVisitResult =
+              FileVisitResult.CONTINUE
+
+            override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
+              Files.delete(file)
+              FileVisitResult.CONTINUE
+            }
+
+            override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult =
+              FileVisitResult.CONTINUE
+
+            override def postVisitDirectory(dir: Path, exc: IOException): FileVisitResult = {
+              Files.delete(dir)
+              FileVisitResult.CONTINUE
+            }
+          }
+        )
+        Files.deleteIfExists(path)
+      } else false
+    }
 }
diff -rN -u old-smederee/modules/hub/src/it/scala/de/smederee/ssh/SshServerProviderTest.scala new-smederee/modules/hub/src/it/scala/de/smederee/ssh/SshServerProviderTest.scala
--- old-smederee/modules/hub/src/it/scala/de/smederee/ssh/SshServerProviderTest.scala	2025-02-02 14:37:43.514052493 +0000
+++ new-smederee/modules/hub/src/it/scala/de/smederee/ssh/SshServerProviderTest.scala	2025-02-02 14:37:43.514052493 +0000
@@ -15,11 +15,17 @@
 import cats.syntax.all._
 import com.comcast.ip4s._
 import de.smederee.hub.BaseSpec
+import de.smederee.hub.config._
 
 import munit._
 
 final class SshServerProviderTest extends BaseSpec {
 
+  val repositoriesDirectory = ResourceSuiteLocalFixture(
+    "repositories-directory",
+    Resource.make(IO(Files.createTempDirectory("test-repo-dir")))(path => deleteDirectory(path) *> IO.unit)
+  )
+
   val serverKeyFile = ResourceSuiteLocalFixture(
     "server-key-file",
     Resource.make(
@@ -32,21 +38,22 @@
 
   val freePort = ResourceFixture(Resource.make(IO(findFreePort()))(_ => IO.unit))
 
-  override def munitFixtures = List(serverKeyFile)
+  override def munitFixtures = List(repositoriesDirectory, serverKeyFile)
 
   freePort.test("run() must create and start a server with the given configuration") { port =>
     port match {
       case None => fail("Could not find a free port for testing!")
       case Some(portNumber) =>
-        val keyfile = serverKeyFile()
-        val config = SshServerConfiguration(
+        val keyfile     = serverKeyFile()
+        val darcsConfig = DarcsConfiguration(Paths.get("darcs"), DirectoryPath(repositoriesDirectory()))
+        val sshConfig = SshServerConfiguration(
           enabled = true,
           genericUser = SshUsername("darcs"),
           host = host"localhost",
           port = portNumber,
           serverKeyFile = keyfile
         )
-        val provider = new SshServerProvider(config)
+        val provider = new SshServerProvider(darcsConfig, sshConfig)
         provider.run().use { server =>
           assert(server.isStarted(), "Server not started!")
           assertEquals(server.getPort(), portNumber.toString.toInt)