From f4dc1ed9eeaea7cfcb9410166c8bc0ed45f4591d Mon Sep 17 00:00:00 2001 From: Aidan Lakshman Date: Fri, 14 Feb 2025 10:38:54 -0500 Subject: [PATCH 1/2] Improve `isdir` checks for filepaths --- R/build.R | 2 +- R/env.R | 2 +- R/package.R | 2 +- R/utils.R | 7 +++++++ tests/testthat/test-errors.R | 4 ++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/R/build.R b/R/build.R index 630ed86..41f4d9a 100644 --- a/R/build.R +++ b/R/build.R @@ -10,7 +10,7 @@ build_package <- function(path, tmpdir, build_args, libpath, quiet) { dir.create(tmpdir, recursive = TRUE, showWarnings = FALSE) tmpdir <- normalizePath(tmpdir) - if (file.info(path)$isdir) { + if (safecheck_isdir(path)) { if (!quiet) cat_head("R CMD build") desc <- desc(path) diff --git a/R/env.R b/R/env.R index 8537750..51e4831 100644 --- a/R/env.R +++ b/R/env.R @@ -42,7 +42,7 @@ load_env <- function(path, targz, package, envir = parent.frame()) { if (!should_load) return() env <- NULL - if (file.info(path)$isdir) { + if (safecheck_isdir(path)) { env_path <- file.path(path, "tools", "check.env") } else { dir.create(tmp <- tempfile()) diff --git a/R/package.R b/R/package.R index 3e01892..983cec5 100644 --- a/R/package.R +++ b/R/package.R @@ -130,7 +130,7 @@ rcmdcheck <- function( error_on <- match.arg(error_on, c("never", "error", "warning", "note")) - if (file.info(path)$isdir) { + if (safecheck_isdir(path)) { path <- find_package_root_file(path = path) } else { path <- normalizePath(path) diff --git a/R/utils.R b/R/utils.R index 40d19de..fc252ea 100644 --- a/R/utils.R +++ b/R/utils.R @@ -213,3 +213,10 @@ data_literal <- function(...) { colClasses = "character" ) } + +safecheck_isdir <- function(path){ + if(!file.exists(path)) stop("path '", path, "' does not exist!") + res <- file.info(path) + if(is.na(res$isdir)) stop("path '", path, "' is not readable!") + res$isdir +} diff --git a/tests/testthat/test-errors.R b/tests/testthat/test-errors.R index 17eb7ae..3a57633 100644 --- a/tests/testthat/test-errors.R +++ b/tests/testthat/test-errors.R @@ -73,3 +73,7 @@ test_that("error_on argument", { tryCatch(rcmdcheck(), error = function(e) e) expect_equal(value, "note") }) + +test_that("error correctly when reading invalid files", { + expect_error(rcmdcheck("Invalid file name"), "path 'Invalid file name' does not exist!") +}) From aa44fbcd54ac25c21bb217e0c9d100c73680351d Mon Sep 17 00:00:00 2001 From: Aidan Lakshman Date: Mon, 17 Feb 2025 11:05:16 -0500 Subject: [PATCH 2/2] set `extra_cols = FALSE` to slightly improve performance --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index fc252ea..76f0453 100644 --- a/R/utils.R +++ b/R/utils.R @@ -216,7 +216,7 @@ data_literal <- function(...) { safecheck_isdir <- function(path){ if(!file.exists(path)) stop("path '", path, "' does not exist!") - res <- file.info(path) + res <- file.info(path, extra_cols = FALSE) if(is.na(res$isdir)) stop("path '", path, "' is not readable!") res$isdir }