Skip to content

Commit

Permalink
update tests and snapshots for cli outputs
Browse files Browse the repository at this point in the history
need to check snapshots carefully
  • Loading branch information
jayhesselberth committed Nov 26, 2023
1 parent f417002 commit 4764286
Show file tree
Hide file tree
Showing 63 changed files with 1,632 additions and 476 deletions.
10 changes: 5 additions & 5 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ data_home_sidebar_authors <- function(pkg = ".") {
bullets <- c(
markdown_text_inline(
pkg$meta$authors$sidebar$before,
pkgdown_field(pkg, c("authors", "sidebar", "before"))
pkgdown_field(c("authors", "sidebar", "before"))
),
authors,
markdown_text_inline(
pkg$meta$authors$sidebar$after,
pkgdown_field(pkg, c("authors", "sidebar", "after"))
pkgdown_field(c("authors", "sidebar", "after"))
)
)

Expand Down Expand Up @@ -96,7 +96,7 @@ author_name <- function(x, authors, pkg) {
if (!is.null(author$html)) {
name <- markdown_text_inline(
author$html,
pkgdown_field(pkg, c("authors", name, "html"))
pkgdown_field(c("authors", name, "html"))
)
}

Expand Down Expand Up @@ -186,8 +186,8 @@ role_lookup <- function(abbr) {

out <- unname(roles[abbr])
if (any(is.na(out))) {
missing <- paste0("'", abbr[is.na(out)], "'", collapse = ", ")
cli::cli_alert_warning("Unknown MARC role abbreviation{?s}: {missing}")
missing <- abbr[is.na(out)]
cli::cli_warn("Unknown MARC role abbreviation{?s}: {.field {missing}}")
out[is.na(out)] <- abbr[is.na(out)]
}
out
Expand Down
9 changes: 6 additions & 3 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ data_home_sidebar <- function(pkg = ".") {

if (length(html_path)) {
if (!file.exists(html_path)) {
cli::cli_abort(
"Can't find file {.file {html_path}} specified by {pkgdown_field(pkg, c('home', 'sidebar', 'html'))}"
)
rel_html_path <- fs::path_rel(html_path, pkg$src_path)
rel_config_path <- pkgdown_config_relpath(pkg)
cli::cli_abort(c(
"Can't locate {.file {rel_html_path}}",
"x" = "{.field {pkgdown_field(c('home', 'sidebar', 'html'))}} in {.file {rel_config_path}} is misconfigured"
))
}
return(read_file(html_path))
}
Expand Down
2 changes: 1 addition & 1 deletion R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ build_redirects <- function(pkg = ".",
build_redirect <- function(entry, index, pkg) {
if (!is.character(entry) || length(entry) != 2) {
cli::cli_abort(
"Entry {.emph index} in {pkgdown_field(pkg, 'redirects')} must be a character vector of length 2.",
"Entry {.emph {index}} in {.field {pkgdown_field('redirects')}} must be a character vector of length 2.",
)
}

Expand Down
30 changes: 8 additions & 22 deletions R/build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,10 @@ check_all_characters <- function(contents, index, pkg) {
any_null <- any(null)

if (any_null) {
abort(
c(
sprintf(
"Item %s in section %s in %s is empty.",
toString(which(null)),
index,
pkgdown_field(pkg, "reference")
),
i = "Either delete the empty line or add a function name."
)
)
cli::cli_abort(c(
"Item {.field {which(null)}} in section {index} in {.field {pkgdown_field('reference')}} is empty.",
i = "Either delete the empty line or add a function name in {.file {pkgdown_config_relpath(pkg)}}."
))
}

not_char <- !purrr::map_lgl(contents, is.character)
Expand All @@ -89,17 +82,10 @@ check_all_characters <- function(contents, index, pkg) {
return(invisible())
}

abort(
c(
sprintf(
"Item %s in section %s in %s must be a character.",
toString(which(not_char)),
index,
pkgdown_field(pkg, "reference")
),
i = "You might need to add '' around e.g. - 'N' or - 'off'."
)
)
cli::cli_abort(c(
"Item {.field {which(not_char)}} in section {index} in {.field {pkgdown_field('reference')}} must be a character.",
i = "You might need to add '' around e.g. - 'N' or - 'off' in {.file {pkgdown_config_relpath(pkg)}}."
))

}

Expand Down
4 changes: 2 additions & 2 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ check_bootstrap_version <- function(version, pkg = list()) {
cli::cli_warn("{.var bootstrap: 4} no longer supported, using {.var bootstrap: 5} instead")
5
} else {
field <- pkgdown_field(pkg, c("template", "bootstrap"))
field <- pkgdown_field(c("template", "bootstrap"))
cli::cli_abort(c(
"Boostrap version must be 3 or 5.",
"x" = "You specified a value of {.var {version}} in {.var {field}}"
"x" = "You specified a value of {.var {version}} in {.field {field}}."
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/rd-example.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ process_conditional_examples <- function(rd) {
if (!is_false) {
new_cond <- paste0("if (FALSE) { # ", cond_expr_str)
cli::cli_warn(
"@examplesIf condition {.var cond_expr_str} FALSE"
"@examplesIf condition {.var cond_expr_str} is FALSE"
)
} else {
new_cond <- "if (FALSE) {"
Expand Down
6 changes: 3 additions & 3 deletions R/rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ as_html.tag_deqn <- function(x, ...) {
as_html.tag_url <- function(x, ...) {
if (length(x) != 1) {
if (length(x) == 0) {
msg <- "Check for empty \\url{} tags."
msg <- "Check for empty \\url{{}} tags."
} else {
msg <- "This may be caused by a \\url tag that spans a line break."
}
Expand Down Expand Up @@ -229,7 +229,7 @@ as_html.tag_Sexpr <- function(x, ...) {
text = as.character(res),
rd = flatten_text(rd_text(as.character(res))),
hide = "",
cli::cli_abort("\\Sexpr{result=", results, "} not yet supported", call. = FALSE)
cli::cli_abort("\\Sexpr{result=", results, "} not yet supported")
)
}

Expand Down Expand Up @@ -565,7 +565,7 @@ parse_opts <- function(string) {
}

stop_bad_tag <- function(tag, msg = NULL) {
bad_tag <- paste0("\\\\", tag, "{}")
bad_tag <- paste0("\\", tag, "{}")
msg_abort <- 'Failed to parse tag {.var {bad_tag}}.'
if (!is.null(msg)) {
msg_abort <- c(msg_abort, "x" = msg)
Expand Down
8 changes: 4 additions & 4 deletions R/sitrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pkgdown_sitrep <- function(pkg = ".") {
warns <- c()

if (is.null(pkg$meta[["url"]])) {
warns <- c(warns, "x" = "{pkgdown_field(pkg, 'url')} is not configured. See vignette {.vignette pkgdown::metatdata}.")
warns <- c(warns, "x" = "{pkgdown_field('url')} is not configured in {.file {pkgdown_config_relpath(pkg)}}. See vignette {.vignette pkgdown::metatdata}.")
}

desc_urls <- pkg$desc$get_urls()
desc_urls <- sub("/$", "", urls)
if (!pkg$meta[["url"]] %in% desc_urls) {
warns <- c(warns, "x" = "DESCRIPTION {.field URL} is empty.")
desc_urls <- sub("/$", "", desc_urls)
if (length(desc_urls) == 0 || !pkg$meta[["url"]] %in% desc_urls) {
warns <- c(warns, "x" = "{.file DESCRIPTION} {.field URL} is empty.")
}

if (length(warns) == 0) {
Expand Down
4 changes: 2 additions & 2 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ check_bootswatch_theme <- function(bootswatch_theme, bs_version, pkg) {
sprintf(
"Can't find Bootswatch theme '%s' (%s) for Bootstrap version '%s' (%s).",
bootswatch_theme,
pkgdown_field(pkg, c("template", "bootswatch")),
pkgdown_field(c("template", "bootswatch")),
bs_version,
pkgdown_field(pkg, c("template", "bootstrap"))
pkgdown_field(c("template", "bootstrap"))
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/topics-external.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ get_rd_from_help <- function(package, alias) {
help <- utils::help((alias), (package))
if (length(help) == 0) {
fun <- paste0(package, "::", alias)
cli::cli_abort("Could not find documentation for {.fn fun}.")
cli::cli_abort("Could not find documentation for {.fn {fun}}.")
return()
}

Expand Down
24 changes: 5 additions & 19 deletions R/utils-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ check_yaml_has <- function(missing, where, pkg) {
}

missing_components <- lapply(missing, function(x) c(where, x))
missing_fields <- pkgdown_fields(pkg, missing_components)
missing_fields <- purrr::map_chr(missing_components, pkgdown_field)

cli::cli_abort(
"Can't find components: {missing_fields}."
"Can't find {.field {missing_fields}} component{?s} in {.file {pkgdown_config_relpath(pkg)}}."
)
}

Expand All @@ -19,27 +19,13 @@ yaml_character <- function(pkg, where) {
} else if (is.character(x)) {
x
} else {
fld <- pkgdown_field(pkg, where)
fld <- pkgdown_field(where)
cli::cli_abort("{fld} must be a character vector")
}
}

pkgdown_field <- function(pkg, field) {
pkgdown_fields(pkg, list(field))
}

pkgdown_fields <- function(pkg, fields, join = ", ") {
fields <- purrr::map_chr(fields, ~ paste0(cli::style_bold(.x), collapse = "."))
fields_str <- paste0(fields, collapse = join)

config_path <- pkgdown_config_path(pkg$src_path)

if (is.null(config_path)) {
fields_str
} else {
config <- src_path(pkgdown_config_relpath(pkg))
paste0('`', fields_str, "` in ", config)
}
pkgdown_field <- function(fields) {
purrr::map_chr(list(fields), ~ paste0(.x, collapse = "."))
}

# print helper ------------------------------------------------------------
Expand Down
116 changes: 108 additions & 8 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,116 @@
# warns about missing images
# links to man/figures are automatically relocated

Code
copy_figures(pkg)

---

Code
build_articles(pkg, lazy = FALSE)

# warns about missing images [plain]

Code
build_articles(pkg)
Message
-- Building articles --
i Writing articles/index.html
i Reading vignettes/html-vignette.Rmd
i Writing articles/html-vignette.html
Condition
Warning:
Missing images in 'vignettes/html-vignette.Rmd': 'foo.png'
i pkgdown can only use images in 'man/figures' and 'vignettes'

# warns about missing images [ansi]

Code
build_articles(pkg)
Condition
Warning:
Missing images in vignettes/html-vignette.Rmd: foo.png
i pkgdown can only use images in man/figures and vignettes

# warns about missing images [unicode]

Code
build_articles(pkg)
Condition
Warning:
Missing images in 'vignettes/html-vignette.Rmd': 'foo.png'
ℹ pkgdown can only use images in 'man/figures' and 'vignettes'

# warns about missing images [fancy]

Code
build_articles(pkg)
Condition
Warning:
Missing images in vignettes/html-vignette.Rmd: foo.png
ℹ pkgdown can only use images in man/figures and vignettes

# articles don't include header-attrs.js script

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

# can build article that uses html_vignette

Code
expect_error(build_article("html-vignette", pkg), NA)

# can override html_document() options

Code
path <- build_article("html-document", pkg)

# html widgets get needed css/js

Code
path <- build_article("widget", pkg)

# can override options with _output.yml

Code
path <- build_article("html-document", pkg)

# can set width

Code
path <- build_article("width", pkg)

# finds external resources referenced by R code in the article html

Code
path <- build_article("resources", pkg)

# BS5 article laid out correctly with and without TOC

Code
init_site(pkg)

---

Code
toc_true_path <- build_article("standard", pkg)

---

Code
toc_false_path <- build_article("toc-false", pkg)

# articles in vignettes/articles/ are unnested into articles/

Code
path <- build_article("articles/nested", pkg)

---

Code
build_redirects(pkg)

# pkgdown deps are included only once in articles

Code
init_site(pkg)

---

Code
path <- build_article("html-deps", pkg)

5 changes: 2 additions & 3 deletions tests/testthat/_snaps/build-favicons.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

Code
build_favicons(pkg)
Output
-- Building favicons -----------------------------------------------------------
Message
Favicons already exist in `pkgdown/`. Set `overwrite = TRUE` to re-create.
Favicons already exist in 'pkgdown'
i Set `overwrite = TRUE` to re-create.

9 changes: 0 additions & 9 deletions tests/testthat/_snaps/build-favicons.new.md

This file was deleted.

5 changes: 5 additions & 0 deletions tests/testthat/_snaps/build-github.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# a CNAME record is built if a url exists in metadata

Code
build_github_pages(pkg)

Loading

0 comments on commit 4764286

Please sign in to comment.