1
2test_that("non-existant file", {
3
4  on.exit(try(unlink(c(zipfile, tmp), recursive = TRUE)))
5  tmp <- tempfile()
6
7  zipfile <- tempfile(fileext = ".zip")
8
9  expect_error(
10    withr::with_dir(
11      dirname(tmp),
12      zipr(zipfile, basename(tmp))
13    ),
14    "Some files do not exist"
15  )
16})
17
18test_that("appending non-existant file", {
19
20  on.exit(try(unlink(c(zipfile, tmp, tmp2), recursive = TRUE)))
21  cat("compress this if you can!", file = tmp <- tempfile())
22
23  zipfile <- tempfile(fileext = ".zip")
24
25  expect_silent(
26    withr::with_dir(
27      dirname(tmp),
28      zipr(zipfile, basename(tmp))
29    )
30  )
31
32  cat("compress this as well, if you can!", file = tmp2 <- tempfile())
33
34  expect_silent(
35    withr::with_dir(
36      dirname(tmp2),
37      zipr_append(zipfile, basename(tmp2))
38    )
39  )
40
41  expect_true(file.exists(zipfile))
42
43  list <- zip_list(zipfile)
44  expect_equal(basename(list$filename), basename(c(tmp, tmp2)))
45})
46
47test_that("non readable file", {
48
49  skip_on_os("windows")
50  skip_on_os("linux")
51
52  on.exit(try(unlink(c(zipfile, tmp), recursive = TRUE)))
53  cat("compress this if you can!", file = tmp <- tempfile())
54  Sys.chmod(tmp, "0000")
55
56  zipfile <- tempfile(fileext = ".zip")
57
58  expect_error(
59    withr::with_dir(
60      dirname(tmp),
61      zipr(zipfile, basename(tmp))
62    ),
63    "Cannot add file"
64  )
65})
66
67test_that("empty archive, no files", {
68  on.exit(try(unlink(zipfile)))
69  zipfile <- tempfile(fileext = ".zip")
70
71  expect_silent(zipr(zipfile, character()))
72
73  expect_true(file.exists(zipfile))
74
75  list <- zip_list(zipfile)
76  expect_equal(nrow(list), 0)
77  expect_equal(list$filename, character())
78})
79
80test_that("single empty directory", {
81  on.exit(try(unlink(c(zipfile, tmp), recursive = TRUE)))
82  dir.create(tmp <- tempfile())
83
84  zipfile <- tempfile(fileext = ".zip")
85
86  expect_silent(
87    withr::with_dir(
88      dirname(tmp),
89      zipr(zipfile, basename(tmp))
90    )
91  )
92
93  expect_true(file.exists(zipfile))
94
95  list <- zip_list(zipfile)
96  expect_equal(nrow(list), 1)
97  expect_equal(list$filename, bns(tmp))
98
99  dir.create(tmp2 <- tempfile())
100  on.exit(try(unlink(tmp2, recursive = TRUE)))
101  utils::unzip(zipfile, exdir = tmp2)
102  expect_equal(dir(tmp2), basename(tmp))
103  expect_true(file.info(file.path(tmp2, dir(tmp2)))$isdir)
104})
105
106test_that("single empty directory, non-recursive", {
107  on.exit(try(unlink(c(zipfile, tmp), recursive = TRUE)))
108  dir.create(tmp <- tempfile())
109
110  zipfile <- tempfile(fileext = ".zip")
111
112  expect_warning(
113    withr::with_dir(
114      dirname(tmp),
115      zipr(zipfile, basename(tmp), recurse = FALSE)
116    ),
117    "directories ignored"
118  )
119
120  expect_true(file.exists(zipfile))
121
122  list <- zip_list(zipfile)
123  expect_equal(nrow(list), 0)
124  expect_equal(list$filename, character())
125})
126
127test_that("appending single empty directory", {
128
129  on.exit(try(unlink(c(zipfile, tmp, tmp2), recursive = TRUE)))
130
131  dir.create(tmp <- tempfile())
132  cat("first file", file = file.path(tmp, "file1"))
133  cat("second file", file = file.path(tmp, "file2"))
134
135  zipfile <- tempfile(fileext = ".zip")
136
137  expect_silent(
138    withr::with_dir(
139      dirname(tmp),
140      zipr(zipfile, basename(tmp))
141    )
142  )
143
144  expect_true(file.exists(zipfile))
145
146  list <- zip_list(zipfile)
147  expect_equal(
148    basename(list$filename),
149    c(basename(tmp), "file1", "file2")
150  )
151
152  dir.create(tmp2 <- tempfile())
153
154  expect_silent(
155    withr::with_dir(
156      dirname(tmp),
157      zipr_append(zipfile, basename(tmp2))
158    )
159  )
160
161  expect_true(file.exists(zipfile))
162
163  list <- zip_list(zipfile)
164  expect_equal(
165    basename(list$filename),
166    c(basename(tmp), "file1", "file2", basename(tmp2))
167  )
168})
169
170test_that("appending single empty directory, non-recursive", {
171
172  on.exit(try(unlink(c(zipfile, tmp, tmp2), recursive = TRUE)))
173
174  dir.create(tmp <- tempfile())
175  cat("first file", file = file.path(tmp, "file1"))
176  cat("second file", file = file.path(tmp, "file2"))
177
178  zipfile <- tempfile(fileext = ".zip")
179
180  expect_silent(
181    withr::with_dir(
182      dirname(tmp),
183      zipr(zipfile, basename(tmp))
184    )
185  )
186
187  expect_true(file.exists(zipfile))
188
189  list <- zip_list(zipfile)
190  expect_equal(
191    basename(list$filename),
192    c(basename(tmp), "file1", "file2")
193  )
194
195  dir.create(tmp2 <- tempfile())
196
197  expect_warning(
198    withr::with_dir(
199      dirname(tmp),
200      zipr_append(zipfile, basename(tmp2), recurse = FALSE)
201    ),
202    "directories ignored"
203  )
204
205  expect_true(file.exists(zipfile))
206
207  list <- zip_list(zipfile)
208  expect_equal(
209    basename(list$filename),
210    c(basename(tmp), "file1", "file2")
211  )
212})
213