Debian Codegraph @

tests/testthat/test-resources.R

raw
1 context("resource discovery")
2
3 sort_resources <- function(resources) {
4 # sort by filename and remove rownames to avoid errors arising from file ordering
5 # -- we don't really care what order these come back in
6 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
7 rownames(resources) <- NULL
8 }
9
10 test_that("R Markdown resource discovery finds expected resources", {
11 # Test with the current version of the template
12 file.copy(pkg_file("rmd/h/default.html"), test_path('resources/template.html'), overwrite = TRUE)
13 resources <- find_external_resources(test_path("resources/rmarkdown.Rmd"))
14 expected <- data.frame(
15 path = c("empty.md", "empty.png", "empty.tsv", "empty.Rmd", "empty.css",
16 "empty.jpg", "empty.html", "template.html", "empty.csv"),
17 explicit = c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
18 web = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE),
19 stringsAsFactors = FALSE)
20
21 resources <- sort_resources(resources)
22 expected <- sort_resources(expected)
23
24 expect_equal(resources, expected)
25 })
26
27 test_that("R Markdown resource discovery finds expected scss when sass is used", {
28 skip_if_not_installed("sass")
29 css_files <- c("empty.scss", "empty.css", "empty2.scss", "empty2.css")
30 yaml <- yaml::as.yaml(list(
31 title = "test",
32 output = list(
33 html_document = list(
34 css = c("empty.scss", "empty.css", "empty2.scss", "empty2.css"),
35 theme = "cerulean"
36 )
37 )
38 ))
39 rmd <- local_rmd_file(knitr::knit_expand(text = c("---", "{{yaml}}", "---", "", "# test")))
40 withr::local_dir(dirname(rmd))
41 file.create(css_files)
42 resources <- find_external_resources(basename(rmd))
43 resource <- resources[resources$path %in% css_files, ]
44 expected <- data.frame(
45 path = css_files,
46 explicit = rep_len(FALSE, length(css_files)),
47 web = !needs_sass(css_files),
48 stringsAsFactors = FALSE)
49 resource <- sort_resources(resource)
50 expected <- sort_resources(expected)
51 expect_equal(resource, expected)
52 })
53
54 test_that("R Markdown resource discovery finds expected scss and css when bslib is used", {
55 skip_if_not_installed("bslib")
56 css_files <- c("empty.scss", "empty.css", "empty2.scss", "empty2.css")
57 yaml <- yaml::as.yaml(list(
58 title = "test",
59 output = list(
60 html_document = list(
61 css = c("empty.scss", "empty.css", "empty2.scss", "empty2.css"),
62 theme = list(version = "5")
63 )
64 )
65 ))
66 rmd <- local_rmd_file(knitr::knit_expand(text = c("---", "{{yaml}}", "---", "", "# test")))
67 withr::local_dir(dirname(rmd))
68 file.create(css_files)
69 resources <- find_external_resources(basename(rmd))
70 resource <- resources[resources$path %in% css_files, ]
71 expected <- data.frame(
72 path = css_files,
73 explicit = rep_len(FALSE, length(css_files)),
74 web = rep_len(FALSE, length(css_files)),
75 stringsAsFactors = FALSE)
76 resource <- sort_resources(resource)
77 expected <- sort_resources(expected)
78 expect_equal(resource, expected)
79 })
80
81
82 test_that("HTML resource discovery finds expected resources", {
83
84 resources <- find_external_resources("resources/html.html")
85 expected <- data.frame(
86 path = c("empty.js", "empty.css", "empty.png"),
87 explicit = c(FALSE, FALSE, FALSE),
88 web = c(TRUE, TRUE, TRUE),
89 stringsAsFactors = FALSE)
90
91 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
92 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
93 expect_equal(resources, expected)
94 })
95
96 test_that("Vanilla Markdown resource discovery finds expected resources", {
97
98 resources <- find_external_resources("resources/markdown.md")
99 expected <- data.frame(
100 path = c("empty.png", "empty.jpg"),
101 explicit = c(FALSE, FALSE),
102 web = c(TRUE, TRUE ),
103 stringsAsFactors = FALSE)
104
105 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
106 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
107 expect_equal(resources, expected)
108 })
109
110
111 test_that("PDF-specific resources are discovered", {
112
113 resources <- find_external_resources("resources/pdf.Rmd")
114 expected <- data.frame(
115 path = c("empty.bib", "empty.csl", "empty.png"),
116 explicit = c(FALSE, FALSE, FALSE),
117 web = c(FALSE, FALSE, TRUE ),
118 stringsAsFactors = FALSE)
119
120 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
121 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
122 expect_equal(resources, expected)
123 })
124
125
126 test_that("bare relative directory references are ignored", {
127 resources <- find_external_resources("resources/period.Rmd")
128 expect_equal(nrow(resources), 0)
129 })
130
131
132 test_that("dependencies in .R files are recursively discovered", {
133 resources <- find_external_resources("resources/readcsv.Rmd")
134 expected <- data.frame(
135 path = c("empty.csv", "readcsv.R", "readcsv-source.R"),
136 explicit = c(FALSE, FALSE, FALSE),
137 web = c(FALSE, FALSE, FALSE),
138 stringsAsFactors = FALSE)
139
140 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
141 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
142 expect_equal(resources, expected)
143 })
144
145 test_that("implicitly discovered directories are ignored", {
146 resources <- find_external_resources("resources/directory-refs.Rmd")
147 expected <- data.frame(
148 path = c("nonempty/empty.csv", "nonempty/empty.jpg"),
149 explicit = c(TRUE, TRUE),
150 web = c(FALSE, TRUE),
151 stringsAsFactors = FALSE)
152
153 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
154 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
155 expect_equal(resources, expected)
156 })
157
158 test_that("resource_files use cases all work", {
159 resources <- find_external_resources("resources/resource-files.Rmd")
160 expected <- data.frame(
161 path = c("nonempty/empty.csv", "csvs/csv1.csv", "csvs/csv2.csv", "csvs/other/csv3.csv", "empty.bib"),
162 explicit = c(TRUE, TRUE, TRUE, TRUE, TRUE),
163 web = c(FALSE, FALSE, FALSE, FALSE, FALSE),
164 stringsAsFactors = FALSE)
165
166 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
167 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
168 expect_equal(resources, expected)
169 })
170
171 test_that("dependencies can be discovered on .R files directly", {
172 resources <- find_external_resources("resources/readcsv-source.R")
173 expected <- data.frame(
174 path = c("empty.csv", "readcsv.R"),
175 explicit = c(FALSE, FALSE),
176 web = c(FALSE, FALSE),
177 stringsAsFactors = FALSE)
178
179 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
180 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
181 expect_equal(resources, expected)
182 })
183
184 test_that("filenames with shell characters can use relative resource paths", {
185 # save current working directory
186 oldwd <- getwd()
187 on.exit(setwd(oldwd), add = TRUE)
188
189 file.rename("resources/file-exists.Rmd", "resources/file exists.Rmd")
190 on.exit(file.rename("resources/file exists.Rmd", "resources/file-exists.Rmd"), add = TRUE)
191
192 # render the file (contains an expression that stops if its resource is not
193 # present)
194 capture.output(output_file <- render("resources/file exists.Rmd"))
195 on.exit(unlink(output_file), add = TRUE)
196 })
197
198 test_that("resources not deleted when filenames contain shell characters", {
199 # save current working directory
200 oldwd <- setwd(test_path("resources"))
201 on.exit(setwd(oldwd), add = TRUE)
202
203 file.rename("file-exists.Rmd", "file exists.Rmd")
204 capture.output(unlink(render("file exists.Rmd")))
205 file.rename("file exists.Rmd", "file-exists.Rmd")
206 expect_true(file.exists("empty.csv"))
207 })
208
209 test_that("resources not deleted when intermediates_dir is same as input", {
210 # save current working directory
211 oldwd <- setwd(test_path("resources"))
212 on.exit(setwd(oldwd), add = TRUE)
213 capture.output(unlink(render("file-exists.Rmd", intermediates_dir = ".")))
214 expect_true(file.exists("empty.csv"))
215 })
216
217 test_that("empty quoted strings don't confuse resource discovery", {
218 resources <- find_external_resources("resources/quotes.Rmd")
219 expected <- data.frame(
220 path = c("empty.csv", "empty.tsv"),
221 explicit = c(FALSE, FALSE),
222 web = c(FALSE, FALSE),
223 stringsAsFactors = FALSE)
224
225 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
226 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
227 expect_equal(resources, expected)
228 })
229
230 test_that("resources are discovered in CSS files", {
231 resources <- find_external_resources("resources/has-css.Rmd")
232 expected <- data.frame(
233 path = c("empty.png", "has-image.css"),
234 explicit = c(FALSE, FALSE),
235 web = c(TRUE, TRUE),
236 stringsAsFactors = FALSE)
237
238 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
239 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
240 expect_equal(resources, expected)
241 })
242
243 test_that("resources are discovered in notebook files", {
244 resources <- find_external_resources("resources/r-notebook.Rmd")
245 expected <- data.frame(
246 path = c("tinyplot.png"),
247 explicit = c(FALSE),
248 web = c(TRUE),
249 stringsAsFactors = FALSE)
250
251 resources <- as.data.frame(resources[order(resources[[1]]), , drop = FALSE])
252 expected <- as.data.frame(expected[order(expected[[1]]), , drop = FALSE])
253 expect_equal(resources, expected)
254 })
255
256 test_that("multiple resources in the includes option can be discovered", {
257 resources <- find_external_resources("resources/multi-includes.Rmd")
258 expected <- data.frame(
259 path = c("dep1.html", "dep2.html"),
260 explicit = FALSE,
261 web = FALSE,
262 stringsAsFactors = FALSE)
263
264 expect_equal(resources, expected)
265 })
266
267 test_that("knitr child are correctly discovered as resources from chunk options", {
268 expect_child_resource_found <- function(child_opts, child) {
269 dir <- withr::local_tempdir("find-child")
270 withr::local_dir(dir)
271 rmd <- "test.Rmd"
272 xfun::write_utf8(
273 text = knitr::knit_expand(text = c("```{r,<<child_opts>>}", "```"), delim = c("<<", ">>")),
274 rmd
275 )
276 xfun::write_utf8(c("Content"), child)
277 expect_contains(find_external_resources(!!rmd)$path, !!child)
278 }
279 child <- "child.Rmd"
280 expect_child_resource_found('child="child.Rmd"', child)
281 expect_child_resource_found(' child = "child.Rmd"', child)
282 expect_child_resource_found('child=c("child.Rmd")', child)
283 expect_child_resource_found("child='child.Rmd'", child)
284 expect_child_resource_found(" child = 'child.Rmd'", child)
285 expect_child_resource_found("child=c('child.Rmd')", child)
286 })