Skip to content

Commit

Permalink
Discourage use of .package (#1865)
Browse files Browse the repository at this point in the history
Fixes #1862
  • Loading branch information
hadley committed Sep 25, 2023
1 parent fe01a63 commit 89107e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
28 changes: 10 additions & 18 deletions R/mock2.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,12 @@
#' }
#' ```
#'
#' To mock here, you'll need to modify `another_function()` inside the
#' To mock this function, you'd need to modify `another_function()` inside the
#' `anotherpackage` package. You _can_ do this by supplying the `.package`
#' argument:
#'
#' ```R
#' local_mocked_bindings(
#' another_function = function(...) "new_value",
#' .package = "anotherpackage"
#' )
#' ```
#'
#' But it's not a great idea to mock a namespace that you don't own because
#' it affects all code in that package, not just code in your package. Instead,
#' it's safer to either import the function into your package, or make a wrapper
#' that you can mock:
#' argument to `local_mocked_bindings()` but we don't recommend it because
#' it will affect all calls to `anotherpackage::another_function()`, not just
#' the calls originating in your package. Instead, it's safer to either import
#' the function into your package, or make a wrapper that you can mock:
#'
#' ```R
#' some_function <- function() {
Expand All @@ -108,10 +99,11 @@
#' @param code Code to execute with specified bindings.
#' @param .env Environment that defines effect scope. For expert use only.
#' @param .package The name of the package where mocked functions should be
#' inserted. Generally, you should not need to supply this as it will be
#' automatically detected when whole package tests are run or when there's
#' one package under active development (i.e. loaded with
#' [pkgload::load_all()]).
#' inserted. Generally, you should not supply this as it will be automatically
#' detected when whole package tests are run or when there's one package
#' under active development (i.e. loaded with [pkgload::load_all()]).
#' We don't recommend using this to mock functions in other packages,
#' as you should not modify namespaces that you don't own.
local_mocked_bindings <- function(..., .package = NULL, .env = caller_env()) {
bindings <- list2(...)
check_bindings(bindings)
Expand Down
27 changes: 10 additions & 17 deletions man/local_mocked_bindings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 89107e8

Please sign in to comment.