Skip to content

Commit

Permalink
Add a path argument to path_abs
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
jimhester committed Mar 14, 2018
1 parent 3f170d3 commit 0cccfea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# fs 1.2.1

* `path_abs()` gains a `start` argument, to specify where the absolute path
should be calculated from (#87).

* Fix for a memory issue reported by ASAN and valgrind.

# fs 1.2.0
Expand Down
11 changes: 7 additions & 4 deletions R/path.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ path_join <- function(parts) {

#' @describeIn path_math returns a normalized, absolute version of a path.
#' @export
path_abs <- function(path) {
path_abs <- function(path, start = ".") {
if (!is_absolute_path(start)) {
start <- path_norm(path(getwd(), start))
}
is_abs <- is_absolute_path(path)
path[is_abs] <- path_norm(path[is_abs])
cwd <- getwd()
path[!is_abs] <- path_norm(path(cwd, path[!is_abs]))
path[!is_abs] <- path_norm(path(start, path[!is_abs]))
path_tidy(path)
}


#' @describeIn path_math collapses redundant separators and
#' up-level references, so `A//B`, `A/B`, `A/.B` and `A/foo/../B` all become
#' `A/B`. If one of the paths is a symbolic link, this may change the meaning
Expand Down Expand Up @@ -170,7 +173,7 @@ path_norm <- function(path) {
#' @describeIn path_math computes the path relative to the `start` path,
#' which can be either a absolute or relative path.
#' @export
#' @param start A starting directory to compute relative path to.
#' @param start A starting directory to compute the path to.
# This implementation is partially derived from
# https://github.com/python/cpython/blob/9c99fd163d5ca9bcc0b7ddd0d1e3b8717a63237c/Lib/posixpath.py#L446
path_rel <- function(path, start = ".") {
Expand Down
13 changes: 2 additions & 11 deletions man/path_math.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion tests/testthat/test-path.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe("path_common", {

# derived from https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Lib/test/test_posixpath.py#L483
describe("path_rel", {
it("works for posix paths", {
it("works for POSIX paths", {
cur_dir <- path_file(getwd())
expect_equal(path_rel("a"), "a")
expect_equal(path_rel(path_abs("a")), "a")
Expand Down Expand Up @@ -308,6 +308,12 @@ describe("path_rel", {
})
})

describe("path_abs", {
it("uses the start parameter", {
expect_equal(path_abs("b", "a"), path_abs("a/b"))
})
})

describe("path_home", {
# The trailing slash is needed to ensure we get path expansion
# on POSIX systems when readline support is not built in. (#60)
Expand Down

0 comments on commit 0cccfea

Please sign in to comment.