Skip to content

Commit ffac6f0

Browse files
committed
TestUtils: add method to create file in a sub-path
This is useful, e.g., for creating dummy scripts to test script discovery.
1 parent d6a9189 commit ffac6f0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,32 @@
4444
* A bunch of helpful functions for unit tests.
4545
*
4646
* @author Johannes Schindelin
47+
* @author Curtis Rueden
4748
*/
4849
public class TestUtils {
4950

51+
/**
52+
* Creates an empty file at the given path, creating intermediate directories
53+
* as necessary.
54+
*
55+
* @param parent The parent directory of the relative path.
56+
* @param path The forward-slash-separated path to create.
57+
* @return a {@link File} pointing at the newly created empty path.
58+
* @throws IOException if the file cannot be created.
59+
*/
60+
public static File createPath(final File parent, final String path)
61+
throws IOException
62+
{
63+
File file = parent;
64+
final String[] elements = path.split("/");
65+
for (int i=0; i<elements.length; i++) {
66+
file = new File(file, elements[i]);
67+
if (i == elements.length - 1) file.createNewFile();
68+
else file.mkdir();
69+
}
70+
return file;
71+
}
72+
5073
/**
5174
* Makes a temporary directory for use with unit tests.
5275
* <p>

0 commit comments

Comments
 (0)