Пример #1
0
  @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));
  }
Пример #2
0
 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);
 }
Пример #3
0
  @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);
  }
Пример #4
0
  @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));
  }
Пример #5
0
 @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")));
 }
Пример #6
0
 @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")));
 }
Пример #7
0
  @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")));
  }
Пример #8
0
 /**
  * @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);
   }
 }
Пример #9
0
 @Test
 public void normalizeWithEmptyPath() {
   // Ensure workaround for Java Path normalize bug
   Path emptyPath = Paths.get("");
   assertThat(MorePaths.normalize(emptyPath), equalTo(emptyPath));
 }