macros: append generated test attribute so others are aware of it#6497
macros: append generated test attribute so others are aware of it#6497Darksonn merged 11 commits intotokio-rs:masterfrom
Conversation
This way other test macros can choose to not generate `#[::core::prelude::v1::test]` to avoid duplicated test runs. This commit also rejects existing `#[::core::prelude::v1::test]` just like how and why it rejects existing `#[test]`.
tokio-macros/Cargo.toml
Outdated
| proc-macro2 = "1.0.60" | ||
| quote = "1" | ||
| syn = { version = "2.0", features = ["full"] } | ||
| syn = { version = "2.0", features = ["full", "extra-traits"] } |
There was a problem hiding this comment.
What do you need this feature for? I prefer to avoid adding these features as they hurt compilation times.
There was a problem hiding this comment.
impl PartialEq for Attribute. I guess I could duplicate code. Let me figure out.
There was a problem hiding this comment.
I see. if it is possible, then try to perform the equality check manually. Thanks!
| error: second test attribute is supplied, try to order it after `tokio::test` | ||
| --> $DIR/macros_invalid_input.rs:49:1 | ||
| | | ||
| 49 | #[::core::prelude::v1::test::test] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
There was a problem hiding this comment.
What? The #[tokio::test] macro is first, so what do you mean by "after"?
There was a problem hiding this comment.
I supposed this to be generated by other preceding test macros, e.g. #[test_log::test]. I guess I should reshape it or keep it origin. Any preference ?
There was a problem hiding this comment.
How about "second test attribute is supplied, consider removing or ordering it after tokio::test" ?
There was a problem hiding this comment.
My challenge with the wording here is that the code that triggers this error has #[tokio::test] first, and then afterwards, it has a #[::core::prelude::v1::test]. So suggesting that you move #[::core::prelude::v1::test] after #[tokio::test] doesn't make since it's already after #[tokio::test]. Perhaps you could say "before" or "above" instead of "after"?
There was a problem hiding this comment.
I know, it is strange. It should be something similar to following.
❯ grep -n -B 2 -A 2 "with_append_test_attribute_and_test_args_and_panic_async" tests/mod.rs
84-#[test_log::test]
85-#[tokio::test]
86:async fn with_append_test_attribute_and_test_args_and_panic_async(x: i8, _y: i8) {
87- assert_eq!(x, 0);
88-}
❯
❯ cargo test with_append_test_attribute_and_test_args_and_panic_async -- --nocapture
error: second test attribute is supplied, consider removing it or ordering it after `tokio::test`
--> tests/mod.rs:84:1
|
84 | #[test_log::test]
| ^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `test_log::test` (in Nightly builds, run with -Z macro-backtrace for more info)
error: could not compile `test-log` (test "mod") due to 2 previous errorsMoving it "before" or "above" will result in duplicated test runs.
I guess we could:
- Introduce dev dependency
test-log. This ways the words should be more intuitively. Is it worth ? - Use the short version "second test attribute is supplied".
- Other suggestions.
Hi @Darksonn, thank you for reviewing. I have updated this with a fixup commit targeting to your two concerns. Regarding the error words, I am open to any sentence, simply not good at 😮💨 .
There was a problem hiding this comment.
Maybe just say "consider removing or changing the order of your test attributes".
…s are aware of it
…o others are aware of it
|
Hi @Darksonn, thank you for your reviewing, I have solved all review comments. Is there anything else left for this to move forward ? Should I do some squash/rebase work ? |
Darksonn
left a comment
There was a problem hiding this comment.
Hi again, thanks for the ping. Sorry about the delay.
tokio-macros/src/entry.rs
Outdated
| syn::Meta::Path(path) => path, | ||
| _ => return false, | ||
| }; | ||
| let segments = ["core", "prelude", "v1", "test"]; |
There was a problem hiding this comment.
It's also available as a few other paths such as ::std::prelude::v1::test and ::core::prelude::rust_2021::test.
There was a problem hiding this comment.
Would this be too paranoid for us ?
Given that Rust has three editions since v1 and new coming in future, together with combinations from "std", "core" and leading colon, this is a pretty large list.
Given the fact that test is there since day one, I would recommend not doing this. I checked test macros listed below and none use variants other than the two. I don't think test macros should generate other variants, if they do, let them fix it to avoid duplicated test runs but not us.
- test-log, test-case, async-std::test, googletest::test uses
::core::prelude::v1::test. - quickcheck, test-strategy, rstest uses
#[test] proptest,tracing_test::traced_test does not generate any
There was a problem hiding this comment.
Perhaps we can match ::[std|core]::prelude::*:test?
There was a problem hiding this comment.
Updated. Additionally, leading colons are optional now as review comments below.
There was a problem hiding this comment.
LGTM. If you can also add some of the variants to the tests, then I think this is ready to merge.
| #[tokio::test] | ||
| #[::core::prelude::v1::test] | ||
| async fn test_has_generated_second_test_attr() {} |
There was a problem hiding this comment.
What about core without the leading colons?
We decided to bump minimum required version of tokio to 1.34. Currently, the newest tokio version is 1.38, but some of the integration tests are eaten when testing with this specific verstion of tokio. Which is why, as of now, we decided not to support this version. The issue with version 1.38 is related to #[tokio::test] and #[ntest::timeout] attributes. Refs: - tokio-rs/tokio#6610 - becheran/ntest#28 - tokio-rs/tokio#6497
We decided to bump minimum required version of tokio to 1.34. Currently, the newest tokio version is 1.38, but some of the integration tests are eaten when testing with this specific verstion of tokio. Which is why, as of now, we decided not to support this version. The issue with version 1.38 is related to #[tokio::test] and #[ntest::timeout] attributes. Refs: - tokio-rs/tokio#6610 - becheran/ntest#28 - tokio-rs/tokio#6497
We decided to bump minimum required version of tokio to 1.34. Currently, the newest tokio version is 1.38, but some of the integration tests are eaten when testing with this specific verstion of tokio. Which is why, as of now, we decided not to support this version. The issue with version 1.38 is related to #[tokio::test] and #[ntest::timeout] attributes. Refs: - tokio-rs/tokio#6610 - becheran/ntest#28 - tokio-rs/tokio#6497
We decided to bump minimum required version of tokio to 1.34. Currently, the newest tokio version is 1.38, but some of the integration tests are eaten when testing with this specific verstion of tokio. Which is why, as of now, we decided not to support this version. The issue with version 1.38 is related to #[tokio::test] and #[ntest::timeout] attributes. Refs: - tokio-rs/tokio#6610 - becheran/ntest#28 - tokio-rs/tokio#6497
This way test macros following `#[rstest]` can decide whether or not to generate test macro to avoid duplicate test runs. It is an attempt to improve capabilities among test macros. Currently, following test from [googletest](https://github.com/google/googletest-rust/blob/21f2948684847922a416252b8118e3eada8e29d6/integration_tests/src/google_test_with_rstest.rs#L52-L57)(`main` branch at 2025-01-16) will run twice. ```rust #[rstest] #[case(1)] #[gtest] fn paramterised_test_should_work_with_rstest_first(#[case] value: u32) -> Result<()> { verify_that!(value, eq(value)) } ``` See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
This way test macros following `#[rstest]` can decide whether or not to generate test macro to avoid duplicate test runs. It is an attempt to improve capabilities among test macros. Currently, following test from [googletest](https://github.com/google/googletest-rust/blob/21f2948684847922a416252b8118e3eada8e29d6/integration_tests/src/google_test_with_rstest.rs#L52-L57)(`main` branch at 2025-01-16) will run twice. ```rust #[rstest] #[case(1)] #[gtest] fn paramterised_test_should_work_with_rstest_first(#[case] value: u32) -> Result<()> { verify_that!(value, eq(value)) } ``` See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
This way test macros following `#[rstest]` can decide whether or not to generate test macro to avoid duplicate test runs. It is an attempt to improve capabilities among test macros. Currently, following test from [googletest](https://github.com/google/googletest-rust/blob/21f2948684847922a416252b8118e3eada8e29d6/integration_tests/src/google_test_with_rstest.rs#L52-L57)(`main` branch at 2025-01-16) will run twice. ```rust #[rstest] #[case(1)] #[gtest] fn paramterised_test_should_work_with_rstest_first(#[case] value: u32) -> Result<()> { verify_that!(value, eq(value)) } ``` See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
This way test macros following `#[rstest]` can decide whether or not to generate test macro to avoid duplicate test runs. It is an attempt to improve capabilities among test macros. Currently, following test from [googletest](https://github.com/google/googletest-rust/blob/21f2948684847922a416252b8118e3eada8e29d6/integration_tests/src/google_test_with_rstest.rs#L52-L57)(`main` branch at 2025-01-16) will run twice. ```rust #[rstest] #[case(1)] #[gtest] fn paramterised_test_should_work_with_rstest_first(#[case] value: u32) -> Result<()> { verify_that!(value, eq(value)) } ``` See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
`#[gtest]` will benefit from la10736/rstest#291, we could also benefit other test macros. It is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. We could deprecate `#[googletest::test]` oneday after rust-lang/rust#82419 released. See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
`#[gtest]` will benefit from la10736/rstest#291, we could also benefit other test macros. It is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. We could deprecate `#[googletest::test]` oneday after la10736/rstest#291 released. See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
`#[gtest]` will benefit from la10736/rstest#291, we could also benefit other test macros. This is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. We could deprecate `#[googletest::test]` oneday after la10736/rstest#291 released. See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
`#[gtest]` will benefit from la10736/rstest#291, we could also benefit other test macros. This is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. We could deprecate `#[googletest::test]` oneday after la10736/rstest#291 released. See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
This is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. Without the fix, the new test will run twice. See also tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, google/googletest-rust#561, kezhuw/stuck#53.
This way we could avoid duplicated test runs if `#[test]` variants already exist See also frondeus/test-case#101, frondeus/test-case#143 and tokio-rs/tokio#6497. Closes d-e-s-o#35.
This way we could avoid duplicated test runs if `#[test]` variants already exist See also frondeus/test-case#101, frondeus/test-case#143 and tokio-rs/tokio#6497. Closes d-e-s-o#35.
This way we could avoid duplicated test runs if `#[test]` variants already exist See also frondeus/test-case#101, frondeus/test-case#143 and tokio-rs/tokio#6497. Closes d-e-s-o#35.
This way we could avoid duplicated test runs if `#[test]` variants already exist See also frondeus/test-case#101, frondeus/test-case#143 and tokio-rs/tokio#6497. Closes #35.
Motivation
Improve compatibility among multiple test proc macros.
Solution
#[::core::prelude::v1::test]to the end of test proc macros. This way, proc macros process later can choose to not generate#[::core::prelude::v1::test]to avoid duplicated test runs.#[::core::prelude::v1::test]to avoid duplicated test runs astokio::testis transforming an invalid test signature to one accepted by#[test]. This is consistent with what we currently treat#[test].Links