Skip to content

Commit

Permalink
Stop logging TempDirectory IOExceptions in Windows
Browse files Browse the repository at this point in the history
In Windows, it is generally not possible to remove files that have been
opened in native code in the same JVM process as they were opened. Avoid
logging TempDirectory errors when files cannot be deleted. Robolectric will
attempt to delete them on its next invocation.

PiperOrigin-RevId: 621189705
  • Loading branch information
hoisie committed Apr 2, 2024
1 parent 85f9641 commit 8e9ec0e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
25 changes: 7 additions & 18 deletions utils/src/main/java/org/robolectric/util/TempDirectory.java
Expand Up @@ -13,10 +13,8 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService;
Expand All @@ -35,15 +33,6 @@ public class TempDirectory {
*/
private static final int DELETE_THREAD_POOL_SIZE = 5;

/**
* Assets related to the Robolectric Native Runtime (shared object files, ICU dat file, fonts) are
* extracted from a Jar file into a TempDirectory during runtime in order to be used. On Windows,
* it is not possible for the current JVM process to delete certain files due to OS constraints.
*/
@SuppressWarnings("ConstantCaseForConstants")
private static final List<String> KNOWN_WINDOWS_UNDELETEABLE_FILES =
Collections.unmodifiableList(Arrays.asList("robolectric-nativeruntime.dll", "icudt68l.dat"));

private static final String TEMP_DIR_PREFIX = "robolectric-";

static final String OBSOLETE_MARKER_FILE_NAME = ".obsolete";
Expand Down Expand Up @@ -153,15 +142,15 @@ public void destroy() {
Files.delete(basePath);
} catch (IOException e) {
if (isWindows()) {
// Windows is much more protective of files that have been opened in native code. For
// instance, unlike in Mac and Linux, it's not possible to delete nativeruntime files
// (dlls, fonts, icu data) in the same process where they were opened. Because of
// this, we need extra cleanup logic for Windows, and we avoid logging to prevent noise
// and confusion.
createFile(OBSOLETE_MARKER_FILE_NAME, "");
// Avoid spurious logging.
for (String s : KNOWN_WINDOWS_UNDELETEABLE_FILES) {
if (e.getLocalizedMessage().contains(s)) {
return;
}
}
} else {
Logger.error("Failed to destroy temp directory", e);
}
Logger.error("Failed to destroy temp directory", e);
}
}

Expand Down
Expand Up @@ -69,7 +69,6 @@ class TempDirectoryTest {
if (!Files.exists(path)) {
latch.countDown()
} else {
System.err.println("DSP rescheduling")
val ignored =
service.schedule(
{ waitForDirectoryDeletion(path, latch, service) },
Expand Down

0 comments on commit 8e9ec0e

Please sign in to comment.