Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that favicons are present and up-to-date #2611

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -83,7 +85,6 @@ 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) {
cli::cli_abort(
c("{.file DESCRIPTION} {.field URL} lacks package url ({url}).", details),
Expand All @@ -92,3 +93,26 @@ check_urls <- function(pkg = ".", call = caller_env()) {
}
}
}

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 {.fn build_favicons}?"
hadley marked this conversation as resolved.
Show resolved Hide resolved
))
}
} else {
cli::cli_abort(c(
"Found package logo but not favicons.",
i = "Do you need to run {.fn build_favicons}?"
hadley marked this conversation as resolved.
Show resolved Hide resolved
))
}
}
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 @@ -273,7 +273,6 @@ file_digest <- function(path) {
}
}


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

Expand Down
Empty file added logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
'DESCRIPTION' URL lacks 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 @@ -43,6 +47,7 @@
Message
-- Sitrep ----------------------------------------------------------------------
v URLs ok.
v Favicons ok.
v Open graph metadata ok.
v Articles metadata ok.
v Reference metadata ok.
Expand All @@ -69,3 +74,21 @@
! 'DESCRIPTION' URL lacks 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()`?

27 changes: 26 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,24 @@ 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
hadley marked this conversation as resolved.
Show resolved Hide resolved
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))
})
Loading