summaryrefslogtreecommitdiff
path: root/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala
blob: b5bacf2bc30a5734cd7f75f31f3c9c365a072029 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package io.crashbox.ci

import java.io.{ ByteArrayOutputStream, File }
import java.nio.file.Files


object TestUtil {

  def withTempFile[A](action: File => A): A = {
    def rm(parent: File): Unit = if (parent.isDirectory) {
      parent.listFiles.foreach{ child =>
        rm(child)
      }
    }
    val dir = Files.createTempDirectory("crashbox-test").toFile
    try action(dir)
    finally rm(dir)
  }

  def withTempStream[A](action: ByteArrayOutputStream => A, size: Int = 1024): A = {
    val out = new ByteArrayOutputStream(size)
    try action(out)
    finally out.close()
  }

}