Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for empty string in use_crate() #388

Merged
merged 14 commits into from
Nov 15, 2024
38 changes: 22 additions & 16 deletions R/use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,33 @@
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}').",
class = "rextendr_error"
)
}

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))
)
if (!is.null(features)) {
features <- c(
"--features",
paste(crate, features, sep = "/", collapse = ",")
)
}

# clear empty options
cargo_add_opts <- purrr::discard(cargo_add_opts, rlang::is_empty)
if (!is.null(git)) {
git <- c("--git", git)

Check warning on line 74 in R/use_crate.R

View check run for this annotation

Codecov / codecov/patch

R/use_crate.R#L74

Added line #L74 was not covered by tests
}

# 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)
Expand All @@ -86,7 +92,7 @@
# run the commmand
processx::run(
"cargo",
c("add", crate, adtl_args),
c("add", crate, features, git, optional),
echo_cmd = TRUE,
wd = rust_folder
)
Expand Down
73 changes: 73 additions & 0 deletions tests/testthat/test-use_crate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
test_that("use_crate() adds dependency to package or workspace", {
Ilia-Kosenkov marked this conversation as resolved.
Show resolved Hide resolved
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",
version = "1.0.1",
path = path
)

metadata <- read_cargo_metadata(path)

dependency <- metadata[["packages"]][["dependencies"]][[1]]
dependency <- dependency[dependency[["name"]] == "serde", ]

expect_equal(dependency[["name"]], "serde")

expect_equal(dependency[["features"]][[1]], "derive")

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",
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
version = "1.0.1"
)
}

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)
})
Loading