-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update TemporaryFolder.newFolder(String) to support passing in paths #1402
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,16 +167,27 @@ public File newFile() throws IOException { | |
} | ||
|
||
/** | ||
* Returns a new fresh folder with the given name under the temporary | ||
* Returns a new fresh folder with the given path under the temporary | ||
* folder. | ||
*/ | ||
public File newFolder(String folder) throws IOException { | ||
return newFolder(new String[]{folder}); | ||
if (new File(folder).isAbsolute()) { | ||
throw new IOException("folder name must be a relative path"); | ||
} | ||
File file = new File(getRoot(), folder); | ||
if (!file.mkdirs()) { | ||
throw new IOException( | ||
"a folder with the name \'" + folder + "\' already exists"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this error message really accurate? I know it's the same as in the other method but I'm sure there are other reasons why a folder could not be created. |
||
} | ||
return file; | ||
} | ||
|
||
/** | ||
* Returns a new fresh folder with the given name(s) under the temporary | ||
* folder. | ||
* folder. For example, if you pass in the strings {@code "parent"} and {@code "child"} | ||
* then a directory named {@code "parent"} will be created under the temporary folder | ||
* and a directory named {@code "child"} will be created under the newly-created | ||
* {@code "parent"} directory. | ||
*/ | ||
public File newFolder(String... folderNames) throws IOException { | ||
File file = getRoot(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,12 +84,32 @@ public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists | |
} | ||
|
||
@Test | ||
public void newFolderWithGivenFolderThrowsIOExceptionIfFolderNameConsistsOfMultiplePathComponents() | ||
public void newFolderWithPathStartingWithFileSeparatorThrowsIOException() | ||
throws IOException { | ||
tempFolder.create(); | ||
thrown.expect(IOException.class); | ||
thrown.expectMessage("name cannot consist of multiple path components"); | ||
thrown.expectMessage("folder name must be a relative path"); | ||
tempFolder.newFolder(File.separator + "temp1"); | ||
} | ||
|
||
@Test | ||
public void newFolderWithPathContainingFileSeparaterCreatesDirectories() | ||
throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFolder("temp1" + File.separator + "temp2"); | ||
File temp1 = new File(tempFolder.getRoot(), "temp1"); | ||
assertFileIsDirectory(temp1); | ||
assertFileIsDirectory(new File(temp1, "temp2")); | ||
} | ||
|
||
@Test | ||
public void newFolderWithPathContainingForwardSlashCreatesDirectories() | ||
throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFolder("temp1/temp2"); | ||
File temp1 = new File(tempFolder.getRoot(), "temp1"); | ||
assertFileIsDirectory(temp1); | ||
assertFileIsDirectory(new File(temp1, "temp2")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test won't run on Windows, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
|
||
@Test | ||
|
@@ -114,7 +134,7 @@ public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultipl | |
@Test | ||
public void createInitializesRootFolder() throws IOException { | ||
tempFolder.create(); | ||
assertFileExists(tempFolder.getRoot()); | ||
assertFileIsDirectory(tempFolder.getRoot()); | ||
} | ||
|
||
@Test | ||
|
@@ -155,7 +175,7 @@ public void newRandomFolderIsCreatedUnderRootFolder() throws IOException { | |
tempFolder.create(); | ||
|
||
File f = tempFolder.newFolder(); | ||
assertFileExists(f); | ||
assertFileIsDirectory(f); | ||
assertFileCreatedUnderRootFolder("Random folder", f); | ||
} | ||
|
||
|
@@ -164,7 +184,7 @@ public void newNestedFoldersCreatedUnderRootFolder() throws IOException { | |
tempFolder.create(); | ||
|
||
File f = tempFolder.newFolder("top", "middle", "bottom"); | ||
assertFileExists(f); | ||
assertFileIsDirectory(f); | ||
assertParentFolderForFileIs(f, new File(tempFolder.getRoot(), | ||
"top/middle")); | ||
assertParentFolderForFileIs(f.getParentFile(), | ||
|
@@ -200,8 +220,20 @@ private void checkFileExists(String msg, File file, boolean exists) { | |
file.exists(), is(exists)); | ||
} | ||
|
||
private void checkFileIsDirectory(String msg, File file, boolean isDirectory) { | ||
assertThat("File is null", file, is(notNullValue())); | ||
assertThat("File '" + file.getAbsolutePath() + "' " + msg, | ||
file.isDirectory(), is(isDirectory)); | ||
} | ||
|
||
private void assertFileExists(File file) { | ||
checkFileExists("does not exist", file, true); | ||
checkFileIsDirectory("is a directory", file, false); | ||
} | ||
|
||
private void assertFileIsDirectory(File file) { | ||
checkFileExists("does not exist", file, true); | ||
checkFileIsDirectory("is not a directory", file, true); | ||
} | ||
|
||
private void assertFileCreatedUnderRootFolder(String msg, File f) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably rename
folder
topath
, right?