Debian Codegraph @

tests/testthat/test-tempdir.R

raw
1 test_that("clean needs to be a single logical", {
2 expect_error(with_tempdir(clean = "sparkling"), "TRUE or FALSE")
3 expect_error(with_tempdir(clean = c(TRUE, FALSE)), "TRUE or FALSE")
4 })
5
6 test_that("tempdir cleans up after itself", {
7 tmp <- with_tempdir(getwd())
8 expect_false(file.exists(tmp))
9 })
10
11 test_that("tempdir will leave the directory alone if clean = FALSE", {
12 tmp <- with_tempdir({
13 cat("x\n", file = "hello.txt")
14 getwd()
15 }, clean = FALSE)
16
17 expect_equal(readLines(file.path(tmp, "hello.txt")), "x")
18 })
19
20 test_that("local_tempdir cleans up after itself", {
21 dir <- character()
22 local({
23 dir <<- local_tempdir()
24 expect_true(dir.exists(dir))
25 })
26 expect_false(dir.exists(dir))
27 })
28
29 test_that("local_tempdir leaves the directory if `clean = FALSE`", {
30 dir <- character()
31 local({
32 dir <<- local_tempdir(clean = FALSE)
33 expect_true(dir.exists(dir))
34 })
35 expect_true(dir.exists(dir))
36
37 unlink(dir, recursive = TRUE)
38 })