From 1095923b6e433860af8f5d71d7ab9a2a8f0c8a06 Mon Sep 17 00:00:00 2001 From: Leanne Kerford Date: Mon, 9 Dec 2024 11:49:25 -0800 Subject: [PATCH] Make sure provided download target exist before writing files to it (#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 --- .../test/java/org/openrewrite/java/JavaParserTest.java | 10 ++++++++++ .../java/JavaParserExecutionContextView.java | 9 ++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java index ccfe243aec1..3115386c7d7 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java @@ -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() { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaParserExecutionContextView.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaParserExecutionContextView.java index 884104913ab..266ee8928a8 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParserExecutionContextView.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParserExecutionContextView.java @@ -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; }