Skip to content

Commit 830745e

Browse files
committed
Fix is_on_disk() for pkgload
1 parent fa18874 commit 830745e

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ Suggests:
2828
cli (>= 3.1.0),
2929
covr,
3030
crayon,
31+
desc,
3132
fs,
3233
glue,
3334
knitr,
3435
magrittr,
3536
methods,
3637
pillar,
38+
pkgload,
3739
rmarkdown,
3840
stats,
3941
testthat (>= 3.2.0),

R/session.R

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,23 @@ detect_installed <- function(info) {
101101
}
102102

103103
is_on_disk <- function(pkg) {
104-
any(file.exists(file.path(.libPaths(), pkg)))
104+
system_file(pkg) != ""
105+
}
106+
107+
system_file <- function(pkg) {
108+
# Important for this to be first because packages loaded with pkgload
109+
# will have a different path than the one in `.libPaths()` (if any).
110+
if (isNamespaceLoaded(pkg)) {
111+
return(.getNamespaceInfo(asNamespace(pkg), "path"))
112+
}
113+
114+
for (path in file.path(.libPaths(), pkg)) {
115+
if (file.exists(path)) {
116+
return(path)
117+
}
118+
}
119+
120+
""
105121
}
106122

107123
pkg_version_info <- function(

tests/testthat/helper-package.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local_package <- function(
2+
path = NULL,
3+
name = "templatepackage",
4+
env = caller_env()
5+
) {
6+
if (is.null(path)) {
7+
path <- withr::local_tempdir(.local_envir = env)
8+
desc <- desc::desc("!new")
9+
desc$set("Package", name)
10+
desc$set("Title", "A test template package")
11+
desc$write(file = file.path(path, "DESCRIPTION"))
12+
}
13+
14+
pkgload::load_all(path, quiet = TRUE)
15+
defer(pkgload::unload(name), envir = env)
16+
17+
path
18+
}

tests/testthat/test-session.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,8 @@ test_that("check_installed() and is_installed() support character vectors", {
272272
expect_true(is_installed(chr()))
273273
expect_null(check_installed(chr()))
274274
})
275+
276+
test_that("is_installed() works with pkgload-loaded packages", {
277+
local_package(name = "rlangtestpkg")
278+
expect_true(is_installed("rlangtestpkg"))
279+
})

0 commit comments

Comments
 (0)