Fix temporary file creation and cleanup during testing

Try to find a suitable location when running under Buck by looking
for buck-out/tmp. If Maven ever invoked with a JAR this would be
$project/target/tmp.

Multiple hooks may be extracted per execution run, so there can
be multiple files to clean up after the test.

Change-Id: I5e5e506fe5070dc6c517cb679023f7a699d0cb62
This commit is contained in:
Shawn Pearce
2013-04-30 16:08:58 -07:00
committed by Gerrit Code Review
parent f2616ebbf3
commit 987a16be91
7 changed files with 101 additions and 68 deletions

View File

@@ -21,10 +21,9 @@ import junit.framework.TestCase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
public class SitePathsTest extends TestCase {
public void testCreate_NotExisting() throws FileNotFoundException {
public void testCreate_NotExisting() throws IOException {
final File root = random();
final SitePaths site = new SitePaths(root);
assertTrue(site.isNew);
@@ -32,7 +31,7 @@ public class SitePathsTest extends TestCase {
assertEquals(new File(root, "etc"), site.etc_dir);
}
public void testCreate_Empty() throws FileNotFoundException {
public void testCreate_Empty() throws IOException {
final File root = random();
try {
assertTrue(root.mkdir());
@@ -91,8 +90,11 @@ public class SitePathsTest extends TestCase {
assertEquals(new File(pfx + "a").getCanonicalFile(), site.resolve(pfx + "a"));
}
private File random() {
final File t = new File("target");
return new File(t, "random-name-" + UUID.randomUUID().toString());
private static File random() throws IOException {
File tmp = File.createTempFile("gerrit_test_", "_site");
if (!tmp.delete()) {
throw new IOException("Cannot create " + tmp.getPath());
}
return tmp;
}
}