Skip to content
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

feat: Check all subdirs in find_root() instead of only the first existing one #100

Closed
wants to merge 3 commits into from
Closed
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
72 changes: 46 additions & 26 deletions R/root.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,65 @@ print.root_criterion <- function(x, ...) {
#' @export
find_root <- function(criterion, path = ".") {
criterion <- as_root_criterion(criterion)
path <- normalizePath(path, winslash = "/", mustWork = TRUE)
start_paths <- path[dir.exists(path)]

start_path <- get_start_path(path, criterion$subdir)
path <- start_path

for (i in seq_len(.MAX_DEPTH)) {
for (f in criterion$testfun) {
if (f(path)) {
return(path)
}
if (length(criterion$subdir) > 0L) {
sub_paths <- file.path(path, criterion$subdir)
sub_paths <- sub_paths[dir.exists(sub_paths)]
if (length(sub_paths) > 0L) {
start_paths <- sub_paths
}
}

if (is_fs_root(path)) {
stop("No root directory found in ", start_path, " or its parent directories. ",
paste(format(criterion), collapse = "\n"),
call. = FALSE
)
}
root <- NULL
max_depth_reached <- FALSE
max_depth_path <- NULL

path <- dirname(path)
}
for (start_path in start_paths) {
cur_path <- start_path

stop("Maximum search of ", .MAX_DEPTH, " exceeded. Last path: ", path, call. = FALSE)
}
for (i in seq_len(.MAX_DEPTH)) {
for (fn in criterion$testfun) {
if (fn(cur_path)) {
root <- cur_path
break
}
}

.MAX_DEPTH <- 100L
if (length(root) > 0L || is_fs_root(cur_path)) {
break
}

get_start_path <- function(path, subdirs) {
path <- normalizePath(path, winslash = "/", mustWork = TRUE)
if (i == .MAX_DEPTH) {
max_depth_reached <- TRUE
max_depth_path <- cur_path
}

cur_path <- dirname(cur_path)
}

for (subdir in subdirs) {
subdir_path <- file.path(path, subdir)
if (dir.exists(subdir_path)) {
return(subdir_path)
if (length(root) > 0L) {
break
}
}

path
if (length(root) > 0L) {
return(root)
}

if (max_depth_reached) {
stop("Maximum search of ", .MAX_DEPTH, " exceeded. Last path: ", max_depth_path, call. = FALSE)
}

stop("No root directory found in ", paste0(start_paths, collapse = ", "), " or ", ifelse(length(start_paths) > 1L, "their", "its"), " parent directories. ",
paste(format(criterion), collapse = "\n"),
call. = FALSE
)
}

.MAX_DEPTH <- 100L

# Borrowed from devtools
is_fs_root <- function(path) {
identical(
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions tests/testthat/test-root.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,32 @@ test_that("stops if depth reached", {
# Checks that search for root terminates for very deep hierarchies
expect_error(find_root_mocked(""), "Maximum search of [0-9]+ exceeded")
})

test_that("all criterion subdirs are checked", {
wd <- normalizePath(getwd(), winslash = "/")
criterion <- root_criterion(
testfun = function(path) file.exists(file.path(path, "target")) && basename(dirname(dirname(path))) == "check_subdirs",
desc = "has file `target` and basename of grandparent dir is `check_subdirs`",
subdir = c("a", "b")
)

expect_equal(
find_root(criterion, "check_subdirs/1"),
file.path(wd, "check_subdirs/1/a")
)

expect_equal(
find_root(criterion, "check_subdirs/2"),
file.path(wd, "check_subdirs/2/b")
)

expect_error(
find_root(criterion, "check_subdirs/3"),
"No root directory found"
)

expect_error(
find_root(criterion, "check_subdirs/4"),
"No root directory found"
)
})
Loading