From d8689d9242f6da8c919da72589c04630fcd108a1 Mon Sep 17 00:00:00 2001 From: Kenneth Blake Vernon <53311626+kbvernon@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:37:59 -0700 Subject: [PATCH] internal utilities to find rust crate and cargo manifest (#399) * internal functions and tests * Update R/find_extendr.R Co-authored-by: Josiah Parry * Update R/find_extendr.R Co-authored-by: Josiah Parry --------- Co-authored-by: Josiah Parry --- R/find_extendr.R | 69 ++++++++++++++++++++++++++++++ tests/testthat/test-find_extendr.R | 33 ++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 R/find_extendr.R create mode 100644 tests/testthat/test-find_extendr.R diff --git a/R/find_extendr.R b/R/find_extendr.R new file mode 100644 index 00000000..f423b5bc --- /dev/null +++ b/R/find_extendr.R @@ -0,0 +1,69 @@ +#' Get path to Rust crate in R package directory +#' +#' @param path character scalar, the R package directory +#' @param error_call call scalar, from rlang docs: "the defused call with which +#' the function running in the frame was invoked" +#' +#' @return character scalar, path to Rust crate +#' +#' @examples +#' \dontrun{ +#' find_extendr_crate() +#' @keywords internal +#' @noRd +#' } +find_extendr_crate <- function( + path = ".", + error_call = rlang::caller_call()) { + check_character(path, call = error_call, class = "rextendr_error") + + rust_folder <- rprojroot::find_package_root_file( + "src", "rust", + path = path + ) + + if (!dir.exists(rust_folder)) { + cli::cli_abort( + "Could not find Rust crate at {.path rust_folder}.", + call = error_call, + class = "rextendr_error" + ) + } + + rust_folder +} + +#' Get path to Cargo manifest in R package directory +#' +#' @param path character scalar, the R package directory +#' @param error_call call scalar, from rlang docs: "the defused call with which +#' the function running in the frame was invoked" +#' +#' @return character scalar, path to Cargo manifest +#' +#' @examples +#' \dontrun{ +#' find_extendr_manifest() +#' @keywords internal +#' @noRd +#' } +find_extendr_manifest <- function( + path = ".", + error_call = rlang::caller_call()) { + check_character(path, call = error_call, class = "rextendr_error") + + manifest_path <- rprojroot::find_package_root_file( + "src", "rust", "Cargo.toml", + path = path + ) + + if (!file.exists(manifest_path)) { + cli::cli_abort( + "Could not find Cargo manifest at {.path manifest_path}.", + call = error_call, + class = "rextendr_error" + ) + } + + manifest_path +} diff --git a/tests/testthat/test-find_extendr.R b/tests/testthat/test-find_extendr.R new file mode 100644 index 00000000..d8a85177 --- /dev/null +++ b/tests/testthat/test-find_extendr.R @@ -0,0 +1,33 @@ +test_that("find_extendr_crate() returns path to Rust crate", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + expect_error(find_extendr_crate(), class = "rextendr_error") + + use_extendr(path, quiet = TRUE) + + rust_folder <- find_extendr_crate() + + expect_true(dir.exists(rust_folder)) +}) + +test_that("find_extendr_manifest() returns path to Cargo manifest", { + skip_if_not_installed("usethis") + + path <- local_package("testpkg") + + # capture setup messages + withr::local_options(usethis.quiet = FALSE) + + expect_error(find_extendr_manifest(), class = "rextendr_error") + + use_extendr(path, quiet = TRUE) + + manifest_path <- find_extendr_manifest() + + expect_true(file.exists(manifest_path)) +})