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
The following commit(s) were added to refs/heads/master by this push:
new c54228f694 GROOVY-10949: Add `asBoolean` to `File` and `Path`
c54228f694 is described below
commit c54228f694d2b1b3282c4f985c10bf3334695d89
Author: Matyrobbrt <[email protected]>
AuthorDate: Fri Jan 13 16:07:41 2023 +0200
GROOVY-10949: Add `asBoolean` to `File` and `Path`
---
.../groovy/runtime/ResourceGroovyMethods.java | 11 +++++
.../runtime/ResourceGroovyMethodsTest.groovy | 50 ++++++++++++++--------
.../groovy/nio/extensions/NioExtensions.java | 26 +++++++++++
.../groovy/nio/extensions/NioExtensionsTest.groovy | 16 +++++++
4 files changed, 84 insertions(+), 19 deletions(-)
diff --git
a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
index 7292158e1c..a6e1bc14f5 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
@@ -150,6 +150,17 @@ public class ResourceGroovyMethods extends
DefaultGroovyMethodsSupport {
return size[0];
}
+ /**
+ * Coerce the file to a {@code boolean} value.
+ *
+ * @param file a {@code File}
+ * @return {@code true} if the file exists, {@code false} otherwise
+ * @since 5.0.0
+ */
+ public static boolean asBoolean(final File file) {
+ return file.exists();
+ }
+
/**
* Create an object output stream for this file.
*
diff --git
a/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
b/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
index 5f2e71826e..b67361e2f0 100644
--- a/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/ResourceGroovyMethodsTest.groovy
@@ -22,6 +22,8 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import static org.junit.Assert.assertEquals
+import static org.junit.Assert.assertFalse
+import static org.junit.Assert.assertTrue
class ResourceGroovyMethodsTest {
@@ -45,7 +47,7 @@ class ResourceGroovyMethodsTest {
void test_Should_write_String_to_File_using_default_encoding() {
File file = temporaryFolder.newFile()
String text = 'Hello World'
-
+
ResourceGroovyMethods.write(file, text)
assert file.text == text
@@ -56,9 +58,9 @@ class ResourceGroovyMethodsTest {
File file = temporaryFolder.newFile()
String text = ""
String encoding = 'UTF-8'
-
+
ResourceGroovyMethods.write(file, text, encoding)
-
+
assert file.getText(encoding) == text
}
@@ -78,9 +80,9 @@ class ResourceGroovyMethodsTest {
File file = temporaryFolder.newFile()
file.write('Hello')
byte[] bytes = ' World'.bytes
-
+
ResourceGroovyMethods.append(file, bytes)
-
+
assert file.text == 'Hello World'
}
@@ -88,9 +90,9 @@ class ResourceGroovyMethodsTest {
void test_Should_append_String_to_File_using_default_encoding() {
File file = temporaryFolder.newFile()
file.write('Hello')
-
+
ResourceGroovyMethods.append(file, ' World')
-
+
assert file.text == 'Hello World'
}
@@ -99,9 +101,9 @@ class ResourceGroovyMethodsTest {
File file = temporaryFolder.newFile()
file.write('Hello')
Reader reader = new StringReader(' World')
-
+
ResourceGroovyMethods.append(file, reader)
-
+
assert file.text == 'Hello World'
}
@@ -109,12 +111,12 @@ class ResourceGroovyMethodsTest {
void
test_Should_append_text_supplied_by_Writer_to_File_using_default_encoding() {
File file = temporaryFolder.newFile()
file.write('Hello')
-
+
Writer writer = new StringWriter()
writer.append(' World')
-
+
ResourceGroovyMethods.append(file, writer)
-
+
assert file.text == 'Hello World'
}
@@ -123,9 +125,9 @@ class ResourceGroovyMethodsTest {
File file = temporaryFolder.newFile()
String encoding = 'UTF-8'
file.write('', encoding)
-
+
ResourceGroovyMethods.append(file, ' ', encoding)
-
+
assert file.getText(encoding) == ' '
}
@@ -135,9 +137,9 @@ class ResourceGroovyMethodsTest {
String encoding = 'UTF-8'
file.write('', encoding)
Reader reader = new CharArrayReader([' ',''] as char[])
-
+
ResourceGroovyMethods.append(file, reader, encoding)
-
+
assert file.getText(encoding) == ' '
}
@@ -146,13 +148,13 @@ class ResourceGroovyMethodsTest {
File file = temporaryFolder.newFile()
String encoding = 'UTF-8'
file.write('', encoding)
-
+
Writer writer = new CharArrayWriter()
writer.append(' ')
writer.append('')
-
+
ResourceGroovyMethods.append(file, writer, encoding)
-
+
assert file.getText(encoding) == ' '
}
@@ -265,6 +267,16 @@ class ResourceGroovyMethodsTest {
assertEquals("", ResourceGroovyMethods.relativePath(new File("a"), new
File("a")))
}
+ @Test
+ void test_asBoolean() {
+ File fileThatExists = temporaryFolder.newFile()
+ assertTrue(ResourceGroovyMethods.asBoolean(fileThatExists))
+
+ File fileThatDoesNotExist = temporaryFolder.newFile()
+ fileThatDoesNotExist.delete()
+ assertFalse(ResourceGroovyMethods.asBoolean(fileThatDoesNotExist))
+ }
+
/**
* Creates empty file of size specified.
*
diff --git
a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
index c2525750b9..7ad79632ff 100644
---
a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
+++
b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
@@ -59,6 +59,7 @@ import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
+import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
@@ -140,6 +141,31 @@ public class NioExtensions extends
DefaultGroovyMethodsSupport {
return name.substring(0, index);
}
+ /**
+ * Coerce the path to a {@code boolean} value.
+ *
+ * @param path a {@code Path} object
+ * @return {@code true} if the file at the path exists, {@code false}
otherwise
+ * @see Files#exists(Path, LinkOption...)
+ * @since 5.0.0
+ */
+ public static boolean asBoolean(final Path path) {
+ return Files.exists(path);
+ }
+
+ /**
+ * Tests whether the file at the path exists.
+ *
+ * @param path a {@code Path} object
+ * @param options options indicating how symbolic links are handled .
+ * @return {@code true} if the file exists; {@code false} if the file does
not exist or its existence cannot be determined
+ * @see Files#exists(Path, LinkOption...)
+ * @since 5.0.0
+ */
+ public static boolean exists(final Path path, LinkOption... options) {
+ return Files.exists(path, options);
+ }
+
/**
* Create an object output stream for this path.
*
diff --git
a/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
b/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
index de87ea21a4..eb289c23c8 100644
---
a/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
+++
b/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
@@ -566,4 +566,20 @@ class NioExtensionsTest extends Specification {
assert NioExtensions.getBytes(path) == [-2, -1, 0, 72, 0, 101, 0, 108,
0, 108, 0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33] as byte[]
}
+ def testAsBoolean() {
+ setup:
+ def path = tempFile.toPath()
+
+ when:
+ path.write('Some text')
+
+ then:
+ assert path
+
+ when:
+ Files.delete(path)
+
+ then:
+ assert !path
+ }
}