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

Update dev tests #352

Merged
merged 7 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions R/create_extendr_package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Create package that uses Rust
#'
#' @description
Expand All @@ -14,7 +13,6 @@
#'
#' @noRd
create_extendr_package <- function(path, ...) {

# error if usethis is not installed
rlang::check_installed("usethis")

Expand Down Expand Up @@ -51,5 +49,4 @@ create_extendr_package <- function(path, ...) {
)

invisible(path)

}
2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ cat_file <- function(...) {

#' Helper function for skipping tests when cargo subcommand is unavailable
#' @param args Character vector, arguments to the `cargo` command. Pass to [processx::run()]'s args param.
skip_if_cargo_bin <- function(args = "--help") {
skip_if_cargo_unavailable <- function(args = "--help") {
tryCatch(
{
processx::run("cargo", args, error_on_status = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("rextendr::clean() removes cargo target directory & binaries", {
skip_if_not_installed("usethis")
skip_if_not_installed("devtools")
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_on_R42_win()

path <- local_package("testpkg")
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-document.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("Running `document` after adding multiple files", {
skip_if_not_installed("usethis")
skip_if_not_installed("devtools")
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()

path <- local_package("testPackage")
use_extendr()
Expand All @@ -18,7 +18,7 @@ test_that("Warn if using older rextendr", {
skip_if_not_installed("usethis")
skip_if_not_installed("devtools")
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()

path <- local_package("futurepkg")
use_extendr()
Expand All @@ -31,7 +31,7 @@ test_that("Update the Config/rextendr/version field in DESCRIPTION file", {
skip_if_not_installed("usethis")
skip_if_not_installed("devtools")
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()

path <- local_package("oldpkg")
use_extendr()
Expand All @@ -47,7 +47,7 @@ test_that("document() warns if NAMESPACE file is malformed", {
skip_if_not_installed("usethis")
skip_if_not_installed("devtools")
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()

path <- local_package("testPackage")
r"(exportPattern("^[[:alpha:]]+"))" |> brio::write_lines("NAMESPACE")
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("`rust_eval()` works", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_equal(rust_eval("2 + 2"), 4)
expect_visible(rust_eval("2 + 2"))
Expand All @@ -17,7 +17,7 @@ test_that("`rust_eval()` works", {
# the order of compilation.
# Returned integer values should be identical to the input sequence.
test_that("multiple `rust_eval_deferred()` work correctly", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

provided_values <- seq_len(5)
deferred_handles <- purrr::map(
Expand All @@ -44,7 +44,7 @@ test_that("multiple `rust_eval_deferred()` work correctly", {
# the reverse order.
# Returned integer values should be identical to the reversed input sequence.
test_that("multiple `rust_eval_deferred()` work correctly in reverse order", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

provided_values <- seq_len(5)

Expand Down Expand Up @@ -72,7 +72,7 @@ test_that("multiple `rust_eval_deferred()` work correctly in reverse order", {
# Third, attempt to execute the same compiled piece of code and
# observe an error.
test_that("`rust_eval_deferred()` disallows multiple executions of the same chunk", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

handle <- rust_eval_deferred("5i32 + 6i32")

Expand All @@ -92,7 +92,7 @@ test_that("`rust_eval_deferred()` disallows multiple executions of the same chun
# Execute code chunk and verify result.
# Test if the wrapper has been removed and dll unloaded.
test_that("`rust_eval_deferred()` environment cleanup", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

handle <- rust_eval_deferred("42i32")
fn_name <- attr(handle, "function_name")
Expand All @@ -116,7 +116,7 @@ test_that("`rust_eval_deferred()` environment cleanup", {
# Compare wrapper function names and dll paths (should be unequal).
# Execute both chunks and test results (should be equal).
test_that("`rust_eval_deferred()` generates unique function names", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_code <- "42f64"

Expand Down
19 changes: 8 additions & 11 deletions tests/testthat/test-extendr_function_options.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
test_that("`extendr` code is compiled with `either` feature and `use_try_from` enabled", {
skip_if_cargo_bin()
skip_if_opted_out_of_dev_tests()
skip_if_cargo_unavailable()

rust_function(
"fn type_aware_sum(input : Either<Integers, Doubles>) -> Either<Rint, Rfloat> {
Expand All @@ -11,8 +10,6 @@ test_that("`extendr` code is compiled with `either` feature and `use_try_from` e
}",
extendr_fn_options = list("use_try_from" = TRUE),
features = "either",
use_dev_extendr = TRUE,
quiet = TRUE # Suppresses warnings while the feature is still experimental
)

int_sum <- type_aware_sum(1:5)
Expand All @@ -27,7 +24,7 @@ test_that("`extendr` code is compiled with `either` feature and `use_try_from` e
})

test_that("`r_name` option renames R function", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_function(
"fn func() -> &'static str {\"Modified Name\"}",
Expand All @@ -42,29 +39,29 @@ test_that("`rust_source()` errors if `extendr_fn_options` contains `NULL` value"
})

test_that("`rust_source()` errors if `extendr_fn_options` contains value of the wrong type", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

# due to the use of purrr here, the error that is emitted is on of class `mutate_error`
# we cannot expect `rextendr_error` from this function.
expect_error(rust_function("fn func() {}", extendr_fn_options = list("use_try_from" = 42L)))
})

test_that("`rust_source()` errors if `extendr_fn_options` contains option with an invalid name", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(rust_function("fn func() {}", extendr_fn_options = list("use try from" = TRUE)))
})

test_that("`rust_source()` errors if `extendr_fn_options` contains two invalid options", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(
rust_function("fn func() {}", extendr_fn_options = list("use try from" = TRUE, "r_name" = NULL))
)
})

test_that("`rust_source()` warns if `extendr_fn_options` contains an unknown option", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_warning( # Unknown option
expect_rextendr_error( # Failed compilation because of the unknonw option
Expand All @@ -76,7 +73,7 @@ test_that("`rust_source()` warns if `extendr_fn_options` contains an unknown opt
test_that(
"`rust_source()` does not warn if `extendr_fn_options` contains an unknown option and `use_dev_extendr` is `TRUE`",
{
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_if_opted_out_of_dev_tests()

expect_rextendr_error( # Failed compilation because of the unknonw option
Expand All @@ -93,7 +90,7 @@ test_that(
test_that(
"`rust_function()` does not emit any messages when `quiet = TRUE`",
{
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_no_message(rust_function(code = "fn func() {}", quiet = TRUE))
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-knitr-engine.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("knitr-engine works", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_if_not_installed("knitr")

options <- knitr::opts_chunk$merge(list(
Expand All @@ -23,7 +23,7 @@ test_that("knitr-engine works", {


test_that("Snapshot test of knitr-engine", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_if_not_installed("knitr")

input <- file.path("../data/test-knitr-engine-source-01.Rmd")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-license_note.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("LICENSE.note is generated properly", {
skip_if_not_installed("usethis")
skip_if_cargo_bin(c("license", "--help"))
skip_if_cargo_unavailable(c("license", "--help"))

local_package("testPackage")
use_extendr()
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-make-module-macro.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("Module macro generation", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_src <- r"(
#[extendr]
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Counter {
})

test_that("Macro generation fails on invalid rust code", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(
make_module_macro("#[extendr]\nlet invalid_var = ();"),
Expand All @@ -72,7 +72,7 @@ test_that("Macro generation fails on invalid rust code", {


test_that("Macro generation fails on invalid comments in code", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(
make_module_macro("/*/*/**/"),
Expand All @@ -99,7 +99,7 @@ test_that("Macro generation fails on invalid comments in code", {


test_that("Rust code cleaning", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_equal(
fill_block_comments(c(
Expand Down Expand Up @@ -129,7 +129,7 @@ test_that("Rust code cleaning", {
})

test_that("Rust metadata capturing", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_equal(
find_extendr_attrs_ids(c(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-name-override.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("Multiple rust functions with the same name", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_src_1 <- "
#[extendr]
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-optional-features.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("Feature 'ndarray' is enabled when no extra dependencies are specified", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_on_R42_win()

input <- file.path("../data/ndarray_example.rs")
Expand All @@ -16,7 +16,7 @@ test_that("Feature 'ndarray' is enabled when no extra dependencies are specified
})

test_that("Feature 'ndarray' is enabled when 'extendr-api' has features enabled", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()
skip_on_R42_win()

input <- file.path("../data/ndarray_example.rs")
Expand All @@ -34,20 +34,20 @@ test_that("Feature 'ndarray' is enabled when 'extendr-api' has features enabled"
})

test_that("Enable multiple features simultaneously", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_function("fn test_multiple_features() {}", features = c("ndarray", "serde", "graphics"))
expect_no_error(test_multiple_features())
})

test_that("Passing integers to `features` results in error", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(rust_function("fn test() {}", features = 1:10))
})

test_that("Passing list to `features` results in error", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_rextendr_error(rust_function("fn test() {}", features = list()))
})
18 changes: 9 additions & 9 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("`rust_source()` works", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

rust_src <- "
#[extendr]
Expand Down Expand Up @@ -44,22 +44,22 @@ test_that("`rust_source()` works", {


test_that("`options` override `toolchain` value in `rust_source`", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

withr::local_options(rextendr.toolchain = "Non-existent-toolchain")
expect_rextendr_error(rust_function("fn rust_test() {}"), "Rust code could not be compiled successfully. Aborting.")
})

test_that("`options` override `patch.crates_io` value in `rust_source`", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

withr::local_options(rextendr.patch.crates_io = list(`extendr-api` = "-1"))
expect_rextendr_error(rust_function("fn rust_test() {}"), "Rust code could not be compiled successfully. Aborting.")
})


test_that("`options` override `rextendr.extendr_deps` value in `rust_source`", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

withr::local_options(rextendr.extendr_deps = list(`extendr-api` = "-1"))
expect_rextendr_error(rust_function("fn rust_test() {}"), "Rust code could not be compiled successfully. Aborting.")
Expand All @@ -69,7 +69,7 @@ test_that("`rust_source` works even when the PATH is not set correctly, which ma
skip_on_os("windows") # On Windows, we have no concern as the only installation method is the official installer
skip_on_os("linux") # On Linux, `cargo` might be on somewhere like `/usr/bin`, which is hard to eliminate
skip_on_cran()
skip_if_cargo_bin()
skip_if_cargo_unavailable()

# Construct PATH without ~/.cargo/bin
local_path <- Sys.getenv("PATH")
Expand All @@ -92,7 +92,7 @@ test_that("`rust_source` works even when the PATH is not set correctly, which ma

# https://github.com/extendr/rextendr/issues/234
test_that("`rust_code()` can compile code from rust file", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

input <- file.path("../data/rust_source.rs")
expect_no_error(rust_source(input, module_name = "test_module"))
Expand All @@ -101,7 +101,7 @@ test_that("`rust_code()` can compile code from rust file", {

# https://github.com/extendr/rextendr/issues/234
test_that("`rust_code()` can compile code from rust file multiple times", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

input <- file.path("../data/rust_source.rs")
expect_no_error(rust_source(input, module_name = "test_module"))
Expand All @@ -112,7 +112,7 @@ test_that("`rust_code()` can compile code from rust file multiple times", {

# https://github.com/extendr/rextendr/issues/234
test_that("`rust_code()` can compile code from rust files with identical names", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

input_1 <- file.path("../data/inner_1/rust_source.rs")
input_2 <- file.path("../data/inner_2/rust_source.rs")
Expand All @@ -126,7 +126,7 @@ test_that("`rust_code()` can compile code from rust files with identical names",

# https://github.com/extendr/rextendr/issues/264
test_that("`rust_source()` should not raise internal error for code without extendr attrs", {
skip_if_cargo_bin()
skip_if_cargo_unavailable()

expect_no_error(rust_source(code = "fn test() {}"))
})
Loading
Loading