Debian Codegraph @

tests/testthat/test-list.R

raw
1 describe("dir_ls", {
2 it("Does not include '.' or double '/' in results", {
3 with_dir_tree(list("foo" = "test"), {
4 expect_equal(dir_ls(), new_fs_path(c(foo = "foo")))
5 expect_equal(dir_ls("."), new_fs_path(c(foo = "foo")))
6 })
7
8 with_dir_tree(list("foo/bar" = "test"), {
9 expect_equal(dir_ls(recurse = TRUE), named_fs_path(c("foo", "foo/bar")))
10 expect_equal(
11 dir_ls(recurse = TRUE, type = "file"),
12 named_fs_path("foo/bar")
13 )
14 expect_equal(
15 dir_ls("./", recurse = TRUE),
16 named_fs_path(c("foo", "foo/bar"))
17 )
18 expect_equal(dir_ls("foo"), named_fs_path("foo/bar"))
19 expect_equal(dir_ls("foo/"), named_fs_path("foo/bar"))
20 })
21 })
22
23 it("Does not follow symbolic links", {
24 with_dir_tree(list("foo/bar/baz" = "test"), {
25 link_create(path_abs("foo"), named_fs_path("foo/bar/qux"))
26 expect_equal(
27 dir_ls(recurse = TRUE),
28 named_fs_path(c("foo", "foo/bar", "foo/bar/baz", "foo/bar/qux"))
29 )
30 expect_equal(
31 dir_ls(recurse = TRUE, type = "symlink"),
32 named_fs_path("foo/bar/qux")
33 )
34 })
35 })
36
37 it("Uses grep to filter output", {
38 with_dir_tree(
39 list(
40 "foo/bar/baz" = "test",
41 "foo/bar/test2" = "",
42 "foo/bar/test3" = ""
43 ),
44 {
45 expect_equal(
46 dir_ls(recurse = TRUE, glob = "*baz"),
47 named_fs_path("foo/bar/baz")
48 )
49 expect_equal(
50 dir_ls(recurse = TRUE, regexp = "baz"),
51 named_fs_path("foo/bar/baz")
52 )
53 expect_equal(
54 dir_ls(recurse = TRUE, regexp = "[23]"),
55 named_fs_path(c("foo/bar/test2", "foo/bar/test3"))
56 )
57 expect_equal(
58 dir_ls(recurse = TRUE, regexp = "(?<=a)z", perl = TRUE),
59 named_fs_path("foo/bar/baz")
60 )
61 }
62 )
63 })
64
65 it("Does not print hidden files by default", {
66 with_dir_tree(
67 list(
68 ".foo" = "foo",
69 "bar" = "bar"
70 ),
71 {
72 expect_equal(dir_ls(), named_fs_path("bar"))
73 expect_equal(dir_ls(all = TRUE), named_fs_path(c(".foo", "bar")))
74 }
75 )
76 })
77
78 it("can find multiple types", {
79 with_dir_tree(
80 list(
81 "file" = "foo",
82 "dir"
83 ),
84 {
85 link_create(path_abs("dir"), "link")
86 expect_equal(dir_ls(type = "file"), named_fs_path("file"))
87 expect_equal(dir_ls(type = "directory"), named_fs_path("dir"))
88 expect_equal(dir_ls(type = "symlink"), named_fs_path("link"))
89 expect_equal(
90 dir_ls(type = c("directory", "symlink")),
91 named_fs_path(c("dir", "link"))
92 )
93 expect_equal(
94 dir_ls(type = c("file", "directory", "symlink")),
95 named_fs_path(c("dir", "file", "link"))
96 )
97 }
98 )
99 })
100 it("works with UTF-8 encoded filenames", {
101 skip_if_not_utf8()
102 skip_on_os("solaris")
103 with_dir_tree("\U7684\U6D4B\U8BD5\U6587\U4EF6", {
104 file_create("fs\U7684\U6D4B\U8BD5\U6587\U4EF6.docx")
105 link_create(path_abs("\U7684\U6D4B\U8BD5\U6587\U4EF6"), "\U7684\U6D4B")
106
107 expect_equal(
108 dir_ls(type = "file"),
109 named_fs_path("fs\U7684\U6D4B\U8BD5\U6587\U4EF6.docx")
110 )
111 expect_equal(
112 dir_ls(type = "directory"),
113 named_fs_path("\U7684\U6D4B\U8BD5\U6587\U4EF6")
114 )
115 expect_equal(
116 dir_ls(type = "symlink"),
117 named_fs_path("\U7684\U6D4B")
118 )
119 expect_equal(
120 path_file(link_path("\U7684\U6D4B")),
121 "\U7684\U6D4B\U8BD5\U6587\U4EF6"
122 )
123 })
124 })
125 it("errors on missing input", {
126 expect_error(dir_ls(NA), class = "invalid_argument")
127 })
128 it("warns if fail == FALSE", {
129 skip_on_os("windows")
130 if (Sys.info()[["effective_user"]] == "root") skip("root user")
131 with_dir_tree(
132 list(
133 "foo",
134 "foo2/bar/baz"
135 ),
136 {
137 file_chmod("foo", "a-r")
138 expect_error(dir_ls(".", recurse = TRUE), class = "EACCES")
139 expect_warning(dir_ls(fail = FALSE, recurse = TRUE), class = "EACCES")
140 file_chmod("foo", "a+r")
141
142 file_chmod("foo2/bar", "a-r")
143 expect_warning(
144 dir_ls("foo2", fail = FALSE, recurse = TRUE),
145 class = "EACCES"
146 )
147 file_chmod("foo2/bar", "a+r")
148 }
149 )
150 })
151 })
152
153 describe("dir_map", {
154 it("can find multiple types", {
155 with_dir_tree(
156 list(
157 "file" = "foo",
158 "dir"
159 ),
160 {
161 nc <- function(x) nchar(x, keepNA = FALSE)
162 expect_equal(dir_map(type = "file", fun = nc), list(4))
163 expect_equal(dir_map(type = "directory", fun = nc), list(3))
164 expect_equal(
165 dir_map(type = c("file", "directory"), fun = nc),
166 list(3, 4)
167 )
168 }
169 )
170 })
171 it("errors on missing input", {
172 expect_error(dir_map(NA, fun = identity), class = "invalid_argument")
173 })
174
175 it("warns if fail == FALSE", {
176 skip_on_os("windows")
177 if (Sys.info()[["effective_user"]] == "root") skip("root user")
178 with_dir_tree(
179 list(
180 "foo",
181 "foo2/bar/baz"
182 ),
183 {
184 file_chmod("foo", "a-r")
185 expect_error(
186 dir_map(".", fun = identity, recurse = TRUE),
187 class = "EACCES"
188 )
189 expect_warning(
190 dir_map(fail = FALSE, fun = identity, recurse = TRUE),
191 class = "EACCES"
192 )
193 file_chmod("foo", "a+r")
194
195 file_chmod("foo2/bar", "a-r")
196 expect_warning(
197 dir_map("foo2", fail = FALSE, fun = identity, recurse = TRUE),
198 class = "EACCES"
199 )
200 file_chmod("foo2/bar", "a+r")
201 }
202 )
203 })
204 })
205
206 describe("dir_walk", {
207 it("can find multiple types", {
208 x <- character()
209 f <- function(p) x <<- p
210
211 with_dir_tree(
212 list(
213 "file" = "foo",
214 "dir"
215 ),
216 {
217 link_create(path_abs("dir"), "link")
218
219 dir_walk(type = "file", fun = f)
220 expect_equal(x, "file")
221
222 dir_walk(type = "directory", fun = f)
223 expect_equal(x, "dir")
224
225 dir_walk(type = "symlink", fun = f)
226 expect_equal(x, "link")
227
228 x <- character()
229 dir_walk(
230 type = c("directory", "symlink"),
231 fun = function(p) x <<- c(x, p)
232 )
233 expect_equal(x, c("dir", "link"))
234
235 x <- character()
236 dir_walk(
237 type = c("file", "directory", "symlink"),
238 fun = function(p) x <<- c(x, p)
239 )
240 expect_equal(x, c("dir", "file", "link"))
241 }
242 )
243 })
244 it("errors on missing input", {
245 expect_error(dir_walk(NA, fun = identity), class = "invalid_argument")
246 })
247
248 it("warns if fail == FALSE", {
249 skip_on_os("windows")
250 if (Sys.info()[["effective_user"]] == "root") skip("root user")
251 with_dir_tree(
252 list(
253 "foo",
254 "foo2/bar/baz"
255 ),
256 {
257 file_chmod("foo", "a-r")
258 expect_error(
259 dir_walk(".", fun = identity, recurse = TRUE),
260 class = "EACCES"
261 )
262 expect_warning(
263 dir_walk(fail = FALSE, fun = identity, recurse = TRUE),
264 class = "EACCES"
265 )
266 file_chmod("foo", "a+r")
267
268 file_chmod("foo2/bar", "a-r")
269 expect_warning(
270 dir_walk("foo2", fail = FALSE, fun = identity, recurse = TRUE),
271 class = "EACCES"
272 )
273 file_chmod("foo2/bar", "a+r")
274 }
275 )
276 })
277 })
278
279 describe("dir_info", {
280 it("is identical to file_info(dir_ls())", {
281 with_dir_tree(
282 list(
283 "file" = "foo",
284 "dir"
285 ),
286 {
287 link_create(path_abs("dir"), "link")
288 expect_identical(dir_info(), file_info(dir_ls()))
289 }
290 )
291 })
292 it("errors on missing input", {
293 expect_error(dir_info(NA), class = "invalid_argument")
294 })
295
296 it("warns if fail == FALSE", {
297 skip_on_os("windows")
298 if (Sys.info()[["effective_user"]] == "root") skip("root user")
299 with_dir_tree(
300 list(
301 "foo",
302 "foo2/bar/baz"
303 ),
304 {
305 file_chmod("foo", "a-r")
306 expect_error(
307 dir_info(".", fun = identity, recurse = TRUE),
308 class = "EACCES"
309 )
310 expect_warning(
311 dir_info(fail = FALSE, fun = identity, recurse = TRUE),
312 class = "EACCES"
313 )
314 file_chmod("foo", "a+r")
315
316 file_chmod("foo2/bar", "a-r")
317 expect_warning(
318 dir_info("foo2", fail = FALSE, fun = identity, recurse = TRUE),
319 class = "EACCES"
320 )
321 file_chmod("foo2/bar", "a+r")
322 }
323 )
324 })
325 })