Debian Codegraph @

tests/testthat/test-copy.R

raw
1 describe("file_copy", {
2 it("copies an empty file and returns the new path", {
3 with_dir_tree(list("foo" = ""), {
4 expect_true(file_exists("foo"))
5 expect_equal(file_copy("foo", "foo2"), fs_path("foo2"))
6 expect_true(file_exists("foo"))
7 expect_true(file_exists("foo2"))
8 })
9 })
10
11 it("copies a non-empty file and returns the new path", {
12 with_dir_tree(
13 list("foo" = "test"),
14 {
15 expect_true(file_exists("foo"))
16 expect_equal(file_copy("foo", "foo2"), fs_path("foo2"))
17 expect_true(file_exists("foo"))
18 expect_true(file_exists("foo2"))
19 expect_equal(readLines("foo2"), readLines("foo"))
20 }
21 )
22 })
23 it("copies multiple files", {
24 with_dir_tree(list("foo" = "test", "bar" = "test2"), {
25 expect_equal(file_exists(c("foo", "bar")), c(foo = TRUE, bar = TRUE))
26 expect_equal(
27 file_copy(c("foo", "bar"), c("foo2", "bar2")),
28 fs_path(c("foo2", "bar2"))
29 )
30 expect_equal(readLines("foo2"), readLines("foo"))
31 expect_equal(readLines("bar2"), readLines("bar"))
32 })
33 })
34 it("errors on missing input", {
35 expect_error(file_copy(NA, "foo2"), class = "invalid_argument")
36 expect_error(file_copy("foo", NA), class = "invalid_argument")
37 })
38 with_dir_tree(list("foo/bar" = "test"), {
39 it("returns the new path and copies the file", {
40 expect_true(file_exists("foo/bar"))
41 expect_false(file_exists("foo/bar2"))
42 expect_equal(file_copy("foo/bar", "foo/bar2"), fs_path("foo/bar2"))
43 expect_true(file_exists("foo/bar"))
44 expect_true(file_exists("foo/bar2"))
45 })
46 it("fails if the new or existing directory does not exist", {
47 expect_true(file_exists("foo/bar"))
48 expect_error(file_copy("baz/bar", "foo/bar3"), class = "ENOENT")
49 expect_error(file_copy("foo/bar", "baz/qux"), class = "ENOENT")
50 })
51 it("errors on missing input", {
52 expect_error(file_copy(NA, "foo"), class = "invalid_argument")
53 expect_error(file_copy("foo/bar", NA), class = "invalid_argument")
54 })
55 })
56 it("copies files in different directories", {
57 with_dir_tree(list("foo/bar" = "test", "foo3/bar2" = "test2", "foo2"), {
58 expect_true(file_exists("foo/bar"))
59 expect_true(file_exists("foo2"))
60 expect_equal(
61 file_copy(c("foo/bar", "foo3/bar2"), "foo2"),
62 fs_path(c("foo2/bar", "foo2/bar2"))
63 )
64 expect_true(file_exists("foo/bar"))
65 expect_true(file_exists("foo3/bar2"))
66 expect_true(file_exists("foo2/bar"))
67 expect_true(file_exists("foo2/bar2"))
68
69 expect_error(file_copy("foo/bar", c("foo2", "foo3")), class = "fs_error")
70 })
71 })
72 })
73
74 describe("link_copy", {
75 it("copies links and returns the new path", {
76 with_dir_tree(list("foo"), {
77 link_create(path_abs("foo"), "loo")
78 expect_true(dir_exists("foo"))
79 expect_true(link_exists("loo"))
80 expect_equal(link_copy("loo", "loo2"), fs_path("loo2"))
81
82 expect_true(link_exists("loo2"))
83 expect_equal(link_path("loo2"), link_path("loo"))
84 })
85 })
86 it("errors on missing input", {
87 expect_error(link_copy(NA, "foo2"), class = "invalid_argument")
88 expect_error(link_copy("foo", NA), class = "invalid_argument")
89 })
90 })
91
92 describe("dir_copy", {
93 it("copies an empty directory and returns the new path", {
94 with_dir_tree(list("foo"), {
95 expect_true(dir_exists("foo"))
96 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2"))
97 expect_true(dir_exists("foo"))
98 expect_true(dir_exists("foo2"))
99 })
100 })
101 it("copies a non-empty directory and returns the new path", {
102 with_dir_tree(
103 list("foo/bar" = "test", "foo/baz" = "test2"),
104 {
105 expect_true(dir_exists("foo"))
106 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2"))
107 expect_true(dir_exists("foo"))
108 expect_true(dir_exists("foo2"))
109 expect_true(file_exists("foo2/bar"))
110 }
111 )
112 })
113 it("copies directories into other directories", {
114 with_dir_tree(
115 list("foo/bar" = "test"),
116 {
117 dir_create("foo2")
118 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2/foo"))
119 expect_true(dir_exists("foo"))
120 expect_true(dir_exists("foo2"))
121 expect_true(dir_exists("foo2/foo"))
122 expect_true(file_exists("foo2/foo/bar"))
123 }
124 )
125 })
126 it("copies nested directories and returns the path", {
127 with_dir_tree(
128 list("foo/bar/baz" = "test", "foo/baz/qux" = "test2"),
129 {
130 expect_true(dir_exists("foo"))
131 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2"))
132 expect_true(dir_exists("foo"))
133 expect_true(dir_exists("foo2"))
134 expect_true(file_exists("foo2/bar/baz"))
135 }
136 )
137 })
138 it("can overwrite directories", {
139 with_dir_tree(
140 list("foo/bar" = "test", "foo2/bar" = "test2"),
141 {
142 dir_copy("foo", "foo2", overwrite = TRUE)
143 expect_equal(readLines("foo/bar"), readLines("foo2/bar"))
144 }
145 )
146 })
147 it("copies absolute paths", {
148 with_dir_tree(
149 list("foo/bar/baz" = "test", "foo/baz/qux" = "test2"),
150 {
151 expect_equal(
152 dir_copy(path_abs("foo"), path_abs("foo2")),
153 path_abs("foo2")
154 )
155 expect_true(dir_exists("foo2"))
156 expect_true(dir_exists(path_abs("foo2")))
157 }
158 )
159 })
160 it("copies hidden files, directories and links", {
161 with_dir_tree(
162 list("foo/.bar/.baz" = "test"),
163 {
164 link_create(path_abs("foo/.bar"), "foo/.qux")
165 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2"))
166 expect_true(dir_exists("foo2"))
167 expect_true(dir_exists("foo2/.bar"))
168 expect_true(file_exists("foo2/.bar/.baz"))
169 expect_true(link_exists("foo2/.qux"))
170 }
171 )
172 })
173 it("copies links and returns the path", {
174 with_dir_tree(
175 list("foo/bar/baz" = "test"),
176 {
177 link_create(path_abs("foo/bar"), "foo/foo")
178 expect_equal(dir_copy("foo", "foo2"), fs_path("foo2"))
179 expect_true(dir_exists("foo2"))
180 expect_true(link_exists("foo2/foo"))
181 expect_equal(link_path("foo2/foo"), link_path("foo/foo"))
182 }
183 )
184 })
185 it("errors on missing input", {
186 expect_error(dir_copy(NA, "foo2"), class = "invalid_argument")
187 expect_error(dir_copy("foo", NA), class = "invalid_argument")
188 })
189 it("dir_copy() works for >1 path", {
190 grandparent_before <- withr::local_tempdir(
191 "dir_copy-multiple_paths-before-"
192 )
193 grandparent_after <- withr::local_tempdir("dir_copy-multiple_paths-after-")
194
195 parents <- dir_create(path(grandparent_before, c("alfa", "beta")))
196 file_create(path(parents, c("apple", "banana")))
197
198 expect_error_free(
199 dir_copy(parents, path(grandparent_after, path_file(parents)))
200 )
201 })
202 })