Skip to content
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

Improve message if file exists instead of folder #1568

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ public File newFolder(String... paths) throws IOException {

lastMkdirsCallSuccessful = file.mkdirs();
if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
throw new IOException(
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
if (file.exists()) {
throw new IOException(
"a file with the path \'" + relativePath.getPath() + "\' exists");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can include already in the message, as createNewFile includes it in error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the word "already" for this message two. But it is wrong here because the user wants to create a folder and not a file. Therefore we cannot say the file is already there.

} else {
throw new IOException(
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
}
}
}
if (!lastMkdirsCallSuccessful) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/junit/rules/TemporaryFolderUsageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOExc
File file = new File(tempFolder.getRoot(), "level1");
assertTrue("Could not create" + file, file.createNewFile());

thrown.expect(IOException.class);
thrown.expectMessage("a file with the path 'level1' exists");
tempFolder.newFolder("level1");
}

@Test
public void newFolderWithGivenFolderThrowsIOExceptionWhenFolderCannotBeCreated() throws IOException {
tempFolder.create();
assertTrue("Could not make folder " + tempFolder.getRoot() + " read only.",
tempFolder.getRoot().setReadOnly());

thrown.expect(IOException.class);
thrown.expectMessage("could not create a folder with the path 'level1'");
tempFolder.newFolder("level1");
Expand Down