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

Add safety around vignette engine when no quarto is available #150

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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`)
})
})
Loading