Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign .Depends to package environment to mimic more closely to library() #61

Merged
merged 6 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# pkgload 0.0.0.9000

* `load_all()` now assigns `DESCRIPTION/Depends` to `.Depends` object of
package environment. (@yiufung pkgload#61)

* `load_all()` now attaches `testthat` if the `attach_testthat` option is
`TRUE`. This allows `load_all()` to more closely mimics the testing
environment. (#56)
Expand Down
3 changes: 3 additions & 0 deletions R/load.r
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ load_all <- function(path = ".", reset = TRUE, recompile = FALSE,
# Copy over objects from the namespace environment
export_ns(package)

# Assign .Depends, if any, to package environment from namespace
assign_depends(package)

# Run hooks
run_pkg_hook(package, "attach")
run_user_hook(package, "attach")
Expand Down
10 changes: 10 additions & 0 deletions R/package-env.r
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export_ns <- function(package) {
}
}

# Assign `.Depends` from namespace
assign_depends <- function(package) {
pkgenv <- pkg_env(package)

desc <- pkg_desc(ns_path(package))
deps <- desc$get_deps()
depends <- unique(deps[deps$type == "Depends"
& deps$package != "R",]$package)
if (length(depends) > 0L) pkgenv$.Depends <- depends
}

#' Return package environment
#'
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-depend.r
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ test_that("Parse dependencies", {
expect_equal(deps$compare, c("<", "=="))
expect_equal(deps$version, c("2.1", "3.0.1"))
})

test_that("Declared dependencies are added to .Depends object", {
load_all("testDependsExists")
expect_equal(get(".Depends", "package:testDependsExists", inherits = FALSE),
"httr")
unload("testDependsExists")
})
8 changes: 8 additions & 0 deletions tests/testthat/testDependsExists/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Package: testDependsExists
Title: Packages in Depends are assigned to .Depends object of package environment
License: GPL-2
Description:
Author: Yiufung <cheongyiufung@gmail.com>
Maintainer: Yiufung <cheongyiufung@gmail.com>
Version: 0.1
Depends: R, httr