Skip to content

Commit 02bd3c5

Browse files
committed
Add quarto_available() function for TRUE / FALSE check of quarto being found
1 parent 8b2a3ba commit 02bd3c5

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-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.9008
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")),

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
- Add several new wrapper function (thanks, @parmsam, #192):
46
- `quarto_list_extensions()` to list installed extensions using `quarto list extensions`
57
- `quarto_remove_extension()` to remove an installed extension using `quarto remove extensions`

R/quarto.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,70 @@ quarto_version <- function() {
5252
as.numeric_version(system2(quarto_bin, "--version", stdout = TRUE))
5353
}
5454

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

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)