Skip to content

Commit

Permalink
Drop rprojroot dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 20, 2023
1 parent 1bdb534 commit 6332428
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Imports:
processx (>= 3.4.2),
ps,
R6,
rprojroot,
stats,
utils,
zip (>= 2.3.0)
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ importFrom(pkgbuild,pkgbuild_process)
importFrom(prettyunits,pretty_bytes)
importFrom(prettyunits,pretty_dt)
importFrom(prettyunits,pretty_sec)
importFrom(rprojroot,find_package_root_file)
importFrom(stats,na.omit)
importFrom(utils,modifyList)
importFrom(utils,untar)
Expand Down
29 changes: 29 additions & 0 deletions R/find-package-root.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
find_package_root <- function(path = ".") {
is_root <- function(path) {
identical(
normalizePath(path, winslash = "/"),
normalizePath(dirname(path), winslash = "/")
)
}

if (!file.exists(path)) {
stop("Path does not exist: ", path)
}
cur_path <- normalizePath(path, winslash = "/")
errmsg <- paste0(
"Could not find R package in `",
path,
"` or its parent directories."
)
max_depth <- 100
for (i in 1:max_depth) {
if (file.exists(file.path(cur_path, "DESCRIPTION"))) {
return(cur_path)
} else if (is_root(cur_path)) {
stop(errmsg)
} else {
cur_path <- dirname(cur_path)
}
}
stop(errmsg, " Checked ", max_depth, " parent directories.") # nocov
}
2 changes: 0 additions & 2 deletions R/solve.R
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,9 @@ pkgplan_install_plan <- function(self, private, downloads) {
sol
}

#' @importFrom rprojroot find_package_root_file
#' @importFrom jsonlite unbox

pkgplan_export_install_plan <- function(self, private, plan_file, version) {
plan_file <- plan_file %||% find_package_root_file("resolution.json")
pkgs <- pkgplan_install_plan(self, private, downloads = FALSE)
cols <- unique(c(
"ref", "package", "version", "type", "direct", "binary",
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ test_temp_dir <- function(pattern = "test-dir-", envir = parent.frame()) {

test_package_root <- function() {
x <- tryCatch(
rprojroot::find_package_root_file(),
find_package_root(),
error = function(e) NULL)

if (!is.null(x)) return(x)

pkg <- testthat::testing_package()
x <- tryCatch(
rprojroot::find_package_root_file(
path = file.path("..", "..", "00_pkg_src", pkg)),
error = function(e) NULL)
find_package_root(
path = file.path("..", "..", "00_pkg_src", pkg)
),
error = function(e) NULL
)

if (!is.null(x)) return(x)

Expand Down
48 changes: 48 additions & 0 deletions tests/testthat/test-find-package-root.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
test_that("find_package_root", {
tmp <- tempfile()
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
mkdirp(file.path(tmp, "a", "b", "c", "d"))
lns <- "Package: this"
writeLines(lns, file.path(tmp, "DESCRIPTION"))

expect_equal(
readLines(file.path(find_package_root(tmp), "DESCRIPTION")),
lns
)

expect_equal(
readLines(file.path(
find_package_root(file.path(tmp, "a")), "DESCRIPTION"
)),
lns
)

expect_equal(
readLines(file.path(
find_package_root(file.path(tmp, "a", "b", "c", "d")), "DESCRIPTION"
)),
lns
)

wd <- getwd()
on.exit(setwd(wd), add = TRUE)
setwd(file.path(tmp, "a", "b", "c"))
expect_equal(
readLines(file.path(find_package_root("."), "DESCRIPTION")),
lns
)
})

test_that("find_package_root errors", {
expect_error(
find_package_root(basename(tempfile())),
"Path does not exist"
)

if (!file.exists("/DESCRIPTION")) {
expect_error(
find_package_root("/"),
"Could not find R package"
)
}
})

0 comments on commit 6332428

Please sign in to comment.