From aeba4b732bac70e0631476056fc981e2a5b73a9d Mon Sep 17 00:00:00 2001 From: kbvernon Date: Wed, 18 Sep 2024 17:10:22 -0600 Subject: [PATCH 1/2] check for empty string --- R/use_crate.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/use_crate.R b/R/use_crate.R index cea33920..c1fcbb04 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -64,7 +64,8 @@ use_crate <- function( ) # clear empty options - cargo_add_opts <- purrr::discard(cargo_add_opts, rlang::is_empty) + is_null_or_empty_string <- function(x){ !nzchar(x) || rlang::is_empty(x) } + cargo_add_opts <- purrr::discard(cargo_add_opts, is_null_or_empty_string) # combine option names and values into single strings adtl_args <- unname(purrr::imap_chr( From 9ae8f3a9363b66c5d580504cbf80b62db040744a Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sun, 22 Sep 2024 16:45:05 -0600 Subject: [PATCH 2/2] draft and test --- R/read_cargo_metadata.R | 44 +++++++++++++++++++++++ tests/testthat/test-read_cargo_metadata.R | 14 ++++++++ 2 files changed, 58 insertions(+) create mode 100644 R/read_cargo_metadata.R create mode 100644 tests/testthat/test-read_cargo_metadata.R diff --git a/R/read_cargo_metadata.R b/R/read_cargo_metadata.R new file mode 100644 index 00000000..15fd0eac --- /dev/null +++ b/R/read_cargo_metadata.R @@ -0,0 +1,44 @@ +#' Retrieve metadata for packages and workspaces +#' +#' @param path character scalar, the R package directory +#' +#' @details +#' For more details, see +#' \href{https://doc.rust-lang.org/cargo/commands/cargo-metadata.html}{Cargo docs} +#' for `cargo-metadata`. See especially "JSON Format" to get a sense of what you +#' can expect to find in the returned list. +#' +#' @return `list`, including the following elements: +#' - "packages" +#' - "workspace_members" +#' - "workspace_default_members" +#' - "resolve" +#' - "target_directory" +#' - "version" +#' - "workspace_root" +#' - "metadata" +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' read_cargo_metadata() +#' } +#' +read_cargo_metadata <- function(path = ".") { + root <- rprojroot::find_package_root_file(path = path) + + rust_folder <- normalizePath( + file.path(root, "src", "rust"), + winslash = "/", + mustWork = FALSE + ) + + out <- processx::run( + "cargo", + args = c("metadata", "--format-version=1", "--no-deps"), + wd = rust_folder + ) + + jsonlite::fromJSON(out[["stdout"]]) +} diff --git a/tests/testthat/test-read_cargo_metadata.R b/tests/testthat/test-read_cargo_metadata.R new file mode 100644 index 00000000..d92aaafd --- /dev/null +++ b/tests/testthat/test-read_cargo_metadata.R @@ -0,0 +1,14 @@ +test_that("read_cargo_metadata() returns package or workspace metadata", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + use_extendr(path, quiet = TRUE) + + out <- read_cargo_metadata() + + expect_s3_class(out, "list") +})