From c6a91f0e35b5cbc0528064459b77585afbd764fe Mon Sep 17 00:00:00 2001 From: Josiah Parry Date: Sat, 7 Sep 2024 13:13:16 -0700 Subject: [PATCH] update use_msrv to fix test and add optional overwrite argument --- NAMESPACE | 1 + R/use_msrv.R | 26 ++++++++--------- man/use_msrv.Rd | 51 ++++++++++++++++++++++++++++++++++ tests/testthat/test-use_msrv.R | 13 ++++++--- 4 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 man/use_msrv.Rd diff --git a/NAMESPACE b/NAMESPACE index 8316fec8..b04fbc0d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(to_toml) export(use_cran_defaults) export(use_crate) export(use_extendr) +export(use_msrv) export(vendor_pkgs) export(write_license_note) importFrom(dplyr,"%>%") diff --git a/R/use_msrv.R b/R/use_msrv.R index 2484b836..20a7113d 100644 --- a/R/use_msrv.R +++ b/R/use_msrv.R @@ -2,9 +2,9 @@ #' #' `use_msrv()` sets the minimum supported rust version for your R package. #' -#' @param version character scalar, the minimum supported Rust version -#' @param path character scalar, path to folder containing DESCRIPTION file -#' +#' @param version character scalar, the minimum supported Rust version. +#' @param path character scalar, path to folder containing DESCRIPTION file. +#' @param overwrite default `FALSE`. Overwrites the `SystemRequirements` field if already set when `TRUE`. #' @details #' #' The minimum supported rust version (MSRV) is determined by the @@ -38,13 +38,11 @@ #' use_msrv("1.67.1") #' } #' -use_msrv <- function(version, path = ".") { - if (length(version) != 1L) { - cli::cli_abort( - "Version must be a character scalar", - class = "rextendr_error" - ) - } +use_msrv <- function(version, path = ".", overwrite = FALSE) { + + check_string(version, class = "rextendr_error") + check_string(path, class = "rextendr_error") + check_bool(overwrite, class = "rextendr_error") msrv_call <- rlang::caller_call() version <- tryCatch(numeric_version(version), error = function(e) { @@ -68,10 +66,12 @@ use_msrv <- function(version, path = ".") { prev <- desc::desc_get("SystemRequirements", file = desc_path)[[1]] prev <- stringi::stri_trim_both(prev) + prev_is_default <- identical(prev, "Cargo (Rust's package manager), rustc") - if (is.na(prev)) { + # if it isn't set update the description or if overwrite is true + if (is.na(prev) || overwrite || prev_is_default) { update_description("SystemRequirements", cur, desc_path = desc_path) - } else if (!identical(cur, prev)) { + } else if (!identical(cur, prev) && !overwrite) { cli::cli_ul( c( "The SystemRequirements field in the {.file DESCRIPTION} file is @@ -80,7 +80,7 @@ use_msrv <- function(version, path = ".") { "{.code SystemRequirements: {cur}}" ) ) - } + } invisible(version) } diff --git a/man/use_msrv.Rd b/man/use_msrv.Rd new file mode 100644 index 00000000..e1616fe7 --- /dev/null +++ b/man/use_msrv.Rd @@ -0,0 +1,51 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_msrv.R +\name{use_msrv} +\alias{use_msrv} +\title{Set the minimum supported rust version (MSRV)} +\usage{ +use_msrv(version, path = ".", overwrite = FALSE) +} +\arguments{ +\item{version}{character scalar, the minimum supported Rust version.} + +\item{path}{character scalar, path to folder containing DESCRIPTION file.} + +\item{overwrite}{default \code{FALSE}. Overwrites the \code{SystemRequirements} field if already set when \code{TRUE}.} +} +\value{ +\code{version} +} +\description{ +\code{use_msrv()} sets the minimum supported rust version for your R package. +} +\details{ +The minimum supported rust version (MSRV) is determined by the +\code{SystemRequirements} field in a package's \code{DESCRIPTION} file. For example, to +set the MSRV to \verb{1.67.0}, the \code{SystemRequirements} must have +\verb{rustc >= 1.67.0}. + +By default, there is no MSRV set. However, some crates have features that +depend on a minimum version of Rust. As of this writing the version of Rust +on CRAN's Fedora machine's is 1.69. If you require a version of Rust that is +greater than that, you must set it in your DESCRIPTION file. + +It is also important to note that if CRAN's machines do not meet the +specified MSRV, they will not be able to build a binary of your package. As a +consequence, if users try to install the package they will be required to +have Rust installed as well. + +To determine the MSRV of your R package, we recommend installing the +\code{cargo-msrv} cli. You can do so by running \verb{cargo install cargo-msrv}. To +determine your MSRV, set your working directory to \code{src/rust} then run +\verb{cargo msrv}. Note that this may take a while. + +For more details, please see +\href{https://github.com/foresterre/cargo-msrv}{cargo-msrv}. +} +\examples{ +\dontrun{ +use_msrv("1.67.1") +} + +} diff --git a/tests/testthat/test-use_msrv.R b/tests/testthat/test-use_msrv.R index 655290de..2ef38dfd 100644 --- a/tests/testthat/test-use_msrv.R +++ b/tests/testthat/test-use_msrv.R @@ -1,13 +1,18 @@ test_that("use_msrv() modifies the MSRV in the DESCRIPTION", { skip_if_not_installed("usethis") - path <- withr::local_package("testpkg") + path <- local_package("testpkg") # capture setup messages withr::local_options(usethis.quiet = FALSE) use_extendr(path, quiet = TRUE) - use_msrv("1.70") + use_msrv("1.70", path) - expect_snapshot(cat(readLines("DESCRIPTION"), sep = "\n")) -}) \ No newline at end of file + d <- desc::desc("DESCRIPTION") + + expect_identical( + "Cargo (Rust's package manager), rustc >= 1.70", + d$get_field("SystemRequirements") + ) +})