| 1 |
test_that("with_envvar sets and unsets variables", { |
| 2 |
|
| 3 |
# Make sure the "set_env_testvar" environment var is not set. |
| 4 |
Sys.unsetenv("set_env_testvar") |
| 5 |
expect_false("set_env_testvar" %in% names(Sys.getenv())) |
| 6 |
|
| 7 |
# Use with_envvar (which calls set_envvar) to temporarily set it to 1 |
| 8 |
expect_identical("1", with_envvar(c("set_env_testvar" = 1), |
| 9 |
Sys.getenv("set_env_testvar"))) |
| 10 |
|
| 11 |
# set_env_testvar shouldn't stay in the list of environment vars |
| 12 |
expect_false("set_env_testvar" %in% names(Sys.getenv())) |
| 13 |
}) |
| 14 |
|
| 15 |
test_that("with_envvar respects suffix and prefix", { |
| 16 |
nested <- function(op1, op2) { |
| 17 |
with_envvar(c(A = 1), action = op1, |
| 18 |
with_envvar(c(A = 2), action = op2, |
| 19 |
Sys.getenv("A")[[1]] |
| 20 |
) |
| 21 |
) |
| 22 |
} |
| 23 |
|
| 24 |
expect_equal(nested("replace", "suffix"), c("1 2")) |
| 25 |
expect_equal(nested("replace", "prefix"), c("2 1")) |
| 26 |
expect_equal(nested("prefix", "suffix"), c("1 2")) |
| 27 |
expect_equal(nested("prefix", "prefix"), c("2 1")) |
| 28 |
expect_equal(nested("suffix", "suffix"), c("1 2")) |
| 29 |
expect_equal(nested("suffix", "prefix"), c("2 1")) |
| 30 |
}) |
| 31 |
|
| 32 |
test_that("local_envvar sets and unsets variables", { |
| 33 |
|
| 34 |
# Make sure the "set_env_testvar" environment var is not set. |
| 35 |
Sys.unsetenv("set_env_testvar") |
| 36 |
expect_false("set_env_testvar" %in% names(Sys.getenv())) |
| 37 |
|
| 38 |
# Use local_envvar (which calls set_envvar) to temporarily set it to 1 |
| 39 |
local({ |
| 40 |
local_envvar(c("set_env_testvar" = 1)) |
| 41 |
expect_identical("1", Sys.getenv("set_env_testvar")) |
| 42 |
}) |
| 43 |
|
| 44 |
# set_env_testvar shouldn't stay in the list of environment vars |
| 45 |
expect_false("set_env_testvar" %in% names(Sys.getenv())) |
| 46 |
}) |
| 47 |
|
| 48 |
test_that("local_envar respects suffix and prefix", { |
| 49 |
nested <- function(op1, op2) { |
| 50 |
local({ |
| 51 |
local_envvar(c(A = 1), action = op1) |
| 52 |
local({ |
| 53 |
local_envvar(c(A = 2), action = op2) |
| 54 |
Sys.getenv("A")[[1]] |
| 55 |
}) |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
expect_equal(nested("replace", "suffix"), c("1 2")) |
| 60 |
expect_equal(nested("replace", "prefix"), c("2 1")) |
| 61 |
expect_equal(nested("prefix", "suffix"), c("1 2")) |
| 62 |
expect_equal(nested("prefix", "prefix"), c("2 1")) |
| 63 |
expect_equal(nested("suffix", "suffix"), c("1 2")) |
| 64 |
expect_equal(nested("suffix", "prefix"), c("2 1")) |
| 65 |
}) |
| 66 |
|
| 67 |
test_that("local_envvar works with null values", { |
| 68 |
Sys.setenv("FOOBAR" = 1) |
| 69 |
local({ |
| 70 |
local_envvar("FOOBAR" = NULL) |
| 71 |
expect_false("FOOBAR" %in% names(Sys.getenv())) |
| 72 |
}) |
| 73 |
}) |