Skip to content

Commit

Permalink
test: set skip conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Jun 26, 2023
1 parent 834de0d commit e5a129d
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/testthat/test-clean.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("rextendr::clean() removes cargo target directory & binaries", {
skip_if_cargo_bin()

path <- local_package("testpkg")
use_extendr()
document()
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-document.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("Running `document` after adding multiple files", {
skip_on_cran()
skip_if_cargo_bin()

path <- local_package("testPackage")
rextendr::use_extendr()
Expand All @@ -12,6 +12,8 @@ test_that("Running `document` after adding multiple files", {
})

test_that("Warn if using older rextendr", {
skip_if_cargo_bin()

path <- local_package("futurepkg")
use_extendr()
desc::desc_set(`Config/rextendr/version` = "999.999")
Expand All @@ -20,6 +22,8 @@ test_that("Warn if using older rextendr", {
})

test_that("Update the Config/rextendr/version field in DESCRIPTION file", {
skip_if_cargo_bin()

path <- local_package("oldpkg")
use_extendr()
desc::desc_set(`Config/rextendr/version` = "0.1")
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("`rust_eval()` works", {
skip_if_cargo_bin()

expect_equal(rust_eval("2 + 2"), 4)
expect_visible(rust_eval("2 + 2"))

Expand All @@ -15,6 +17,8 @@ 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()

provided_values <- seq_len(5)
deferred_handles <- purrr::map(
provided_values,
Expand All @@ -40,6 +44,8 @@ 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()

provided_values <- seq_len(5)

deferred_handles <- purrr::map(
Expand All @@ -66,6 +72,8 @@ 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()

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

testthat::expect_equal(handle(), 5L + 6L)
Expand All @@ -84,6 +92,8 @@ 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()

handle <- rust_eval_deferred("42i32")
fn_name <- attr(handle, "function_name")
dll_path <- attr(handle, "dll_path")
Expand All @@ -106,6 +116,8 @@ 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()

rust_code <- "42f64"

handle_1 <- rust_eval_deferred(rust_code)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-extendr_function_options.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("`extendr` code is compiled with `either` feature and `use_try_from` enabled", {
skip_if_cargo_bin()

rust_function(
"fn type_aware_sum(input : Either<Integers, Doubles>) -> Either<Rint, Rfloat> {
match input {
Expand All @@ -24,6 +26,8 @@ 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()

rust_function(
"fn func() -> &'static str {\"Modified Name\"}",
extendr_fn_options = list("r_name" = "not_original_name")
Expand All @@ -37,22 +41,30 @@ 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()

# 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()

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()

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()

expect_warning( # Unknown option
expect_rextendr_error( # Failed compilation because of the unknonw option
rust_function("fn func() {}", extendr_fn_options = list("unknown_option" = 42L))
Expand All @@ -63,6 +75,8 @@ 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()

expect_rextendr_error( # Failed compilation because of the unknonw option
rust_function(
code = "fn func() {}",
Expand All @@ -77,6 +91,8 @@ test_that(
test_that(
"`rust_function()` does not emit any messages when `quiet = TRUE`",
{
skip_if_cargo_bin()

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

options <- knitr::opts_chunk$merge(list(
code = "2 + 2",
comment = "##",
Expand All @@ -20,6 +23,9 @@ test_that("knitr-engine works", {


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

input <- file.path("../data/test-knitr-engine-source-01.Rmd")
output <- withr::local_file("snapshot_knitr_test.md")

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-license_note.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_that("LICENSE.note is generated properly", {
skip_on_cran()
skip_if_cargo_bin(c("license", "--help"))

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

rust_src <- r"(
#[extendr]
/* multiline
Expand Down Expand Up @@ -60,6 +62,8 @@ impl Counter {
})

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

expect_rextendr_error(
make_module_macro("#[extendr]\nlet invalid_var = ();"),
"Rust code contains invalid attribute macros."
Expand All @@ -68,6 +72,8 @@ test_that("Macro generation fails on invalid rust code", {


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

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


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

expect_equal(
fill_block_comments(c(
"Nested /*/* this is */ /*commented*/ out */",
Expand Down Expand Up @@ -121,6 +129,8 @@ test_that("Rust code cleaning", {
})

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

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

rust_src_1 <- "
#[extendr]
fn rust_fn_1() -> i32 { 1i32 }
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-optional-features.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("Feature 'ndarray' is enabled when no extra dependencies are specified", {
skip_if_cargo_bin()

input <- file.path("../data/ndarray_example.rs")
rust_source(
file = input,
Expand All @@ -13,6 +15,8 @@ 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()

input <- file.path("../data/ndarray_example.rs")
rust_source(
file = input,
Expand All @@ -28,19 +32,27 @@ test_that("Feature 'ndarray' is enabled when 'extendr-api' has features enabled"
})

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

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()

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

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

expect_rextendr_error(rust_function("fn test() {}", features = list()))
})

test_that("Enabling experimental feature raises warning", {
skip_if_cargo_bin()

expect_warning(
rust_function(
"fn test_either(_x : Either<Integers, Doubles>) {}",
Expand All @@ -54,6 +66,8 @@ test_that("Enabling experimental feature raises warning", {
})

test_that("Enabling experimental feature does not raise warning if `use_dev_extendr` is `TRUE`", {
skip_if_cargo_bin()

expect_no_warning(
rust_function(
"fn test_either(_x : Either<Integers, Doubles>) {}",
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("`rust_source()` works", {
skip_if_cargo_bin()

rust_src <- "
#[extendr]
fn hello() -> &'static str {
Expand Down Expand Up @@ -42,17 +44,23 @@ test_that("`rust_source()` works", {


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

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()

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()

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 @@ -61,6 +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()

# Construct PATH without ~/.cargo/bin
local_path <- Sys.getenv("PATH")
Expand All @@ -83,13 +92,17 @@ 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()

input <- file.path("../data/rust_source.rs")
expect_no_error(rust_source(input, module_name = "test_module"))
expect_equal(test_method(), 42L)
})

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

input <- file.path("../data/rust_source.rs")
expect_no_error(rust_source(input, module_name = "test_module"))
expect_no_error(rust_source(input, module_name = "test_module"))
Expand All @@ -99,6 +112,8 @@ 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()

input_1 <- file.path("../data/inner_1/rust_source.rs")
input_2 <- file.path("../data/inner_2/rust_source.rs")

Expand All @@ -111,5 +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()

expect_no_error(rust_source(code = "fn test() {}"))
})
2 changes: 2 additions & 0 deletions tests/testthat/test-use_dev_extendr.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("`use_dev_extendr = TRUE` works together with `features`", {
skip_if_cargo_bin()

rust_function(
"fn uses_either() -> Either<Rint, Rfloat> { Either::Left(Rint::from(42i32)) }",
features = "either",
Expand Down
Loading

0 comments on commit e5a129d

Please sign in to comment.