Skip to content

Commit

Permalink
Check that favicons are present and up-to-date (#2611)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored May 31, 2024
1 parent 44f90ab commit 85411c1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 9 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)

* `pkgdown_sitrep()`/`check_pkgdown()` now check that you have up-to-date favicons if you have a package logo.
* pkgdown now uses httr2 instead of httr (#2600).
* New `template.math-rendering` allows you to control how math is rendered across your site. The default uses `mathml` which is low-dependency, but has the lowest fidelity. You can also use `mathjax`, the previous default, and `katex`, a faster alternative. (#1966).
* Mathjax now uses version 3.2.2.
Expand Down
5 changes: 4 additions & 1 deletion R/build-favicons.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ copy_favicons <- function(pkg = ".") {

has_favicons <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
unname(file_exists(path_favicons(pkg)))
}

unname(file_exists(path(pkg$src_path, "pkgdown", "favicon")))
path_favicons <- function(pkg) {
path(pkg$src_path, "pkgdown", "favicon")
}
26 changes: 25 additions & 1 deletion R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ pkgdown_sitrep <- function(pkg = ".") {
}

error_to_sitrep("URLs", check_urls(pkg))
error_to_sitrep("Favicons", check_favicons(pkg))
error_to_sitrep("Open graph metadata", data_open_graph(pkg))
error_to_sitrep("Articles metadata", data_articles_index(pkg))
error_to_sitrep("Reference metadata", data_reference_index(pkg))

}

error_to_sitrep <- function(title, code) {
Expand Down Expand Up @@ -80,10 +82,32 @@ check_urls <- function(pkg = ".", call = caller_env()) {
} else {
desc_urls <- pkg$desc$get_urls()
desc_urls <- sub("/$", "", desc_urls)

if (!pkg$meta[["url"]] %in% desc_urls) {
msg <- "{.field URL} is missing package url ({url})."
config_abort(pkg, c(msg, details), path = "DESCRIPTION", call = call)
}
}
}

check_favicons <- function(pkg) {
if (!has_logo(pkg)) {
return()
}

if (has_favicons(pkg)) {
logo <- find_logo(pkg$src_path)
favicon <- path(path_favicons(pkg), "favicon.ico")

if (out_of_date(logo, favicon)) {
cli::cli_abort(c(
"Package logo is newer than favicons.",
i = "Do you need to rerun {.run [build_favicons()](pkgdown::build_favicons())}?"
))
}
} else {
cli::cli_abort(c(
"Found package logo but not favicons.",
i = "Do you need to run {.run [build_favicons()](pkgdown::build_favicons())}?"
))
}
}
15 changes: 10 additions & 5 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ local_envvar_pkgdown <- function(pkg, scope = parent.frame()) {
)
}

local_pkgdown_site <- function(path = NULL, meta = list(), env = caller_env()) {
local_pkgdown_site <- function(path = NULL,
meta = list(),
desc = list(),
env = caller_env()) {
check_string(path, allow_null = TRUE)

dst_path <- withr::local_tempdir(.local_envir = env)
Expand All @@ -27,10 +30,12 @@ local_pkgdown_site <- function(path = NULL, meta = list(), env = caller_env()) {
if (is.null(path)) {
path <- withr::local_tempdir(.local_envir = env)

desc <- desc::desc("!new")
desc$set("Package", "testpackage")
desc$set("Title", "A test package")
desc$write(file = path(path, "DESCRIPTION"))
description <- desc::desc("!new")
description$set("Package", "testpackage")
description$set("Title", "A test package")
if (length(desc) > 0)
inject(description$set(!!!desc))
description$write(file = path(path, "DESCRIPTION"))

# Default to BS5 only if template not specified
meta$template <- meta$template %||% list(bootstrap = 5)
Expand Down
1 change: 0 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ file_digest <- function(path) {
}
}


made_by_pkgdown <- function(path) {
if (!file_exists(path)) return(TRUE)

Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
x Bootstrap 3 is deprecated; please switch to Bootstrap 5.
i Learn more at <https://www.tidyverse.org/blog/2021/12/pkgdown-2-0-0/#bootstrap-5>.
v URLs ok.
x Favicons not ok.
Found package logo but not favicons.
Do you need to run `build_favicons()`?
v Open graph metadata ok.
v Articles metadata ok.
v Reference metadata ok.
Expand All @@ -20,6 +23,7 @@
x URLs not ok.
In DESCRIPTION, URL is missing package url (http://test.org).
See details in `vignette(pkgdown::metadata)`.
v Favicons ok.
v Open graph metadata ok.
v Articles metadata ok.
x Reference metadata not ok.
Expand All @@ -42,6 +46,7 @@
Message
-- Sitrep ----------------------------------------------------------------------
v URLs ok.
v Favicons ok.
v Open graph metadata ok.
v Articles metadata ok.
v Reference metadata ok.
Expand All @@ -68,3 +73,21 @@
! In DESCRIPTION, URL is missing package url (https://testpackage.r-lib.org).
i See details in `vignette(pkgdown::metadata)`.

# check_favicons reports problems

Code
check_favicons(pkg)
Condition
Error in `check_favicons()`:
! Found package logo but not favicons.
i Do you need to run `build_favicons()`?

---

Code
check_favicons(pkg)
Condition
Error in `check_favicons()`:
! Package logo is newer than favicons.
i Do you need to rerun `build_favicons()`?

28 changes: 27 additions & 1 deletion tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ test_that("checks fails on first problem", {
})

test_that("both inform if everything is ok", {
pkg <- test_path("assets/open-graph")
pkg <- local_pkgdown_site(
meta = list(url = "https://example.com"),
desc = list(URL = "https://example.com")
)

expect_snapshot({
pkgdown_sitrep(pkg)
check_pkgdown(pkg)
Expand All @@ -47,3 +51,25 @@ test_that("check_urls reports problems", {
pkg <- test_path("assets/cname")
expect_snapshot(check_urls(pkg), error = TRUE)
})

# check favicons --------------------------------------------------------------

test_that("check_favicons reports problems", {
pkg <- local_pkgdown_site()

# no logo no problems
expect_no_error(check_favicons(pkg))

# logo but no favicons
file_touch(path(pkg$src_path, "logo.svg"))
expect_snapshot(check_favicons(pkg), error = TRUE)

# logo and old favicons
dir_create(path_favicons(pkg))
file_touch(path(path_favicons(pkg), "favicon.ico"), Sys.time() - 86400)
expect_snapshot(check_favicons(pkg), error = TRUE)

# logo and new favicons
file_touch(path(path_favicons(pkg), "favicon.ico"), Sys.time() + 86400)
expect_no_error(check_favicons(pkg))
})

0 comments on commit 85411c1

Please sign in to comment.