Skip to content

Commit

Permalink
add test helpers for async
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Dec 8, 2021
1 parent cd2c6fd commit 315b2fa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ Suggests:
htmltools,
knitr,
pkgdown,
downlit
downlit,
remotes,
usethis
URL: https://subugoe.github.io/metacheck, https://github.com/subugoe/metacheck
BugReports: https://github.com/subugoe/metacheck/issues
Remotes:
Expand Down
43 changes: 43 additions & 0 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,49 @@
#' @noRd
throwaway <- "[email protected]"

#' Install metacheck in temp library path
#' Necessary for any testing in testthat with [future::multisession()]
#' @noRd
local_mc <- function(env = parent.frame()) {
withr::local_temp_libpaths(.local_envir = env)
remotes::install_local(
path = usethis::proj_get(),
dependencies = FALSE,
upgrade = FALSE,
force = TRUE
)
}

#' Temporarily set future plan
#' @inheritParams future::strategy
#' @noRd
local_strategy <- function(strategy, env = parent.frame()) {
old_plan <- future::plan(strategy = strategy)
withr::defer(future::plan(old_plan), env = env)
}

#' Future strategies to be tested
#' @noRd
strategies <- list(
sequential = future::sequential,
multicore = future::multicore,
multisession = future::multisession
)

#' Skipping unsupported strategies
skip_if_strategy_unsupported <- function(strategy_name = names(strategies)) {
strategy_name <- rlang::arg_match(strategy_name)
if (strategy_name == "multicore" && !future::supportsMulticore()) {
return(testthat::skip("Multicore is not supported"))
}
if (strategy_name == "multisession" && testthat::is_parallel()) {
return(testthat::skip(
"Multisession futures cannot be tested inside parallel tests."
))
}
invisible(TRUE)
}

#' used often b/c plain json doesn't cover complex objects
#' @noRd
expect_snapshot_value2 <- purrr::partial(
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# multisession futures need locally installed metacheck
local_mc(env = testthat::teardown_env())

0 comments on commit 315b2fa

Please sign in to comment.