Skip to content

Commit

Permalink
Make sure provided download target exist before writing files to it (#…
Browse files Browse the repository at this point in the history
…4762)

When we use the default download location we create the folder if it is missing. We also need to do this if a custom location is provided
  • Loading branch information
lkerford authored Dec 9, 2024
1 parent 16df13a commit 1095923
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ void dependenciesFromResources(@TempDir Path temp) throws Exception {
"directory contains guava-30.0-jre.jar which has the same prefix");
}

@Test
void getParserClasspathDownloadCreateRequiredFolder(@TempDir Path temp) throws Exception {
Path updatedTemp = Path.of(temp.toString(), "someFolder");
assertThat(updatedTemp.toFile().exists()).isFalse();
JavaParserExecutionContextView ctx = JavaParserExecutionContextView.view(new InMemoryExecutionContext());
ctx.setParserClasspathDownloadTarget(updatedTemp.toFile());
ctx.getParserClasspathDownloadTarget();
assertThat(updatedTemp.toFile().exists()).isTrue();
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3222")
void parseFromByteArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public JavaParserExecutionContextView setParserClasspathDownloadTarget(File fold
public File getParserClasspathDownloadTarget() {
File target = getMessage(PARSER_CLASSPATH_DOWNLOAD_LOCATION);
if (target == null) {
File defaultTarget = new File(System.getProperty("user.home") + "/.rewrite/classpath");
if (!defaultTarget.mkdirs() && !defaultTarget.exists()) {
throw new UncheckedIOException(new IOException("Failed to create directory " + defaultTarget.getAbsolutePath()));
}
return defaultTarget;
target = new File(System.getProperty("user.home") + "/.rewrite/classpath");
}
if (!target.mkdirs() && !target.exists()) {
throw new UncheckedIOException(new IOException("Failed to create directory " + target.getAbsolutePath()));
}
return target;
}
Expand Down

0 comments on commit 1095923

Please sign in to comment.