Skip to content

Commit

Permalink
Merge pull request #145 from quarto-dev/quarto-bin-sitrep
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Jan 29, 2024
2 parents 0a7f2b6 + 34c7f26 commit f986fb6
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: quarto
Title: R Interface to 'Quarto' Markdown Publishing System
Version: 1.3.10
Version: 1.3.11
Authors@R: c(
person("JJ", "Allaire", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0174-9868")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(is_using_quarto)
export(quarto_add_extension)
export(quarto_binary_sitrep)
export(quarto_create_project)
export(quarto_inspect)
export(quarto_path)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# quarto (development version)

- Add `quarto_binary_sitrep()` to check possible difference in Quarto binary used by this package, and the one used by RStudio IDE (thanks, @jthomasmock, #12).

- Add `quarto_create_project()` function that calls `quarto create project <type> <name>` (thanks, @maelle, #87).

- Add `is_using_quarto()` to check if a directory requires using Quarto (i.e. it has a `_quarto.yml` or at least one `*.qmd` file) (thanks, @hadley, #103).
Expand Down
93 changes: 91 additions & 2 deletions R/quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#'
#' @export
quarto_path <- function() {
path_env <- Sys.getenv("QUARTO_PATH", unset = NA)
path_env <- get_quarto_path_env()
if (is.na(path_env)) {
path <- unname(Sys.which("quarto"))
if (nzchar(path)) path else NULL
Expand All @@ -16,10 +16,19 @@ quarto_path <- function() {
}
}

get_quarto_path_env <- function() {
Sys.getenv("QUARTO_PATH", unset = NA_character_)
}

quarto_not_found_msg <- c(
"Quarto command-line tools path not found! ",
"Please make sure you have installed and added Quarto to your PATH or set the QUARTO_PATH environment variable."
)

find_quarto <- function() {
path <- quarto_path()
if (is.null(path)) {
stop("Quarto command-line tools path not found! Please make sure you have installed and added Quarto to your PATH or set the QUARTO_PATH environment variable.")
cli::cli_abort(quarto_not_found_msg)
} else {
return(path)
}
Expand Down Expand Up @@ -95,3 +104,83 @@ check_quarto_version <- function(ver, what, url) {
)
}
}

#' Check configurations for quarto binary used
#'
#' This function check the configuration for the quarto package R package to
#' detect a possible difference with version used by RStudio IDE.
#'
#' @param verbose if `FALSE`, only return the result of the check.
#' @param debug if `TRUE`, print more information about value set in configurations.
#'
#' @returns `TRUE` if this package should be using the same quarto binary as the
#' RStudio IDE. `FALSE` otherwise if a difference is detected or quarto is not
#' found. Use `verbose = TRUE` or`debug = TRUE` to get detailed information.
#' @examples
#' quarto_binary_sitrep(verbose = FALSE)
#' quarto_binary_sitrep(verbose = TRUE)
#' quarto_binary_sitrep(debug = TRUE)
#'
#' @export
quarto_binary_sitrep <- function(verbose = TRUE, debug = FALSE) {

quarto_found <- normalizePath(quarto_path(), mustWork = FALSE)
if (is.null(quarto_found)) {
if (verbose) {
cli::cli_alert_danger(quarto_not_found_msg)
}
return(FALSE)
}

same_config <- TRUE
if (debug) verbose <- TRUE


# Quarto R package situation ----
if (verbose) {
cli::cli_alert_success(c("i" = "quarto R package will use {.path {quarto_found}}"))
}

quarto_r_env <- normalizePath(get_quarto_path_env(), mustWork = FALSE)
quarto_system <- normalizePath(unname(Sys.which("quarto")), mustWork = FALSE)
# quarto R package will use QUARTO_PATH env var with higher priority than latest version on path $PATH
# and RStudio IDE does not use this environment variable
if (!is.na(quarto_r_env) && identical(quarto_r_env, quarto_found)) {
same_config <- FALSE
if (verbose) {
cli::cli_alert_warning(c(
"It is configured through {.envvar QUARTO_PATH} environment variable. ",
"RStudio IDE will likely use another binary."
))
}
} else if (nzchar(quarto_system) && identical(quarto_system, quarto_found)) {
if (debug) {
cli::cli_alert_info(c(
" It is configured to use the latest version found in the {.emph PATH} environment variable."
))
}
}

# RStudio IDE known situation ----

# RStudio IDE > Render button will use RSTUDIO_QUARTO env var with higher priority than latest version on path $PATH
rstudio_env <- Sys.getenv("RSTUDIO_QUARTO", unset = NA)
if (!is.na(rstudio_env)) {
rstudio_env <- normalizePath(rstudio_env, mustWork = FALSE)
if (!identical(rstudio_env, quarto_found)) {
same_config <- FALSE
if (verbose) {
cli::cli_alert_danger(c(
"RStudio IDE render button seems configured to use {.path {rstudio_env}}."
))
if (debug) {
cli::cli_alert_warning(c(
" It is configured through {.envvar RSTUDIO_QUARTO} environment variable."
))
}
}
}
}

return(same_config)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ reference:
- quarto_path
- quarto_version
- is_using_quarto
- quarto_binary_sitrep

28 changes: 28 additions & 0 deletions man/quarto_binary_sitrep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/testthat/_snaps/quarto.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,35 @@
Output
[1] FALSE

# quarto CLI sitrep

Code
quarto_binary_sitrep(debug = TRUE)
Message
v quarto R package will use '<QUARTO_PATH path>'
! It is configured through `QUARTO_PATH` environment variable. RStudio IDE will likely use another binary.
Output
[1] FALSE

---

Code
quarto_binary_sitrep(debug = TRUE)
Message
v quarto R package will use '<quarto full path>'
i It is configured to use the latest version found in the PATH environment variable.
x RStudio IDE render button seems configured to use '<RSTUDIO_QUARTO path>'.
! It is configured through `RSTUDIO_QUARTO` environment variable.
Output
[1] FALSE

---

Code
quarto_binary_sitrep(debug = TRUE)
Message
v quarto R package will use '<quarto full path>'
i It is configured to use the latest version found in the PATH environment variable.
Output
[1] TRUE

6 changes: 4 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ expect_snapshot_qmd_output <- function(name, input, output_file = NULL, ...) {
}


transform_quarto_cli_in_output <- function(full_path = FALSE) {
transform_quarto_cli_in_output <- function(full_path = FALSE, normalize_path = FALSE) {
return(
function(lines) {
if (full_path) {
lines <- gsub(find_quarto(), "<quarto full path>", lines, fixed = TRUE)
quarto_found <- find_quarto()
if (normalize_path) {quarto_found <- normalizePath(quarto_found, mustWork = FALSE)}
lines <- gsub(quarto_found, "<quarto full path>", lines, fixed = TRUE)
# seems like there are quotes around path in CI windows
lines <- gsub("\"<quarto full path>\"", "<quarto full path>", lines, fixed = TRUE)
return(lines)
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,35 @@ test_that("is_using_quarto correctly check directory", {
withr::local_dir(dirname(qmd))
withr::local_file("test.txt")
})

test_that("quarto CLI sitrep", {
skip_if_no_quarto()
skip_on_cran()
local_reproducible_output(width = 1000)
dummy_quarto_path <- normalizePath("dummy", mustWork = FALSE)
withr::with_envvar(
list(QUARTO_PATH = dummy_quarto_path, RSTUDIO_QUARTO = NA),
expect_snapshot(
quarto_binary_sitrep(debug = TRUE),
transform = function(lines) gsub(dummy_quarto_path, "<QUARTO_PATH path>", lines, fixed = TRUE)
)
)
withr::with_envvar(
list(QUARTO_PATH = NA, RSTUDIO_QUARTO = dummy_quarto_path),
expect_snapshot(
quarto_binary_sitrep(debug = TRUE),
transform = function(lines) {
lines <- gsub(dummy_quarto_path, "<RSTUDIO_QUARTO path>", lines, fixed = TRUE)
transform_quarto_cli_in_output(full_path = TRUE, normalize_path = TRUE)(lines)
}
)
)

withr::with_envvar(
list(QUARTO_PATH = NA, RSTUDIO_QUARTO = NA),
expect_snapshot(
quarto_binary_sitrep(debug = TRUE),
transform = transform_quarto_cli_in_output(full_path = TRUE, normalize_path = TRUE)
)
)
})

0 comments on commit f986fb6

Please sign in to comment.