Skip to content

Commit 45ef579

Browse files
authored
Add quarto_available() function for TRUE / FALSE check (#247)
* Add quarto_available() function for TRUE / FALSE check of quarto being found * document * add link to other docs * Add to pkgdown website
1 parent 520b97c commit 45ef579

File tree

9 files changed

+150
-1
lines changed

9 files changed

+150
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9009
3+
Version: 1.4.4.9010
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(is_using_quarto)
44
export(quarto_add_extension)
5+
export(quarto_available)
56
export(quarto_binary_sitrep)
67
export(quarto_create_project)
78
export(quarto_inspect)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# quarto (development version)
22

3+
- Add `quarto_available()` function to check if Quarto CLI is found (thanks, @hadley, #187).
4+
35
- `quarto_render()` now correctly set `as_job` when not inside RStudio IDE and required **rstudioapi** functions are not available (#203).
46

57
- Add several new wrapper function (thanks, @parmsam, #192):

R/quarto.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#'
88
#' @return Path to quarto binary (or `NULL` if not found)
99
#'
10+
#' @seealso [quarto_version()] to check the version of the binary found, [quarto_available()] to check if Quarto CLI is available and meets some requirements.
11+
#'
1012
#' @export
1113
quarto_path <- function(normalize = TRUE) {
1214
path_env <- get_quarto_path_env()
@@ -46,12 +48,77 @@ find_quarto <- function() {
4648
#' If it returns `99.9.9` then it means you are using a dev version.
4749
#'
4850
#' @return a [`numeric_version`][base::numeric_version] with the quarto version found
51+
#' @seealso [quarto_available()] to check if the version meets some requirements.
4952
#' @export
5053
quarto_version <- function() {
5154
quarto_bin <- find_quarto()
5255
as.numeric_version(system2(quarto_bin, "--version", stdout = TRUE))
5356
}
5457

58+
59+
#' Check if quarto is available and version meet some requirements
60+
#'
61+
#' This function allows to test if Quarto is available and meets version requirement, a min, max or
62+
#' in between requirement.
63+
#'
64+
#' If `min` and `max` are provided, this will check if Quarto version is
65+
#' in-between two versions. If non is provided (keeping the default `NULL` for
66+
#' both), it will just check for Quarto availability version and return `FALSE` if not found.
67+
#'
68+
#' @param min Minimum version expected.
69+
#' @param max Maximum version expected
70+
#' @param error If `TRUE`, will throw an error if Quarto is not available or does not meet the requirement. Default is `FALSE`.
71+
#'
72+
#' @return logical. `TRUE` if requirement is met, `FALSE` otherwise.
73+
#'
74+
#' @examples
75+
#' # Is there an active version available ?
76+
#' quarto_available()
77+
#' # check for a minimum requirement
78+
#' quarto_available(min = "1.5")
79+
#' # check for a maximum version
80+
#' quarto_available(max = "1.6")
81+
#' # only returns TRUE if Pandoc version is between two bounds
82+
#' quarto_available(min = "1.4", max = "1.6")
83+
#'
84+
#' @export
85+
quarto_available <- function(min = NULL, max = NULL, error = FALSE) {
86+
found <- FALSE
87+
is_above <- is_below <- TRUE
88+
if (!is.null(min) && !is.null(max)) {
89+
if (min > max) {
90+
cli::cli_abort(c(
91+
"Minimum version {.strong {min}} cannot be greater than maximum version {.strong {max}}."
92+
))
93+
}
94+
}
95+
quarto_version <- tryCatch(
96+
quarto_version(),
97+
error = function(e) NULL
98+
)
99+
if (!is.null(quarto_version)) {
100+
if (!is.null(min)) is_above <- quarto_version >= min
101+
if (!is.null(max)) is_below <- quarto_version <= max
102+
found <- is_above && is_below
103+
}
104+
if (!found && error) {
105+
cli::cli_abort(c(
106+
if (is.null(min) && is.null(max)) {
107+
"Quarto is not available."
108+
} else {
109+
"Quarto version requirement not met."
110+
},
111+
"*" = if (!is_above) {
112+
paste0(" Minimum version expected is ", min, ".")
113+
},
114+
"*" = if (!is_below) {
115+
paste0(" Maximum version expected is ", max, ".")
116+
}
117+
))
118+
}
119+
return(found)
120+
}
121+
55122
#' @importFrom processx run
56123
quarto_run <- function(
57124
args = character(),

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ reference:
4343
- quarto_inspect
4444
- quarto_path
4545
- quarto_version
46+
- quarto_available
4647
- is_using_quarto
4748
- quarto_binary_sitrep
4849

man/quarto_available.Rd

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/quarto_path.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/quarto_version.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-quarto.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,37 @@ test_that("quarto.quiet options controls echo and overwrite function argument",
122122
expect_output(quarto_render(qmd, quiet = TRUE))
123123
})
124124
})
125+
126+
test_that("quarto_available()", {
127+
expect_error(
128+
quarto_available("1.5", "1.4"),
129+
regexp = "Minimum version .* cannot be greater than maximum version .*"
130+
)
131+
132+
# Mock no quarto found
133+
with_mocked_bindings(
134+
quarto_version = function(...) NULL,
135+
{
136+
expect_false(quarto_available())
137+
expect_error(quarto_available(error = TRUE))
138+
}
139+
)
140+
141+
local_mocked_bindings(
142+
quarto_version = function(...) as.numeric_version("1.8.4")
143+
)
144+
145+
expect_true(quarto_available())
146+
expect_true(quarto_available("1", "2"))
147+
expect_false(quarto_available(max = "1.5"))
148+
expect_error(
149+
quarto_available(max = "1.6", error = TRUE),
150+
regexp = "Maximum version expected is 1.6"
151+
)
152+
expect_true(quarto_available(min = "1.8.4"))
153+
expect_false(quarto_available(min = "1.9.5"))
154+
expect_error(
155+
quarto_available(min = "1.9.5", error = TRUE),
156+
regexp = "Minimum version expected is 1.9.5"
157+
)
158+
})

0 commit comments

Comments
 (0)