All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.misk.testing.TemporaryFolder.kt Maven / Gradle / Ivy

There is a newer version: 2024.09.17.200749-4708422
Show newest version
package misk.testing

import com.google.inject.Provides
import misk.inject.KAbstractModule
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
import java.nio.file.Files
import java.nio.file.Path
import jakarta.inject.Inject
import jakarta.inject.Singleton

/** A temporary folder for use by a given test */
class TemporaryFolder(val root: Path) {
  /** Deletes all files and folders under the temporary folder */
  fun delete() {
    root.toFile().deleteRecursively()
  }

  /** @return a new folder with a randomly generated name */
  fun newFolder(): Path = Files.createTempDirectory(root, "")

  /** @return a new folder with the given name */
  fun newFolder(name: String): Path = Files.createDirectories(root.resolve(name))

  /** @return a new file with a randomly generated name */
  fun newFile(): Path = Files.createTempFile(root, "", "")

  /** @return a new file with the given name */
  fun newFile(name: String): Path = Files.createFile(root.resolve(name))
}

class TemporaryFolderModule : KAbstractModule() {
  override fun configure() {
    multibind().to()
  }

  @Provides
  @Singleton
  fun provideTemporaryFolder(): TemporaryFolder {
    val tempDir = Files.createTempDirectory("test-")
    tempDir.toFile().deleteOnExit()

    return TemporaryFolder(tempDir)
  }

  class DeleteTempFolder @Inject constructor(
    private val tempDir: TemporaryFolder
  ) : AfterEachCallback {
    override fun afterEach(context: ExtensionContext) {
      tempDir.delete()
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy