Skip to content

Commit

Permalink
Dynamically bind shims
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 29, 2019
1 parent 9f3a92d commit 7fde6d0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
5 changes: 1 addition & 4 deletions R/conflicts.R
Expand Up @@ -5,10 +5,7 @@ conflicts_register <- function(pkgs = pkgs_attached()) {
map2(names(conflicts), conflicts, conflict_disambiguate, env = env)

# Shim library() and require() so we can rebuild
env_bind(env,
library = shim_library,
require = shim_require
)
shims_bind(env)

env
}
Expand Down
44 changes: 27 additions & 17 deletions R/shim.R
@@ -1,13 +1,23 @@
shim_library <- function(package,
help,
pos = 2,
lib.loc = NULL,
character.only = FALSE,
logical.return = FALSE,
warn.conflicts = TRUE,
quietly = FALSE,
verbose = getOption("verbose")
) {
shims_bind <- function(env = caller_env()) {
env_bind(env,
library = shim_library_3_1,
require = shim_require_3_1
)
}

# library -----------------------------------------------------------------


shim_library_3_1 <- function(package,
help,
pos = 2,
lib.loc = NULL,
character.only = FALSE,
logical.return = FALSE,
warn.conflicts = TRUE,
quietly = FALSE,
verbose = getOption("verbose")
) {

if (!missing(package)) {
package <- package_name(enquo(package), character.only = character.only)
Expand All @@ -26,7 +36,6 @@ shim_library <- function(package,
quietly = quietly,
verbose = verbose
)

} else if (!missing(help)) {
help <- package_name(enquo(help), character.only = character.only)
library(
Expand All @@ -39,14 +48,15 @@ shim_library <- function(package,
logical.return = logical.return
)
}

}

shim_require <- function(package,
lib.loc = NULL,
quietly = FALSE,
warn.conflicts = TRUE,
character.only = FALSE) {
# require -----------------------------------------------------------------

shim_require_3_1 <- function(package,
lib.loc = NULL,
quietly = FALSE,
warn.conflicts = TRUE,
character.only = FALSE) {

package <- package_name(enquo(package), character.only = character.only)

Expand Down
28 changes: 16 additions & 12 deletions tests/testthat/test-shim.R
@@ -1,25 +1,28 @@
context("test-shim.R")

test_that("shimmed arguments match unshimmed", {
expect_equal(formals(shim_require), formals(base::require))
expect_equal(formals(shim_library), formals(base::library))
shims_bind()
expect_equal(formals(require), formals(base::require))
expect_equal(formals(library), formals(base::library))
})

test_that("shims load package with conflicts silently", {
red <- function() {}
shims_bind()

expect_message(shim_library(crayon), NA)
expect_message(library(crayon), NA)
detach("package:crayon")

expect_message(shim_require(crayon, quietly = TRUE), NA)
expect_message(require(crayon, quietly = TRUE), NA)
detach("package:crayon")
})

test_that("detaching package removes shims", {
skip_if_not("chr" %in% pkg_ls("crayon") && "chr" %in% pkg_ls("rlang"))
shims_bind()

shim_library(crayon)
shim_library(rlang)
library(crayon)
library(rlang)
expect_true(exists("chr", ".conflicts", inherits = FALSE))

detach("package:crayon")
Expand All @@ -28,22 +31,23 @@ test_that("detaching package removes shims", {
})

test_that("shimmed help returns same as unshimmed", {
shims_bind()

expect_equal(
shim_library(help = "rlang"),
library(help = "rlang"),
base::library(help = "rlang")
)

expect_equal(
shim_library(help = rlang),
library(help = rlang),
base::library(help = rlang)
)
})

test_that("shimmed library() returns same as unshimmed", {
expect_equal(
shim_library(),
base::library()
)
shims_bind()

expect_equal(library(), base::library())
})

# package_name ------------------------------------------------------------
Expand Down

0 comments on commit 7fde6d0

Please sign in to comment.