Debian Codegraph @

tests/testthat/test-files.R

raw
1
2 test_that("directories can be copied", {
3
4 source <- renv_scope_tempfile("renv-source-")
5 target <- renv_scope_tempfile("renv-target-")
6
7 ensure_directory(source)
8
9 range <- 1:10
10 files <- sprintf("%02i.txt", range)
11 for (i in range)
12 file.create(file.path(source, sprintf("%02i.txt", i)))
13
14 expect_setequal(files, list.files(source))
15 renv_file_copy(source, target)
16 expect_setequal(list.files(source), list.files(target))
17
18 })
19
20 test_that("directories can be moved", {
21
22 source <- renv_scope_tempfile("renv-source-")
23 target <- renv_scope_tempfile("renv-target-")
24
25 ensure_directory(source)
26
27 range <- 1:10
28 files <- sprintf("%02i.txt", range)
29 for (i in range)
30 file.create(file.path(source, sprintf("%02i.txt", i)))
31
32 renv_file_move(source, target)
33 expect_true(!file.exists(source))
34 expect_true(file.exists(target))
35 expect_setequal(files, list.files(target))
36
37 })
38
39 test_that("attempts to link files work", {
40
41 source <- renv_scope_tempfile("renv-source-")
42 target <- renv_scope_tempfile("renv-target-")
43
44 dir.create(source)
45 renv_file_link(source, target)
46 expect_true(renv_file_same(source, target))
47
48 })
49
50 test_that("scoped backups are cleared as necessary", {
51
52 source <- renv_scope_tempfile("renv-source-")
53 target <- renv_scope_tempfile("renv-target-")
54
55 writeLines("source", source)
56 writeLines("target", target)
57
58 local({
59 callback <- renv_file_backup(target)
60 defer(callback())
61 expect_true(!file.exists(target))
62 })
63
64 expect_true(file.exists(target))
65 expect_equal(readLines(target), "target")
66
67 local({
68 callback <- renv_file_backup(target)
69 defer(callback())
70 writeLines("mutate", target)
71 })
72
73 expect_true(file.exists(target))
74 expect_equal(readLines(target), "mutate")
75 list.files(tempdir())
76
77 files <- list.files(tempdir())
78 backup <- grep("^\\.renv-backup-", files)
79 expect_length(backup, 0)
80
81 })
82
83 test_that("renv tempfiles are deleted at end of scope", {
84
85 path <- NULL
86 path2 <- NULL
87 local({
88 path <<- renv_scope_tempfile()
89 path2 <<- renv_scope_tempfile()
90 file.create(path, path2)
91 expect_true(file.exists(path))
92 expect_true(file.exists(path2))
93 })
94 expect_false(file.exists(path))
95 expect_false(file.exists(path2))
96
97 })
98
99 test_that("renv_file_find finds parent files", {
100
101 base <- renv_scope_tempfile("renv-files-")
102 rest <- c("alpha/beta/gamma")
103 tip <- file.path(base, rest)
104 ensure_directory(tip)
105
106 found <- renv_file_find(tip, function(path) {
107 if (basename(path) == "alpha")
108 return(path)
109 })
110
111 expect_true(renv_file_same(found, file.path(base, "alpha")))
112
113 })
114
115 test_that("attempts to overwrite existing files are handled appropriately", {
116
117 source <- renv_scope_tempfile("renv-source-")
118 target <- renv_scope_tempfile("renv-target-")
119
120 writeLines("alpha", con = source)
121 writeLines("beta", con = target)
122
123 expect_error(renv_file_copy(source, target))
124 expect_true(renv_file_copy(source, target, overwrite = TRUE))
125
126 })
127
128 test_that("permissions, timestamps are preserved", {
129
130 skip("failing test; mtime sometimes differs?")
131 source <- renv_scope_tempfile("renv-source-")
132 target <- renv_scope_tempfile("renv-target-")
133
134 ensure_directory(source)
135
136 range <- 1:10
137 files <- sprintf("%02i.txt", range)
138 for (i in range) {
139 Sys.sleep(0.01)
140 file.create(file.path(source, sprintf("%02i.txt", i)))
141 }
142
143 renv_file_copy(source, target)
144
145 srcfiles <- list.files(source, full.names = TRUE)
146 tgtfiles <- list.files(target, full.names = TRUE)
147
148 srcinfo <- renv_file_info(srcfiles)
149 tgtinfo <- renv_file_info(tgtfiles)
150
151 rownames(srcinfo) <- rownames(tgtinfo) <- basename(srcfiles)
152
153 fields <- setdiff(names(srcinfo), c("ctime", "atime"))
154 expect_equal(srcinfo[fields], tgtinfo[fields])
155
156 })
157
158 test_that("renv can list files not representable in the native encoding", {
159
160 skip_if(renv_platform_unix() && !renv_l10n_utf8())
161
162 renv_scope_tempdir()
163 evil <- "\u9b3c"
164
165 file.create(evil)
166 defer(unlink(evil))
167
168 files <- renv_file_list(getwd(), full.names = FALSE)
169 expect_true(evil %in% files)
170
171 })
172
173 test_that("renv can detect broken junctions / symlinks", {
174
175 renv_scope_tempdir()
176
177 if (renv_platform_windows()) {
178
179 file.create("file")
180 dir.create("dir")
181 dir.create("nowhere")
182 Sys.junction("dir", "junction")
183 Sys.junction("nowhere", "broken")
184 unlink("nowhere", recursive = TRUE)
185
186 } else {
187
188 file.create("file")
189 dir.create("dir")
190
191 file.symlink("file", "filelink")
192 file.symlink("dir", "dirlink")
193 file.symlink("oops", "broken")
194
195 }
196
197 paths <- list.files()
198 broken <- renv_file_broken(paths)
199 expect_equal(paths[broken], "broken")
200
201 })
202
203 test_that("renv can detect junction points", {
204 skip_on_cran()
205 skip_if(!renv_platform_windows())
206
207 renv_scope_tempdir()
208 dir.create("source")
209 defer(unlink("source", recursive = TRUE))
210
211 # create some files -- should give the directory a size,
212 # but this seems unreliable?
213 files <- sprintf("source/%05i.txt", 1:100)
214 file.create(files)
215
216 # create a junction point
217 Sys.junction("source", "junction")
218 defer(unlink("junction", recursive = TRUE))
219
220 # check that they're the same
221 expect_true(renv_file_same("source", "junction"))
222 })