Skip to content

Commit

Permalink
Fail if system.file() is supplied path starting with inst
Browse files Browse the repository at this point in the history
Closes #104
  • Loading branch information
lionel- committed Jun 2, 2022
1 parent 3a5a1c2 commit c923564
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# pkgload (development version)

* The `system.file()` shim now fails if you supply a path that starts
with `inst` to better reproduce the behaviour with installed
packages (#104).

* `load_all()` now imports its dependencies lazily to avoid parallel
installation issues (#89).

Expand Down
9 changes: 9 additions & 0 deletions R/shims.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ shim_system.file <- function(...,
# be installed. To fully duplicate R's package-building and installation
# behavior would be complicated, so we'll just use this simple method.

if (dots_n(...) && is_string(..1)) {
if (is_string("inst", ..1) || grepl("^inst/", ..1)) {
cli::cli_abort(c(
"Paths can't start with `inst`",
i = "Files in `inst` are installed at top-level."
))
}
}

pkg_path <- find.package(package)

# First look in inst/
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/shim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# system.file() fails if path starts with `inst` (#104)

Code
(expect_error(shim_system.file("inst/WORDLIST", package = "pkgload", mustWork = TRUE))
)
Output
<error/rlang_error>
Error in `shim_system.file()`:
! Paths can't start with `inst`
i Files in `inst` are installed at top-level.
Code
(expect_error(shim_system.file("inst", "WORDLIST", package = "pkgload",
mustWork = TRUE)))
Output
<error/rlang_error>
Error in `shim_system.file()`:
! Paths can't start with `inst`
i Files in `inst` are installed at top-level.

11 changes: 11 additions & 0 deletions tests/testthat/test-shim.r
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ test_that("Replacement system.file returns correct values when installed", {
test_that("division operator is not interpreted as a path (#198)", {
expect_null(dev_topic_find("/"))
})

test_that("system.file() fails if path starts with `inst` (#104)", {
expect_true(
is_string(shim_system.file(package = "pkgload", mustWork = TRUE))
)

expect_snapshot({
(expect_error(shim_system.file("inst/WORDLIST", package = "pkgload", mustWork = TRUE)))
(expect_error(shim_system.file("inst", "WORDLIST", package = "pkgload", mustWork = TRUE)))
})
})

0 comments on commit c923564

Please sign in to comment.