Skip to content

Commit

Permalink
update use_msrv to fix test and add optional overwrite argument
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Sep 7, 2024
1 parent 23e2cb1 commit c6a91f0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,"%>%")
Expand Down
26 changes: 13 additions & 13 deletions R/use_msrv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -80,7 +80,7 @@ use_msrv <- function(version, path = ".") {
"{.code SystemRequirements: {cur}}"
)
)
}
}

Check warning on line 83 in R/use_msrv.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_msrv.R,line=83,col=4,[trailing_whitespace_linter] Trailing whitespace is superfluous.

invisible(version)
}
51 changes: 51 additions & 0 deletions man/use_msrv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions tests/testthat/test-use_msrv.R
Original file line number Diff line number Diff line change
@@ -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"))
})
d <- desc::desc("DESCRIPTION")

Check warning on line 13 in tests/testthat/test-use_msrv.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-use_msrv.R,line=13,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
expect_identical(
"Cargo (Rust's package manager), rustc >= 1.70",
d$get_field("SystemRequirements")
)
})

0 comments on commit c6a91f0

Please sign in to comment.