diff --git a/rstest/Cargo.toml b/rstest/Cargo.toml index e36fddd..2a60aeb 100644 --- a/rstest/Cargo.toml +++ b/rstest/Cargo.toml @@ -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"]} diff --git a/rstest_macros/Cargo.toml b/rstest_macros/Cargo.toml index adb78eb..fba39ea 100644 --- a/rstest_macros/Cargo.toml +++ b/rstest_macros/Cargo.toml @@ -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] diff --git a/rstest_reuse/Cargo.toml b/rstest_reuse/Cargo.toml index 20af24e..484ebdf 100644 --- a/rstest_reuse/Cargo.toml +++ b/rstest_reuse/Cargo.toml @@ -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 diff --git a/rstest_reuse/README.md b/rstest_reuse/README.md index 1aed789..2276283 100644 --- a/rstest_reuse/README.md +++ b/rstest_reuse/README.md @@ -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 @@ -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), @@ -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 diff --git a/rstest_reuse/checkoutlist.md b/rstest_reuse/checkoutlist.md index 4a0dc15..ebc1f0b 100644 --- a/rstest_reuse/checkoutlist.md +++ b/rstest_reuse/checkoutlist.md @@ -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` diff --git a/rstest_reuse/src/lib.rs b/rstest_reuse/src/lib.rs index b4fb5fb..5372aab 100644 --- a/rstest_reuse/src/lib.rs +++ b/rstest_reuse/src/lib.rs @@ -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::()); + + 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, @@ -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() } diff --git a/rstest_reuse/tests/acceptance.rs b/rstest_reuse/tests/acceptance.rs index 36e157c..0309c03 100644 --- a/rstest_reuse/tests/acceptance.rs +++ b/rstest_reuse/tests/acceptance.rs @@ -12,12 +12,17 @@ pub fn resources>(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) -> 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)) } @@ -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"); @@ -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); } @@ -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()); diff --git a/rstest_reuse/tests/resources/export_template.rs b/rstest_reuse/tests/resources/export_template.rs index 17c39aa..534eae9 100644 --- a/rstest_reuse/tests/resources/export_template.rs +++ b/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, *}; @@ -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) {} diff --git a/rstest_reuse/tests/resources/export_template_root.rs b/rstest_reuse/tests/resources/export_template_root.rs new file mode 100644 index 0000000..8c7a33a --- /dev/null +++ b/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) {} diff --git a/rstest_reuse/tests/resources/import_from_mod.rs b/rstest_reuse/tests/resources/import_from_mod.rs deleted file mode 100644 index 967c60d..0000000 --- a/rstest_reuse/tests/resources/import_from_mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -use rstest_reuse; - -#[macro_use] -mod template { - use rstest::rstest; - use rstest_reuse::template; - - #[template] - #[rstest(a, b, case(2, 2), case(4/2, 2))] - fn two_simple_cases(a: u32, b: u32) {} -} - -mod user { - use rstest::rstest; - use rstest_reuse::apply; - - #[apply(two_simple_cases)] - fn it_works(a: u32, b: u32) { - assert!(a == b); - } -} diff --git a/rstest_reuse/tests/resources/import_template.rs b/rstest_reuse/tests/resources/import_template.rs new file mode 100644 index 0000000..129b52d --- /dev/null +++ b/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); +}