diff --git a/NAMESPACE b/NAMESPACE index 897b0a7..e02334e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(assert_non_na) export(assert_non_na_and_unique) +export(assert_version_package) export(assert_version_r) export(clump_month_date) export(clump_week_date) diff --git a/R/assert-version.R b/R/assert-version.R index bf00881..4aea58f 100644 --- a/R/assert-version.R +++ b/R/assert-version.R @@ -1,5 +1,5 @@ #' @name assert_version -#' @aliases assert_version_r +#' @aliases assert_version_r, assert_version_package #' @title I that the local machine is using an acceptable version. #' #' @description Assert that the local machine is using a version that satisfies @@ -25,10 +25,20 @@ #' @author Will Beasley #' #' @examples +#' # Check R #' assert_version_r("3.1.0") #' assert_version_r() #' # Fails: assert_version_r("99.1.0") - +#' +#' # Check packages +#' assert_version_package("base", "3.1.0") +#' assert_version_package("OuhscMunge", "0.1.0") +#' # Fails: assert_version_package("base", "99.1.0") +#' # Fails: assert_version_package("OuhscMunge", "9.1.0") +#' # Fails: +#' # Fails: assert_version_package("OuhscMungeee", "9.1.0") +#' +#' #' @export assert_version_r <- function(minimum = base::package_version("4.2.1")) { checkmate::assert_vector(minimum, len = 1, any.missing = FALSE) @@ -58,6 +68,64 @@ assert_version_r <- function(minimum = base::package_version("4.2.1")) { ) |> stop() } else { - TRUE + invisible(TRUE) + } +} + +#' @export +assert_version_package <- function( + package_name, + minimum, + installation_code = "" +) { + checkmate::assert_character(package_name, len = 1, min.chars = 1, any.missing = FALSE) + checkmate::assert_vector(minimum, len = 1, any.missing = FALSE) + checkmate::assert_character(installation_code, len = 1, min.chars = 0, any.missing = FALSE) + + package_is_installed <- requireNamespace(package_name, quietly = TRUE) + + installation_message <- + if (1L <= nchar(installation_code)) { + " Install the package with `%s`." |> + sprintf(installation_code) + } else { + "" + } + + if (!package_is_installed) { + "The package '%s' not installed.%s" |> + sprintf(package_name, installation_message) |> + stop() + } + + minimum <- + if (inherits(minimum, "package_version")) { + as.character(minimum) + } else if (inherits(minimum, "character")) { + # Make sure it can be recognized as a version + as.character(base::package_version(minimum)) + } else { + stop("The value passed to `minimum` must inherit either from 'character' or `package_version`.") + } + + current <- as.character(utils::packageVersion(package_name)) + + comparison <- + utils::compareVersion( + current, + minimum + ) + + if (comparison < 0 ) { + "Your version of the `%s` package is too old. It is %s, but needs to be at least %s.%s" |> + sprintf( + package_name, + current, + minimum, + installation_message + ) |> + stop() + } else { + invisible(TRUE) } } diff --git a/man/assert_version.Rd b/man/assert_version.Rd index 6f12c20..9e12fd5 100644 --- a/man/assert_version.Rd +++ b/man/assert_version.Rd @@ -3,6 +3,8 @@ \name{assert_version} \alias{assert_version} \alias{assert_version_r} +\alias{assert_version_r,} +\alias{assert_version_package} \title{I that the local machine is using an acceptable version.} \usage{ assert_version_r(minimum = base::package_version("4.2.1")) @@ -32,9 +34,20 @@ for the native pipe. Future versions of OuhscMunge will likely increase the default value to keep pace with important developments to R. } \examples{ +# Check R assert_version_r("3.1.0") assert_version_r() # Fails: assert_version_r("99.1.0") + +# Check packages +assert_version_package("base", "3.1.0") +assert_version_package("OuhscMunge", "0.1.0") +# Fails: assert_version_package("base", "99.1.0") +# Fails: assert_version_package("OuhscMunge", "9.1.0") +# Fails: +# Fails: assert_version_package("OuhscMungeee", "9.1.0") + + } \author{ Will Beasley diff --git a/tests/testthat/test-assert-version-package.R b/tests/testthat/test-assert-version-package.R new file mode 100644 index 0000000..2c8dd08 --- /dev/null +++ b/tests/testthat/test-assert-version-package.R @@ -0,0 +1,30 @@ +library(testthat) + +test_that("old minimum", { + r <- assert_version_package("base", "3.1.0") + expect_true(r) + + r <- assert_version_package("OuhscMunge", "0.1.0") + expect_true(r) + + assert_version_package("base", package_version("3.1.0")) + expect_true(r) + + assert_version_package("OuhscMunge", package_version("0.1.0")) + expect_true(r) +}) + +test_that("minimum that throws an error", { + expected_error_message <- "Your version of the `base` package is too old" + expect_error( + assert_version_package("base", "99.1.0"), + expected_error_message + ) + + expect_error( + assert_version_package("base", package_version("99.1.0")), + expected_error_message + ) +}) +stop("TODO: test installation message for non-existent package.") +stop("TODO: test installation message for an old package.")