diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/vfs')
9 files changed, 202 insertions, 127 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 09ac79eec6..278a288dda 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 @@ -379,7 +379,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicFileLinkExists() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xFile); assertThat(someLink.exists()).isTrue(); assertThat(someLink.statIfFound()).isNotNull(); @@ -389,7 +389,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicFileLinkIsSymbolicLink() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xFile); assertThat(someLink.isSymbolicLink()).isTrue(); } @@ -398,7 +398,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicFileLinkIsFile() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xFile); assertThat(someLink.isFile()).isTrue(); } @@ -407,7 +407,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicFileLinkIsNotDirectory() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xFile); assertThat(someLink.isDirectory()).isFalse(); } @@ -416,7 +416,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicDirLinkExists() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xEmptyDirectory); assertThat(someLink.exists()).isTrue(); assertThat(someLink.statIfFound()).isNotNull(); @@ -426,7 +426,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicDirLinkIsSymbolicLink() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xEmptyDirectory); assertThat(someLink.isSymbolicLink()).isTrue(); } @@ -435,7 +435,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicDirLinkIsDirectory() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xEmptyDirectory); assertThat(someLink.isDirectory()).isTrue(); } @@ -444,7 +444,7 @@ public abstract class FileSystemTest { @Test public void testSymbolicDirLinkIsNotFile() throws Exception { Path someLink = absolutize("some-link"); - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { someLink.createSymbolicLink(xEmptyDirectory); assertThat(someLink.isFile()).isFalse(); } @@ -1287,7 +1287,7 @@ public abstract class FileSystemTest { Path xNonEmptyDirectoryBar = xNonEmptyDirectory.getChild("bar"); xNonEmptyDirectory.setWritable(false); - if (testFS.supportsSymbolicLinksNatively(xNonEmptyDirectoryBar.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xNonEmptyDirectoryBar)) { try { createSymbolicLink(xNonEmptyDirectoryBar, xNonEmptyDirectoryFoo); fail("No exception thrown."); @@ -1335,19 +1335,19 @@ public abstract class FileSystemTest { @Test public void testResolveSymlinks() throws Exception { - if (testFS.supportsSymbolicLinksNatively(xLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xLink)) { createSymbolicLink(xLink, xFile); FileSystemUtils.createEmptyFile(xFile); - assertThat(testFS.resolveOneLink(xLink.getLocalPath())).isEqualTo(xFile.getPathString()); + assertThat(testFS.resolveOneLink(xLink)).isEqualTo(xFile.asFragment()); assertThat(xLink.resolveSymbolicLinks()).isEqualTo(xFile); } } @Test public void testResolveDanglingSymlinks() throws Exception { - if (testFS.supportsSymbolicLinksNatively(xLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xLink)) { createSymbolicLink(xLink, xNothing); - assertThat(testFS.resolveOneLink(xLink.getLocalPath())).isEqualTo(xNothing.getPathString()); + assertThat(testFS.resolveOneLink(xLink)).isEqualTo(xNothing.asFragment()); try { xLink.resolveSymbolicLinks(); fail(); @@ -1358,15 +1358,15 @@ public abstract class FileSystemTest { @Test public void testResolveNonSymlinks() throws Exception { - if (testFS.supportsSymbolicLinksNatively(xFile.getLocalPath())) { - assertThat(testFS.resolveOneLink(xFile.getLocalPath())).isNull(); + if (testFS.supportsSymbolicLinksNatively(xFile)) { + assertThat(testFS.resolveOneLink(xFile)).isNull(); assertThat(xFile.resolveSymbolicLinks()).isEqualTo(xFile); } } @Test public void testCreateHardLink_Success() throws Exception { - if (!testFS.supportsHardLinksNatively(xFile.getLocalPath())) { + if (!testFS.supportsHardLinksNatively(xFile)) { return; } xFile.createHardLink(xLink); @@ -1379,7 +1379,7 @@ public abstract class FileSystemTest { @Test public void testCreateHardLink_NeitherOriginalNorLinkExists() throws Exception { - if (!testFS.supportsHardLinksNatively(xFile.getLocalPath())) { + if (!testFS.supportsHardLinksNatively(xFile)) { return; } @@ -1398,7 +1398,7 @@ public abstract class FileSystemTest { @Test public void testCreateHardLink_OriginalDoesNotExistAndLinkExists() throws Exception { - if (!testFS.supportsHardLinksNatively(xFile.getLocalPath())) { + if (!testFS.supportsHardLinksNatively(xFile)) { return; } @@ -1419,7 +1419,7 @@ public abstract class FileSystemTest { @Test public void testCreateHardLink_BothOriginalAndLinkExist() throws Exception { - if (!testFS.supportsHardLinksNatively(xFile.getLocalPath())) { + if (!testFS.supportsHardLinksNatively(xFile)) { return; } /* Both original file and link file exist */ @@ -1437,7 +1437,6 @@ public abstract class FileSystemTest { } protected boolean isHardLinked(Path a, Path b) throws IOException { - return testFS.stat(a.getLocalPath(), false).getNodeId() - == testFS.stat(b.getLocalPath(), false).getNodeId(); + return testFS.stat(a, false).getNodeId() == testFS.stat(b, false).getNodeId(); } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemUtilsTest.java b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemUtilsTest.java index 3b9a2dc76d..0e9b74ed3b 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemUtilsTest.java @@ -851,8 +851,8 @@ public class FileSystemUtilsTest { FileSystemUtils.createHardLink(linkPath, originalPath); assertThat(originalPath.exists()).isTrue(); assertThat(linkPath.exists()).isTrue(); - assertThat(fileSystem.stat(linkPath.getLocalPath(), false).getNodeId()) - .isEqualTo(fileSystem.stat(originalPath.getLocalPath(), false).getNodeId()); + assertThat(fileSystem.stat(linkPath, false).getNodeId()) + .isEqualTo(fileSystem.stat(originalPath, false).getNodeId()); } @Test @@ -892,11 +892,11 @@ public class FileSystemUtilsTest { assertThat(linkPath1.exists()).isTrue(); assertThat(linkPath2.exists()).isTrue(); assertThat(linkPath3.exists()).isTrue(); - assertThat(fileSystem.stat(linkPath1.getLocalPath(), false).getNodeId()) - .isEqualTo(fileSystem.stat(originalPath1.getLocalPath(), false).getNodeId()); - assertThat(fileSystem.stat(linkPath2.getLocalPath(), false).getNodeId()) - .isEqualTo(fileSystem.stat(originalPath2.getLocalPath(), false).getNodeId()); - assertThat(fileSystem.stat(linkPath3.getLocalPath(), false).getNodeId()) - .isEqualTo(fileSystem.stat(originalPath3.getLocalPath(), false).getNodeId()); + assertThat(fileSystem.stat(linkPath1, false).getNodeId()) + .isEqualTo(fileSystem.stat(originalPath1, false).getNodeId()); + assertThat(fileSystem.stat(linkPath2, false).getNodeId()) + .isEqualTo(fileSystem.stat(originalPath2, false).getNodeId()); + assertThat(fileSystem.stat(linkPath3, false).getNodeId()) + .isEqualTo(fileSystem.stat(originalPath3, false).getNodeId()); } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/GlobTest.java b/src/test/java/com/google/devtools/build/lib/vfs/GlobTest.java index 70fc86dc48..5b0958fae1 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/GlobTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/GlobTest.java @@ -54,18 +54,15 @@ public class GlobTest { @Before public final void initializeFileSystem() throws Exception { - fs = - new InMemoryFileSystem() { - @Override - public Collection<Dirent> readdir(LocalPath path, boolean followSymlinks) - throws IOException { - if (throwOnReaddir != null - && path.getPathString().equals(throwOnReaddir.getPathString())) { - throw new FileNotFoundException(path.getPathString()); - } - return super.readdir(path, followSymlinks); - } - }; + fs = new InMemoryFileSystem() { + @Override + public Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { + if (path.equals(throwOnReaddir)) { + throw new FileNotFoundException(path.getPathString()); + } + return super.readdir(path, followSymlinks); + } + }; tmpPath = fs.getPath("/globtmp"); for (String dir : ImmutableList.of("foo/bar/wiz", "foo/barnacle/wiz", diff --git a/src/test/java/com/google/devtools/build/lib/vfs/PathTrieTest.java b/src/test/java/com/google/devtools/build/lib/vfs/PathTrieTest.java new file mode 100644 index 0000000000..0807b4aa5d --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/vfs/PathTrieTest.java @@ -0,0 +1,78 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.vfs; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link PathTrie}. */ +@RunWith(JUnit4.class) +public class PathTrieTest { + @Test + public void empty() { + PathTrie<Integer> pathTrie = new PathTrie<>(); + assertThat(pathTrie.get(PathFragment.EMPTY_FRAGMENT)).isNull(); + assertThat(pathTrie.get(PathFragment.create("/x"))).isNull(); + assertThat(pathTrie.get(PathFragment.create("/x/y"))).isNull(); + } + + @Test + public void simpleBranches() { + PathTrie<Integer> pathTrie = new PathTrie<>(); + pathTrie.put(PathFragment.create("/a"), 1); + pathTrie.put(PathFragment.create("/b"), 2); + + assertThat(pathTrie.get(PathFragment.EMPTY_FRAGMENT)).isNull(); + assertThat(pathTrie.get(PathFragment.create("/a"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/a/b"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/a/b/c"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/b"))).isEqualTo(2); + assertThat(pathTrie.get(PathFragment.create("/b/c"))).isEqualTo(2); + } + + @Test + public void nestedDirectories() { + PathTrie<Integer> pathTrie = new PathTrie<>(); + pathTrie.put(PathFragment.create("/a/b/c"), 3); + assertThat(pathTrie.get(PathFragment.EMPTY_FRAGMENT)).isNull(); + assertThat(pathTrie.get(PathFragment.create("/a"))).isNull(); + assertThat(pathTrie.get(PathFragment.create("/a/b"))).isNull(); + assertThat(pathTrie.get(PathFragment.create("/a/b/c"))).isEqualTo(3); + assertThat(pathTrie.get(PathFragment.create("/a/b/c/d"))).isEqualTo(3); + + pathTrie.put(PathFragment.create("/a"), 1); + assertThat(pathTrie.get(PathFragment.EMPTY_FRAGMENT)).isNull(); + assertThat(pathTrie.get(PathFragment.create("/b"))).isNull(); + assertThat(pathTrie.get(PathFragment.create("/a"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/a/b"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/a/b/c"))).isEqualTo(3); + assertThat(pathTrie.get(PathFragment.create("/a/b/c/d"))).isEqualTo(3); + + pathTrie.put(PathFragment.ROOT_FRAGMENT, 0); + assertThat(pathTrie.get(PathFragment.EMPTY_FRAGMENT)).isEqualTo(0); + assertThat(pathTrie.get(PathFragment.create("/b"))).isEqualTo(0); + assertThat(pathTrie.get(PathFragment.create("/a"))).isEqualTo(1); + assertThat(pathTrie.get(PathFragment.create("/a/b"))).isEqualTo(1); + } + + @Test + public void unrootedDirectoriesError() { + PathTrie<Integer> pathTrie = new PathTrie<>(); + assertThrows(IllegalArgumentException.class, () -> pathTrie.put(PathFragment.create("a"), 1)); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/vfs/SearchPathTest.java b/src/test/java/com/google/devtools/build/lib/vfs/SearchPathTest.java index b49af40df9..6c545ac790 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/SearchPathTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/SearchPathTest.java @@ -38,7 +38,7 @@ public class SearchPathTest { assertThat(SearchPath.parse(fs, "/:/bin")).isEqualTo(searchPath); assertThat(SearchPath.parse(fs, ".:/:/bin")).isEqualTo(searchPath); - fs.getOutputStream(fs.getPath("/bin/exe").getLocalPath()).write(new byte[5]); + fs.getOutputStream(fs.getPath("/bin/exe")).write(new byte[5]); assertThat(SearchPath.which(searchPath, "exe")).isNull(); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/SymlinkAwareFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/SymlinkAwareFileSystemTest.java index b9f6485c8a..4720a6e773 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/SymlinkAwareFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/SymlinkAwareFileSystemTest.java @@ -78,7 +78,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { assertThat(linkPath.isDirectory(Symlinks.NOFOLLOW)).isFalse(); assertThat(linkPath.isDirectory(Symlinks.FOLLOW)).isFalse(); - if (testFS.supportsSymbolicLinksNatively(linkPath.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(linkPath)) { assertThat(linkPath.getFileSize(Symlinks.NOFOLLOW)).isEqualTo(newPath.toString().length()); assertThat(linkPath.getFileSize()).isEqualTo(newPath.getFileSize(Symlinks.NOFOLLOW)); } @@ -196,7 +196,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { PathFragment relative = PathFragment.create(linkTarget); linkPath.delete(); createSymbolicLink(linkPath, relative); - if (testFS.supportsSymbolicLinksNatively(linkPath.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(linkPath)) { assertThat(linkPath.getFileSize(Symlinks.NOFOLLOW)).isEqualTo(linkTarget.length()); assertThat(linkPath.readSymbolicLink()).isEqualTo(relative); } @@ -224,7 +224,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { // The path may not be a symlink, neither on Darwin nor on Linux. String nonLinkEntry = null; - for (String child : testFS.getDirectoryEntries(rootPath.getLocalPath())) { + for (String child : testFS.getDirectoryEntries(rootPath)) { Path p = rootPath.getChild(child); if (!p.isSymbolicLink() && p.isDirectory()) { nonLinkEntry = p.getBaseName(); @@ -259,7 +259,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { Path link = absolutize("recursive-link"); createSymbolicLink(link, link); - if (testFS.supportsSymbolicLinksNatively(link.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(link)) { try { link.resolveSymbolicLinks(); fail(); @@ -276,7 +276,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { createSymbolicLink(link2, link1); createSymbolicLink(link1, link2); - if (testFS.supportsSymbolicLinksNatively(link1.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(link1)) { try { link1.resolveSymbolicLinks(); fail(); @@ -288,7 +288,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { @Test public void testResolveSymbolicLinksENOENT() { - if (testFS.supportsSymbolicLinksNatively(xDanglingLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xDanglingLink)) { try { xDanglingLink.resolveSymbolicLinks(); fail(); @@ -302,7 +302,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { public void testResolveSymbolicLinksENOTDIR() throws IOException { Path badLinkTarget = xFile.getChild("bad"); // parent is not a directory! Path badLink = absolutize("badLink"); - if (testFS.supportsSymbolicLinksNatively(badLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(badLink)) { createSymbolicLink(badLink, badLinkTarget); try { badLink.resolveSymbolicLinks(); @@ -317,7 +317,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { @Test public void testResolveSymbolicLinksWithUplevelRefs() throws IOException { - if (testFS.supportsSymbolicLinksNatively(xLinkToFile.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xLinkToFile)) { // Create a series of links that refer to xFile as ./xFile, // ./../foo/xFile, ./../../bar/foo/xFile, etc. They should all resolve // to xFile. @@ -335,7 +335,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { @Test public void testReadSymbolicLink() throws IOException { - if (testFS.supportsSymbolicLinksNatively(xDanglingLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xDanglingLink)) { assertThat(xDanglingLink.readSymbolicLink().toString()).isEqualTo(xNothing.toString()); } @@ -364,7 +364,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { throws IOException { xEmptyDirectory.setWritable(false); Path xChildOfReadonlyDir = xEmptyDirectory.getChild("x"); - if (testFS.supportsSymbolicLinksNatively(xChildOfReadonlyDir.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xChildOfReadonlyDir)) { try { xChildOfReadonlyDir.createSymbolicLink(xNothing); fail(); @@ -386,7 +386,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { assertThat(someLink.isSymbolicLink()).isTrue(); assertThat(someLink.exists(Symlinks.NOFOLLOW)).isTrue(); // the link itself exists assertThat(someLink.exists()).isFalse(); // ...but the referent doesn't - if (testFS.supportsSymbolicLinksNatively(someLink.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(someLink)) { try { someLink.resolveSymbolicLinks(); } catch (FileNotFoundException e) { @@ -398,7 +398,7 @@ public abstract class SymlinkAwareFileSystemTest extends FileSystemTest { @Test public void testCannotCreateSymbolicLinkWithoutParent() throws IOException { Path xChildOfMissingDir = xNothing.getChild("x"); - if (testFS.supportsSymbolicLinksNatively(xChildOfMissingDir.getLocalPath())) { + if (testFS.supportsSymbolicLinksNatively(xChildOfMissingDir)) { try { xChildOfMissingDir.createSymbolicLink(xFile); fail(); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java index a330197a99..1e877a5255 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/UnionFileSystemTest.java @@ -54,8 +54,8 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { private UnionFileSystem createDefaultUnionFileSystem() { return new UnionFileSystem( ImmutableMap.of( - LocalPath.create("/in"), inDelegate, - LocalPath.create("/out"), outDelegate), + PathFragment.create("/in"), inDelegate, + PathFragment.create("/out"), outDelegate), defaultDelegate); } @@ -76,9 +76,9 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { @Test public void testBasicDelegation() throws Exception { unionfs = createDefaultUnionFileSystem(); - LocalPath fooPath = LocalPath.create("/foo"); - LocalPath inPath = LocalPath.create("/in"); - LocalPath outPath = LocalPath.create("/out/in.txt"); + Path fooPath = unionfs.getPath("/foo"); + Path inPath = unionfs.getPath("/in"); + Path outPath = unionfs.getPath("/out/in.txt"); assertThat(unionfs.getDelegate(inPath)).isSameAs(inDelegate); assertThat(unionfs.getDelegate(outPath)).isSameAs(outDelegate); assertThat(unionfs.getDelegate(fooPath)).isSameAs(defaultDelegate); @@ -101,7 +101,7 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { @Test public void testDefaultFileSystemRequired() throws Exception { try { - new UnionFileSystem(ImmutableMap.of(), null); + new UnionFileSystem(ImmutableMap.<PathFragment, FileSystem>of(), null); fail("Able to create a UnionFileSystem with no default!"); } catch (NullPointerException expected) { // OK - should fail in this case. @@ -114,16 +114,16 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { public void testPrefixDelegation() throws Exception { unionfs = new UnionFileSystem( - ImmutableMap.of( - LocalPath.create("/foo"), inDelegate, - LocalPath.create("/foo/bar"), outDelegate), + ImmutableMap.<PathFragment, FileSystem>of( + PathFragment.create("/foo"), inDelegate, + PathFragment.create("/foo/bar"), outDelegate), defaultDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/foo/foo.txt"))).isSameAs(inDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/foo/bar/foo.txt"))).isSameAs(outDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/foo/bar/../foo.txt"))).isSameAs(inDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/bar/foo.txt"))).isSameAs(defaultDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/foo/bar/../.."))).isSameAs(defaultDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/foo/foo.txt"))).isSameAs(inDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/foo/bar/foo.txt"))).isSameAs(outDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/foo/bar/../foo.txt"))).isSameAs(inDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/bar/foo.txt"))).isSameAs(defaultDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/foo/bar/../.."))).isSameAs(defaultDelegate); } // Checks that files cannot be modified when the filesystem is created @@ -133,17 +133,17 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { unionfs = new UnionFileSystem( ImmutableMap.of( - LocalPath.create("/rw"), new XAttrInMemoryFs(BlazeClock.instance()), - LocalPath.create("/ro"), + PathFragment.create("/rw"), new XAttrInMemoryFs(BlazeClock.instance()), + PathFragment.create("/ro"), new XAttrInMemoryFs(BlazeClock.instance()) { @Override - public boolean supportsModifications(LocalPath path) { + public boolean supportsModifications(Path path) { return false; } }), defaultDelegate); - LocalPath rwPath = LocalPath.create("/rw/foo.txt"); - LocalPath roPath = LocalPath.create("/ro/foo.txt"); + Path rwPath = unionfs.getPath("/rw/foo.txt"); + Path roPath = unionfs.getPath("/ro/foo.txt"); assertThat(unionfs.supportsModifications(rwPath)).isTrue(); assertThat(unionfs.supportsModifications(roPath)).isFalse(); } @@ -152,18 +152,18 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { // delegate filesystems; i.e. they can be seen from the filesystem of the parent. @Test public void testDelegateRootDirectoryCreation() throws Exception { - LocalPath foo = LocalPath.create("/foo"); - LocalPath bar = LocalPath.create("/bar"); - LocalPath out = LocalPath.create("/out"); + Path foo = unionfs.getPath("/foo"); + Path bar = unionfs.getPath("/bar"); + Path out = unionfs.getPath("/out"); assertThat(unionfs.createDirectory(foo)).isTrue(); assertThat(unionfs.createDirectory(bar)).isTrue(); assertThat(unionfs.createDirectory(out)).isTrue(); - LocalPath outFile = LocalPath.create("/out/in"); - FileSystemUtils.writeContentAsLatin1(unionfs, outFile, "Out"); + Path outFile = unionfs.getPath("/out/in"); + FileSystemUtils.writeContentAsLatin1(outFile, "Out"); // FileSystemTest.setUp() silently creates the test root on the filesystem... Path testDirUnderRoot = unionfs.getPath(workingDir.asFragment().subFragment(0, 1)); - assertThat(unionfs.getDirectoryEntries(LocalPath.create("/"))) + assertThat(unionfs.getDirectoryEntries(unionfs.getRootDirectory())) .containsExactly( foo.getBaseName(), bar.getBaseName(), @@ -172,30 +172,31 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { assertThat(unionfs.getDirectoryEntries(out)).containsExactly(outFile.getBaseName()); assertThat(defaultDelegate).isSameAs(unionfs.getDelegate(foo)); - assertThat(unionfs.adjustPath(foo, defaultDelegate)).isEqualTo(foo); + assertThat(unionfs.adjustPath(foo, defaultDelegate).asFragment()).isEqualTo(foo.asFragment()); assertThat(defaultDelegate).isSameAs(unionfs.getDelegate(bar)); assertThat(outDelegate).isSameAs(unionfs.getDelegate(outFile)); assertThat(outDelegate).isSameAs(unionfs.getDelegate(out)); // As a fragment (i.e. without filesystem or root info), the path name should be preserved. - assertThat(unionfs.adjustPath(outFile, outDelegate)).isEqualTo(outFile); + assertThat(unionfs.adjustPath(outFile, outDelegate).asFragment()) + .isEqualTo(outFile.asFragment()); } // Ensure that the right filesystem is still chosen when paths contain "..". @Test public void testDelegationOfUpLevelReferences() throws Exception { - assertThat(unionfs.getDelegate(LocalPath.create("/in/../foo.txt"))).isSameAs(defaultDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/out/../in"))).isSameAs(inDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/out/../in/../out/foo.txt"))) + assertThat(unionfs.getDelegate(unionfs.getPath("/in/../foo.txt"))).isSameAs(defaultDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/out/../in"))).isSameAs(inDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/out/../in/../out/foo.txt"))) .isSameAs(outDelegate); - assertThat(unionfs.getDelegate(LocalPath.create("/in/./foo.txt"))).isSameAs(inDelegate); + assertThat(unionfs.getDelegate(unionfs.getPath("/in/./foo.txt"))).isSameAs(inDelegate); } // Basic *explicit* cross-filesystem symlink check. // Note: This does not work implicitly yet, as the next test illustrates. @Test public void testCrossDeviceSymlinks() throws Exception { - assertThat(unionfs.createDirectory(LocalPath.create("/out"))).isTrue(); + assertThat(unionfs.createDirectory(unionfs.getPath("/out"))).isTrue(); // Create an "/in" directory directly on the output delegate to bypass the // UnionFileSystem's mapping. @@ -204,8 +205,8 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { outStream.write('i'); outStream.close(); - LocalPath outFoo = LocalPath.create("/out/foo"); - unionfs.createSymbolicLink(outFoo, "../in/bar.txt"); + Path outFoo = unionfs.getPath("/out/foo"); + unionfs.createSymbolicLink(outFoo, PathFragment.create("../in/bar.txt")); assertThat(unionfs.stat(outFoo, false).isSymbolicLink()).isTrue(); try { @@ -215,13 +216,24 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { // OK } - LocalPath resolved = unionfs.resolveSymbolicLinks(outFoo); - InputStream barInput = unionfs.getInputStream(resolved); + Path resolved = unionfs.resolveSymbolicLinks(outFoo); + assertThat(resolved.getFileSystem()).isSameAs(unionfs); + InputStream barInput = resolved.getInputStream(); int barChar = barInput.read(); barInput.close(); assertThat(barChar).isEqualTo('i'); } + @Test + public void testNoDelegateLeakage() throws Exception { + assertThat(unionfs.getPath("/in/foo.txt").getFileSystem()).isSameAs(unionfs); + assertThat(unionfs.getPath("/in/foo/bar").getParentDirectory().getFileSystem()) + .isSameAs(unionfs); + unionfs.createDirectory(unionfs.getPath("/out")); + unionfs.createDirectory(unionfs.getPath("/out/foo")); + unionfs.createDirectory(unionfs.getPath("/out/foo/bar")); + } + // Write using the VFS through a UnionFileSystem and check that the file can // be read back in the same location using standard Java IO. // There is a similar test in UnixFileSystem, but this is essential to ensure @@ -230,11 +242,12 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { public void testDelegateOperationsReflectOnLocalFilesystem() throws Exception { unionfs = new UnionFileSystem( - ImmutableMap.of(workingDir.getLocalPath().getParentDirectory(), new UnixFileSystem()), + ImmutableMap.<PathFragment, FileSystem>of( + workingDir.getParentDirectory().asFragment(), new UnixFileSystem()), defaultDelegate); // This is a child of the current tmpdir, and doesn't exist on its own. // It would be created in setup(), but of course, that didn't use a UnixFileSystem. - unionfs.createDirectory(workingDir.getLocalPath()); + unionfs.createDirectory(workingDir); Path testFile = unionfs.getPath(workingDir.getRelative("test_file").asFragment()); assertThat(testFile.asFragment().startsWith(workingDir.asFragment())).isTrue(); String testString = "This is a test file"; @@ -243,7 +256,7 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { assertThat(new String(FileSystemUtils.readContentAsLatin1(testFile))).isEqualTo(testString); } finally { testFile.delete(); - assertThat(unionfs.delete(workingDir.getLocalPath())).isTrue(); + assertThat(unionfs.delete(workingDir)).isTrue(); } } @@ -252,7 +265,8 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { public void testCreateParentsAcrossMapping() throws Exception { unionfs = new UnionFileSystem( - ImmutableMap.of(LocalPath.create("/out/dir"), outDelegate), defaultDelegate); + ImmutableMap.<PathFragment, FileSystem>of(PathFragment.create("/out/dir"), outDelegate), + defaultDelegate); Path outDir = unionfs.getPath("/out/dir/biz/bang"); FileSystemUtils.createDirectoryAndParents(outDir); assertThat(outDir.isDirectory()).isTrue(); @@ -264,7 +278,8 @@ public class UnionFileSystemTest extends SymlinkAwareFileSystemTest { } @Override - public byte[] getxattr(LocalPath path, String name) { + public byte[] getxattr(Path path, String name) { + assertThat(path.getFileSystem()).isSameAs(this); return (name.equals(XATTR_KEY)) ? XATTR_VAL.getBytes(UTF_8) : null; } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/UnixLocalPathTest.java b/src/test/java/com/google/devtools/build/lib/vfs/UnixLocalPathTest.java index 16ae52cc4c..2cdb4014ae 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/UnixLocalPathTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/UnixLocalPathTest.java @@ -162,17 +162,6 @@ public class UnixLocalPathTest extends LocalPathAbstractTest { assertThat(create("/..")).isEqualTo(create("/..")); } - @Test - public void testRootsUnix() { - assertThat(create("/").getDrive().getPathString()).isEqualTo("/"); - assertThat(create("/usr").getDrive().getPathString()).isEqualTo("/"); - assertThrows(IllegalArgumentException.class, () -> create("").getDrive()); - - assertThat(create("/").isRoot()).isTrue(); - assertThat(create("/usr").isRoot()).isFalse(); - assertThat(create("").isRoot()).isFalse(); - } - @Override protected OsPathPolicy getFilePathOs() { return new UnixOsPathPolicy(); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/WindowsLocalPathTest.java b/src/test/java/com/google/devtools/build/lib/vfs/WindowsLocalPathTest.java index 88c7b6f248..ac5acef1f4 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/WindowsLocalPathTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/WindowsLocalPathTest.java @@ -14,9 +14,9 @@ package com.google.devtools.build.lib.vfs; import static com.google.common.truth.Truth.assertThat; -import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.testutil.MoreAsserts; import com.google.devtools.build.lib.vfs.LocalPath.OsPathPolicy; import com.google.devtools.build.lib.vfs.LocalPath.WindowsOsPathPolicy; import com.google.devtools.build.lib.vfs.LocalPath.WindowsOsPathPolicy.ShortPathResolver; @@ -101,14 +101,8 @@ public class WindowsLocalPathTest extends LocalPathAbstractTest { @Test public void testisAbsoluteWindows() { assertThat(create("C:/").isAbsolute()).isTrue(); - } - - // We support unix paths to make test sharing easier - @Test - public void testUnixPathSupport() { + // test that msys paths turn into absolute paths assertThat(create("/").isAbsolute()).isTrue(); - assertThat(create("/foo").isAbsolute()).isTrue(); - assertThat(create("/foo").getParentDirectory().getPathString()).isEqualTo("/"); } @Test @@ -116,7 +110,22 @@ public class WindowsLocalPathTest extends LocalPathAbstractTest { assertThat(create("C:/foo").relativeTo(create("C:/"))).isEqualTo(create("foo")); // Case insensitivity test assertThat(create("C:/foo/bar").relativeTo(create("C:/FOO"))).isEqualTo(create("bar")); - assertThrows(IllegalArgumentException.class, () -> create("D:/foo").relativeTo(create("C:/"))); + MoreAsserts.assertThrows( + IllegalArgumentException.class, () -> create("D:/foo").relativeTo(create("C:/"))); + } + + @Test + public void testAbsoluteUnixPathIsRelativeToWindowsUnixRoot() { + assertThat(create("/").getPathString()).isEqualTo("C:/fake/msys"); + assertThat(create("/foo/bar").getPathString()).isEqualTo("C:/fake/msys/foo/bar"); + assertThat(create("/foo/bar").getPathString()).isEqualTo("C:/fake/msys/foo/bar"); + } + + @Test + public void testAbsoluteUnixPathReferringToDriveIsRecognized() { + assertThat(create("/c/foo").getPathString()).isEqualTo("C:/foo"); + assertThat(create("/c/foo").getPathString()).isEqualTo("C:/foo"); + assertThat(create("/c:").getPathString()).isNotEqualTo("C:/foo"); } @Test @@ -152,16 +161,4 @@ public class WindowsLocalPathTest extends LocalPathAbstractTest { // Assert relative paths that look like short paths are untouched assertThat(create("progra~1").getPathString()).isEqualTo("progra~1"); } - - @Test - public void testRootsWindows() { - assertThat(create("C:/").getDrive().getPathString()).isEqualTo("C:/"); - assertThat(create("C:/usr").getDrive().getPathString()).isEqualTo("C:/"); - assertThrows(IllegalArgumentException.class, () -> create("").getDrive()); - - assertThat(create("C:/").isRoot()).isFalse(); - assertThat(create("C:/usr").isRoot()).isFalse(); - assertThat(create("/").isRoot()).isTrue(); - assertThat(create("").isRoot()).isFalse(); - } } |