Skip to content

Expand path in path_has_parent() #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# fs (development version)

* `path_has_parent()` now expands `~` (#412).

* New `path_select_components()` function to select components of one or
more paths (#326, @Tazinho).

Expand Down
2 changes: 2 additions & 0 deletions R/path.R
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,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))

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-path.R
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,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))
Expand Down
Loading