Debian Codegraph @

tests/testthat/test-with.R

raw
1 test_that("with_options works", {
2 expect_false(identical(getOption("scipen"), 999))
3 expect_equal(with_options(c(scipen=999), getOption("scipen")), 999)
4 expect_false(identical(getOption("scipen"), 999))
5
6 expect_false(identical(getOption("zyxxyzyx"), "qwrbbl"))
7 expect_equal(with_options(c(zyxxyzyx="qwrbbl"), getOption("zyxxyzyx")), "qwrbbl")
8 expect_false(identical(getOption("zyxxyzyx"), "qwrbbl"))
9 })
10
11 test_that("with_libpaths works and resets library", {
12 lib <- .libPaths()
13 new_lib <- "."
14 with_libpaths(
15 new_lib,
16 {
17 expect_equal(normalizePath(new_lib), normalizePath(.libPaths()[[1L]]))
18 }
19 )
20 expect_equal(lib, .libPaths())
21 })
22
23 test_that("with_temp_libpaths works and resets library", {
24 lib <- .libPaths()
25 with_temp_libpaths(
26 expect_equal(.libPaths()[-1], lib)
27 )
28 expect_equal(lib, .libPaths())
29 })
30
31 test_that("with_temp_libpaths has an action argument", {
32 lib <- .libPaths()
33 with_temp_libpaths(
34 action = "suffix",
35 expect_equal(.libPaths()[-length(.libPaths())], lib)
36 )
37 expect_equal(lib, .libPaths())
38 })
39
40 test_that("with_ works", {
41 res <- NULL
42 set <- function(new) {
43 res <<- c(res, 1L)
44 }
45 reset <- function(old) {
46 res <<- c(res, 3L)
47 }
48 with_res <- with_(set, reset)
49 with_res(NULL, res <- c(res, 2L))
50 expect_equal(res, 1L:3L)
51 })
52
53 test_that("with_ works on functions without arguments", {
54 res <- NULL
55 set <- function() {
56 res <<- c(res, 1L)
57 }
58 reset <- function(x) {
59 res <<- c(res, 3L)
60 }
61 with_res <- with_(set, reset)
62 with_res(res <- c(res, 2L))
63 expect_equal(res, 1L:3L)
64 })
65
66 test_that("with_libpaths works and resets library", {
67 lib <- .libPaths()
68 new_lib <- "."
69 with_libpaths(
70 new_lib,
71 {
72 expect_equal(normalizePath(new_lib), normalizePath(.libPaths()[[1L]]))
73 }
74 )
75 expect_equal(lib, .libPaths())
76 })
77
78 test_that("with_makevars works and resets the Makevars file", {
79 current <- tempfile()
80 writeLines(con = current, c("CFLAGS=-O3"), sep = "\n")
81 new <- c(CFLAGS = "-O0")
82 with_makevars(
83 new, path = current,
84 {
85 expect_equal("CFLAGS=-O0", readLines(Sys.getenv("R_MAKEVARS_USER")))
86 }
87 )
88 expect_equal("CFLAGS=-O3", readLines(current))
89 })
90
91 test_that("with_makevars changes only the defined variables", {
92 current_name <- tempfile()
93 current <- c("CFLAGS=-O3", "LDFLAGS=-lz")
94 writeLines(con = current_name, current, sep = "\n")
95 new <- c(CFLAGS = "-O0")
96 with_makevars(
97 new, path = current_name,
98 {
99 expect_equal(c("CFLAGS=-O0", "LDFLAGS=-lz"), readLines(Sys.getenv("R_MAKEVARS_USER")))
100 }
101 )
102 expect_equal(current, readLines(current_name))
103 })
104
105 test_that("with_makevars works with alternative assignments", {
106 current <- tempfile()
107 writeLines(con = current, c("CFLAGS=-O3"), sep = "\n")
108 new <- c(CFLAGS = "-O0")
109 with_makevars(
110 new, path = current, assignment = "+=",
111 {
112 expect_equal("CFLAGS+=-O0", readLines(Sys.getenv("R_MAKEVARS_USER")))
113 }
114 )
115 expect_equal("CFLAGS=-O3", readLines(current))
116 })
117
118 test_that("with_makevars uses the existing R_MAKEVARS_USER by default", {
119 tf <- tempfile()
120 local_envvar("R_MAKEVARS_USER" = tf)
121 on.exit(unlink(tf))
122 writeLines(con = tf, c("CFLAGS=-O3", "CXXFLAGS=-O3"), sep = "\n")
123 new <- c(CFLAGS = "-O0")
124 with_makevars(
125 new,
126 {
127 expect_equal(readLines(Sys.getenv("R_MAKEVARS_USER")), c("CFLAGS=-O0", "CXXFLAGS=-O3"))
128 }
129 )
130 expect_equal(readLines(tf), c("CFLAGS=-O3", "CXXFLAGS=-O3"))
131 })
132
133 test_that("set_makevars works as expected", {
134 expect_equal(set_makevars(character(0)), NULL)
135
136 tmp_old <- tempfile()
137 tmp_new <- tempfile()
138
139 # empty old file
140 set_makevars(c(CFLAGS = "-O3"), tmp_old, tmp_new)
141 expect_equal(readLines(tmp_new), c("CFLAGS=-O3"))
142
143 # non-empty old file without new field
144 writeLines(con=tmp_old, c("LDFLAGS=-lz"))
145 set_makevars(c(CFLAGS = "-O3"), tmp_old, tmp_new)
146 expect_equal(readLines(tmp_new), c("LDFLAGS=-lz", "CFLAGS=-O3"))
147
148 # non-empty old file without multiple field definitions (error)
149 writeLines(con=tmp_old, c("CFLAGS=-O0", "CFLAGS=-O1"))
150 expect_error(set_makevars(c(CFLAGS = "-O3"), tmp_old, tmp_new))
151
152 unlink(tmp_old)
153 unlink(tmp_new)
154 })
155
156 test_that("with_dir works as expected", {
157 old <- normalizePath(getwd())
158 with_dir("..", {
159 expect_equal(normalizePath(getwd()), normalizePath(file.path(old, "..")))
160 })
161 expect_equal(normalizePath(getwd()), normalizePath(old))
162 })
163
164 test_that("supplying a getter to `with_()` shields against early exits", {
165 my_get <- function(x) {
166 out <- as.list(state)[names(x)]
167 names(out) <- names(x)
168 out
169 }
170 my_set <- function(x) {
171 old <- my_get(x)
172
173 mapply(function(nm, val) state[[nm]] <- val, names(x), x)
174 rlang::signal("", "my_cnd")
175
176 invisible(old)
177 }
178
179 state <- new.env()
180 my_with_unsafe <- withr::with_(my_set)
181 my_with_safe <- withr::with_(my_set, get = my_get)
182
183 expect_safe_and_unsafe_unwinding(
184 state,
185 my_with_unsafe,
186 my_with_safe
187 )
188 })