rust: add Derive macro unit tests
Subsystems (2)
cover rust: add Derive macro unit tests
We don't currently test our proc macros and it'd be nice to do so.
Usually this would be done with something like
https://crates.io/crates/trybuild which runs cargo and tries to compile a test input, and checks for success/failure. However we cannot use it with meson directly, plus it would drag in a lot of dependencies anyway.
Instead of compiling, we can easily just expand test input into token streams since we already split macro implementation into separate functions, allowing us to either get a TokenStream back or a compile error message.
You can run the added tests directly with this meson command:
meson test rust-qemu-api-macros-tests
---
Manos Pitsidianakis (2):
rust/qemu-api-macros: normalize TryInto output rust/qemu-api-macros: add unit tests
rust/qemu-api-macros/meson.build | 3 + rust/qemu-api-macros/src/lib.rs | 7 +- rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++ rust/qemu-api-macros/src/utils.rs | 1 + 4 files changed, 144 insertions(+), 2 deletions(-)
---
base-commit: c77283dd5d79149f4e7e9edd00f65416c648ee59
change-id: 20250704-rust_add_derive_macro_unit_tests-3e7daf905d62
--
γαῖα πυρί μιχθήτω
PATCH 1/2 rust/qemu-api-macros: normalize TryInto output
Remove extraneous ; and add missing trailing comma to TryInto derive macro to match rustfmt style. We will add a test in the followup commit and we would like the inlined output in the test body to be properly formatted as well.
No functional changes intended.
--- rust/qemu-api-macros/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
rust/qemu-api-macros/src/lib.rs +2 -2
@@ -201,7 +201,7 @@ fn derive_tryinto_body(let discriminants:Vec <&Ident > = variants.iter ().map (|f| &f.ident).collect ();Ok (quote! { - #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*; + #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*match value { #(#discriminants => core::result::Result ::Ok (#name::#discriminants),)* _ => core::result::Result ::Err (value),
@@ -229,7 +229,7 @@ pub const fn from_bits(value: #repr) -> Self { #body }) { Ok(x) => x, - Err(_) => panic!(#errmsg) + Err(_) => panic!(#errmsg), } } }
Signature · 1 line
2.47.2
Re: [PATCH 1/2] rust/qemu-api-macros: normalize TryInto output
Quoted 10 lines · Manos Pitsidianakis
Date: Fri, 04 Jul 2025 13:26:57 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [PATCH 1/2]rust/qemu-api-macros: normalize TryInto output
X-Mailer: b4 0.14.2Remove extraneous
;and add missing trailing comma to TryInto derive macro to match rustfmt style. We will add a test in the followup commit and we would like the inlined output in the test body to be properly formatted as well.
Quoted 6 lines
No functional changes intended.
Signed-off-by Manos Pitsidianakis <[email protected]>--- rust/qemu-api-macros/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
PATCH 2/2 rust/qemu-api-macros: add unit tests
In reply to Manos PitsidianakisAdd unit tests to check Derive macro output for expected error messages, or for expected correct codegen output.
--- rust/qemu-api-macros/meson.build | 3 + rust/qemu-api-macros/src/lib.rs | 3 + rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++ rust/qemu-api-macros/src/utils.rs | 1 + 4 files changed, 142 insertions(+)
rust/qemu-api-macros/meson.build +3 -0
@@ -17,3 +17,6 @@ _qemu_api_macros_rs = rust.proc_macro( qemu_api_macros = declare_dependency( link_with: _qemu_api_macros_rs, ) + +rust.test('rust-qemu-api-macros-tests', _qemu_api_macros_rs, + suite: ['unit', 'rust'])
rust/qemu-api-macros/src/lib.rs +3 -0
@@ -15,6 +15,9 @@mod bits;use bits::BitsConstInternal ; +#[cfg(test)] +mod tests; +fn get_fields <'a>( input: &'aDeriveInput , msg: &str,
rust/qemu-api-macros/src/tests.rs new file +135 -0
135 lines not on this page · raw message
rust/qemu-api-macros/src/utils.rs +1 -0
@@ -5,6 +5,7 @@use proc_macro2::Span ;use quote::quote_spanned; +#[derive( Debug )] pub enum MacroError {Message (String ,Span ),ParseError (syn::Error ),
Signature · 1 line
2.47.2
Re: [PATCH 2/2] rust/qemu-api-macros: add unit tests
Quoted 72 lines · Manos Pitsidianakis
Date: Fri, 04 Jul 2025 13:26:58 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [PATCH 2/2]rust/qemu-api-macros: add unit tests
X-Mailer: b4 0.14.2Add unit tests to check Derive macro output for expected error messages, or for expected correct codegen output.
Signed-off-by Manos Pitsidianakis <[email protected]>--- rust/qemu-api-macros/meson.build | 3 + rust/qemu-api-macros/src/lib.rs | 3 + rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++ rust/qemu-api-macros/src/utils.rs | 1 + 4 files changed, 142 insertions(+)rust/qemu-api-macros/meson.build +3
-0@@ -17,3 +17,6 @@ _qemu_api_macros_rs = rust.proc_macro( qemu_api_macros = declare_dependency( link_with: _qemu_api_macros_rs, ) + +rust.test('rust-qemu-api-macros-tests', _qemu_api_macros_rs, + suite: ['unit', 'rust'])rust/qemu-api-macros/src/lib.rs +3
-0@@ -15,6 +15,9 @@mod bits;use bits::BitsConstInternal ; +#[cfg(test)] +mod tests; +fn get_fields <'a>( input: &'aDeriveInput , msg: &str,rust/qemu-api-macros/src/tests.rs new file +25
-0@@ -0,0 +1,135 @@ +// Copyright 2025, Linaro Limited +// Author(s): Manos Pitsidianakis <[email protected]> +// SPDX-License-Identifier: GPL-2.0-or-later + +use super ::*; +use quote::quote; + +macro_rules! derive_compile_fail { + ($derive_fn:ident, $input:expr, $error_msg:expr) => {{ +let input: proc_macro2::TokenStream = $input; +let error_msg: &str = $error_msg; +let derive_fn:fn (input: syn::DeriveInput ) ->Result <proc_macro2::TokenStream ,MacroError > = + $derive_fn; + +let input: syn::DeriveInput = syn::parse2 (input).unwrap (); +let result =derive_fn (input); +let MacroError ::Message (err, _) = result.unwrap_err ()else { +panic! () + }; +assert_eq! (err, error_msg); + }}; +} + +macro_rules! derive_compile { + ($derive_fn:ident, $input:expr, $($expected:tt)*) => {{
rust/qemu-api-macros/src/tests.rs line 35
Quoted 10 lines
rust/qemu-api-macros/src/tests.rs new file +10
-0+let input: proc_macro2::TokenStream = $input; + let expected: proc_macro2::TokenStream = $($expected)*; +let derive_fn:fn (input: syn::DeriveInput ) ->Result <proc_macro2::TokenStream ,MacroError > = + $derive_fn; + +let input: syn::DeriveInput = syn::parse2 (input).unwrap (); +let result =derive_fn (input).unwrap (); +assert_eq! (result.to_string (), expected.to_string ()); + }}; +}
Good examples to test macros. LGTM,
Quoted 3 lines
You can run the added tests directly with this meson command:
meson test rust-qemu-api-macros-tests
Hi Manos, do you meet this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/mesonbuild/mesonmain.py", line 146, in run
return options.run_func(options)
File "/usr/lib/python3/dist-packages/mesonbuild/mtest.py", line 2004, in run
b = build.load(options.wd)
File "/usr/lib/python3/dist-packages/mesonbuild/build.py", line 2868, in load
obj = pickle.load(f)
ModuleNotFoundError: No module named 'mesonbuild.options'
ERROR: Unhandled python exception
This is a Meson bug and should be reported!
---
But I've tested this series via make check.
Quoted 16 lines · Zhao Liu
Quoted 4 lines
You can run the added tests directly with this meson command:
meson test rust-qemu-api-macros-tests
Hi Manos, do you meet this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/mesonbuild/mesonmain.py", line 146, in run
returnoptions.run_func(options)
File "/usr/lib/python3/dist-packages/mesonbuild/mtest.py", line 2004, in run
b =build.load(options.wd)
File "/usr/lib/python3/dist-packages/mesonbuild/build.py", line 2868, in load
obj =pickle.load(f)
ModuleNotFoundError: No module named 'mesonbuild.options'
Quoted 10 lines
ERROR: Unhandled python exception
This is a Meson bug and should be reported!
---But I've tested this series via
make check.
Sounds like it used your global meson installation instead of the one from the virtual environment of your build directory.
Thank you for testing!
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Quoted 12 lines · Manos Pitsidianakis
We don't currently test our proc macros and it'd be nice to do so.
Usually this would be done with something like
https://crates.io/crates/trybuild which runs cargo and tries to compile a test input, and checks for success/failure. However we cannot use it with meson directly, plus it would drag in a lot of dependencies anyway.Instead of compiling, we can easily just expand test input into token streams since we already split macro implementation into separate functions, allowing us to either get a TokenStream back or a compile error message.
Quoted 5 lines
You can run the added tests directly with this meson command:
meson test rust-qemu-api-macros-tests
Signed-off-by Manos Pitsidianakis <[email protected]>
Nice. Adjusted for MacroError removal and applied, thanks.
Paolo