diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java | 91 |
1 files changed, 84 insertions, 7 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java index aba86119ab..8b4255b510 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemTest.java @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.vfs; import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -26,18 +27,17 @@ import com.google.devtools.build.lib.testutil.TestUtils; import com.google.devtools.build.lib.unix.NativePosixFiles; import com.google.devtools.build.lib.util.Fingerprint; import com.google.devtools.build.lib.util.Preconditions; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.FileAlreadyExistsException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** * This class handles the generic tests that any filesystem must pass. @@ -50,6 +50,7 @@ public abstract class FileSystemTest { private long savedTime; protected FileSystem testFS; protected boolean supportsSymlinks; + protected boolean supportsHardlinks; protected Path workingDir; // Some useful examples of various kinds of files (mnemonic: "x" = "eXample") @@ -67,6 +68,7 @@ public abstract class FileSystemTest { workingDir = testFS.getPath(getTestTmpDir()); cleanUpWorkingDirectory(workingDir); supportsSymlinks = testFS.supportsSymbolicLinksNatively(); + supportsHardlinks = testFS.supportsHardLinksNatively(); // % ls -lR // -rw-rw-r-- xFile @@ -1203,7 +1205,7 @@ public abstract class FileSystemTest { public void testWritingToReadOnlyFileThrowsException() throws Exception { xFile.setWritable(false); try { - FileSystemUtils.writeContent(xFile, "hello, world!".getBytes()); + FileSystemUtils.writeContent(xFile, "hello, world!".getBytes(UTF_8)); fail("No exception thrown."); } catch (IOException e) { assertThat(e).hasMessage(xFile + " (Permission denied)"); @@ -1212,7 +1214,7 @@ public abstract class FileSystemTest { @Test public void testReadingFromUnreadableFileThrowsException() throws Exception { - FileSystemUtils.writeContent(xFile, "hello, world!".getBytes()); + FileSystemUtils.writeContent(xFile, "hello, world!".getBytes(UTF_8)); xFile.setReadable(false); try { FileSystemUtils.readContent(xFile); @@ -1367,4 +1369,79 @@ public abstract class FileSystemTest { } } + @Test + public void testCreateHardLink_Success() throws Exception { + if (!supportsHardlinks) { + return; + } + xFile.createHardLink(xLink); + assertTrue(xFile.exists()); + assertTrue(xLink.exists()); + assertTrue(xFile.isFile()); + assertTrue(xLink.isFile()); + assertTrue(isHardLinked(xFile, xLink)); + } + + @Test + public void testCreateHardLink_NeitherOriginalNorLinkExists() throws Exception { + if (!supportsHardlinks) { + return; + } + + /* Neither original file nor link file exists */ + xFile.delete(); + try { + xFile.createHardLink(xLink); + fail("expected FileNotFoundException: File \"xFile\" linked from \"xLink\" does not exist"); + } catch (FileNotFoundException expected) { + assertThat(expected).hasMessage("File \"xFile\" linked from \"xLink\" does not exist"); + } + assertFalse(xFile.exists()); + assertFalse(xLink.exists()); + } + + @Test + public void testCreateHardLink_OriginalDoesNotExistAndLinkExists() throws Exception { + + if (!supportsHardlinks) { + return; + } + + /* link file exists and original file does not exist */ + xFile.delete(); + FileSystemUtils.createEmptyFile(xLink); + + try { + xFile.createHardLink(xLink); + fail("expected FileNotFoundException: File \"xFile\" linked from \"xLink\" does not exist"); + } catch (FileNotFoundException expected) { + assertThat(expected).hasMessage("File \"xFile\" linked from \"xLink\" does not exist"); + } + assertFalse(xFile.exists()); + assertTrue(xLink.exists()); + } + + @Test + public void testCreateHardLink_BothOriginalAndLinkExist() throws Exception { + + if (!supportsHardlinks) { + return; + } + /* Both original file and link file exist */ + FileSystemUtils.createEmptyFile(xLink); + + try { + xFile.createHardLink(xLink); + fail("expected FileAlreadyExistsException: New link file \"xLink\" already exists"); + } catch (FileAlreadyExistsException expected) { + assertThat(expected).hasMessage("New link file \"xLink\" already exists"); + } + assertTrue(xFile.exists()); + assertTrue(xLink.exists()); + assertFalse(isHardLinked(xFile, xLink)); + } + + protected boolean isHardLinked(Path a, Path b) throws IOException { + return testFS.stat(a, false).getNodeId() == testFS.stat(b, false).getNodeId(); + } } |