Skip to content

Commit 0a22816

Browse files
TESTS: Better, but not perfect, startup and cleanup of each test
1 parent 32088a7 commit 0a22816

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

tests/incl/end.R

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
1-
## Restore original state
2-
options(oopts)
1+
## Undo future strategy
32
future::plan(oplan)
3+
4+
5+
## Undo options
6+
## (a) Added
7+
added <- setdiff(names(options()), names(oopts0))
8+
opts <- vector("list", length = length(added))
9+
names(opts) <- added
10+
options(opts)
11+
## (b) Modified
12+
options(oopts)
13+
## (c) Removed, e.g. future.plan=NULL
14+
removed <- setdiff(names(oopts0), names(options()))
15+
opts <- oopts0[removed]
16+
options(opts)
17+
## (d) Assert that everything was undone
18+
stopifnot(identical(options(), oopts0))
19+
20+
21+
## Undo system environment variables
22+
## (a) Added
23+
cenvs <- Sys.getenv()
24+
added <- setdiff(names(cenvs), names(oenvs0))
25+
for (name in added) Sys.unsetenv(name)
26+
## (b) Missing
27+
missing <- setdiff(names(oenvs0), names(cenvs))
28+
if (length(missing) > 0) do.call(Sys.setenv, as.list(oenvs0[missing]))
29+
## (c) Modified?
30+
for (name in intersect(names(cenvs), names(oenvs0))) {
31+
## WORKAROUND: On Linux Wine, base::Sys.getenv() may
32+
## return elements with empty names. /HB 2016-10-06
33+
if (nchar(name) == 0) next
34+
if (!identical(cenvs[[name]], oenvs0[[name]])) {
35+
do.call(Sys.setenv, as.list(oenvs0[name]))
36+
}
37+
}
38+
## (d) Assert that everything was undone
39+
stopifnot(identical(Sys.getenv(), oenvs0))
40+
41+
42+
## Undo variables
443
rm(list = c(setdiff(ls(), ovars)))
44+
45+
46+
## Travis CI specific: Explicit garbage collection because it
47+
## looks like Travis CI might run out of memory during 'covr'
48+
## testing and we now have so many tests. /HB 2017-01-11
49+
if ("covr" %in% loadedNamespaces()) gc()

tests/incl/start,load-only.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
## Record original state
22
ovars <- ls()
3+
oenvs <- oenvs0 <- Sys.getenv()
4+
oopts0 <- options()
5+
6+
covr_testing <- ("covr" %in% loadedNamespaces())
7+
on_solaris <- grepl("^solaris", R.version$os)
8+
on_macos <- grepl("^darwin", R.version$os)
9+
on_githubactions <- as.logical(Sys.getenv("GITHUB_ACTIONS", "FALSE"))
10+
11+
## Default options
312
oopts <- options(
413
warn = 1L,
514
mc.cores = 2L,
615
future.debug = FALSE,
7-
future.wait.interval = 0.1 ## Speed up await() and delete()
16+
future.wait.interval = 0.1, ## Speed up await() and delete()
17+
## Reset the following during testing in case
18+
## they are set on the test system
19+
future.availableCores.system = NULL,
20+
future.availableCores.fallback = NULL
821
)
922
oopts$future.delete <- getOption("future.delete")
1023
oplan <- future::plan()

0 commit comments

Comments
 (0)