From bee41817be64dc1f681716eb9aa693e345ac9d6c Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Tue, 4 Apr 2023 09:10:15 +0000 Subject: [PATCH] Expand path in `path_has_parent()` --- NEWS.md | 2 ++ R/path.R | 2 ++ tests/testthat/test-path.R | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/NEWS.md b/NEWS.md index 0a87e675..da5d0f35 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fs (development version) +* `path_has_parent()` now expands `~` (#412). + # fs 1.6.1 No user visible changes. diff --git a/R/path.R b/R/path.R index 91a91908..14a01915 100644 --- a/R/path.R +++ b/R/path.R @@ -480,7 +480,9 @@ path_filter <- function(path, glob = NULL, regexp = NULL, invert = FALSE, ...) { #' @export path_has_parent <- function(path, parent) { path <- path_abs(path) + path <- path_expand(path) parent <- path_abs(parent) + parent <- path_expand(parent) res <- logical(length(path)) diff --git a/tests/testthat/test-path.R b/tests/testthat/test-path.R index ff0b638f..c425823c 100644 --- a/tests/testthat/test-path.R +++ b/tests/testthat/test-path.R @@ -545,6 +545,10 @@ describe("path_has_parent", { expect_true(path_has_parent("foo/bar", "foo")) expect_true(path_has_parent("path/myfiles/myfile", "path/to/files/../../myfiles")) + + # expands path + expect_true(path_has_parent("~/a", path_expand("~/a"))) + expect_true(path_has_parent(path_expand("~/a"), "~/a")) }) it("works with multiple paths", { expect_equal(path_has_parent(c("/a/b/c", "x/y"), "/a/b"), c(TRUE, FALSE))