summaryrefslogtreecommitdiff
path: root/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala
diff options
context:
space:
mode:
Diffstat (limited to 'crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala')
-rw-r--r--crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala33
1 files changed, 33 insertions, 0 deletions
diff --git a/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala
new file mode 100644
index 0000000..034c6a3
--- /dev/null
+++ b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala
@@ -0,0 +1,33 @@
+package io.crashbox.ci
+
+import java.io.{ ByteArrayOutputStream, File }
+import java.nio.file.Files
+
+object IOUtil {
+
+ def withTempDir[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()
+ }
+
+ def withTemp[A](action: (File, ByteArrayOutputStream) => A, size: Int = 1024): A = {
+ withTempDir { d =>
+ withTempStream { s =>
+ action(d, s)
+ }
+ }
+ }
+
+}