Skip to content

Commit a58965d

Browse files
committed
Better test clean up
- Attempt to resolve "random" build failures in GH CI on macOS - Local build OK
1 parent d2505c9 commit a58965d

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

commons-vfs2/src/test/java/org/apache/commons/vfs2/PermissionsTests.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,17 @@
1616
*/
1717
package org.apache.commons.vfs2;
1818

19-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20-
import static org.junit.jupiter.api.Assertions.assertEquals;
2119
import static org.junit.jupiter.api.Assertions.assertFalse;
2220
import static org.junit.jupiter.api.Assertions.assertNotNull;
23-
import static org.junit.jupiter.api.Assertions.assertNull;
24-
import static org.junit.jupiter.api.Assertions.assertSame;
25-
import static org.junit.jupiter.api.Assertions.assertNotSame;
26-
import static org.junit.jupiter.api.Assertions.assertThrows;
2721
import static org.junit.jupiter.api.Assertions.assertTrue;
28-
import static org.junit.jupiter.api.Assertions.fail;
29-
30-
31-
32-
33-
34-
3522

3623
import java.io.OutputStream;
3724
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
3827

28+
import org.apache.commons.io.file.PathUtils;
29+
import org.apache.commons.io.file.StandardDeleteOption;
3930
import org.apache.commons.lang3.SystemUtils;
4031
import org.apache.commons.vfs2.provider.local.LocalFileSystem;
4132
import org.junit.jupiter.api.AfterEach;
@@ -48,24 +39,20 @@
4839
*/
4940
public class PermissionsTests extends AbstractProviderTestCase {
5041

51-
public static final String FILENAME = "permission.txt";
42+
public static final String FILE_NAME = "permission.txt";
5243

5344
private FileObject createTestFile() throws Exception {
5445
// Get the scratch folder
5546
final FileObject scratchFolder = getWriteFolder();
5647
assertNotNull(scratchFolder);
57-
5848
// Make sure the test folder is empty
5949
scratchFolder.delete(Selectors.EXCLUDE_SELF);
6050
scratchFolder.createFolder();
61-
6251
// Create direct child of the test folder
63-
final FileObject file = scratchFolder.resolveFile(FILENAME);
52+
final FileObject file = scratchFolder.resolveFile(FILE_NAME);
6453
assertFalse(file.exists());
65-
6654
// Create the source file
6755
final String content = "Here is some sample content for the file. Blah Blah Blah.";
68-
6956
try (OutputStream os = file.getContent().getOutputStream()) {
7057
os.write(content.getBytes(StandardCharsets.UTF_8));
7158
}
@@ -94,9 +81,14 @@ private boolean isWindows() {
9481
@AfterEach
9582
public void tearDown() throws Exception {
9683
final FileObject scratchFolder = getWriteFolder();
97-
final FileObject file = scratchFolder.resolveFile(FILENAME);
84+
final FileObject file = scratchFolder.resolveFile(FILE_NAME);
9885
file.setWritable(true, true);
99-
file.delete();
86+
if (!file.delete()) {
87+
final Path path = file.getPath();
88+
if (getFileSystem() instanceof LocalFileSystem) {
89+
PathUtils.deleteFile(path, StandardDeleteOption.OVERRIDE_READ_ONLY);
90+
}
91+
}
10092
}
10193

10294
/**
@@ -105,20 +97,16 @@ public void tearDown() throws Exception {
10597
@Test
10698
public void testExecutable() throws Exception {
10799
final FileObject file = createTestFile();
108-
109100
// On Windows, all files are executable
110101
if (isWindows()) {
111102
assertTrue(file.isExecutable(), "File expected to be executable: " + file);
112-
113103
} else {
114104
// Set the executable flag for owner
115105
assertTrue(file.setExecutable(true, true), "Setting executable permission failed: " + file);
116106
assertTrue(file.isExecutable(), "File expected to be executable: " + file);
117-
118107
// Set the executable flag for all
119108
assertTrue(file.setExecutable(true, false), "Setting executable permission failed: " + file);
120109
assertTrue(file.isExecutable(), "File expected to be executable: " + file);
121-
122110
// Clear the executable flag
123111
assertTrue(file.setExecutable(false, true), "Setting executable permission failed: " + file);
124112
assertFalse(file.isExecutable(), "File expected to be not executable: " + file);
@@ -131,19 +119,16 @@ public void testExecutable() throws Exception {
131119
@Test
132120
public void testReadable() throws Exception {
133121
final FileObject file = createTestFile();
134-
135122
if (isWindows()) {
136123
// On Windows, all owned files are readable
137124
assertTrue(file.isReadable(), "File expected to be readable: " + file);
138125
} else {
139126
// Set the readable permission for owner
140127
assertTrue(file.setReadable(true, true), "Setting read permission failed: " + file);
141128
assertTrue(file.isReadable(), "File expected to be readable: " + file);
142-
143129
// Set the readable permission for all
144130
assertTrue(file.setReadable(true, false), "Setting read permission failed: " + file);
145131
assertTrue(file.isReadable(), "File expected to be readable: " + file);
146-
147132
// Clear the readable permission
148133
assertTrue(file.setReadable(false, true), "Setting read permission failed: " + file);
149134
assertFalse(file.isReadable(), "File expected to be not readable: " + file);
@@ -156,15 +141,12 @@ public void testReadable() throws Exception {
156141
@Test
157142
public void testWriteable() throws Exception {
158143
final FileObject file = createTestFile();
159-
160144
// Set the write permission for owner
161145
assertTrue(file.setWritable(true, true), "Setting write permission failed: " + file);
162146
assertTrue(file.isWriteable(), "File expected to be writable: " + file);
163-
164147
// Set the write permission for all
165148
assertTrue(file.setWritable(true, false), "Setting write permission failed: " + file);
166149
assertTrue(file.isWriteable(), "File expected to be writable: " + file);
167-
168150
// Clear the write permission
169151
assertTrue(file.setWritable(false, true), "Setting write permission failed: " + file);
170152
assertFalse(file.isWriteable(), "File expected to be not writable: " + file);

0 commit comments

Comments
 (0)