Supply temporary directory to java_test()
Similar to genrule() give the test running JVM a temporary directory
below buck-out that is uniquely named for this test target. Send
the path in by both the TMP environment variable and java.io.tmpdir
system property.
Fixes https://github.com/facebook/buck/issues/13
diff --git a/src/com/facebook/buck/java/JUnitStep.java b/src/com/facebook/buck/java/JUnitStep.java
index fe6fdd3..333e472 100644
--- a/src/com/facebook/buck/java/JUnitStep.java
+++ b/src/com/facebook/buck/java/JUnitStep.java
@@ -24,6 +24,7 @@
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
@@ -60,6 +61,8 @@
private final boolean isDebugEnabled;
+ private final String tmpDirectory;
+
private final String testRunnerClassesDirectory;
/**
@@ -69,12 +72,14 @@
* that will be run, as well as an entry for JUnit.
* @param testClassNames the fully qualified names of the Java tests to run
* @param directoryForTestResults directory where test results should be written
+ * @param tmpDirectory directory tests can use for local file scratch space.
*/
public JUnitStep(
Set<String> classpathEntries,
Set<String> testClassNames,
List<String> vmArgs,
String directoryForTestResults,
+ String tmpDirectory,
boolean isCodeCoverageEnabled,
boolean isDebugEnabled) {
this(classpathEntries,
@@ -83,6 +88,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ tmpDirectory,
System.getProperty("buck.testrunner_classes",
new File("build/testrunner/classes").getAbsolutePath()));
}
@@ -95,6 +101,7 @@
String directoryForTestResults,
boolean isCodeCoverageEnabled,
boolean isDebugEnabled,
+ String tmpDirectory,
String testRunnerClassesDirectory) {
this.classpathEntries = ImmutableSet.copyOf(classpathEntries);
this.testClassNames = ImmutableSet.copyOf(testClassNames);
@@ -102,6 +109,7 @@
this.directoryForTestResults = Preconditions.checkNotNull(directoryForTestResults);
this.isCodeCoverageEnabled = isCodeCoverageEnabled;
this.isDebugEnabled = isDebugEnabled;
+ this.tmpDirectory = tmpDirectory;
this.testRunnerClassesDirectory = Preconditions.checkNotNull(testRunnerClassesDirectory);
}
@@ -114,6 +122,7 @@
protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
ImmutableList.Builder<String> args = ImmutableList.builder();
args.add("java");
+ args.add(String.format("-Djava.io.tmpdir=%s", tmpDirectory));
// Add the output property for EMMA so if the classes are instrumented, coverage.ec will be
// placed in the EMMA output folder.
@@ -180,6 +189,13 @@
return args.build();
}
+ @Override
+ public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
+ ImmutableMap.Builder<String, String> env = ImmutableMap.builder();
+ env.put("TMP", tmpDirectory);
+ return env.build();
+ }
+
private void warnUser(ExecutionContext context, String message) {
context.getStdErr().println(context.getAnsi().asWarningText(message));
}
diff --git a/src/com/facebook/buck/java/JavaTestRule.java b/src/com/facebook/buck/java/JavaTestRule.java
index 4e7acb0..0b6ae5c 100644
--- a/src/com/facebook/buck/java/JavaTestRule.java
+++ b/src/com/facebook/buck/java/JavaTestRule.java
@@ -147,8 +147,9 @@
ImmutableList.Builder<Step> steps = ImmutableList.builder();
String pathToTestOutput = getPathToTestOutput();
- MakeCleanDirectoryStep mkdirClean = new MakeCleanDirectoryStep(pathToTestOutput);
- steps.add(mkdirClean);
+ String tmpDirectory = getPathToTmpDirectory();
+ steps.add(new MakeCleanDirectoryStep(pathToTestOutput));
+ steps.add(new MakeCleanDirectoryStep(tmpDirectory));
// If there are android resources, then compile the uber R.java files and add them to the
// classpath used to run the test runner.
@@ -171,6 +172,7 @@
testClassNames,
amendVmArgs(vmArgs, executionContext.getTargetDeviceOptional()),
pathToTestOutput,
+ tmpDirectory,
executionContext.isCodeCoverageEnabled(),
executionContext.isDebugEnabled());
steps.add(junit);
@@ -226,6 +228,13 @@
getBuildTarget().getShortName());
}
+ private String getPathToTmpDirectory() {
+ return String.format("%s/%s__java_test_%s_tmp__",
+ BuckConstant.GEN_DIR,
+ getBuildTarget().getBasePathWithSlash(),
+ getBuildTarget().getShortName());
+ }
+
@Override
public Callable<TestResults> interpretTestResults(final ExecutionContext context) {
return new Callable<TestResults>() {
diff --git a/test/com/facebook/buck/java/JUnitStepTest.java b/test/com/facebook/buck/java/JUnitStepTest.java
index 2f2fa0a..0c50a49 100644
--- a/test/com/facebook/buck/java/JUnitStepTest.java
+++ b/test/com/facebook/buck/java/JUnitStepTest.java
@@ -50,6 +50,7 @@
List<String> vmArgs = ImmutableList.of(vmArg1, vmArg2);
String directoryForTestResults = "buck-out/gen/theresults/";
+ String directoryForTemp = "buck-out/gen/thetmp/";
boolean isCodeCoverageEnabled = false;
boolean isDebugEnabled = false;
String testRunnerClassesDirectory = "build/classes/junit";
@@ -61,6 +62,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ directoryForTemp,
testRunnerClassesDirectory);
ExecutionContext executionContext = EasyMock.createMock(ExecutionContext.class);
@@ -74,6 +76,7 @@
MoreAsserts.assertListEquals(
ImmutableList.of(
"java",
+ "-Djava.io.tmpdir=" + directoryForTemp,
vmArg1,
vmArg2,
"-verbose",
@@ -102,6 +105,7 @@
List<String> vmArgs = ImmutableList.of(vmArg1, vmArg2);
String directoryForTestResults = "buck-out/gen/theresults/";
+ String directoryForTemp = "buck-out/gen/thetmp/";
boolean isCodeCoverageEnabled = false;
boolean isDebugEnabled = true;
String testRunnerClassesDirectory = "build/classes/junit";
@@ -113,6 +117,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ directoryForTemp,
testRunnerClassesDirectory);
@@ -129,6 +134,7 @@
MoreAsserts.assertListEquals(
ImmutableList.of(
"java",
+ "-Djava.io.tmpdir=" + directoryForTemp,
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005",
vmArg1,
vmArg2,