Skip to content

Commit

Permalink
Switch tests to use BS5 (#2536)
Browse files Browse the repository at this point in the history
Fixes #2527
  • Loading branch information
hadley authored May 14, 2024
1 parent d72a902 commit 55c8f32
Show file tree
Hide file tree
Showing 22 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* BS5 templates no longer include empty link to logo when none exists (#2536).
* `build_articles()` now reports if you are missing alt-text for any images (#2357).
* `check_pkgdown()` and `pkgdown_sitrep()` have been unified so that they both report on the same problems. They now only differ in the style of their output: `pkgdown_sitrep()` reports whether each category is ok or not ok, while `check_pkgdown()` errors on the first issue (#2463).
* `build_site()` automatically runs `pkgdown_sitrep()` at the start of the process (#2380).
Expand Down
4 changes: 3 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ data_template <- function(pkg = ".", depth = 0L) {
name = pkg$package,
version = as.character(pkg$version)
)
out$logo <- list(src = logo_path(pkg, depth = depth))
if (has_logo(pkg)) {
out$logo <- list(src = logo_path(pkg, depth = depth))
}
out$site <- list(
root = up_path(depth),
title = pkg$meta$title %||% pkg$package
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/_snaps/build-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
Code
cat(examples)
Output
testpackage:::f()
#> [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441

2 changes: 0 additions & 2 deletions tests/testthat/_snaps/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package:
name: testpackage
version: 1.0.0
logo:
src: ~
site:
root: ''
title: testpackage
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/assets/articles-images/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
template:
bootstrap: 5

figures:
dev: "jpeg"
fig.ext: "jpg"
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/assets/articles/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template:
bootstrap: 5
3 changes: 3 additions & 0 deletions tests/testthat/assets/cname/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
url: https://testpackage.r-lib.org

template:
bootstrap: 5
3 changes: 3 additions & 0 deletions tests/testthat/assets/figure/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
template:
bootstrap: 5

figures:
dev: "jpeg"
fig.ext: "jpg"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
template:
bootstrap: 5
assets: pkgdown/assets
1 change: 1 addition & 0 deletions tests/testthat/assets/open-graph-customized/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
url: http://example.com/pkg

template:
bootstrap: 5
opengraph:
image:
src: man/figures/card.png
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/assets/open-graph/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
url: http://example.com/pkg

template:
bootstrap: 5
3 changes: 3 additions & 0 deletions tests/testthat/assets/reference-selector/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
url: http://test.org

template:
bootstrap: 5
3 changes: 3 additions & 0 deletions tests/testthat/assets/reference/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
url: http://test.org

template:
bootstrap: 5
2 changes: 2 additions & 0 deletions tests/testthat/assets/search-site/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
url: http://pkgdown.r-lib.org

template:
bootstrap: 3
params:
docsearch:
api_key: test-api-key
index_name: test-index-name

2 changes: 2 additions & 0 deletions tests/testthat/assets/sidebar/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template:
bootstrap: 5
3 changes: 3 additions & 0 deletions tests/testthat/assets/site-bad-logo/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
url: http://example.com/pkg

template:
bootstrap: 5
17 changes: 13 additions & 4 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ test_that("image links relative to output", {
skip_on_cran()
pkg <- local_pkgdown_site(test_path("assets/articles-images"))

suppressMessages(init_site(pkg))
suppressMessages(copy_figures(pkg))
suppressMessages(build_article("kitten", pkg))

html <- xml2::read_html(path(pkg$dst_path, "articles", "kitten.html"))
src <- xpath_attr(html, "//img", "src")
src <- xpath_attr(html, "//main//img", "src")

expect_equal(src, c(
# knitr::include_graphics()
Expand Down Expand Up @@ -47,8 +48,8 @@ test_that("warns about missing alt-text", {
})

test_that("articles don't include header-attrs.js script", {
pkg <- as_pkgdown(test_path("assets/articles"))
withr::defer(clean_site(pkg, quiet = TRUE))
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))

suppressMessages(path <- build_article("standard", pkg))

Expand All @@ -61,13 +62,15 @@ test_that("articles don't include header-attrs.js script", {

test_that("can build article that uses html_vignette", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))

# theme is not set since html_vignette doesn't support it
expect_snapshot(expect_error(build_article("html-vignette", pkg), NA))
})

test_that("can override html_document() options", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(path <- build_article("html-document", pkg))

# Check that number_sections is respected
Expand All @@ -84,6 +87,7 @@ test_that("can override html_document() options", {

test_that("html widgets get needed css/js", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(path <- build_article("widget", pkg))

html <- xml2::read_html(path)
Expand All @@ -96,6 +100,7 @@ test_that("html widgets get needed css/js", {

test_that("can override options with _output.yml", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(path <- build_article("html-document", pkg))

# Check that number_sections is respected
Expand All @@ -108,6 +113,7 @@ test_that("can set width", {
code:
width: 50
")
suppressMessages(init_site(pkg))

suppressMessages(path <- build_article("width", pkg))
html <- xml2::read_html(path)
Expand Down Expand Up @@ -160,6 +166,7 @@ test_that("articles in vignettes/articles/ are unnested into articles/", {
skip_on_cran()

pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(path <- build_article("articles/nested", pkg))

expect_equal(
Expand Down Expand Up @@ -263,6 +270,7 @@ test_that("check doesn't include getting started vignette", {

test_that("titles are escaped when needed", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(build_article(pkg = pkg, name = "needs-escape"))

html <- xml2::read_html(path(pkg$dst_path, "articles/needs-escape.html"))
Expand All @@ -273,10 +281,11 @@ test_that("titles are escaped when needed", {

test_that("output is reproducible by default, i.e. 'seed' is respected", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(init_site(pkg))
suppressMessages(build_article(pkg = pkg, name = "random"))

output <- xml2::read_html(path(pkg$dst_path, "articles/random.html")) %>%
rvest::html_node("div.contents > pre") %>%
rvest::html_node("main > pre") %>%
rvest::html_text() %>%
# replace line feeds with whitespace to make output platform independent
gsub("\r", "", .)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test_that("data_home_sidebar() works by default", {

test_that("data_home_sidebar() can be removed", {
pkg <- local_pkgdown_site(test_path("assets/sidebar"))
suppressMessages(init_site(pkg))
pkg$meta$home$sidebar <- FALSE
# not built by data_home_sidebar()
expect_false(data_home_sidebar(pkg))
Expand Down
20 changes: 13 additions & 7 deletions tests/testthat/test-build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ test_that("examples_env sets width", {
expect_equal(getOption("width"), 50)
})


test_that("test usage ok on rendered page", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
pkg <- local_pkgdown_site(test_path("assets/reference"), "
template:
bootstrap: 3
")
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "c"))

html <- xml2::read_html(path(pkg$dst_path, "reference", "c.html"))
expect_equal(xpath_text(html, "//div[@id='ref-usage']", trim = TRUE), "c()")
clean_site(pkg, quiet = TRUE)

pkg <- local_pkgdown_site(test_path("assets/reference"), "
template:
bootstrap: 5
")
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "c"))
html <- xml2::read_html(path(pkg$dst_path, "reference", "c.html"))
Expand All @@ -50,7 +51,9 @@ test_that("test usage ok on rendered page", {

test_that(".Rd without usage doesn't get Usage section", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "e"))

html <- xml2::read_html(path(pkg$dst_path, "reference", "e.html"))
expect_equal(xpath_length(html, "//div[@id='ref-usage']"), 0)
clean_site(pkg, quiet = TRUE)
Expand Down Expand Up @@ -93,10 +96,11 @@ test_that("pkgdown html dependencies are suppressed from examples in references"

test_that("examples are reproducible by default, i.e. 'seed' is respected", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "f"))

examples <- xml2::read_html(path(pkg$dst_path, "reference", "f.html")) %>%
rvest::html_node("div#ref-examples div.sourceCode") %>%
rvest::html_node("code .r-out") %>%
rvest::html_text() %>%
# replace line feeds with whitespace to make output platform independent
gsub("\r", "", .)
Expand All @@ -106,6 +110,7 @@ test_that("examples are reproducible by default, i.e. 'seed' is respected", {

test_that("arguments get individual ids", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "a"))

html <- xml2::read_html(path(pkg$dst_path, "reference", "a.html"))
Expand All @@ -115,6 +120,7 @@ test_that("arguments get individual ids", {

test_that("title and page title escapes html", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(init_site(pkg))
suppressMessages(build_reference(pkg, topics = "g"))

html <- xml2::read_html(path(pkg$dst_path, "reference", "g.html"))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-build-search-docs.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("docsearch.json and sitemap.xml are valid", {
test_that("docsearch.json and sitemap.xml are valid for BS 3 site", {
pkg <- local_pkgdown_site(test_path("assets/search-site"))

suppressMessages(build_site(pkg, new_process = FALSE))
Expand All @@ -10,7 +10,7 @@ test_that("docsearch.json and sitemap.xml are valid", {
expect_true(xml2::xml_validate(xml2::read_xml(xml), schema))
})

test_that("build_search() builds the expected search`.json with an URL", {
test_that("build_search() builds the expected search.json with an URL", {
pkg <- local_pkgdown_site(test_path("assets/news"), '
url: https://example.com
template:
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-figure.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test_that("can override defaults in _pkgdown.yml", {
withr::local_temp_libpaths()

pkg <- local_pkgdown_site(test_path("assets/figure"))
suppressMessages(init_site(pkg))

callr::rcmd("INSTALL", pkg$src_path, show = FALSE, fail_on_status = TRUE)

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ test_that("package_vignettes() sorts articles alphabetically by file name", {

test_that("override works correctly for as_pkgdown", {
pkgdown <- as_pkgdown(test_path("assets/articles-images"))
expected_list <- list(figures = list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1))
expect_equal(pkgdown$meta, expected_list)

expected_list <- list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1)
expect_equal(pkgdown$meta$figures, expected_list)

modified_pkgdown <- as_pkgdown(pkgdown, override = list(figures = list(dev = "png")))
modified_list <- list(figures = list(dev = "png", fig.ext = "jpg", fig.width = 3, fig.asp = 1))
modified_pkgdown$meta
expect_equal(modified_pkgdown$meta, modified_list)
expect_equal(modified_pkgdown$meta$figures$dev, "png")
})
# titles ------------------------------------------------------------------

Expand Down

0 comments on commit 55c8f32

Please sign in to comment.