Skip to content

Commit

Permalink
Use custom cfg instead of custom feature for savvy-cli test (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation authored May 18, 2024
1 parent 7bfdb36 commit 25eafb8
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ r-test = "run --manifest-path ./savvy-cli/Cargo.toml -- test"
# makes the linker ignore these problems.
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=/FORCE:UNRESOLVED"]

[toolchain]
channel = "nightly"
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@

* `r_print!()` and `r_eprint!()` now can print strings containing `%`.

### Breaking Change

* The notation for `savvy-cli test` is now changed to `#[cfg(feature =
"savvy-test")]` from `#[cfg(savvy_test)]`. This is to avoid the upcoming
change in Cargo ([ref](https://blog.rust-lang.org/2024/05/06/check-cfg.html)).

## [v0.6.3] (2024-05-05)

### New features
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ altrep = ["savvy-ffi/altrep"]
# Support logger
logger = ["log", "env_logger"]

savvy-test = []

[build-dependencies]
cc = "1"

Expand Down
2 changes: 1 addition & 1 deletion R-package/src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ fn set_name_external(x: &mut Person, name: &str) -> savvy::Result<()> {
x.set_name(name)
}

#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod tests {
#[test]
fn test_to_upper() -> savvy::Result<()> {
Expand Down
15 changes: 12 additions & 3 deletions book/src/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ pub fn foo() -> savvy::Result<()> {

### Test module

You can write tests under a module marked with `#[cfg(savvy_test)]` instead of
You can write tests under a module marked with `#[cfg(feature = "savvy-test")]` instead of
`#[cfg(test)]`. A `#[test]` function needs to have the return value of
`savvy::Result<()>`, which is the same convention as `#[savvy]`.
To check if an SEXP contains the expected data, `assert_eq_r_code` is convenient.

```rust
#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod test {
use savvy::{OwnedIntegerSexp, assert_eq_r_code};

Expand All @@ -73,6 +73,15 @@ mod test {
}
```

Note that `savvy-test` is just a marker for `savvy-cli`, not a real feature. So,
in theory, you don't really need this. However, in reality, you probably want to
add it to the `[features]` section of `Cargo.toml` because otherwise Cargo warns.

```toml
[features]
savvy-test = []
```

To test a function that takes user-supplied SEXPs like `IntegerSexp`, you can
use `.as_read_only()` to convert from the corresponding `Owned-` type. For
example, if you have a function `your_fn()` that accepts `IntegerSexp`, you can
Expand All @@ -85,7 +94,7 @@ pub fn your_fn(x: IntegerSexp) -> savvy::Result<()> {
// ...snip...
}

#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod test {
use savvy::OwnedIntegerSexp;

Expand Down
4 changes: 2 additions & 2 deletions savvy-bindgen/src/gen/templates/lib_rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ mod test1 {
}
}

// Tests marked under `#[cfg(savvy_test)]` are run by `savvy-cli test`, which
// Tests marked under `#[cfg(feature = "savvy-test")]` are run by `savvy-cli test`, which
// executes the Rust code on a real R session so that you can use R things for
// testing.
#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod test1 {
// The return type must be `savvy::Result<()>`
#[test]
Expand Down
6 changes: 3 additions & 3 deletions savvy-bindgen/src/parse_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl ParsedResult {
let is_test_mod = item_mod
.attrs
.iter()
.any(|attr| attr == &parse_quote!(#[cfg(savvy_test)]));
.any(|attr| attr == &parse_quote!(#[cfg(feature = "savvy-test")]));

match (&item_mod.content, is_test_mod) {
(None, false) => {
Expand Down Expand Up @@ -346,10 +346,10 @@ fn transform_test_mod(
) -> ParsedTestCase {
let mut item_mod = item_mod.clone();

// Remove #[cfg(savvy_test)]
// Remove #[cfg(feature = "savvy-test")]
item_mod
.attrs
.retain(|attr| attr != &parse_quote!(#[cfg(savvy_test)]));
.retain(|attr| attr != &parse_quote!(#[cfg(feature = "savvy-test")]));

item_mod.ident = format_ident!("__UNIQUE_PREFIX__mod_{}", item_mod.ident);

Expand Down
2 changes: 1 addition & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn assert_eq_r_code<T1: Into<Sexp>, T2: AsRef<str>>(actual: T1, expected: T2
assert!(is_r_identical(actual, parsed));
}

#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod test {
use crate::{IntegerSexp, RealSexp};

Expand Down
2 changes: 1 addition & 1 deletion src/sexp/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl IndexMut<usize> for OwnedIntegerSexp {
}
}

#[cfg(savvy_test)]
#[cfg(feature = "savvy-test")]
mod test {
use super::OwnedIntegerSexp;
use crate::NotAvailableValue;
Expand Down

0 comments on commit 25eafb8

Please sign in to comment.