diff --git a/R/settings.R b/R/settings.R index 7e47ffb8d..801cf596d 100644 --- a/R/settings.R +++ b/R/settings.R @@ -22,13 +22,13 @@ read_settings <- function(filename) { } if (!is.null(config_file)) { - f <- function(e) { + malformed <- function(e) { stop("Malformed config file, ensure it ends in a newline\n ", conditionMessage(e), call. = FALSE) } - tryCatch( - config <- read.dcf(config_file, all = TRUE), - warning = f, - error = f + config <- tryCatch( + read.dcf(config_file, all = TRUE), + warning = malformed, + error = malformed ) } else { config <- NULL @@ -69,7 +69,7 @@ find_default_encoding <- function(filename) { return(NULL) } - root_path <- find_rproj_or_package(filename) + root_path <- find_package(filename, allow_rproj = TRUE) rproj_enc <- get_encoding_from_dcf(find_rproj_at(root_path)) if (!is.null(rproj_enc)) { return(rproj_enc) diff --git a/R/settings_utils.R b/R/settings_utils.R index 40465927e..237d34f2c 100644 --- a/R/settings_utils.R +++ b/R/settings_utils.R @@ -3,24 +3,20 @@ has_description <- function(path) { !is.na(desc_info$size) && desc_info$size > 0.0 && !desc_info$isdir } -find_package <- function(path) { - path <- normalizePath(path) - depth <- 2L - while (!has_description(path)) { - path <- dirname(path) - if (is_root(path) || depth <= 0L) { - return(NULL) - } - depth <- depth - 1L - } - path +has_rproj <- function(path) { + length(Sys.glob(file.path(path, "*.Rproj"))) > 0L } -find_rproj_or_package <- function(path) { - path <- normalizePath(path, mustWork = FALSE) +find_package <- function(path, allow_rproj = FALSE, max_depth = 2L) { + path <- normalizePath(path, mustWork = !allow_rproj) + if (allow_rproj) { + found <- function(path) has_description(path) || has_rproj(path) + } else { + found <- function(path) has_description(path) + } - depth <- 2L - while (!(has_description(path) || has_rproj(path))) { + depth <- max_depth + while (!found(path)) { path <- dirname(path) if (is_root(path) || depth <= 0L) { return(NULL) @@ -30,10 +26,6 @@ find_rproj_or_package <- function(path) { path } -has_rproj <- function(path) { - length(head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)) == 1L -} - find_rproj_at <- function(path) { head(Sys.glob(file.path(path, "*.Rproj")), n = 1L) } diff --git a/tests/testthat/test-settings.R b/tests/testthat/test-settings.R index 3ee128c02..0a0d87ca1 100644 --- a/tests/testthat/test-settings.R +++ b/tests/testthat/test-settings.R @@ -102,7 +102,7 @@ test_that("it has a smart default for encodings", { pkg_file <- test_path("dummy_packages", "cp1252", "R", "cp1252.R") expect_identical( - normalizePath(find_rproj_at(find_rproj_or_package(proj_file)), winslash = "/"), + normalizePath(find_rproj_at(find_package(proj_file, allow_rproj = TRUE)), winslash = "/"), normalizePath(test_path("dummy_projects", "project", "project.Rproj"), winslash = "/") ) expect_identical(