frondeus / test-case

Rust procedural macro attribute for adding test cases easily
MIT License
614 stars 39 forks source link

fix: improve compatibility among test proc macros #143

Open kezhuw opened 6 months ago

kezhuw commented 6 months ago

This pr proposes a generic mechanism among different test proc macros to avoid to generate multiple [::core::prelude::v1::test] on test method.

proc_macro_attribute function is fed with tokens after its attribute and no tokens before it.

Give the above, this pr proposes test proc macros to append newly generated macros after existing ones. This way, proc macros processed later can read all macros including generated and handwritten and make further decisions. Specifically, proc macros can append #[::core::prelude::v1::test] only if it does not exist.

Macros that transform test method signature can append #[::core::prelude::v1::test] directly without checking its existence once they generate valid signature for test method.

Closes #101.

kezhuw commented 6 months ago

This comes from my experience in kezhuw/stuck#51.

https://github.com/kezhuw/stuck/blob/629ebefaf00de809011ff93a762e7ecb26e256ce/src/task/session.rs#L609-L616

Currently, above tests are run twice each as both test_case and stuck::test generate #[::core::prelude::v1::test]. Same symptom as #101.

I worked out a solution not only for test_case and stuck::test, but also for test-log, tokio::test and etc. I think it need a cooperation among crates.

One can take kezhuw/stuck#53 as an example for how it solve this. It depends on patch from this pr. cargo test session_cancellation can show the difference with switch [patch.crates-io] in Cargo.toml.

@frondeus @d-e-s-o @taiki-e @Darksonn Sorry for ping you, but would you mind take a look at this and check whether we have a chance to solve this in general ?

Darksonn commented 6 months ago

So, to sum up, your proposal is that test macros should add #[::core::prelude::v1::test] at the end and only if the function doesn't already have the attribute?

kezhuw commented 6 months ago

Yeh, that is it. And I think test_case and tokio::test could free from check existence. test_log::test should check the existence and append #[::core::prelude::v1::test] only if it does not already have.

kezhuw commented 6 months ago

@tamird expressed similar in d-e-s-o/test-log#35

in favor of parsing other attributes on the test function and inserting #[test] only if nothing else has already done it.

I think it is also encouraged for decoration test macros to not generate any form of #[test] as what traced_test did. I expressed this in https://github.com/kezhuw/stuck/pull/53#issuecomment-2064108047.

To be simple, test proc macros should choose either of two.

  1. Don't generate any form of #[test].
  2. Use #[::core::prelude::v1::test] to avoid name collision. And add it to the end of proc macros only if it does not exist.

Hopefully, we can solve this without breaking changes.

kezhuw commented 6 months ago

Added a fixup commit to error duplicated test attributes which is similar to tokio::test.

kezhuw commented 5 months ago

Hi @frondeus, would you mind take a look at this ?

It solves #101. I have synced this with tokio counterpart tokio-rs/tokio#6497.