Debian Codegraph @

tests/testthat/test-tempfile.R

raw
1 test_that("with_tempfile works", {
2
3 f1 <- character()
4 f2 <- character()
5
6 with_tempfile("file1", {
7 writeLines("foo", file1)
8 expect_equal(readLines(file1), "foo")
9 with_tempfile("file2", {
10 writeLines("bar", file2)
11 expect_equal(readLines(file1), "foo")
12 expect_equal(readLines(file2), "bar")
13
14 f2 <<- file2
15 })
16 expect_false(file.exists(f2))
17 f1 <<- file1
18 })
19 expect_false(file.exists(f1))
20 })
21
22 test_that("local_tempfile with `new` works with a warning", {
23
24 f1 <- character()
25 f2 <- character()
26
27 f <- function() {
28 expect_warning(
29 local_tempfile("file1"),
30 "is deprecated"
31 )
32 writeLines("foo", file1)
33 expect_equal(readLines(file1), "foo")
34 expect_warning(
35 local_tempfile("file2"),
36 "is deprecated"
37 )
38 writeLines("bar", file2)
39 expect_equal(readLines(file1), "foo")
40 expect_equal(readLines(file2), "bar")
41 f1 <<- file1
42 f2 <<- file2
43 }
44 f()
45
46 expect_false(file.exists(f1))
47 expect_false(file.exists(f2))
48 })
49
50 test_that("local_tempfile works", {
51 f1 <- character()
52 f2 <- character()
53
54 f <- function() {
55 file1 <- local_tempfile()
56
57 writeLines("foo", file1)
58 expect_equal(readLines(file1), "foo")
59
60 file2 <- local_tempfile()
61 writeLines("bar", file2)
62 expect_equal(readLines(file1), "foo")
63 expect_equal(readLines(file2), "bar")
64 f1 <<- file1
65 f2 <<- file2
66 }
67 f()
68
69 expect_false(file.exists(f1))
70 expect_false(file.exists(f2))
71 })
72
73 test_that("local_tempfile() can add data", {
74 path <- local_tempfile(lines = c("a", "b"))
75 expect_equal(readLines(path), c("a", "b"))
76 })
77
78 test_that("local_tempfile() always writes \n", {
79 path <- local_tempfile(lines = "x")
80 expect_equal(file.size(path), 2)
81 expect_equal(readChar(path, file.size(path)), "x\n")
82 })
83
84 test_that("local_tempfile() uses UTF-8", {
85 utf8 <- "\u00e1" # á
86 latin1 <- iconv(utf8, "UTF-8", "latin1")
87
88 path <- local_tempfile(lines = latin1)
89
90 local_options(encoding = "native.enc")
91 expect_equal(readLines(path, encoding = "UTF-8"), utf8)
92 })