Skip to content

Commit

Permalink
Add safety around vignette engine when no quarto is available (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Feb 2, 2024
1 parent b1733d7 commit fc18963
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
47 changes: 47 additions & 0 deletions R/utils-vignettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ vig_engine <- function(..., quarto_format) {
vweave_quarto <- function(format) {
meta <- get_meta(format)
function(file, driver, syntax, encoding, quiet = FALSE, ...) {
# protect if Quarto is not installed
if (is.null(quarto_path())) {
msg <- c(
"Quarto binary is required to build Quarto vignettes but is not available.",
i = "Please make sure it is installed and found by {.code find_quarto()}."
)
if (is_R_CMD_check()) {
cli::cli_inform(msg)
} else {
cli::cli_abort(msg, call = NULL)
}
return(vweave_empty(file))
}
quarto_render(file, ..., output_format = format, metadata = meta)
}
}
Expand All @@ -29,6 +42,18 @@ get_meta <- function(format) {
if (format == "html") {
return(get_meta_for_html())
}
if (format == "pdf") {
return(get_meta_for_pdf())
}
}

get_meta_for_pdf <- function() {
meta <- list()
meta$format$pdf <- list(
# don't try to install CTAN package on CRAN environment
`latex-auto-install` = !is_cran_check()
)
meta
}

get_meta_for_html <- function() {
Expand All @@ -45,3 +70,25 @@ get_meta_for_html <- function() {
)
meta
}

is_R_CMD_check <- function() {
!is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA)) ||
tolower(Sys.getenv("_R_CHECK_LICENSE_")) == "true"
}

# from knitr internal
is_cran_check <- function() {
is_cran() && is_R_CMD_check()
}

is_cran = function() {
!rlang::is_interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false")))
}

# trick from knitr to avoid problem on R CMD check (e.g. when no Quarto available)
# It will silently skip the vignette
vweave_empty <- function(file, ..., .reason = 'Quarto') {
out <- sprintf("%s.html", tools::file_path_sans_ext(basename(file)))
writeLines(sprintf("The vignette could not be built because %s is not available.", .reason), out)
out
}
16 changes: 16 additions & 0 deletions tests/testthat/test-utils-vignettes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("don't auto install CTAN package on CRAN", {
skip_on_cran()
skip_if_not_installed("withr")
withr::with_envvar(list(
# simulate non CRAN
"NOT_CRAN" = "false",
# simulate R CMD check
"_R_CHECK_LICENSE_" = "true"), {
expect_false(get_meta_for_pdf()$format$pdf$`latex-auto-install`)
})
withr::with_envvar(list(
"_R_CHECK_PACKAGE_NAME_" = NA
), {
expect_true(get_meta_for_pdf()$format$pdf$`latex-auto-install`)
})
})

0 comments on commit fc18963

Please sign in to comment.