diff --git a/NEWS.md b/NEWS.md index ac2b34a6..a6448607 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * New `path_select_components()` function to select components of one or more paths (#326, @Tazinho). +* `dir_exists()` follows relative symlinks in non-current directories + (@heavywatal, #395). + # fs 1.6.6 * No changes. diff --git a/R/access.R b/R/access.R index 538bed52..8aeeb1ed 100644 --- a/R/access.R +++ b/R/access.R @@ -44,7 +44,7 @@ dir_exists <- function(path) { res <- is_dir(path) links <- is_link(path) - res[links] <- is_dir(link_path(path[links])) + res[links] <- is_dir(path_real(path[links])) !is.na(res) & res } diff --git a/tests/testthat/test-access.R b/tests/testthat/test-access.R index 26cf1481..71f1241f 100644 --- a/tests/testthat/test-access.R +++ b/tests/testthat/test-access.R @@ -34,6 +34,9 @@ describe("file_access", { with_dir_tree(list("foo/bar" = "test"), { link_create(path_abs("foo"), "loo") + if (!is_windows()) { + link_create("foo", "relloo") + } describe("file_exists", { it("returns true for files that exist, false for those that do not", { @@ -64,6 +67,15 @@ with_dir_tree(list("foo/bar" = "test"), { }) it("returns true for links to directories, like -d in bash", { expect_equal(dir_exists("loo"), c(loo = TRUE)) + if (!is_windows()) { + expect_equal(dir_exists("relloo"), c(relloo = TRUE)) + } + .old_wd <- setwd("foo") + expect_equal(dir_exists("../loo"), c("../loo" = TRUE)) + if (!is_windows()) { + expect_equal(dir_exists("../relloo"), c("../relloo" = TRUE)) + } + setwd(.old_wd) }) it("returns FALSE on missing input", { expect_identical(dir_exists(NA_character_), structure(names = NA, FALSE))