Skip to content

Commit

Permalink
[IO-856] Try test on all OSs for GitHub CI
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 10, 2024
1 parent b59bc1d commit 7bf5374
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -57,6 +58,7 @@
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.ChronoZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand All @@ -65,6 +67,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.CRC32;
Expand Down Expand Up @@ -2241,6 +2244,11 @@ public static LineIterator lineIterator(final File file, final String charsetNam
}
}

private static List<File> list(final File directory, final Predicate<File> predicate, final String[] extensions) {
return Arrays.stream(extensions != null ? directory.listFiles((FilenameFilter) new SuffixFileFilter(extensions)) : directory.listFiles())
.filter(predicate).collect(Collectors.toList());
}

private static AccumulatorPathVisitor listAccumulate(final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter,
final FileVisitOption... options) throws IOException {
final boolean isDirFilterSet = dirFilter != null;
Expand Down Expand Up @@ -2322,9 +2330,13 @@ public static Collection<File> listFiles(final File directory, final IOFileFilte
* @return a collection of {@link File} with the matching files
*/
public static Collection<File> listFiles(final File directory, final String[] extensions, final boolean recursive) {
try (Stream<File> fileStream = Uncheck.get(() -> streamFiles(directory, recursive, extensions))) {
return toList(fileStream);
// IO-856
if (recursive) {
final List<File> list = list(directory, File::isFile, extensions);
list(directory, File::isDirectory, null).forEach(d -> list.addAll(listFiles(d, extensions, true)));
return list;
}
return list(directory, File::isFile, extensions);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -44,8 +45,6 @@
import org.apache.commons.lang3.function.Consumers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;

/**
Expand Down Expand Up @@ -204,12 +203,12 @@ public void testListFilesByExtension() {

files = FileUtils.listFiles(temporaryFolder, extensions, true);
fileNames = filesToFilenames(files);
assertEquals(4, fileNames.size());
assertEquals(4, fileNames.size(), fileNames::toString);
assertTrue(fileNames.contains("dummy-file.txt"));
assertFalse(fileNames.contains("dummy-index.html"));

files = FileUtils.listFiles(temporaryFolder, null, false);
assertEquals(2, files.size());
assertEquals(2, files.size(), files::toString);
fileNames = filesToFilenames(files);
assertTrue(fileNames.contains("dummy-build.xml"));
assertTrue(fileNames.contains("README"));
Expand Down Expand Up @@ -238,7 +237,6 @@ public void testListFilesWithDeletion() throws IOException {
* Tests <a href="https://issues.apache.org/jira/browse/IO-856">IO-856</a> ListFiles should not fail on vanishing files.
*/
@Test
@EnabledOnOs(value = OS.WINDOWS)
public void testListFilesWithDeletionThreaded() throws ExecutionException, InterruptedException {
// test for IO-856
// create random directory in tmp, create the directory if it does not exist
Expand All @@ -251,30 +249,38 @@ public void testListFilesWithDeletionThreaded() throws ExecutionException, Inter
final byte[] bytes = "TEST".getBytes(StandardCharsets.UTF_8);
final CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
final long endTime = System.currentTimeMillis() + waitTime;
int count = 0;
while (System.currentTimeMillis() < endTime) {
final File file = new File(tempDir.getAbsolutePath(), UUID.randomUUID() + ".deletetester");
file.deleteOnExit();
try {
Files.write(file.toPath(), bytes);
count++;
} catch (final Exception e) {
fail("Could not create test file: '" + file.getAbsolutePath() + "': " + e, e);
}
if (!file.delete()) {
fail("Could not delete test file: '" + file.getAbsolutePath() + "'");
}
}
// System.out.printf("Created %,d%n", count);
});
final CompletableFuture<Void> c2 = CompletableFuture.runAsync(() -> {
final long endTime = System.currentTimeMillis() + waitTime;
int max = 0;
try {
while (System.currentTimeMillis() < endTime) {
FileUtils.listFiles(tempDir, new String[] { "\\.deletetester" }, false);
Collection<File> files = FileUtils.listFiles(tempDir, new String[] { "\\.deletetester" }, false);
assertNotNull(files);
max = Math.max(max, files.size());
}
} catch (final Exception e) {
System.out.printf("List size max %,d%n", max);
fail("IO-856 test failure: " + e, e);
// The exception can be hidden.
e.printStackTrace();
}
// System.out.printf("List size max %,d%n", max);
});
// wait for the threads to finish
c1.get();
Expand Down

0 comments on commit 7bf5374

Please sign in to comment.