Debian Codegraph @

tests/testthat/test-file.R

raw
1 describe("with_file", {
2 it("can use unnamed arguments", {
3 with_file("file1", {
4 writeLines("foo", "file1")
5 expect_equal(readLines("file1"), "foo")
6 with_file("file2", {
7 writeLines("bar", "file2")
8 expect_equal(readLines("file1"), "foo")
9 expect_equal(readLines("file2"), "bar")
10 })
11 expect_false(file.exists("file2"))
12 })
13 expect_false(file.exists("file1"))
14 })
15
16 it("can use named arguments", {
17 with_file(list("file1" = writeLines("foo", "file1")), {
18 expect_equal(readLines("file1"), "foo")
19 with_file(list("file2" = writeLines("bar", "file2")), {
20 expect_equal(readLines("file1"), "foo")
21 expect_equal(readLines("file2"), "bar")
22 })
23 expect_false(file.exists("file2"))
24 })
25 expect_false(file.exists("file1"))
26 })
27 it("works with multiple files", {
28 with_file(
29 list("file1" = writeLines("foo", "file1"),
30 "file2",
31 "file3" = writeLines("baz", "file3")), {
32
33 writeLines("bar", "file2")
34 expect_equal(readLines("file1"), "foo")
35 expect_equal(readLines("file2"), "bar")
36 expect_equal(readLines("file3"), "baz")
37 })
38 expect_false(file.exists("file1"))
39 expect_false(file.exists("file2"))
40 expect_false(file.exists("file3"))
41 })
42 })
43
44 describe("local_file", {
45 it("works with unnamed arguments", {
46 f <- function() {
47 local_file("file1")
48 writeLines("foo", "file1")
49 expect_equal(readLines("file1"), "foo")
50 }
51 expect_no_output(f())
52 expect_false(file.exists("file1"))
53 })
54
55 it("works with named arguments", {
56 f <- function() {
57 local_file(list("file1" = writeLines("foo", "file1")))
58 expect_equal(readLines("file1"), "foo")
59 }
60 expect_no_output(f())
61 expect_false(file.exists("file1"))
62 })
63
64 it("can delete directories", {
65 path <- character()
66 f <- function() {
67 path <<- local_file(tempfile())
68 dir.create(path)
69 file.create(file.path(path, "foo"))
70 expect_true(file.exists(path))
71 }
72 expect_no_output(f())
73 expect_false(file.exists(path))
74 })
75 })