Skip to content

Commit

Permalink
Fix and simplify reactlog's version check approach. (#4012)
Browse files Browse the repository at this point in the history
* Close #4011. Fix and simplify reactlog's version check approach.

* Better variable name

* Use test_path() for consistent path location

* Just use packageDescription()

* Update tests/testthat/test-reactlog.R

* Update tests/testthat/test-reactlog.R

* Update DESCRIPTION
  • Loading branch information
cpsievert authored Mar 22, 2024
1 parent 7ed68ed commit 7742b65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
59 changes: 6 additions & 53 deletions R/graph.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
# Check that the version of an suggested package satisfies the requirements
#
# @param package The name of the suggested package
# @param version The version of the package
check_suggested <- function(package, version = NULL) {

if (is_installed(package, version)) {
return()
}

msg <- paste0(
sQuote(package),
if (is.na(version %||% NA)) "" else paste0("(>= ", version, ")"),
" must be installed for this functionality."
)

if (interactive()) {
message(msg, "\nWould you like to install it?")
if (utils::menu(c("Yes", "No")) == 1) {
return(utils::install.packages(package))
}
}

stop(msg, call. = FALSE)
}




# domain is like session

Expand Down Expand Up @@ -110,34 +82,15 @@ renderReactlog <- function(sessionToken = NULL, time = TRUE) {
time = time
)
}
check_reactlog <- function() {
check_suggested("reactlog", reactlog_version())
}
# read reactlog version from description file
# prevents version mismatch in code and description file
reactlog_version <- local({
version <- NULL
function() {
if (!is.null(version)) return(version)

desc <- read.dcf(system_file("DESCRIPTION", package = "shiny"))
suggests <- desc[1,"Suggests"][[1]]
suggests_pkgs <- strsplit(suggests, "\n")[[1]]

reactlog_info <- suggests_pkgs[grepl("reactlog", suggests_pkgs)]
if (length(reactlog_info) == 0) {
stop("reactlog can not be found in shiny DESCRIPTION file")
}

reactlog_info <- sub("^[^\\(]*\\(", "", reactlog_info)
reactlog_info <- sub("\\)[^\\)]*$", "", reactlog_info)
reactlog_info <- sub("^[>= ]*", "", reactlog_info)

version <<- package_version(reactlog_info)
version
check_reactlog <- function() {
if (!is_installed("reactlog", reactlog_min_version)) {
rlang::check_installed("reactlog", reactlog_min_version)
}
})
}

# Should match the (suggested) version in DESCRIPTION file
reactlog_min_version <- "1.0.0"

RLog <- R6Class(
"RLog",
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-reactlog.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,16 @@ test_that("message logger appears", {
})

})


test_that("reactlog_version is as expected", {
suggests <- strsplit(packageDescription("shiny")$Suggests, ",")[[1]]
reactlog <- trimws(
grep("reactlog", suggests, value = TRUE)
)
expect_length(reactlog, 1)
expect_equal(
reactlog,
sprintf("reactlog (>= %s)", reactlog_min_version)
)
})

0 comments on commit 7742b65

Please sign in to comment.