From b7325ec636c69daf7cfc97739407cd0edbff1af3 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 6 Mar 2015 21:27:31 -0500 Subject: [PATCH] Add some handy TestUtils to create scratch files --- .../com/typesafe/config/impl/TestUtils.scala | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/config/src/test/scala/com/typesafe/config/impl/TestUtils.scala b/config/src/test/scala/com/typesafe/config/impl/TestUtils.scala index b8536960..57a6f893 100644 --- a/config/src/test/scala/com/typesafe/config/impl/TestUtils.scala +++ b/config/src/test/scala/com/typesafe/config/impl/TestUtils.scala @@ -796,4 +796,37 @@ abstract trait TestUtils { assertEquals("found expected validation problems, got '" + problems + "' and expected '" + expecteds + "'", expecteds.size, problems.size) } + + protected def writeFile(f: File, content: String): Unit = { + val writer = new java.io.PrintWriter(f, "UTF-8") + writer.append(content) + writer.close() + } + + private def deleteRecursive(f: File): Unit = { + if (f.exists) { + if (f.isDirectory) { + val children = f.listFiles() + if (children ne null) { + for (c <- children) + deleteRecursive(c) + } + } + f.delete() + } + } + + protected def withScratchDirectory[T](testcase: String)(body: File => T): Unit = { + val target = new File("config/target") + if (!target.isDirectory) + throw new RuntimeException(s"Expecting $target to exist") + val suffix = java.lang.Integer.toHexString(java.util.concurrent.ThreadLocalRandom.current.nextInt) + val scratch = new File(target, s"$testcase-$suffix") + scratch.mkdirs() + try { + body(scratch) + } finally { + deleteRecursive(scratch) + } + } }