Skip to content

Commit

Permalink
Fix #175 and release the new version 0.5.0
Browse files Browse the repository at this point in the history
that introduce two new breaking changes:
dismiss `#[macro_use]` support and
now `#[export]` don't lift your macro at
the root of your crate
  • Loading branch information
la10736 committed Jan 29, 2023
1 parent eeb9b51 commit b12f16c
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 64 deletions.
2 changes: 1 addition & 1 deletion rstest/Cargo.toml
Expand Up @@ -31,7 +31,7 @@ async-std = {version = "1.12.0", features = ["attributes"]}
lazy_static = "1.4.0"
mytest = {package = "rstest", version = "0.16.0", default-features = false}
pretty_assertions = "1.2.1"
rstest_reuse = {version = "0.4.0", path = "../rstest_reuse"}
rstest_reuse = {version = "0.5.0", path = "../rstest_reuse"}
rstest_test = {version = "0.10.0", path = "../rstest_test"}
temp_testdir = "0.2.3"
tokio = {version = "1.19.2", features = ["rt", "macros"]}
Expand Down
2 changes: 1 addition & 1 deletion rstest_macros/Cargo.toml
Expand Up @@ -32,7 +32,7 @@ actix-rt = "2.7.0"
async-std = {version = "1.12.0", features = ["attributes"]}
pretty_assertions = "1.2.1"
rstest = {version = "0.16.0", default-features = false}
rstest_reuse = {version = "0.4.0", path = "../rstest_reuse"}
rstest_reuse = {version = "0.5.0", path = "../rstest_reuse"}
rstest_test = {version = "0.10.0", path = "../rstest_test"}

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion rstest_reuse/Cargo.toml
Expand Up @@ -12,7 +12,7 @@ license = "MIT/Apache-2.0"
name = "rstest_reuse"
readme = "README.md"
repository = "https://github.com/la10736/rstest"
version = "0.4.0"
version = "0.5.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
56 changes: 45 additions & 11 deletions rstest_reuse/README.md
Expand Up @@ -5,7 +5,7 @@

# Reuse `rstest`'s parametrized cases

:warning: [**Version 0.2.0 introduce a breaking change**](#export-attribute)
:warning: [**Version 0.5.0 introduce a breaking change**](#dismiss-macro_use-attribute-support)

This crate give a way to define a tests set and apply them to every case you need to
test. With `rstest` crate you can define a tests list but if you want to apply the same tests
Expand Down Expand Up @@ -171,22 +171,49 @@ is not enougth: this statment doesn't include `rstest_reuse` but just its public

## `#[export]` Attribute

:warning: **Version 0.2.0 introduce a breaking change**
:warning: **Version 0.5.0 introduce a breaking change**

If you want to export your template at the root of your crate you should annotate it by
`#[export]` attribute. Use `#[export]` attribute also if you need to use the
template from another crate.
Now `#[export]` attribute give you the possibility to export your template across crates
but don't lift the macro definition at the top of your crate (that was the default behaviour
prior the 0.5.0 version).

This was the default behaviour in the 0.1.x versions.
Now if you want put your template at the root of your crate you can define it in the root
module or reexport it at the top with something like the following line at the top of
your crate:

Example: note that we don't use `#[macro_use]` attribute.
```rust
pub use my::modules::path::of::my::template::my_template;
```

When you want to export your template you should also take care to declare `rstest_reuse` as `pub`
at the top of your crate to enable to use it from the modules that would import the template.

So in this case in the crate that would export template you should put at the root of your
crate

```rust
#[cfg(test)]
pub use rstest_reuse;
```

And not just `use rstest_reuse` like in the standard cases.

## Dismiss `#[macro_use]` Attribute Support

:warning: **Version 0.5.0 introduce a breaking change**

Till version 0.4.0 you can use `#[macro_use]` to annotate your modules and lift your
macro template to the up level. Now `rstest` leverege only on import and paths like all
othter function and original macro is hidden by a random name.

So now if you would use your template from other module you should import it like any
other symbol.

```rust
mod inner {
mod sub {
pub(crate) mod sub {
use rstest_reuse::*;
#[template]
#[export]
#[rstest(a, b,
case(2, 2),
case(4/2, 2),
Expand All @@ -198,10 +225,17 @@ mod inner {
use rstest_reuse::*;
use rstest::*;

#[apply(two_simple_cases)]
fn it_works(a: u32, b: u32) {
#[apply(inner::sub::two_simple_cases)]
fn it_works_by_path(a: u32, b: u32) {
assert!(a == b);
}

use inner::sub::two_simple_cases
#[apply(inner::sub::two_simple_cases)]
fn it_works_after_use(a: u32, b: u32) {
assert!(a == b);
}

```

## Disclamer
Expand Down
6 changes: 3 additions & 3 deletions rstest_reuse/checkoutlist.md
Expand Up @@ -3,9 +3,9 @@
- [ ] Update rustup
- [ ] Update dependency `cargo upgrade`
- [ ] Run all test
- [ ] Stable: `cargo +stable test`
- [ ] Beta: `cargo +beta test`
- [ ] Nightly: `cargo +nightly test`
- [ ] Stable: `RSTEST_TEST_CHANNEL=stable; cargo +${RSTEST_TEST_CHANNEL} test`
- [ ] Beta: `RSTEST_TEST_CHANNEL=beta; cargo +${RSTEST_TEST_CHANNEL} test`
- [ ] Nightly: `RSTEST_TEST_CHANNEL=nightly; cargo +${RSTEST_TEST_CHANNEL} test`
- [ ] Check Cargo.toml version
- [ ] Check README
- [ ] prepare deploy `cargo publish --dry-run`
Expand Down
29 changes: 20 additions & 9 deletions rstest_reuse/src/lib.rs
Expand Up @@ -337,19 +337,30 @@ pub fn template(_args: proc_macro::TokenStream, input: proc_macro::TokenStream)
None => std::mem::take(&mut attributes),
};

let mut tokens = match get_export(&attributes) {
Some(_) => {
let (macro_attribute, visibility) = match get_export(&attributes) {
Some(_) => (
quote! {
#[macro_export]
}
}
None => quote! {},
},
quote! {
pub
},
),
None => (
quote! {},
quote! {
pub(crate)
},
),
};

let macro_name = template.sig.ident.clone();
tokens.extend(quote! {
let macro_name_rand = format_ident!("{}_{}", macro_name, rand::random::<u64>());

let tokens = quote! {
/// Apply #macro_name template to given body
macro_rules! #macro_name {
#macro_attribute
macro_rules! #macro_name_rand {
( $test:item ) => {
$crate::rstest_reuse::merge_attrs! {
#template,
Expand All @@ -358,8 +369,8 @@ pub fn template(_args: proc_macro::TokenStream, input: proc_macro::TokenStream)
}
}
#[allow(unused_imports)]
pub(crate) use #macro_name as #macro_name;
});
#visibility use #macro_name_rand as #macro_name;
};
tokens.into()
}

Expand Down
38 changes: 24 additions & 14 deletions rstest_reuse/tests/acceptance.rs
Expand Up @@ -12,12 +12,17 @@ pub fn resources<O: AsRef<Path>>(name: O) -> PathBuf {
Path::new("tests").join("resources").join(name)
}

fn create_prj(name: &str) -> Project {
let prj = ROOT_PROJECT.subproject(name);
prj.add_local_dependency("rstest_reuse");
prj.add_dependency("rstest", r#""*""#);
prj
}

fn prj(res: impl AsRef<Path>) -> Project {
let prj_name = sanitize_name(testname());

let prj = ROOT_PROJECT.subproject(&prj_name);
prj.add_local_dependency("rstest_reuse");
prj.add_dependency("rstest", r#""*""#);
let prj = create_prj(&prj_name);
prj.set_code_file(resources(res))
}

Expand Down Expand Up @@ -81,16 +86,6 @@ fn in_mod() {
.assert(output);
}

#[test]
fn import_from_mod_by_macro_use() {
let (output, _) = run_test("import_from_mod.rs");

TestResults::new()
.ok("user::it_works::case_1")
.ok("user::it_works::case_2")
.assert(output);
}

#[test]
fn import_from_mod() {
let (output, _) = run_test("qualify_template_use.rs");
Expand Down Expand Up @@ -150,7 +145,8 @@ fn enable_export_macros() {

TestResults::new()
.ok("foo::bar::test::case_1")
.ok("test::case_1")
.ok("test_path::case_1")
.ok("test_import::case_1")
.assert(output);
}

Expand All @@ -173,6 +169,20 @@ fn no_local_macro_should_not_compile() {
assert!(!output.status.success());
}

#[test]
fn should_export_main_root() {
// Add project with template
let _prj_template =
create_prj("export_template_root").set_code_file(resources("export_template_root.rs"));

// Main test project that use template
let prj = prj("import_template.rs");
prj.add_path_dependency("export_template_root", "../export_template_root");

let output = prj.run_tests().unwrap();
TestResults::new().ok("test::case_1").assert(output);
}

lazy_static! {
static ref ROOT_DIR: TempDir = TempDir::new(std::env::temp_dir().join("rstest_reuse"), false);
static ref ROOT_PROJECT: Project = Project::new(ROOT_DIR.as_ref());
Expand Down
18 changes: 15 additions & 3 deletions rstest_reuse/tests/resources/export_template.rs
@@ -1,7 +1,7 @@
use rstest_reuse;

mod foo {
mod bar {
pub(crate) mod bar {
use rstest::rstest;
use rstest_reuse::{self, *};

Expand All @@ -19,9 +19,21 @@ mod foo {
}

use rstest::rstest;
use rstest_reuse::apply;
use rstest_reuse::*;

#[apply(foo::bar::my_template)]
fn test_path(#[case] s: &str) {
assert_eq!("bar", s);
}

use foo::bar::my_template;
#[apply(my_template)]
fn test(#[case] s: &str) {
fn test_import(#[case] s: &str) {
assert_eq!("bar", s);
}

#[template]
#[export]
#[rstest]
#[case("bar")]
fn root_level(#[case] s: &str) {}
8 changes: 8 additions & 0 deletions rstest_reuse/tests/resources/export_template_root.rs
@@ -0,0 +1,8 @@
pub use rstest_reuse;
use rstest_reuse::template;

#[template]
#[export]
#[rstest]
#[case("bar")]
fn root_level(#[case] s: &str) {}
21 changes: 0 additions & 21 deletions rstest_reuse/tests/resources/import_from_mod.rs

This file was deleted.

8 changes: 8 additions & 0 deletions rstest_reuse/tests/resources/import_template.rs
@@ -0,0 +1,8 @@
use export_template_root::root_level;
use rstest::*;
use rstest_reuse::apply;

#[apply(root_level)]
fn test(#[case] s: &str) {
assert_eq!("bar", s);
}

0 comments on commit b12f16c

Please sign in to comment.