| 1 |
|
| 2 |
test_that("case() handles control flow as expected", { |
| 3 |
|
| 4 |
for (i in 1:3) |
| 5 |
case(i == 2 ~ break) |
| 6 |
|
| 7 |
expect_equal(i, 2) |
| 8 |
|
| 9 |
value <- local({ |
| 10 |
case(FALSE ~ stop(), TRUE ~ return(42)) |
| 11 |
24 |
| 12 |
}) |
| 13 |
|
| 14 |
expect_equal(value, 42) |
| 15 |
|
| 16 |
}) |
| 17 |
|
| 18 |
test_that("common utils work as expected", { |
| 19 |
expect_equal(lines(1, 2, 3), "1\n2\n3") |
| 20 |
|
| 21 |
if (nzchar(Sys.which("git"))) |
| 22 |
expect_equal(git(), Sys.which("git")) |
| 23 |
else |
| 24 |
expect_error(git()) |
| 25 |
}) |
| 26 |
|
| 27 |
test_that("versions are compared as expected", { |
| 28 |
|
| 29 |
expect_equal(renv_version_compare("0.1.0", "0.2.0"), -1L) |
| 30 |
expect_equal(renv_version_compare("0.2.0", "0.2.0"), +0L) |
| 31 |
expect_equal(renv_version_compare("0.3.0", "0.2.0"), +1L) |
| 32 |
|
| 33 |
}) |
| 34 |
|
| 35 |
test_that("insert inserts text at expected anchor point", { |
| 36 |
|
| 37 |
text <- c("alpha", "beta", "gamma") |
| 38 |
|
| 39 |
inserted <- insert(text, "beta", "BETA") |
| 40 |
expect_equal(inserted, c("alpha", "BETA", "gamma")) |
| 41 |
|
| 42 |
inserted <- insert(text, "BETA", "BETA", "beta") |
| 43 |
expect_equal(inserted, c("alpha", "beta", "BETA", "gamma")) |
| 44 |
|
| 45 |
}) |
| 46 |
|
| 47 |
test_that("renv_path_aliased() correctly forms aliased path", { |
| 48 |
path <- "~/some/path" |
| 49 |
expanded <- path.expand(path) |
| 50 |
expect_equal(path, renv_path_aliased(expanded)) |
| 51 |
}) |
| 52 |
|
| 53 |
test_that("find() returns first non-null matching value", { |
| 54 |
|
| 55 |
data <- list(x = 1, y = 2, z = 3) |
| 56 |
|
| 57 |
value <- find(data, function(datum) { |
| 58 |
if (datum == 2) |
| 59 |
return(42) |
| 60 |
}) |
| 61 |
expect_equal(value, 42) |
| 62 |
|
| 63 |
value <- find(data, function(datum) { |
| 64 |
if (datum == 4) |
| 65 |
return(42) |
| 66 |
}) |
| 67 |
expect_null(value) |
| 68 |
|
| 69 |
}) |
| 70 |
|
| 71 |
test_that("recursing() reports if we're recursing", { |
| 72 |
|
| 73 |
f <- function(i) { |
| 74 |
|
| 75 |
if (recursing()) |
| 76 |
expect_true(i == 2) |
| 77 |
else |
| 78 |
expect_true(i == 1) |
| 79 |
|
| 80 |
|
| 81 |
if (i < 2) |
| 82 |
f(i + 1) |
| 83 |
|
| 84 |
if (recursing()) |
| 85 |
expect_true(i == 2) |
| 86 |
else |
| 87 |
expect_true(i == 1) |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
f(1) |
| 92 |
|
| 93 |
|
| 94 |
}) |
| 95 |
|
| 96 |
test_that("sys.call(sys.parent()) does what we think it does", { |
| 97 |
|
| 98 |
inner <- function() { as.character(sys.call(sys.parent())[[1L]]) } |
| 99 |
outer <- function() { inner() } |
| 100 |
expect_equal(outer(), "outer") |
| 101 |
|
| 102 |
}) |
| 103 |
|
| 104 |
test_that("new() creates objects", { |
| 105 |
|
| 106 |
oop <- new({ |
| 107 |
.data <- NULL |
| 108 |
get <- function() { .data } |
| 109 |
set <- function(data) { .data <<- data } |
| 110 |
}) |
| 111 |
|
| 112 |
expect_identical(oop$get(), NULL) |
| 113 |
expect_identical(oop$set(42L), 42L) |
| 114 |
expect_identical(oop$get(), 42L) |
| 115 |
|
| 116 |
expect_null(oop$data) |
| 117 |
|
| 118 |
data <- get(".data", envir = oop) |
| 119 |
expect_equal(data, 42L) |
| 120 |
|
| 121 |
}) |
| 122 |
|
| 123 |
test_that("rows(), cols() does what we want", { |
| 124 |
|
| 125 |
indices <- list( |
| 126 |
c(1, 3, 5), |
| 127 |
c(TRUE, FALSE) |
| 128 |
) |
| 129 |
|
| 130 |
for (index in indices) { |
| 131 |
|
| 132 |
lhs <- mtcars[index, ] |
| 133 |
rhs <- rows(mtcars, index) |
| 134 |
rownames(lhs) <- rownames(rhs) <- NULL |
| 135 |
expect_equal(lhs, rhs) |
| 136 |
|
| 137 |
lhs <- mtcars[index] |
| 138 |
rhs <- cols(mtcars, index) |
| 139 |
expect_equal(lhs, rhs) |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
}) |
| 144 |
|
| 145 |
test_that("visited() works as expected", { |
| 146 |
|
| 147 |
envir <- new.env(parent = emptyenv()) |
| 148 |
expect_false(visited("hello", envir)) |
| 149 |
expect_true(visited("hello", envir)) |
| 150 |
expect_true(visited("hello", envir)) |
| 151 |
|
| 152 |
envir$hello <- FALSE |
| 153 |
expect_false(visited("hello", envir)) |
| 154 |
expect_true(visited("hello", envir)) |
| 155 |
|
| 156 |
}) |
| 157 |
|
| 158 |
test_that("ensure_directory() works even under contention", { |
| 159 |
skip_on_cran() |
| 160 |
skip_if(getRversion() < "4.0.0") |
| 161 |
|
| 162 |
n <- 4 |
| 163 |
waitfile <- tempfile("renv-wait-") |
| 164 |
target <- tempfile("renv-directory-") |
| 165 |
|
| 166 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 167 |
defer(close(server$socket)) |
| 168 |
|
| 169 |
script <- renv_test_code({ |
| 170 |
|
| 171 |
renv:::summon() |
| 172 |
|
| 173 |
# create the directory |
| 174 |
wait_until(file.exists, waitfile) |
| 175 |
ok <- tryCatch( |
| 176 |
{ ensure_directory(target); TRUE }, |
| 177 |
error = function(e) FALSE |
| 178 |
) |
| 179 |
|
| 180 |
# notify parent |
| 181 |
conn <- renv_socket_connect(port = port, open = "wb") |
| 182 |
defer(close(conn)) |
| 183 |
serialize(ok, connection = conn) |
| 184 |
|
| 185 |
}, list(port = server$port, waitfile = waitfile, target = target)) |
| 186 |
|
| 187 |
for (i in 1:n) { |
| 188 |
system2( |
| 189 |
command = R(), |
| 190 |
args = c("--vanilla", "--slave", "-f", renv_shell_path(script)), |
| 191 |
stdout = FALSE, |
| 192 |
stderr = FALSE, |
| 193 |
wait = FALSE |
| 194 |
) |
| 195 |
} |
| 196 |
|
| 197 |
file.create(waitfile) |
| 198 |
|
| 199 |
responses <- stack() |
| 200 |
for (i in 1:n) local({ |
| 201 |
conn <- renv_socket_accept(server$socket, open = "rb", timeout = 3) |
| 202 |
defer(close(conn)) |
| 203 |
responses$push(unserialize(conn)) |
| 204 |
}) |
| 205 |
|
| 206 |
expect_true(all(unlist(responses$data()))) |
| 207 |
|
| 208 |
}) |
| 209 |
|
| 210 |
test_that("warnify() propagates errors as warnings", { |
| 211 |
|
| 212 |
# here's why we don't use 'error = warning' |
| 213 |
result <- tryCatch( |
| 214 |
tryCatch(stop("ouch"), error = warning), |
| 215 |
error = function(e) "error", |
| 216 |
warning = function(w) "warning" |
| 217 |
) |
| 218 |
|
| 219 |
expect_equal(result, "error") |
| 220 |
|
| 221 |
# warnify does something closer to what we expect |
| 222 |
result <- tryCatch( |
| 223 |
tryCatch(stop("ouch"), error = warnify), |
| 224 |
error = function(e) "error", |
| 225 |
warning = function(w) "warning" |
| 226 |
) |
| 227 |
|
| 228 |
expect_equal(result, "warning") |
| 229 |
|
| 230 |
}) |
| 231 |
|
| 232 |
# https://github.com/rstudio/renv/issues/1733 |
| 233 |
test_that("plural() and nplural() handle non-scalar counts", { |
| 234 |
|
| 235 |
actual <- plural("file", 0:3) |
| 236 |
expected <- c("files", "file", "files", "files") |
| 237 |
expect_equal(actual, expected) |
| 238 |
|
| 239 |
actual <- nplural("file", 0:3) |
| 240 |
expected <- c("0 files", "1 file", "2 files", "3 files") |
| 241 |
expect_equal(actual, expected) |
| 242 |
}) |