Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import groovy.transform.stc.PickFirstResolver;
import groovy.transform.stc.SimpleType;
import groovy.util.CharsetToolkit;
import org.apache.groovy.util.SystemUtil;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -153,15 +154,30 @@ public void doCall(Object[] args) {

/**
* Coerce the file to a {@code boolean} value.
* <p>
* By default this returns whether the file exists. Set the system property
* {@code groovy.truth.file.exists.enabled} to {@code false} to restore the
* pre-Groovy-5 behavior where any non-{@code null} {@code File} is truthy
* regardless of whether the underlying file exists.
*
* @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) {
if (!FILE_EXISTS_ENABLED) return file != null;
return file.exists();
}

/**
* When {@code true} (the default), coercing a {@link File} or {@link java.nio.file.Path}
* to a {@code boolean} returns whether the underlying file exists. Set the system property
* {@code groovy.truth.file.exists.enabled} to {@code false} to restore the pre-Groovy-5
* behavior where any non-{@code null} reference is truthy.
*/
public static final boolean FILE_EXISTS_ENABLED = Boolean.parseBoolean(
SystemUtil.getSystemPropertySafe("groovy.truth.file.exists.enabled", "true"));

/**
* Create an object output stream for this file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.codehaus.groovy.runtime.FormatHelper;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.ResourceGroovyMethods;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -147,13 +148,19 @@ public static String getBaseName(Path self) {

/**
* Coerce the path to a {@code boolean} value.
* <p>
* By default this returns whether the file at the path exists. Set the
* system property {@code groovy.truth.file.exists.enabled} to {@code false}
* to restore the pre-Groovy-5 behavior where any non-{@code null} {@code Path}
* is truthy regardless of whether the underlying file exists.
*
* @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) {
if (!ResourceGroovyMethods.FILE_EXISTS_ENABLED) return path != null;
return Files.exists(path);
}

Expand Down
Loading