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 1465e68..b2dad30 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;
@@ -61,6 +62,8 @@
private final boolean isDebugEnabled;
+ private final String tmpDirectory;
+
private final String testRunnerClassesDirectory;
/**
@@ -70,12 +73,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,
@@ -84,6 +89,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ tmpDirectory,
System.getProperty("buck.testrunner_classes",
new File("build/testrunner/classes").getAbsolutePath()));
}
@@ -96,6 +102,7 @@
String directoryForTestResults,
boolean isCodeCoverageEnabled,
boolean isDebugEnabled,
+ String tmpDirectory,
String testRunnerClassesDirectory) {
this.classpathEntries = ImmutableSet.copyOf(classpathEntries);
this.testClassNames = ImmutableSet.copyOf(testClassNames);
@@ -103,6 +110,7 @@
this.directoryForTestResults = Preconditions.checkNotNull(directoryForTestResults);
this.isCodeCoverageEnabled = isCodeCoverageEnabled;
this.isDebugEnabled = isDebugEnabled;
+ this.tmpDirectory = tmpDirectory;
this.testRunnerClassesDirectory = Preconditions.checkNotNull(testRunnerClassesDirectory);
}
@@ -115,6 +123,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.
@@ -181,6 +190,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 23b468d..3833c78 100644
--- a/src/com/facebook/buck/java/JavaTestRule.java
+++ b/src/com/facebook/buck/java/JavaTestRule.java
@@ -160,8 +160,9 @@
ImmutableList.Builder<Step> steps = ImmutableList.builder();
Path pathToTestOutput = getPathToTestOutputDirectory();
- 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.
@@ -184,6 +185,7 @@
testClassNames,
amendVmArgs(vmArgs, executionContext.getTargetDeviceOptional()),
pathToTestOutput.toString(),
+ tmpDirectory,
executionContext.isCodeCoverageEnabled(),
executionContext.isDebugEnabled());
steps.add(junit);
@@ -252,6 +254,13 @@
);
}
+ 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) {
final ImmutableSet<String> contacts = getContacts();
diff --git a/test/com/facebook/buck/java/JUnitStepTest.java b/test/com/facebook/buck/java/JUnitStepTest.java
index 17cf135..77d4ac3 100644
--- a/test/com/facebook/buck/java/JUnitStepTest.java
+++ b/test/com/facebook/buck/java/JUnitStepTest.java
@@ -53,6 +53,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";
@@ -64,6 +65,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ directoryForTemp,
testRunnerClassesDirectory);
ExecutionContext executionContext = EasyMock.createMock(ExecutionContext.class);
@@ -77,6 +79,7 @@
MoreAsserts.assertListEquals(
ImmutableList.of(
"java",
+ "-Djava.io.tmpdir=" + directoryForTemp,
vmArg1,
vmArg2,
"-verbose",
@@ -105,6 +108,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";
@@ -116,6 +120,7 @@
directoryForTestResults,
isCodeCoverageEnabled,
isDebugEnabled,
+ directoryForTemp,
testRunnerClassesDirectory);
@@ -133,6 +138,7 @@
MoreAsserts.assertListEquals(
ImmutableList.of(
"java",
+ "-Djava.io.tmpdir=" + directoryForTemp,
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005",
vmArg1,
vmArg2,