Debian Codegraph @

tests/testthat/test-file.R

raw
1 describe("file_info", {
2 with_dir_tree(
3 list(
4 "foo/bar" = "test",
5 "NA" = ""
6 ),
7 {
8 link_create(path_abs("foo"), "foo2")
9
10 it("returns a correct tibble", {
11 x <- file_info(c("foo", "foo/bar", "foo2", "NA"))
12 expect_s3_class(x, c("tbl", "tbl_df", "data.frame"))
13 expect_length(x, 18)
14 expect_equal(nrow(x), 4)
15 expect_equal(as.character(x$path), c("foo", "foo/bar", "foo2", "NA"))
16 expect_equal(
17 as.character(x$type),
18 c("directory", "file", "symlink", "file")
19 )
20 })
21
22 it("returns NA if a file does not exist", {
23 x <- file_info("missing")
24 expect_s3_class(x, c("tbl", "tbl_df", "data.frame"))
25 expect_length(x, 18)
26 expect_equal(nrow(x), 1)
27 expect_equal(as.character(x$path), "missing")
28 expect_equal(sum(is.na(x)), 17)
29 })
30 it("returns NA on NA input", {
31 x <- file_info(NA_character_)
32 expect_s3_class(x, c("tbl", "tbl_df", "data.frame"))
33 expect_length(x, 18)
34 expect_equal(nrow(x), 1)
35 expect_equal(as.character(x$path), NA_character_)
36 expect_equal(sum(is.na(x)), 18)
37 })
38 it("can be subset as a data.frame", {
39 x <- file_info("foo/bar")
40 class(x) <- "data.frame"
41 for (col in seq_along(x)) {
42 expect_true(length(x[[1, col]]) == 1)
43 }
44 })
45 it("can be subset as a tibble", {
46 x <- file_info("foo/bar")
47 expect_s3_class(x, "tbl_df")
48 for (col in seq_along(x)) {
49 expect_true(length(x[[1, col]]) == 1)
50 }
51 })
52 }
53 )
54 })
55
56 describe("file_size", {
57 with_dir_tree(
58 list(
59 "foo" = "test",
60 "bar" = "test2"
61 ),
62 {
63 it("returns the correct file size", {
64 expect_equal(
65 file_size("foo"),
66 stats::setNames(file_info("foo")$size, "foo")
67 )
68 expect_equal(
69 file_size("bar"),
70 stats::setNames(file_info("bar")$size, "bar")
71 )
72 })
73 it("returns an object of class fs_bytes, numeric (#239)", {
74 expect_equal(class(file_size("foo")), c("fs_bytes", "numeric"))
75 expect_equal(class(file_size("foo")[]), c("fs_bytes", "numeric"))
76 })
77 }
78 )
79 })
80
81 # Windows permissions only really allow setting read only, so we will skip
82 # the more thorough tests
83 if (!is_windows()) {
84 describe("file_chmod", {
85 with_dir_tree(list("foo/bar" = "test"), {
86 it("returns the input path and changes permissions with symbolic input", {
87 expect_equal(file_chmod("foo/bar", "u=rw,go=r"), fs_path("foo/bar"))
88 expect_equal(
89 as.character(file_info("foo/bar")$permissions),
90 as.character(fs_perms("644"))
91 )
92 expect_equal(file_chmod("foo/bar", "u+x"), fs_path("foo/bar"))
93 expect_equal(
94 as.character(file_info("foo/bar")$permissions),
95 as.character(fs_perms("744"))
96 )
97 expect_equal(file_chmod("foo/bar", "g-r"), fs_path("foo/bar"))
98 expect_equal(
99 as.character(file_info("foo/bar")$permissions),
100 as.character(fs_perms("704"))
101 )
102 expect_equal(file_chmod("foo/bar", "o-r"), fs_path("foo/bar"))
103 expect_equal(
104 as.character(file_info("foo/bar")$permissions),
105 as.character(fs_perms("700"))
106 )
107 })
108
109 it("returns the input path and changes permissions with octal input", {
110 expect_equal(file_chmod("foo/bar", "644"), fs_path("foo/bar"))
111 expect_equal(
112 as.character(file_info("foo/bar")$permissions),
113 as.character(fs_perms("644"))
114 )
115 expect_equal(file_chmod("foo/bar", "744"), fs_path("foo/bar"))
116 expect_equal(
117 as.character(file_info("foo/bar")$permissions),
118 as.character(fs_perms("744"))
119 )
120 expect_equal(file_chmod("foo/bar", "704"), fs_path("foo/bar"))
121 expect_equal(
122 as.character(file_info("foo/bar")$permissions),
123 as.character(fs_perms("704"))
124 )
125 expect_equal(file_chmod("foo/bar", "700"), fs_path("foo/bar"))
126 expect_equal(
127 as.character(file_info("foo/bar")$permissions),
128 as.character(fs_perms("700"))
129 )
130 })
131
132 it("returns the input path and changes permissions with display input", {
133 expect_equal(file_chmod("foo/bar", "rw-r--r--"), fs_path("foo/bar"))
134 expect_true(file_info("foo/bar")$permissions == "644")
135 expect_equal(file_chmod("foo/bar", "rwxr--r--"), fs_path("foo/bar"))
136 expect_equal(
137 as.character(file_info("foo/bar")$permissions),
138 as.character(fs_perms("744"))
139 )
140 expect_equal(file_chmod("foo/bar", "rwx---r--"), fs_path("foo/bar"))
141 expect_equal(
142 as.character(file_info("foo/bar")$permissions),
143 as.character(fs_perms("704"))
144 )
145 expect_equal(file_chmod("foo/bar", "rwx------"), fs_path("foo/bar"))
146 expect_equal(
147 as.character(file_info("foo/bar")$permissions),
148 as.character(fs_perms("700"))
149 )
150 })
151
152 it("errors if given an invalid mode", {
153 expect_snapshot(
154 error = TRUE,
155 file_chmod("foo", "g+S"),
156 transform = transform_error
157 )
158 })
159 it("errors on missing input", {
160 expect_error(file_chmod(NA, "u+x"), class = "invalid_argument")
161 })
162
163 it("is vectorized over files and permissions", {
164 file_create("foo/baz")
165 files <- c("foo/bar", "foo/baz")
166 expect_equal(file_chmod(files, "000"), fs_path(files))
167 expect_true(all(file_info(files)$permissions == c("000", "000")))
168
169 expect_equal(file_chmod(files, "644"), fs_path(files))
170 expect_true(all(file_info(files)$permissions == c("644", "644")))
171
172 expect_equal(file_chmod(files, c("u+x", "o+x")), fs_path(files))
173 expect_true(all(file_info(files)$permissions == c("744", "645")))
174
175 expect_error(
176 file_chmod(files, c("u+x", "o+x", "g+x")),
177 class = "invalid_argument"
178 )
179 })
180 })
181 })
182 }
183
184 describe("file_chown", {
185 with_dir_tree(list("foo/bar" = "test"), {
186 it("changes the ownership of a file, returns the input path", {
187 skip("need elevated permissions to change uid")
188
189 # Make everyone have write access, so we can delete this after changing ownership
190 file_chmod("foo/bar", "a+w")
191
192 # Change ownership to root
193 expect_equal(file_chown("foo/bar", user_id = 0), "foo/bar")
194 expect_true(file_info("foo/bar")$user == "root")
195 })
196 it("errors on missing input", {
197 expect_error(file_chown(NA, user_id = 0), class = "invalid_argument")
198 })
199 })
200 })
201
202 describe("file_move", {
203 it("renames files in the same directory", {
204 with_dir_tree(list("foo" = "test"), {
205 expect_true(file_exists("foo"))
206 expect_equal(file_move("foo", "bar"), fs_path("bar"))
207 expect_false(file_exists("foo"))
208 expect_true(file_exists("bar"))
209 })
210 })
211
212 it("renames files in different directories", {
213 with_dir_tree(list("foo/bar" = "test", "foo2"), {
214 expect_true(file_exists("foo/bar"))
215 expect_true(file_exists("foo2"))
216 expect_equal(file_move("foo/bar", "foo2"), fs_path("foo2/bar"))
217 expect_false(file_exists("foo/bar"))
218 expect_true(file_exists("foo2/bar"))
219
220 expect_error(file_move("foo/bar", c("foo2", "foo3")), class = "fs_error")
221 })
222 })
223 it("errors on missing input", {
224 expect_error(file_move(NA, "foo2"), class = "invalid_argument")
225 expect_error(file_move("foo", NA), class = "invalid_argument")
226 })
227 })
228
229 describe("file_touch", {
230 it("updates modification_time and access_time", {
231 with_dir_tree("dir", {
232 file_create("foo")
233 now <- Sys.time()
234
235 file_touch("foo", now)
236 old <- file_info("foo")
237
238 file_touch("foo", now + 10, now + 20)
239
240 new <- file_info("foo")
241 expect_true(old$modification_time < new$modification_time)
242 expect_true(old$access_time < new$access_time)
243
244 file_touch("foo", now - 10, now - 20)
245 new2 <- file_info("foo")
246 expect_true(old$modification_time > new2$modification_time)
247 expect_true(old$access_time > new2$access_time)
248 })
249 })
250
251 it("errors on missing input", {
252 expect_error(file_touch(NA), class = "invalid_argument")
253 })
254
255 it("creates the file if it doesn't exist", {
256 with_dir_tree("dir", {
257 file_touch("foo")
258 expect_true(file_exists("foo"))
259 })
260 })
261 })