Skip to content

Commit

Permalink
Merge pull request #107 from ropensci/phantom_load_bugfix
Browse files Browse the repository at this point in the history
Fix bugs involving roxygen2::roxygenise() attaching the target package to the search path
  • Loading branch information
slager authored Apr 12, 2024
2 parents edbcc81 + 5da55c9 commit 78dbd04
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
13 changes: 8 additions & 5 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@ package_build <- function(packageName = NULL,
.multilog_warn("DataPackageR failed")
)
.multilog_trace("Building documentation")
roxygen2::roxygenise(package_path,
clean = TRUE
)

local({
on.exit({
if (packageName %in% devtools::package_info('attached')$package){
devtools::unload(packageName)
}
})
roxygen2::roxygenise(package_path, clean = TRUE)
})
.multilog_trace("Building package")
location <- build(package_path,
path = dirname(package_path),
vignettes = vignettes
)
# try to install and then reload the package in the current session
if (install) {
devtools::unload(packageName)
install.packages(location, repos = NULL, type = "source", ...)
}
.next_steps()
Expand Down
10 changes: 8 additions & 2 deletions R/processData.R
Original file line number Diff line number Diff line change
Expand Up @@ -757,14 +757,20 @@ document <- function(path = ".", install = TRUE, ...) {
overwrite = TRUE
)
.multilog_trace("Rebuilding data package documentation.")
devtools::document(pkg = path)
local({
on.exit({
if (basename(path) %in% devtools::package_info('attached')$package){
devtools::unload(basename(path))
}
})
devtools::document(pkg = path)
})
location <- devtools::build(
pkg = path, path = dirname(path),
vignettes = FALSE, quiet = TRUE
)
# try to install and then reload the package in the current session
if (install) {
devtools::unload(basename(path))
install.packages(location, repos = NULL, type = "source", ...)
}
return(TRUE)
Expand Down
6 changes: 4 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@ datapackage_skeleton(
# If the build is run in non-interactive mode, the description will read
# "Package built in non-interactive mode". You may update it later.
dir.create(file.path(tempdir(),"lib"))
package_build(packageName = file.path(tempdir(),"mtcars20"), install = TRUE, lib = file.path(tempdir(),"lib"))
package_build(packageName = file.path(tempdir(),"mtcars20"), install = FALSE,
lib = file.path(tempdir(),"lib"))
# Update the autogenerated roxygen documentation in data-raw/documentation.R.
# edit(file.path(tempdir(),"mtcars20","R","mtcars20.R"))
# 4. Rebuild the documentation.
document(file.path(tempdir(),"mtcars20"), install = TRUE, lib = file.path(tempdir(),"lib"))
document(file.path(tempdir(),"mtcars20"), install = FALSE,
lib = file.path(tempdir(),"lib"))
# Let's use the package we just created.
install.packages(file.path(tempdir(),"mtcars20_1.0.tar.gz"), type = "source", repos = NULL)
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,18 @@ datapackage_skeleton(
# If the build is run in non-interactive mode, the description will read
# "Package built in non-interactive mode". You may update it later.
dir.create(file.path(tempdir(),"lib"))
package_build(packageName = file.path(tempdir(),"mtcars20"), install = TRUE, lib = file.path(tempdir(),"lib"))
#> Warning: package 'mtcars20' is in use and will not be installed
package_build(packageName = file.path(tempdir(),"mtcars20"), install = FALSE,
lib = file.path(tempdir(),"lib"))

# Update the autogenerated roxygen documentation in data-raw/documentation.R.
# edit(file.path(tempdir(),"mtcars20","R","mtcars20.R"))

# 4. Rebuild the documentation.
document(file.path(tempdir(),"mtcars20"), install = TRUE, lib = file.path(tempdir(),"lib"))
#> Warning: package 'mtcars20' is in use and will not be installed
document(file.path(tempdir(),"mtcars20"), install = FALSE,
lib = file.path(tempdir(),"lib"))

# Let's use the package we just created.
install.packages(file.path(tempdir(),"mtcars20_1.0.tar.gz"), type = "source", repos = NULL)
#> Warning: package 'mtcars20' is in use and will not be installed
library(mtcars20)
data("cars_over_20") # load the data
cars_over_20 # Now we can use it.
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-phantom_loading.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
testthat::test_that(
"no phantom package loading from roxygenise() or associated warnings",
{
# test README.Rmd sequence that led to warnings
processing_code <- system.file(
"extdata", "tests", "subsetCars.Rmd", package = "DataPackageR"
)
pkg_name <- "mtcars20"
on.exit(
if (pkg_name %in% devtools::package_info('attached')$package){
devtools::unload(pkg_name)
}
)
# remove this directory on exit
temp_dir <- withr::local_tempdir()
pkg_path <- file.path(temp_dir, pkg_name)

datapackage_skeleton(
pkg_name, force = TRUE,
code_files = processing_code,
r_object_names = "cars_over_20",
path = temp_dir)

expect_no_warning(package_build(pkg_path, install = FALSE))
# test phantom pkg loading side effect from roxygen2::roxygenise()
expect_false(
res1 <- pkg_name %in% devtools::package_info('attached')$package
)

# reset for next test
if (res1) devtools::unload(pkg_name)

# test phantom pkg loading side effect from roxygen2::roxygenise()
# which is called by devtools::document()
expect_no_warning(document(pkg_path, install = FALSE))
expect_false(
res2 <- pkg_name %in% devtools::package_info('attached')$package
)
}
)

0 comments on commit 78dbd04

Please sign in to comment.