Skip to content

Commit

Permalink
internal utilities to find rust crate and cargo manifest (#399)
Browse files Browse the repository at this point in the history
* internal functions and tests

* Update R/find_extendr.R

Co-authored-by: Josiah Parry <[email protected]>

* Update R/find_extendr.R

Co-authored-by: Josiah Parry <[email protected]>

---------

Co-authored-by: Josiah Parry <[email protected]>
  • Loading branch information
kbvernon and JosiahParry authored Nov 20, 2024
1 parent 4aefce1 commit d8689d9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
69 changes: 69 additions & 0 deletions R/find_extendr.R
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions tests/testthat/test-find_extendr.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit d8689d9

Please sign in to comment.