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

Update TemporaryFolder.newFolder(String) to support passing in paths #1402

Merged

Conversation

kcooney
Copy link
Member

@kcooney kcooney commented Jan 1, 2017

that include path separator characters. The overload of newFolder() that
supports passing in multiple strings still does not allow path separators.

@kcooney kcooney added the 4.13 label Jan 2, 2017
if (folder.startsWith(File.separator)) {
throw new IOException("Folder name cannot start with a file separator");
}
return newFolder(folder.split(FILE_SEPARATOR_REGEX));
Copy link
Member

Choose a reason for hiding this comment

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

Instead of the regex, can we use new File(folder) and call getParent() until it returns null? This way a test could use newFolder("foo/bar") and be able to run on Windows and Unix.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't realize that File supported / as a path separator for Windows.

Instead of your solution, how about I rewite this method to use mkdirs()?

Copy link
Member

Choose a reason for hiding this comment

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

How would you report already existing intermediate folders (see newFolder(String[])) that way?

Copy link
Member Author

Choose a reason for hiding this comment

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

newFolder(String...) does not report already existing intermediate directories

Copy link
Member

Choose a reason for hiding this comment

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

From the code it looks like it does… 😯

Copy link
Member

@marcphilipp marcphilipp Jan 2, 2017

Choose a reason for hiding this comment

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

Never mind... 🙈

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to use mkdirs() and added a test for paths that contain a forward slash and for paths that contain the file separator.

tempFolder.newFolder("temp1/temp2");
File temp1 = new File(tempFolder.getRoot(), "temp1");
assertFileIsDirectory(temp1);
assertFileIsDirectory(new File(temp1, "temp2"));
Copy link
Member

Choose a reason for hiding this comment

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

This test won't run on Windows, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@kcooney
Copy link
Member Author

kcooney commented Jan 2, 2017

BTW, the behavior changes in 4.12 broke some of our tests. When I am back in the office tomorrow I will verify that this change resolves the problems.

@kcooney kcooney force-pushed the TemporaryFolder-support-path-separator branch from 370cae0 to b94b56e Compare January 2, 2017 22:39
@kcooney
Copy link
Member Author

kcooney commented Jan 2, 2017

PTAL. Thanks, @marcphilipp !

Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

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

Thanks!

…ashes.

In JUnit 4.11, newFolder(String) was changed to no longer support paths
with a file separator. This has been fixed. The overload of newFolder()
that supports passing in multiple strings still does not allow strings
containing file separators.
@kcooney kcooney force-pushed the TemporaryFolder-support-path-separator branch from b94b56e to c47bb64 Compare January 3, 2017 18:42
@kcooney
Copy link
Member Author

kcooney commented Jan 3, 2017

I verified that this is a regression from the behavior in JUnit 4.11 and changed the commit comment accordingly.

I also removed the use of Pattern (I forgot to remove that when I changed the code to use mkdirs).

Any objections to merging this?

Note I'm also considering changing newFolder(String...) to support paths with slashes, since the inconsistency is weird and supporting slashes would be consistent with java.nio.file.Paths.get(String, String...)

@@ -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 {
Copy link
Member

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 to path, right?

File file = new File(getRoot(), folder);
if (!file.mkdirs()) {
throw new IOException(
"a folder with the name \'" + folder + "\' already exists");
Copy link
Member

Choose a reason for hiding this comment

The 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.

@marcphilipp
Copy link
Member

I verified that this is a regression from the behavior in JUnit 4.11 and changed the commit comment accordingly.

I also removed the use of Pattern (I forgot to remove that when I changed the code to use mkdirs).

👍

Any objections to merging this?

None at all.

Note I'm also considering changing newFolder(String...) to support paths with slashes, since the inconsistency is weird and supporting slashes would be consistent with java.nio.file.Paths.get(String, String...)

I think supporting slashes is great! I've tried to find out why it was implemented that way. From #946 it seems 4.10 used only mkdir, 4.11 introduced newFolder(String...) but kept using mkdir. Supporting slashes should be okay because it would only be more tolerant than before and no existing call should break.

@kcooney kcooney merged commit c85c614 into junit-team:master Jan 3, 2017
@marcphilipp
Copy link
Member

Please add a description of this improvement to the draft of the release notes.

@kcooney
Copy link
Member Author

kcooney commented Jan 6, 2017

@marcphilipp I updated the release notes.

Sorry, I somehow didn't see some of your comments. I'll address them soon.

@marcphilipp
Copy link
Member

Thanks! And no worries about those comments. 🙂

@kcooney kcooney modified the milestone: 4.13 Aug 6, 2017
@kcooney kcooney removed the 4.13 label Aug 6, 2017
sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
…ashes. (junit-team#1402)

In JUnit 4.11, newFolder(String) was changed to no longer support paths
with a file separator. This has been fixed. The overload of newFolder()
that supports passing in multiple strings still does not allow strings
containing file separators.
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
…ashes. (junit-team#1402)

In JUnit 4.11, newFolder(String) was changed to no longer support paths
with a file separator. This has been fixed. The overload of newFolder()
that supports passing in multiple strings still does not allow strings
containing file separators.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants