From 31a6370bd8f801922d44d061a322cb2aa28730bb Mon Sep 17 00:00:00 2001 From: "Watal M. Iwasaki" Date: Tue, 20 Sep 2022 23:31:26 +0900 Subject: [PATCH] Realize relative symlinks properly in dir_exists() Fixes #395 --- NEWS.md | 2 ++ R/access.R | 2 +- tests/testthat/test-access.R | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d43a5e17..686e5498 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * inputs to `path_real()` and `path_join()` are coerced to character for consistency with other functions (@raymondben, #370) +* `dir_exists()` follows relative symlinks in non-current directories (@heavywatal, #395) + # fs 1.5.2 * `file_create()` and `dir_create()` now return the correct path when `...` arguments are used (@davidchall, #333). 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 3180849b..415275d6 100644 --- a/tests/testthat/test-access.R +++ b/tests/testthat/test-access.R @@ -30,6 +30,7 @@ describe("file_access", { with_dir_tree(list("foo/bar" = "test"), { link_create(path_abs("foo"), "loo") + link_create("foo", "relloo") describe("file_exists", { it("returns true for files that exist, false for those that do not", { @@ -60,6 +61,11 @@ 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)) + expect_equal(dir_exists("relloo"), c(relloo = TRUE)) + .old_wd <- setwd("foo") + expect_equal(dir_exists("../loo"), c("../loo" = TRUE)) + 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))