@@ -699,9 +699,14 @@ run_test_file <- function( file
699699 }
700700 prfile <- paste(" Running" ,gsub(" " ," ." ,sprintf(" %-30s" ,basename(file ))))
701701
702- # internal use only: load pkg if passed by run_test_dir
702+ # load pkg if passed by run_test_dir and not already loaded.
703703 L <- list (... )
704- if (! is.null(L $ pkg )) require(L $ pkg , character.only = TRUE , quiet = TRUE )
704+ needs_pkg <- ! is.null(L $ pkg )
705+ pkgs_loaded_before <- .packages()
706+ if ( needs_pkg && ! (L $ pkg %in% pkgs_loaded_before ) ){
707+ require(L $ pkg , character.only = TRUE , quiet = TRUE )
708+ }
709+
705710 # evaluate expressions one by one
706711 for ( i in seq_along(parsed ) ){
707712 expr <- parsed [[i ]]
@@ -715,6 +720,16 @@ run_test_file <- function( file
715720 if (verbose == 1 ) print_status(prfile , o , color )
716721 if (verbose > = 1 ) catf(" \n " )
717722
723+ # clean up side effects: unload all pkgs loaded in this function
724+ # plus all pkgs that came with it (e.g. via 'depends', or those
725+ # loaded while running the test file)
726+ if (needs_pkg && ! (L $ pkg %in% pkgs_loaded_before )){
727+ pkgs_to_unload <- setdiff(.packages(), pkgs_loaded_before )
728+ for (pkg in pkgs_to_unload ){
729+ detach(paste0(" package:" ,pkg ), unload = TRUE , character.only = TRUE )
730+ }
731+ }
732+
718733 # returns a 'list' of 'tinytest' objects
719734 test_output <- o $ gimme()
720735 structure(test_output , class = " tinytests" )
@@ -821,9 +836,11 @@ run_test_dir <- function(dir="inst/tinytest", pattern="^test.*\\.[rR]"
821836 , remove_side_effects = remove_side_effects
822837 , ... )
823838 } else {
824- test_output <- parallel :: mclapply(testfiles
825- , run_test_file , at_home = at_home , verbose = min(verbose ,1 )
826- , color = color , remove_side_effects = TRUE , ... )
839+ cl <- parallel :: makeCluster(ncpu , outfile = " " )
840+ test_output <- parallel :: parLapply(cl , testfiles
841+ , run_test_file , at_home = at_home , verbose = min(verbose ,1 )
842+ , color = color , remove_side_effects = TRUE , ... )
843+ parallel :: stopCluster(cl )
827844 }
828845 # by using '(mc)lapply' we get a list of tinytests objects. We need to unwind
829846 # one level to a list of 'tinytest' objects and class it 'tinytests'.
0 commit comments