-
Notifications
You must be signed in to change notification settings - Fork 323
/
test-build-articles.R
194 lines (152 loc) · 6.38 KB
/
test-build-articles.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
test_that("can recognise intro variants", {
expect_true(article_is_intro("package", "package"))
expect_true(article_is_intro("articles/package", "package"))
expect_true(article_is_intro("pack-age", "pack.age"))
expect_true(article_is_intro("articles/pack-age", "pack.age"))
})
test_that("links to man/figures are automatically relocated", {
# weird path differences that I don't have the energy to dig into
skip_on_cran()
pkg <- local_pkgdown_site(test_path("assets/man-figures"))
expect_output(copy_figures(pkg))
expect_output(build_articles(pkg, lazy = FALSE))
html <- xml2::read_html(path(pkg$dst_path, "articles", "kitten.html"))
src <- xpath_attr(html, "//img", "src")
expect_equal(src, c(
"../reference/figures/kitten.jpg",
"../reference/figures/kitten.jpg",
"another-kitten.jpg"
))
# And files aren't copied
expect_false(dir_exists(path(pkg$dst_path, "man")))
})
test_that("warns about missing images", {
skip_if_not_installed("rlang", "0.99")
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
expect_snapshot(build_articles(pkg))
})
test_that("articles don't include header-attrs.js script", {
pkg <- as_pkgdown(test_path("assets/articles"))
withr::defer(clean_site(pkg))
expect_output(path <- build_article("standard", pkg))
html <- xml2::read_html(path)
js <- xpath_attr(html, ".//body//script", "src")
# included for pandoc 2.7.3 - 2.9.2.1 improve accessibility
js <- js[basename(js) != "empty-anchor.js"]
expect_equal(js, character())
})
test_that("can build article that uses html_vignette", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
# theme is not set since html_vignette doesn't support it
expect_output(expect_error(build_article("html-vignette", pkg), NA))
})
test_that("can override html_document() options", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
expect_output(path <- build_article("html-document", pkg))
# Check that number_sections is respected
html <- xml2::read_html(path)
expect_equal(xpath_text(html, ".//h2//span"), c("1", "2"))
# But title isn't affected
expect_equal(xpath_text(html, ".//h1"), "html_document + as_is")
# And no links or scripts are inlined
expect_equal(xpath_length(html, ".//body//link"), 0)
expect_equal(xpath_length(html, ".//body//script"), 0)
})
test_that("html widgets get needed css/js", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
expect_output(path <- build_article("widget", pkg))
html <- xml2::read_html(path)
css <- xpath_attr(html, ".//body//link", "href")
js <- xpath_attr(html, ".//body//script", "src")
expect_true("diffviewer.css" %in% basename(css))
expect_true("diffviewer.js" %in% basename(js))
})
test_that("can override options with _output.yml", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
expect_output(path <- build_article("html-document", pkg))
# Check that number_sections is respected
html <- xml2::read_html(path)
expect_equal(xpath_text(html, ".//h2//span"), c("1", "2"))
})
test_that("can set width", {
pkg <- local_pkgdown_site(test_path("assets/articles"), "
code:
width: 50
")
expect_output(path <- build_article("width", pkg))
html <- xml2::read_html(path)
expect_equal(xpath_text(html, ".//pre")[[2]], "## [1] 50")
})
test_that("finds external resources referenced by R code in the article html", {
# weird path differences that I don't have the energy to dig into
skip_on_cran()
pkg <- local_pkgdown_site(test_path("assets", "articles-resources"))
expect_output(path <- build_article("resources", pkg))
# ensure that we the HTML references `<img src="external.png" />` directly
expect_equal(
xpath_attr(xml2::read_html(path), ".//img", "src"),
"external.png"
)
# expect that `external.png` was copied to the rendered article directory
expect_true(
file_exists(path(path_dir(path), "external.png"))
)
})
test_that("BS5 sidebar is removed if TOC is not used", {
pkg <- local_pkgdown_site(test_path("assets/articles"), "
template:
bootstrap: 5
")
expect_output(init_site(pkg))
expect_output(toc_false_path <- build_article("toc-false", pkg))
toc_false_html <- xml2::read_html(toc_false_path)
# We don't have a main with .col-md-9 if TOC isn't present
xpath_contents <- ".//main[contains(@class, 'col-md-9')]"
expect_equal(xpath_length(toc_false_html, xpath_contents), 0)
# The #pkgdown-sidebar is suppressed if the article has toc: false
expect_equal(xpath_length(toc_false_html, ".//aside"), 0)
})
test_that("articles in vignettes/articles/ are unnested into articles/", {
# weird path differences that I don't have the energy to dig into
skip_if(identical(R.version$crt, "ucrt"))
pkg <- local_pkgdown_site(test_path("assets/articles"))
expect_output(path <- build_article("articles/nested", pkg))
expect_equal(path, file.path(pkg$dst_path, "articles", "nested.html"))
# Check automatic redirect from articles/articles/foo.html -> articles/foo.html
pkg$meta$url <- "https://example.com"
expect_output(build_redirects(pkg))
# Check that the redirect file exists in <dst>/articles/articles/
redirect_path <- path(pkg$dst_path, "articles", "articles", "nested.html")
expect_true(file_exists(redirect_path))
# Check that we redirect to correct location
html <- xml2::read_html(redirect_path)
expect_match(
xpath_attr(html, ".//meta[@http-equiv = 'refresh']", "content"),
"https://example.com/articles/nested.html",
fixed = TRUE
)
})
test_that("pkgdown deps are included only once in articles", {
pkg <- local_pkgdown_site(test_path("assets/articles"), "
template:
bootstrap: 5
")
expect_output(init_site(pkg))
expect_output(path <- build_article("html-deps", pkg))
html <- xml2::read_html(path)
# jquery is only loaded once, even though it's also added by code in the article
expect_equal(xpath_length(html, ".//script[(@src and contains(@src, '/jquery'))]"), 1)
# same for bootstrap js and css
str_subset_bootstrap <- function(x) {
bs_rgx <- "bootstrap-[\\d.]+" # ex: bootstrap-5.1.0 not bootstrap-toc,
grep(bs_rgx, x, value = TRUE, perl = TRUE)
}
bs_js_src <- str_subset_bootstrap(
xpath_attr(html, ".//script[(@src and contains(@src, '/bootstrap'))]", "src")
)
expect_length(bs_js_src, 1)
bs_css_href <- str_subset_bootstrap(
xpath_attr(html, ".//link[(@href and contains(@href, '/bootstrap'))]", "href")
)
expect_length(bs_css_href, 1)
})