Debian Codegraph @

tests/testthat/test-scope.R

raw
1
2 test_that(".libPaths() scoping works as expected", {
3
4 libpaths <- .libPaths()
5
6 local({
7 dir <- renv_path_normalize(tempdir())
8 renv_scope_libpaths(dir)
9 expect_true(.libPaths()[1] == dir)
10 })
11
12 expect_true(.libPaths()[1] == libpaths[1])
13
14 })
15
16 test_that("options() scoping works as expected", {
17
18 opts <- list(
19 download.file.method = "curl",
20 download.file.extra = NULL
21 )
22
23 old <- options("download.file.method", "download.file.extra")
24 local({
25 do.call(renv_scope_options, opts)
26 expect_equal(opts, options("download.file.method", "download.file.extra"))
27 })
28
29 expect_equal(old, options("download.file.method", "download.file.extra"))
30
31 })
32
33 test_that("environment variable scoping works as expected", {
34 renv_scope_envvars("RENV_TEST_ENVVAR_B" = "0")
35
36 # set and later unset a variable
37 local({
38 renv_scope_envvars("RENV_TEST_ENVVAR_A" = "1")
39 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_A"), "1")
40 })
41 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_A", unset = NA), NA_character_)
42
43 # override and restore variables
44 local({
45 renv_scope_envvars("RENV_TEST_ENVVAR_A" = "1", "RENV_TEST_ENVVAR_B" = "2")
46 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_A"), "1")
47 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_B"), "2")
48 })
49 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_B"), "0")
50 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_A", unset = NA), NA_character_)
51
52 # unset and reset variable
53 local({
54 renv_scope_envvars("RENV_TEST_ENVVAR_B" = NULL)
55 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_B", unset = NA), NA_character_)
56 })
57 expect_identical(Sys.getenv("RENV_TEST_ENVVAR_B"), "0")
58
59 })
60
61 test_that("nested attempts to scope libpaths are properly handled", {
62
63 libpaths <- .libPaths()
64
65 local({
66 dir <- renv_path_normalize(tempdir())
67 renv_scope_libpaths(dir)
68 renv_scope_libpaths(dir)
69 expect_true(.libPaths()[1] == dir)
70 })
71
72 expect_true(.libPaths()[1] == libpaths[1])
73
74 })
75
76 test_that("renv_scope_trace works", {
77
78 count <- 0
79
80 local({
81 renv_scope_trace(what = identity, tracer = function() count <<- count + 1)
82 identity(1)
83 })
84
85 identity(1)
86
87 expect_equal(count, 1L)
88
89 })
90
91 test_that("can temporarily replace binding", {
92 envir <- environment()
93 x <- 10
94
95 local({
96 renv_scope_binding(envir, "x", 20)
97 expect_equal(x, 20)
98 })
99
100 expect_equal(x, 10)
101 })
102
103 test_that("can temporarily create binding", {
104 envir <- environment()
105
106 local({
107 renv_scope_binding(envir, "x", 20)
108 expect_equal(x, 20)
109 })
110
111 expect_false(exists("x", inherits = FALSE))
112 })
113
114 test_that("can temporarily replace locked binding", {
115 original <- utils::adist
116
117 utils <- asNamespace("utils")
118 local({
119 renv_scope_binding(utils, "adist", 20)
120 expect_equal(utils::adist, 20)
121 })
122
123 expect_equal(utils::adist, original)
124 })