You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/04/25 07:17:48 UTC

[groovy] 02/02: GROOVY-4990: Ability to create missing parent directories when using File.write()

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit fa43e1f0e4df89996ca24ff907e3b9b82b7c22e8
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Apr 25 17:17:37 2021 +1000

    GROOVY-4990: Ability to create missing parent directories when using File.write()
---
 .../org/codehaus/groovy/runtime/ResourceGroovyMethods.java   | 12 ++++++++++++
 .../codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy | 11 +++++------
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
index 038f92f..418d81a 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
@@ -743,6 +743,18 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Creates, if needed, any parent directories for this File.
+     *
+     * @param self a File
+     * @return itself
+     * @throws IOException if the parent directories couldn't be created
+     */
+    public static File createParentDirectories(File self) throws IOException {
+        Files.createDirectories(self.getParentFile().toPath()).toFile();
+        return self;
+    }
+
+    /**
      * Write the text to the File without writing a BOM.
      *
      * @param file a File
diff --git a/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
index 3e03d18..5f2e718 100644
--- a/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
@@ -31,15 +31,14 @@ class ResourceGroovyMethodsTest {
 
 	@Test
 	void test_Should_write_should_create_missing_directories_and_write() {
-
 		final String filename = "testing.txt"
-		File file = new File(temporaryFolder.getRoot().getAbsolutePath() + "\\foo\\bar\\" + filename);
+		File file = new File(temporaryFolder.getRoot().getAbsolutePath() + "\\foo\\bar\\" + filename)
 		String text = "foobar"
 		String encoding = "UTF-8"
 
-		ResourceGroovyMethods.write(file, text, encoding)
+		ResourceGroovyMethods.write(file.createParentDirectories(), text, encoding)
 
-		assert file.getText(encoding) == text;
+		assert file.getText(encoding) == text
 	}
 
 	@Test
@@ -214,7 +213,7 @@ class ResourceGroovyMethodsTest {
 		try {
 			ResourceGroovyMethods.directorySize(new File("doesn't exist"))
 			fail("directorySize() should fail when directory specified doesn't exist")
-		} catch (IOException expected) {
+		} catch (IOException ignore) {
 		}
 
 		File tempFile = File.createTempFile("testDirectorySizeExceptions", "")
@@ -222,7 +221,7 @@ class ResourceGroovyMethodsTest {
 		try {
 			ResourceGroovyMethods.directorySize(tempFile)
 			fail("directorySize() should fail when a file is specified")
-		} catch (IllegalArgumentException expected) {
+		} catch (IllegalArgumentException ignore) {
 		}
 
 		tempFile.delete()