From aeba4b732bac70e0631476056fc981e2a5b73a9d Mon Sep 17 00:00:00 2001 From: kbvernon Date: Wed, 18 Sep 2024 17:10:22 -0600 Subject: [PATCH 1/9] 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 5c2b879939e5cccb6b73382fe685249e201a5655 Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sat, 9 Nov 2024 15:26:23 -0700 Subject: [PATCH 2/9] reformat line --- R/use_crate.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/use_crate.R b/R/use_crate.R index c1fcbb04..4103130a 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -64,7 +64,10 @@ use_crate <- function( ) # clear empty options - is_null_or_empty_string <- function(x){ !nzchar(x) || rlang::is_empty(x) } + 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 From e24b7b36cab380cde84b916e4114bcbf77aae2a2 Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sat, 9 Nov 2024 15:26:35 -0700 Subject: [PATCH 3/9] add test --- tests/testthat/test-use_crate.R | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/testthat/test-use_crate.R diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R new file mode 100644 index 00000000..7254168f --- /dev/null +++ b/tests/testthat/test-use_crate.R @@ -0,0 +1,30 @@ +test_that("use_crate() adds dependency to package or workspace", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + use_extendr(path, quiet = TRUE) + + use_crate( + "serde", + features = "derive", + git = "https://github.com/serde-rs/serde", + version = "1.0.1" + ) + + metadata <- read_cargo_metadata(path) + + dependency <- metadata[["packages"]][["dependencies"]][[1]] + dependency <- dependency[dependency[["name"]] == "serde", ] + + expect_equal(dependency[["name"]], "serde") + + expect_equal(dependency[["features"]], "derive") + + expect_equal(dependency[["source"]], "git+https://github.com/serde-rs/serde") + + expect_equal(dependency[["req"]], "^1.0.1") +}) From c3a1bbcdd66e7c653a4831fb7c012048d1527385 Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sat, 9 Nov 2024 17:02:15 -0700 Subject: [PATCH 4/9] fix processx and cargo issues --- R/use_crate.R | 39 +++++++++++++++++---------------- tests/testthat/test-use_crate.R | 8 +++---- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/R/use_crate.R b/R/use_crate.R index 4103130a..38cfcd5d 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -52,31 +52,32 @@ use_crate <- function( check_bool(optional) check_string(path) + if (!is.null(version) && !is.null(git)) { + cli::cli_abort( + "Cannot specify a git URL ('{git}') with a version ('{version}')." + ) + } + if (!is.null(version)) { crate <- paste0(crate, "@", version) } - # combine main options - cargo_add_opts <- list( - "--features" = paste0(features, collapse = " "), - "--git" = git, - "--optional" = tolower(as.character(optional)) - ) - - # clear empty options - is_null_or_empty_string <- function(x) { - !nzchar(x) || rlang::is_empty(x) + if (!is.null(features)) { + features <- c( + "--features", + paste(crate, features, sep = "/", collapse = ",") + ) } - cargo_add_opts <- purrr::discard(cargo_add_opts, is_null_or_empty_string) + if (!is.null(git)) { + git <- c("--git", git) + } - # combine option names and values into single strings - adtl_args <- unname(purrr::imap_chr( - cargo_add_opts, - function(x, i) { - paste(i, paste0(x, collapse = " ")) - } - )) + if (optional) { + optional <- "--optional" + } else { + optional <- NULL + } # get rust directory in project folder root <- rprojroot::find_package_root_file(path = path) @@ -90,7 +91,7 @@ use_crate <- function( # run the commmand processx::run( "cargo", - c("add", crate, adtl_args), + c("add", crate, features, git), echo_cmd = TRUE, wd = rust_folder ) diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index 7254168f..e94815ff 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -10,9 +10,9 @@ test_that("use_crate() adds dependency to package or workspace", { use_crate( "serde", - features = "derive", - git = "https://github.com/serde-rs/serde", - version = "1.0.1" + features = "derive", + version = "1.0.1", + path = path ) metadata <- read_cargo_metadata(path) @@ -24,7 +24,5 @@ test_that("use_crate() adds dependency to package or workspace", { expect_equal(dependency[["features"]], "derive") - expect_equal(dependency[["source"]], "git+https://github.com/serde-rs/serde") - expect_equal(dependency[["req"]], "^1.0.1") }) From 26fd0bb89cff923bc6ed9866b97c9e04460689aa Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sat, 9 Nov 2024 17:16:27 -0700 Subject: [PATCH 5/9] unpack list column in expect and format --- tests/testthat/test-use_crate.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index e94815ff..fb6ee772 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -10,7 +10,7 @@ test_that("use_crate() adds dependency to package or workspace", { use_crate( "serde", - features = "derive", + features = "derive", version = "1.0.1", path = path ) @@ -22,7 +22,7 @@ test_that("use_crate() adds dependency to package or workspace", { expect_equal(dependency[["name"]], "serde") - expect_equal(dependency[["features"]], "derive") + expect_equal(dependency[["features"]][[1]], "derive") expect_equal(dependency[["req"]], "^1.0.1") }) From 2b211a753a8f03ed5fc972dd2982e39bac9e275a Mon Sep 17 00:00:00 2001 From: kbvernon Date: Sat, 9 Nov 2024 17:53:58 -0700 Subject: [PATCH 6/9] include 'optional' in additional args --- R/use_crate.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/use_crate.R b/R/use_crate.R index 38cfcd5d..144a30d6 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -91,7 +91,7 @@ use_crate <- function( # run the commmand processx::run( "cargo", - c("add", crate, features, git), + c("add", crate, features, git, optional), echo_cmd = TRUE, wd = rust_folder ) From 530d2e7e56e159908b4f545b95e52beda0d1bab6 Mon Sep 17 00:00:00 2001 From: kbvernon Date: Thu, 14 Nov 2024 15:45:12 -0700 Subject: [PATCH 7/9] test for error --- R/use_crate.R | 3 ++- tests/testthat/test-use_crate.R | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/R/use_crate.R b/R/use_crate.R index 144a30d6..66496a7c 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -54,7 +54,8 @@ use_crate <- function( if (!is.null(version) && !is.null(git)) { cli::cli_abort( - "Cannot specify a git URL ('{git}') with a version ('{version}')." + "Cannot specify a git URL ('{git}') with a version ('{version}').", + class = "rextendr_error" ) } diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index fb6ee772..d135a87f 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -26,3 +26,24 @@ test_that("use_crate() adds dependency to package or workspace", { expect_equal(dependency[["req"]], "^1.0.1") }) + +test_that("use_crate() errors when user passes git and version arguments", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + use_extendr(path, quiet = TRUE) + + fn <- function() { + use_crate( + "serde", + git = "https://github.com/serde-rs/serde", + version = "1.0.1" + ) + } + + expect_error(fn(), class = "rextendr_error") +}) From a3dae972ca9144a3ee39bdee2af1fc5047068bf9 Mon Sep 17 00:00:00 2001 From: kbvernon Date: Thu, 14 Nov 2024 17:49:23 -0700 Subject: [PATCH 8/9] test optional=TRUE --- tests/testthat/test-use_crate.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index d135a87f..5f15570b 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -47,3 +47,27 @@ test_that("use_crate() errors when user passes git and version arguments", { expect_error(fn(), class = "rextendr_error") }) + +test_that("use_crate(optional = TRUE) adds optional dependency", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + use_extendr(path, quiet = TRUE) + + use_crate( + "serde", + optional = TRUE, + path = path + ) + + metadata <- read_cargo_metadata(path) + + dependency <- metadata[["packages"]][["dependencies"]][[1]] + dependency <- dependency[dependency[["name"]] == "serde", ] + + expect_identical(dependency[["optional"]], TRUE) +}) From cb1c985d244d2a4c7f7e6d0cbf67ab77efa8aace Mon Sep 17 00:00:00 2001 From: kbvernon Date: Fri, 15 Nov 2024 09:14:48 -0700 Subject: [PATCH 9/9] test git parameter --- tests/testthat/test-use_crate.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index 5f15570b..b3419279 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -71,3 +71,27 @@ test_that("use_crate(optional = TRUE) adds optional dependency", { expect_identical(dependency[["optional"]], TRUE) }) + +test_that("use_crate(git = ) adds dependency with git source", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + use_extendr(path, quiet = TRUE) + + use_crate( + "serde", + git = "https://github.com/serde-rs/serde", + path = path + ) + + metadata <- read_cargo_metadata(path) + + dependency <- metadata[["packages"]][["dependencies"]][[1]] + dependency <- dependency[dependency[["name"]] == "serde", ] + + expect_equal(dependency[["source"]], "git+https://github.com/serde-rs/serde") +})