From: =?utf-8?q?Nicol=C3=A1s_Antinori?= Date: Thu, 29 Oct 2026 13:59:02 -0300 Subject: [PATCH v2 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: <20261029-rust_kunit_v6_send-v2-2-5ce551efc48e@gmail.com> References: <20261029-rust_kunit_v6_send-v2-0-5ce551efc48e@gmail.com> In-Reply-To: <20261029-rust_kunit_v6_send-v2-0-5ce551efc48e@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: 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, =?utf-8?q?Nicol=C3=A1s_Antinori?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5079; i=nico.antinori.7@gmail.com; h=from:subject:message-id; bh=zzc2fwAl7G0oF5NHbgAZSoqWvnfQ18grBJuQDw4KJ6o=; b=owGbwMvMwCV2QdXfwqNJ1YjxtFoSQ9bj6pZZTq5XXzhrVeUXqtR/NffM8tku2Mz4dUOp8Jb/r P9Y6hd3lLIwiHExyIopsszlfP/Z2Na8olx6UxDMHFYmkCEMXJwCMJEVDQx/uKe9Pn5Czrqk4oRM l9CP2ZMDizn4TgRNNBJyd+pNnvZrL8P/2IPR7bZLwtLuzSrsd3rNePBQ5M4J/o8v9lbtKggOZ3/ JBQA= 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. 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 5e688b743ee2152867e2f99c7f18b86e1b7fbe3d..ba3040d302b95af6d6e648a229ac8e9ea2c31a8c 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -367,6 +367,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 288b53d21c4e7c8d9b5148d50c1a833f73e53015..173792563ca8b42e39ec0a53798f7b3bb75bf9cf 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