Skip to content

Commit

Permalink
Remove globally() in favor of explicit setting of the function env
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Apr 28, 2022
1 parent 3e15629 commit 6c62494
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
5 changes: 0 additions & 5 deletions tests/testthat/helper-eval.R

This file was deleted.

11 changes: 6 additions & 5 deletions tests/testthat/test-furrr-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ test_that("can export globals with sequential futures", {
fn <- function(x) {
exists("y")
}
environment(fn) <- .GlobalEnv

# With named list
opts <- furrr_options(globals = list(y = 1))
expect_identical(future_map_lgl(1:2, fn, .options = opts), c(TRUE, TRUE))

wrapper <- function(options = furrr_options()) {
# With character lookup in caller env
wrapper <- function(fn) {
y <- 1
future_map_lgl(1, fn, .options = options)
future_map_lgl(1, fn, .options = furrr_options(globals = "y"))
}
environment(wrapper) <- .GlobalEnv

# With character lookup in caller env
opts <- furrr_options(globals = "y")
expect_identical(wrapper(opts), TRUE)
expect_identical(wrapper(fn), TRUE)
})

test_that("validates `globals`", {
Expand Down
28 changes: 13 additions & 15 deletions tests/testthat/test-future-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,21 @@ furrr_test_that("`.f` globals are only looked up in the function env of `.f` (#1
})

furrr_test_that("`...` globals/packages are found", {
fn <- globally({
function(x, fn_arg) {
fn_arg()
}
})
# We set the function environments to the global environment to ensure
# that they aren't set to something else while `test()` is running

# Function is passed through `...`
# Evaluate in the global env so rlang isn't captured in the function env
# as a package to load
fn_arg <- globally({
# `rlang` needs to be loaded on the worker!
expr <- rlang::expr
fn <- function(x, fn_arg) {
fn_arg()
}
environment(fn) <- .GlobalEnv

function() {
expr(1)
}
})
fn_arg_env <- new_environment(list(x = 1), parent = .GlobalEnv)

# This function is passed through `...`
fn_arg <- function() {
x
}
environment(fn_arg) <- fn_arg_env

expect_identical(
future_map(1:2, fn, fn_arg = fn_arg),
Expand Down

0 comments on commit 6c62494

Please sign in to comment.