Skip to content

Commit b164a97

Browse files
committed
TestUtils: never create test directories in /tmp/
Curtis Rueden offered reluctance to fall back to creating tests in /tmp/ when it was determined that the tests are run in in unknown location. This developer agrees. Therefore, let's test carefully that we found some sensible location close to the .class files, handling unit tests run via Maven extra nicely. For details, see the pull request corresponding to this topic branch: #70 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 9a38683 commit b164a97

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/main/java/org/scijava/test/TestUtils.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,25 @@ public static File createTemporaryDirectory(final String prefix,
103103
final Class<?> forClass, final String suffix) throws IOException
104104
{
105105
final URL directory = ClassUtils.getLocation(forClass);
106-
if (directory != null && "file".equals(directory.getProtocol())) {
107-
final String path = directory.getPath();
108-
if (path != null && path.endsWith("/target/test-classes/")) {
109-
final File baseDirectory =
110-
new File(path.substring(0, path.length() - 13));
111-
final File file = new File(baseDirectory, prefix + suffix);
112-
if (file.exists()) FileUtils.deleteRecursively(file);
113-
if (!file.mkdir()) throw new IOException("Could not make directory " + file);
114-
return file;
115-
}
106+
if (directory == null) {
107+
throw new IllegalArgumentException("No location for class " + forClass);
108+
}
109+
if (!"file".equals(directory.getProtocol())) {
110+
throw new IllegalArgumentException("Invalid directory: " + directory);
116111
}
117-
return FileUtils.createTemporaryDirectory(prefix, suffix);
112+
final String path = directory.getPath();
113+
if (path == null) throw new IllegalArgumentException("Directory has null path");
114+
final File baseDirectory;
115+
if (path.endsWith("/target/test-classes/")) {
116+
baseDirectory = new File(path).getParentFile();
117+
} else {
118+
baseDirectory = new File(path);
119+
}
120+
121+
final File file = new File(baseDirectory, prefix + suffix);
122+
if (file.exists()) FileUtils.deleteRecursively(file);
123+
if (!file.mkdir()) throw new IOException("Could not make directory " + file);
124+
return file;
118125
}
119126

120127
/**

0 commit comments

Comments
 (0)