Skip to content

Commit 59b4cb6

Browse files
committed
Add non verbose mode with invisible TRUE/FALSE output
1 parent e47d571 commit 59b4cb6

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

R/utils-versions.R

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
#' @param version Character string specifying the Quarto version to check.
88
#' Defaults to the currently installed version detected by [quarto_version()].
99
#' Use "99.9.9" to indicate a development version.
10+
#' @param verbose Logical indicating whether to print informational messages.
11+
#' Defaults to `TRUE`. When `FALSE`, the function runs silently and only
12+
#' returns the logical result.
1013
#'
11-
#' @return Invisibly returns `NULL`. The function is called for its side effects
12-
#' of printing informational messages about version status.
14+
#' @return Invisibly returns a logical value:
15+
#' - `TRUE` if an update is available
16+
#' - `FALSE` if no update is needed or when using development version
17+
#' The function is primarily called for its side effects of printing
18+
#' informational messages (when `verbose = TRUE`).
1319
#'
1420
#' @details
1521
#' The function handles three scenarios:
@@ -34,17 +40,32 @@
3440
#' # Check development version (will skip check)
3541
#' check_newer_version("99.9.9")
3642
#'
43+
#' # Check silently without messages
44+
#' result <- check_newer_version(verbose = FALSE)
45+
#' if (result) {
46+
#' message("Update available!")
47+
#' }
48+
#'
3749
#' @seealso
3850
#' [quarto_version()] for getting the current Quarto version,
3951
#'
4052
#' @export
41-
check_newer_version <- function(version = quarto_version()) {
53+
check_newer_version <- function(version = quarto_version(), verbose = TRUE) {
54+
inform_if_verbose <- function(...) {
55+
void <- function(...) invisible(NULL)
56+
if (verbose) {
57+
return(cli::cli_inform(...))
58+
} else {
59+
return(void(...))
60+
}
61+
}
62+
4263
if (version == "99.9.9") {
43-
rlang::inform(c(
64+
inform_if_verbose(c(
4465
"i" = "Skipping version check for development version.",
4566
">" = "Please update using development mode."
4667
))
47-
return(invisible())
68+
return(invisible(FALSE))
4869
}
4970
stable <- latest_available_version("stable")
5071
if (version > stable) {
@@ -54,27 +75,33 @@ check_newer_version <- function(version = quarto_version()) {
5475
} else {
5576
update <- FALSE
5677
}
57-
cli::cli_inform(c(
58-
"i" = "You are using prerelease version of Quarto: {version}.",
59-
if (update) {
60-
">" = "A newer version is available: {prerelease}. You can download it from {.url https://quarto.org/docs/download/prerelease.html} or your preferred package manager if available."
61-
} else {
62-
"v" = "You are using the latest prerelease version."
63-
}
64-
))
78+
inform_if_verbose(
79+
c(
80+
"i" = "You are using prerelease version of Quarto: {version}.",
81+
if (update) {
82+
">" = "A newer version is available: {prerelease}. You can download it from {.url https://quarto.org/docs/download/prerelease.html} or your preferred package manager if available."
83+
} else {
84+
"v" = "You are using the latest prerelease version."
85+
}
86+
)
87+
)
88+
return(invisible(update))
6589
} else if (version < stable) {
66-
cli::cli_inform(c(
90+
inform_if_verbose(c(
6791
"i" = "You are using an older version of Quarto: {version}.",
6892
" " = "The latest stable version is: {stable}.",
6993
">" = "You can download new version from https://quarto.org/docs/download/ or your preferred package manager if available."
7094
))
95+
return(invisible(TRUE))
7196
} else {
72-
cli::cli_inform(c(
97+
inform_if_verbose(c(
7398
"i" = "You are using the latest stable version of Quarto: {version}."
7499
))
100+
return(invisible(FALSE))
75101
}
76102
}
77103

104+
78105
versions_urls <- list(
79106
stable = "https://quarto.org/docs/download/_download.json",
80107
prerelease = "https://quarto.org/docs/download/_prerelease.json"

tests/testthat/test-utils-versions.R

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,19 @@ test_that("latest_available_published works", {
4545

4646
test_that("check_newer_version handles development version", {
4747
expect_message(
48-
check_newer_version("99.9.9"),
49-
"Skipping version check for development version"
48+
expect_invisible(
49+
expect_false(
50+
check_newer_version("99.9.9"),
51+
"Skipping version check for development version"
52+
)
53+
)
54+
)
55+
expect_no_message(
56+
expect_invisible(
57+
expect_false(
58+
check_newer_version("99.9.9", FALSE)
59+
)
60+
)
5061
)
5162
})
5263

@@ -56,8 +67,8 @@ test_that("check_newer_version handles stable version scenarios", {
5667
return("1.5.3")
5768
}
5869
)
59-
expect_snapshot(check_newer_version("1.0.0"))
60-
expect_snapshot(check_newer_version("1.5.3"))
70+
expect_snapshot(expect_invisible(expect_true(check_newer_version("1.0.0"))))
71+
expect_snapshot(expect_invisible(expect_false(check_newer_version("1.5.3"))))
6172
})
6273

6374
test_that("check_newer_version handles prerelease version scenarios", {
@@ -72,10 +83,18 @@ test_that("check_newer_version handles prerelease version scenarios", {
7283
)
7384

7485
expect_snapshot(
75-
check_newer_version("1.6.3")
86+
expect_invisible(
87+
expect_true(
88+
check_newer_version("1.6.3")
89+
)
90+
)
7691
)
7792
expect_snapshot(
78-
check_newer_version("1.6.5")
93+
expect_invisible(
94+
expect_false(
95+
check_newer_version("1.6.5")
96+
)
97+
)
7998
)
8099
})
81100

@@ -156,7 +175,3 @@ test_that("argument validation works", {
156175
expect_error(latest_available_version("invalid"), "should be one of")
157176
expect_error(latest_available_published("invalid"), "should be one of")
158177
})
159-
160-
test_that("function returns invisible NULL", {
161-
expect_invisible(check_newer_version("99.9.9"))
162-
})

0 commit comments

Comments
 (0)