Debian Codegraph @

tests/testthat/test-shinywrappers.R

raw
1 test_that("isTemp passes sanity checks", {
2 t <- tempfile(fileext = ".txt")
3 writeLines("hello", t)
4 on.exit(unlink(t), add = TRUE)
5
6 expect_true(isTemp(t, mustExist = TRUE))
7 expect_false(isTemp(path.expand("~"), mustExist = TRUE))
8 expect_false(isTemp(".", mustExist = TRUE))
9
10 # Tempdir itself isn't a temp file
11 expect_false(isTemp(tempdir(), mustExist = TRUE))
12
13 # Malformed temp dir isn't a problem
14 expect_error(isTemp(path.expand("~"), tempDir = "", mustExist = TRUE))
15
16 # path normalization
17 expect_true(isTemp(".", tempDir = normalizePath(".."), mustExist = TRUE))
18 expect_true(isTemp(normalizePath("."), tempDir = "..", mustExist = TRUE))
19 expect_true(isTemp(".", tempDir = "..", mustExist = TRUE))
20
21 # not based on simple string matching
22 subdir <- tempfile()
23 dir.create(subdir)
24 on.exit(unlink(subdir), add = TRUE)
25
26 badpath <- paste0(subdir, "1")
27 writeLines("hello", badpath)
28 on.exit(unlink(badpath), add = TRUE)
29
30 expect_false(isTemp(badpath, tempDir = subdir, mustExist = TRUE))
31 # While we're here, make sure that paths can count as temp
32 expect_true(isTemp(subdir, mustExist = TRUE))
33 })
34
35 test_that("isTemp symlink scenarios", {
36 testthat::skip_on_os("windows")
37
38 faketmp <- file.path(getwd(), "faketmp")
39 file.symlink(tempdir(), faketmp)
40 on.exit(unlink(faketmp), add = TRUE)
41
42 t <- tempfile(fileext = ".txt")
43 writeLines("hello", t)
44 on.exit(unlink(t), add = TRUE)
45
46 expect_true(isTemp(t, mustExist = TRUE))
47 expect_true(isTemp(t, tempDir = faketmp, mustExist = TRUE))
48 expect_true(isTemp(file.path(faketmp, basename(t)), mustExist = TRUE))
49 expect_true(isTemp(file.path(faketmp, basename(t)), tempDir = faketmp, mustExist = TRUE))
50 })