@@ -257,24 +257,19 @@ n_times_faster_with_cache <- function(x1, x2 = x1, ...,
257257 out <- purrr :: map(1 : n , n_times_faster_bench ,
258258 x1 = x1 , x2 = x2 , fun = fun ,
259259 ... , n = n , clear = clear
260- ) %> %
260+ )
261+ out <- out %> %
261262 purrr :: map_dbl(
262263 ~ unname(.x $ first [" elapsed" ] / .x $ second [" elapsed" ])
263264 ) %> %
264265 mean()
265266
266- if (clear %in% c(" always" , " final" )) {
267- clear_testthat_cache()
268- }
269267 out
270268}
271269
272270
273271n_times_faster_bench <- function (i , x1 , x2 , fun , ... , n , clear ) {
274- fresh_testthat_cache()
275- if ((clear == " always" ) || (clear == " all but last" & n != i )) {
276- on.exit(clear_testthat_cache())
277- }
272+ local_test_setup(cache = TRUE )
278273 first <- system.time(fun(x1 , ... ))
279274
280275 if (is.null(x2 )) {
@@ -284,7 +279,8 @@ n_times_faster_bench <- function(i, x1, x2, fun, ..., n, clear) {
284279 }
285280 list (
286281 first = first ,
287- second = second
282+ second = second ,
283+ cache = cache_info(format = " tabular" )
288284 )
289285}
290286
@@ -333,9 +329,34 @@ generate_test_samples <- function() {
333329# ' @include ui-caching.R
334330clear_testthat_cache <- purrr :: partial(cache_clear , " testthat" , ask = FALSE )
335331activate_testthat_cache <- purrr :: partial(cache_activate , " testthat" )
336- fresh_testthat_cache <- function () {
337- clear_testthat_cache()
338- activate_testthat_cache()
332+
333+ # ' Establish testing setup for current environment
334+ # '
335+ # ' @param cache Whether or not to create and activate a cache in a temporary
336+ # ' directory.
337+ # ' @param .local_envir The environment to use for scoping.
338+ # ' @details
339+ # ' * make styler quiet.
340+ local_test_setup <- function (cache = FALSE ,
341+ .local_envir = parent.frame()) {
342+ current_cache <- cache_info(format = " tabular" )
343+ withr :: local_options(
344+ list (" styler.quiet" = TRUE , " R.cache.rootPath" = tempfile()),
345+ .local_envir = .local_envir
346+ )
347+ if (cache ) {
348+ withr :: defer(
349+ {
350+ clear_testthat_cache()
351+ cache_activate(basename(current_cache $ location ))
352+ if (! current_cache $ activated ) {
353+ cache_deactivate()
354+ }
355+ },
356+ envir = .local_envir
357+ )
358+ activate_testthat_cache()
359+ }
339360}
340361
341362cache_more_specs_default <- function () {
0 commit comments