Is there a way for devtools::test() to also run Rust tests?
#509
|
Hi! I'm looking for a convenient way to run all of my tests (both R and Rust). Right now I run But there might a single liner, that I can run from R? Ideally it would be |
Replies: 2 comments 3 replies
|
I do not see an exported The package does have an internal The most reliable one-liner from R is to invoke Cargo yourself: processx::run("cargo", "test", wd = "src/rust", echo = TRUE)
devtools::test()or wrap both in a small project-local helper: test_all <- function() {
processx::run("cargo", "test", wd = "src/rust", echo = TRUE)
devtools::test()
} |
|
I am on Windows and needed some extra steps to make this work. Here is what worked for me (also included commands for cargo fmt and cargo clippy): # ==========================================
# 0. RUST CHECKS (Runs in Terminal via R)
# ==========================================
# preliminaries to avoid linker error
#####################################
# Define the base path to the GCC libraries in Rtools45
rtools_gcc_base <- "C:/rtools45/x86_64-w64-mingw32.static.posix/lib/gcc/x86_64-w64-mingw32.static.posix"
# Find the versioned folder inside it (e.g., "13.2.0")
gcc_version_folder <- list.files(rtools_gcc_base)[1]
full_lib_path <- file.path(rtools_gcc_base, gcc_version_folder)
# Define file paths
source_file <- file.path(full_lib_path, "libgcc.a")
target_file <- file.path(full_lib_path, "libgcc_eh.a")
# Copy and rename the file
if (file.exists(source_file) && !file.exists(target_file)) {
file.copy(source_file, target_file)
message("Success! libgcc_eh.a created.")
} else if (file.exists(target_file)) {
message("libgcc_eh.a already exists. You are good to go!")
} else {
warning("Could not find libgcc.a. Please check your Rtools path.")
}
#################################################################
# Run format, clippy, and tests using the GNU toolchain
# Format code
system2("cargo", args = c("fmt", "--manifest-path", "src/rust/Cargo.toml"))
# Run clippy (linter)
system2("cargo", args = c("clippy", "--manifest-path", "src/rust/Cargo.toml"))
# Run Rust unit tests with the GNU toolchain
pkgbuild::with_build_tools({
withr::with_envvar(c("CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER" = "gcc"), {
system2("cargo",
args = c("test",
"--manifest-path", "src/rust/Cargo.toml",
"--target", "x86_64-pc-windows-gnu"))
})
})
|
I do not see an exported
devtools::test()hook inrextendrthat automatically runscargo test.The package does have an internal
run_cargo()helper, but it is marked internal and is not an exported user API. Separately,rextendr::document()is now deprecated because packages created withuse_extendr()integrate wrapper generation into the normal build/document flow; that does not make Rust tests part ofdevtools::test().The most reliable one-liner from R is to invoke Cargo yourself:
or wrap both in a small project-local helper: