Skip to content

Commit

Permalink
Use fake filesystems to make Guava tests work better [under Windows](#…
Browse files Browse the repository at this point in the history
…2130) and Android, and prepare jimfs for some preliminary Android testing.

RELNOTES=n/a
PiperOrigin-RevId: 594298059
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 29, 2023
1 parent 2d30e0d commit bf7911f
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions guava-tests/test/com/google/common/io/MoreFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ public void testTouch() throws IOException {
Files.delete(temp);
assertFalse(Files.exists(temp));
MoreFiles.touch(temp);

if (isAndroid()) {
// TODO: b/317997723 - Test touch() more after it works under Android's library desugaring.
return;
}

assertTrue(Files.exists(temp));
MoreFiles.touch(temp);
assertTrue(Files.exists(temp));
Expand All @@ -251,13 +257,13 @@ public void testTouchTime() throws IOException {
}

public void testCreateParentDirectories_root() throws IOException {
if (isWindows()) {
return; // TODO: b/136041958 - *Sometimes* fails with "A:\: The device is not ready"
// We use a fake filesystem to sidestep flaky problems with Windows (b/136041958).
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path root = fs.getRootDirectories().iterator().next();
assertNull(root.getParent());
assertNull(root.toRealPath().getParent());
MoreFiles.createParentDirectories(root); // test that there's no exception
}
Path root = root();
assertNull(root.getParent());
assertNull(root.toRealPath().getParent());
MoreFiles.createParentDirectories(root); // test that there's no exception
}

public void testCreateParentDirectories_relativePath() throws IOException {
Expand Down Expand Up @@ -311,13 +317,20 @@ public void testCreateParentDirectories_nonDirectoryParentExists() throws IOExce
}

public void testCreateParentDirectories_symlinkParentExists() throws IOException {
if (isWindows()) {
return; // TODO: b/136041958 - *Sometimes* fails with FileAlreadyExistsException
/*
* We use a fake filesystem to sidestep:
*
* - flaky problems with Windows (b/136041958)
*
* - the lack of support for symlinks in the default filesystem under Android's desugared
* java.nio.file
*/
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path symlink = fs.getPath("linkToDir");
Files.createSymbolicLink(symlink, fs.getRootDirectories().iterator().next());
Path file = symlink.resolve("foo");
MoreFiles.createParentDirectories(file);
}
Path symlink = tempDir.resolve("linkToDir");
Files.createSymbolicLink(symlink, root());
Path file = symlink.resolve("foo");
MoreFiles.createParentDirectories(file);
}

public void testGetFileExtension() {
Expand Down Expand Up @@ -357,30 +370,37 @@ public void testGetNameWithoutExtension() {
}

public void testPredicates() throws IOException {
Path file = createTempFile();
Path dir = tempDir.resolve("dir");
Files.createDirectory(dir);
/*
* We use a fake filesystem to sidestep the lack of support for symlinks in the default
* filesystem under Android's desugared java.nio.file.
*/
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path file = fs.getPath("file");
Files.createFile(file);
Path dir = fs.getPath("dir");
Files.createDirectory(dir);

assertTrue(MoreFiles.isDirectory().apply(dir));
assertFalse(MoreFiles.isRegularFile().apply(dir));
assertTrue(MoreFiles.isDirectory().apply(dir));
assertFalse(MoreFiles.isRegularFile().apply(dir));

assertFalse(MoreFiles.isDirectory().apply(file));
assertTrue(MoreFiles.isRegularFile().apply(file));
assertFalse(MoreFiles.isDirectory().apply(file));
assertTrue(MoreFiles.isRegularFile().apply(file));

Path symlinkToDir = tempDir.resolve("symlinkToDir");
Path symlinkToFile = tempDir.resolve("symlinkToFile");
Path symlinkToDir = fs.getPath("symlinkToDir");
Path symlinkToFile = fs.getPath("symlinkToFile");

Files.createSymbolicLink(symlinkToDir, dir);
Files.createSymbolicLink(symlinkToFile, file);
Files.createSymbolicLink(symlinkToDir, dir);
Files.createSymbolicLink(symlinkToFile, file);

assertTrue(MoreFiles.isDirectory().apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir));
assertTrue(MoreFiles.isDirectory().apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir));

assertFalse(MoreFiles.isDirectory().apply(symlinkToFile));
assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile));
assertFalse(MoreFiles.isDirectory().apply(symlinkToFile));
assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile));

assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile));
assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile));
}
}

/**
Expand Down Expand Up @@ -576,6 +596,7 @@ public void testDeleteDirectoryContents_symlinkToDir_sdsNotSupported_allowInsecu
* not possible to protect against this if the file system doesn't.
*/
public void testDirectoryDeletion_directorySymlinkRace() throws IOException {
int iterations = isAndroid() ? 100 : 5000;
for (DirectoryDeleteMethod method : EnumSet.allOf(DirectoryDeleteMethod.class)) {
try (FileSystem fs = newTestFileSystem(SECURE_DIRECTORY_STREAM)) {
Path dirToDelete = fs.getPath("dir/b/i");
Expand All @@ -586,7 +607,7 @@ public void testDirectoryDeletion_directorySymlinkRace() throws IOException {
startDirectorySymlinkSwitching(changingFile, symlinkTarget, executor);

try {
for (int i = 0; i < 5000; i++) {
for (int i = 0; i < iterations; i++) {
try {
Files.createDirectories(changingFile);
Files.createFile(dirToDelete.resolve("j/k"));
Expand Down Expand Up @@ -708,4 +729,8 @@ public void assertDeleteSucceeded(Path path) throws IOException {
private static boolean isWindows() {
return OS_NAME.value().startsWith("Windows");
}

private static boolean isAndroid() {
return System.getProperty("java.runtime.name", "").contains("Android");
}
}

0 comments on commit bf7911f

Please sign in to comment.