Closed FirelightFlagboy closed 6 months ago
Nice catch!!! Thx for reporting it!
This issue is just related to magic_conversion
that is applied just to literal str
.
The bug is here: I assume that you have imported rstest
as rstest
:
https://github.com/la10736/rstest/blob/b32e182e96e1927491f1bf2d76b6d3ded939d957/rstest_macros/src/render/inject.rs#L103
I don't know how I can fix it but I can investigate is there is a way to identify the new crate name on compile time and use it in procedural macro.
Seams https://docs.rs/proc-macro-crate/latest/proc_macro_crate does the job... but is not really clean and some edge-case are not covered.
The base Idea is to parse the Cargo.toml
and extract the new name.... maybe there's something better.
Having the attribute crate
would be enough for me since we have a proc-macro
that wrap our tests.
But for other people that don't, that would be nice if the new name of the crate could be inferred with proc-macro-crate
as you said.
I'm not sure if I understand your point correctly... in procedural macro I cannot use $crate
to identify the macro crate. Why you say "Having the attribute crate would be enough for me since we have a proc-macro that wrap our tests"? How do you think I can implement a fix that is suitable for your case?
By
Having the attribute crate would be enough for me since we have a proc-macro that wrap our tests
I mean something like:
#[cfg(test)]
mod tests {
use super::*;
use renamed_rstest::rstest;
#[rstest(crate = "renamed_rstest")]
#[case("2", "2", "22")]
fn it_works(#[case] left: &str, #[case] right: &str, #[case] expected: &str) {
assert_eq!(join(left, right), expected);
}
}
Where the proc-macro rstest
could take an attribute crate
that is "set" to the actual rstest
crate name.
tokio
does that for the proc-macro test
:
use tokio as tokio1;
#[tokio1::test(crate = "tokio1")]
async fn my_test() {
println!("Hello world");
}
Ok.... I got it. I don't love it but is pragmatic. The downside here is that you should write it in all tests :cry: I'll implement the following logic:
rstest
: this will not work with the (deprecated) compact formrstest
name discovering that use proc-macro-crate
(disable by default)rstest
as last fallback.The real issue here is.... I don't know when I'll can do it :cry:
Take a look to substrate/primitives/api/proc-macro/src/utils.rs where thy solve the issue
Take a look to substrate/primitives/api/proc-macro/src/utils.rs where thy solve the issue
I'm not sure to understand what you mean. I can't find a file named substrate/primitives/api/proc-macro/src/utils.rs
, maybe you mean rstest_macros/src/utils.rs
?
Sorry.. it was just a note for me... in https://github.com/paritytech/polkadot-sdk/blob/master/substrate/primitives/api/proc-macro/src/utils.rs they handling the same problem and solve it without introducing the string name needs.
Ok, I've implemented it.
Now you can import and use it with any name:
my_great_name = {package = "rstest", version = "0.20.0" }
and in your code use
use my_great_name::{rstest, fixture}
I'll publish this version ASAP
Oh, nice I'll try that this week :)
Currently the crate
rstest
cannot be renamed.That would be nice if it could have something like the proc-macro
tokio::test
where you can rename the crate by providing the attributecrate
: https://docs.rs/tokio-macros/latest/tokio_macros/attr.test.html#rename-packageReproduction steps
I've create a simple rust project with
cargo new --lib rstest-renamed-bug
and edited the following files:src/lib.rs
with the following content:Cargo.toml
with the following content:How does that expand ?
If we look at the expanded code using
cargo-expand
:We have the following rust output:
The problem is caused by the
use rstest::magic_conversion::*;
lines