Skip to content

Commit 915895d

Browse files
committed
GROOVY-11996: Provide a groovy.truth.file.exists.enabled system property which when false reverts to Groovy 4 behavior (test)
1 parent 7d924a8 commit 915895d

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package groovy
20+
21+
import groovy.junit6.plugin.ForkedJvm
22+
import org.junit.jupiter.api.Test
23+
24+
import java.nio.file.Files
25+
import java.nio.file.Path
26+
27+
/**
28+
* Tests that when {@code groovy.truth.file.exists.enabled=false}, GROOVY-11996
29+
* restores the pre-Groovy-5 behavior where any non-null {@link File} or
30+
* {@link Path} is truthy regardless of whether the underlying file exists.
31+
* <p>
32+
* Each test runs in a freshly forked JVM with the property set, so that
33+
* {@code ResourceGroovyMethods.FILE_EXISTS_ENABLED} is initialised to
34+
* {@code false} at class load.
35+
*/
36+
@ForkedJvm(systemProperties = ['groovy.truth.file.exists.enabled=false'])
37+
final class FileExistsDisabledTest {
38+
39+
@Test
40+
void existingFileIsTruthy() {
41+
File f = File.createTempFile('present', '.txt')
42+
f.deleteOnExit()
43+
assert f.exists()
44+
assert f
45+
}
46+
47+
@Test
48+
void missingFileIsTruthy() {
49+
File f = new File(System.getProperty('java.io.tmpdir'), "missing-${UUID.randomUUID()}.txt")
50+
assert !f.exists()
51+
assert f
52+
}
53+
54+
@Test
55+
void nullFileIsFalsy() {
56+
File f = null
57+
assert !f
58+
}
59+
60+
@Test
61+
void existingPathIsTruthy() {
62+
Path p = Files.createTempFile('present', '.txt')
63+
p.toFile().deleteOnExit()
64+
assert Files.exists(p)
65+
assert p
66+
}
67+
68+
@Test
69+
void missingPathIsTruthy() {
70+
Path p = Path.of(System.getProperty('java.io.tmpdir'), "missing-${UUID.randomUUID()}.txt")
71+
assert !Files.exists(p)
72+
assert p
73+
}
74+
75+
@Test
76+
void nullPathIsFalsy() {
77+
Path p = null
78+
assert !p
79+
}
80+
}

0 commit comments

Comments
 (0)