From: =?utf-8?q?Nicol=C3=A1s_Antinori?= Date: Fri, 13 Nov 2026 15:20:56 -0300 Subject: [PATCH v3 2/3] rust: kunit: allow same test name with different #[cfg(...)] MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20261113-rust_kunit_v6_send-v3-2-0d7832c07166@gmail.com> References: <20261113-rust_kunit_v6_send-v3-0-0d7832c07166@gmail.com> In-Reply-To: <20261113-rust_kunit_v6_send-v3-0-0d7832c07166@gmail.com> To: Alice Ryhl , Benno Lossin , Boqun Feng , Brendan Higgins , Daniel Almeida , Danilo Krummrich , David Gow , Gary Guo , Miguel Ojeda , =?utf-8?q?Onur_=C3=96zkan?= Cc: Shuah Khan , Alexandre Courbot , Andreas Hindborg , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Burak Emir , Rae Moar , Tamir Duberstein , Trevor Gross , Yury Norov , kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, =?utf-8?q?Nicol=C3=A1s_Antinori?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5166; i=nico.antinori.7@gmail.com; h=from:subject:message-id; bh=z+rWrk/eWvf2ggetV8Ph1CCp9hKJ1q4AZXFHqAQl1qA=; b=owGbwMvMwCV2QdXfwqNJ1YjxtFoSQ9b3ULMZqzPbXy7/lTt3tnu46uQy/9wF2prfa9f84rm6M 65gxc2tHaUsDGJcDLJiiixzOd9/NrY1ryiX3hQEM4eVCWQIAxenAEyE14rhfyZrdck7va0Ovyy/ OpVdsOeve3t8wVx7gSKHDK9p5fxm8gz/1GuSrXZHnXp9Y7J6zXHmrrM7XZZeUmnrFi1ft0vx/cM v/AA= X-Developer-Key: i=nico.antinori.7@gmail.com; a=openpgp; fpr=9D09EFF3333D3778771BB252D0254F3848822532 X-Endpoint-Received: by B4 Relay for nico.antinori.7@gmail.com/default with auth_id=1086 List-Id: B4 Relay Submissions Sometimes it is necessary to test the same code paths under different configurations. The `#[cfg(...)]` macro can be used to check if specific configurations are enabled and run tests accordingly. Currently, defining multiple tests with the same name under different `#[cfg(...)]` attributes results in a compilation error. This patch removes that restriction, allowing identical test names across different configurations. Reviewed-by: David Gow Reviewed-by: Gary Guo Signed-off-by: Nicolás Antinori --- rust/kernel/kunit.rs | 22 ++++++++++++++++++++++ rust/macros/kunit.rs | 36 ++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index be02b9e4ae70f4913a5074c512d4adf78e34335c..76be6156f452cc174019e6565b54320967521874 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -361,6 +361,28 @@ fn rust_test_kunit_in_kunit_test() { assert!(in_kunit_test()); } + // Tests with cfg have the same name on purpose because we are implicitly testing that + // tests with the same name but different configs do not throw a compilation error + #[test] + #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "y")] + fn rust_test_kunit_run_only_active_cfg() { + assert!(in_kunit_test()); + } + + #[test] + #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "n")] + fn rust_test_kunit_run_only_active_cfg() { + // This test should never run because of the `cfg`. + assert!(false) + } + + #[test] + #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "INVALID_VALUE")] + fn rust_test_kunit_run_only_active_cfg() { + // This test should never run because of the `cfg`. + assert!(false) + } + #[test] #[should_panic] fn rust_test_kunit_panic_in_kunit_test() { diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs index ac7495deab17d6579e3f213b7018516c9ca68425..476315c3f9e1b9d987b4f4121b36745d83ad87bb 100644 --- a/rust/macros/kunit.rs +++ b/rust/macros/kunit.rs @@ -45,6 +45,8 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result