@Test public void testExpandHomeDir() { Path homeDir = Paths.get(System.getProperty("user.home")); assertEquals( "Must expand home dir.", homeDir.resolve("foo"), MorePaths.expandHomeDir(Paths.get("~/foo"))); assertEquals( "Must expand home dir by itself too.", homeDir, MorePaths.expandHomeDir(Paths.get("~"))); Path relativePath = Paths.get("foo/bar"); assertEquals( "Must not expand relative paths.", relativePath, MorePaths.expandHomeDir(relativePath)); Path absolutePath = Paths.get("/foo/bar"); assertEquals( "Must not expand absolute paths.", absolutePath, MorePaths.expandHomeDir(absolutePath)); Path funnyHomePath = Paths.get("~jacko/foo"); assertEquals( "Must only expand home paths starting with ~/", funnyHomePath, MorePaths.expandHomeDir(funnyHomePath)); }
private static Path getCacheDir(Path root, Optional<String> value) { String cacheDir = value.or(root.resolve(BuckConstant.DEFAULT_CACHE_DIR).toString()); Path toReturn = root.getFileSystem().getPath(cacheDir); toReturn = MorePaths.expandHomeDir(toReturn); if (toReturn.isAbsolute()) { return toReturn; } ImmutableSet<Path> filtered = MorePaths.filterForSubpaths(ImmutableSet.of(toReturn), root); if (filtered.isEmpty()) { // OK. For some reason the relative path managed to be out of our directory. return toReturn; } return Iterables.getOnlyElement(filtered); }
@Test public void testCreateRelativeSymlinkToFilesInRoot() throws IOException { ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot()); tmp.newFile("biz.txt"); Path pathToDesiredLinkUnderProjectRoot = Paths.get("gamma.txt"); Path pathToExistingFileUnderProjectRoot = Paths.get("biz.txt"); Path relativePath = MorePaths.createRelativeSymlink( pathToDesiredLinkUnderProjectRoot, pathToExistingFileUnderProjectRoot, projectFilesystem); assertEquals("biz.txt", relativePath.toString()); Path absolutePathToDesiredLinkUnderProjectRoot = projectFilesystem.resolve(pathToDesiredLinkUnderProjectRoot); assertTrue(Files.isSymbolicLink(absolutePathToDesiredLinkUnderProjectRoot)); Path targetOfSymbolicLink = Files.readSymbolicLink(absolutePathToDesiredLinkUnderProjectRoot); assertEquals(relativePath, targetOfSymbolicLink); Path absolutePathToExistingFileUnderProjectRoot = projectFilesystem.resolve(pathToExistingFileUnderProjectRoot); Files.write(absolutePathToExistingFileUnderProjectRoot, "Hello, World!".getBytes()); String dataReadFromSymlink = new String(Files.readAllBytes(absolutePathToDesiredLinkUnderProjectRoot)); assertEquals("Hello, World!", dataReadFromSymlink); }
@Test public void testFilterForSubpaths() { Path root = tmp.getRoot(); ImmutableSortedSet<Path> paths = MorePaths.asPaths( ImmutableSet.of( ".buckd", "foo/bar", root.toAbsolutePath() + "/buck-cache", Paths.get("/root/not/in/test").toAbsolutePath().toString())); assertEquals( "Set should have been filtered down to paths contained under root.", ImmutableSet.of(Paths.get(".buckd"), Paths.get("foo/bar"), Paths.get("buck-cache")), MorePaths.filterForSubpaths(paths, root)); }
@Test public void relativeWithEmptyPath() { // Ensure workaround for Windows Path relativize bug Path p1 = Paths.get(""); Path p2 = Paths.get("foo"); assertThat(MorePaths.relativize(p1, p2), equalTo(Paths.get("foo"))); }
@Test public void relativizeWithDotDotPathDoesNotAddExtraDotDotPath() { // Ensure workaround for bug JDK-6925169 for ".." case. Path p1 = Paths.get("a/../b"); Path p2 = Paths.get("c/d/e"); assertThat(MorePaths.relativize(p1, p2), equalTo(Paths.get("../c/d/e"))); }
@Test public void testGetRelativePath() { // Path on base directory. assertEquals(Paths.get("file"), MorePaths.getRelativePath(Paths.get("file"), Paths.get(""))); // Path on base directory (using null). assertEquals(Paths.get("file"), MorePaths.getRelativePath(Paths.get("file"), null)); // Path internal to base directory. assertEquals( Paths.get("dir/file"), MorePaths.getRelativePath(Paths.get("base/dir/file"), Paths.get("base"))); // Path external to base directory. assertEquals( Paths.get("../dir1/file"), MorePaths.getRelativePath(Paths.get("dir1/file"), Paths.get("base"))); }
/** * @param path Absolute path or path relative to the project root. * @return If {@code path} is relative, it is returned. If it is absolute and is inside the * project root, it is relativized to the project root and returned. Otherwise an absent value * is returned. */ public Optional<Path> getPathRelativeToProjectRoot(Path path) { path = path.normalize(); if (path.isAbsolute()) { if (path.startsWith(projectRoot)) { return Optional.of(MorePaths.relativize(projectRoot, path)); } else { return Optional.absent(); } } else { return Optional.of(path); } }
@Test public void normalizeWithEmptyPath() { // Ensure workaround for Java Path normalize bug Path emptyPath = Paths.get(""); assertThat(MorePaths.normalize(emptyPath), equalTo(emptyPath)); }