From patchwork Thu Jul 20 06:38:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13319841 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC514EB64DD for ; Thu, 20 Jul 2023 06:40:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231274AbjGTGk5 (ORCPT ); Thu, 20 Jul 2023 02:40:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231266AbjGTGkl (ORCPT ); Thu, 20 Jul 2023 02:40:41 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BED23583 for ; Wed, 19 Jul 2023 23:40:04 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5834d9ec5f7so25561857b3.1 for ; Wed, 19 Jul 2023 23:40:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689835196; x=1690439996; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=NqyBwYcbKCWrE+16W+dtidCOefNt3yC66lUvAXJt1+s=; b=IlTKnP6Hl3Uub4PdrzLciuVSeDE8WsRXahT7kfpq0odCgAnJWCMHF979FftsDo5mMe SoAunJVXgM3wrjDRvaR7UCvHnidfSONnnormq22bhHGlj4n1M88NakgAfG1o44iJDWIC GjQQPGWkYthFqwnfYEAxYf//QexU1tgz2GwPAD4d7rulKOIx5o7dlACHv+Xn6iRBiK2H BVP+hxhLpvvYvcaSlzQOaACvy56RrzNES6NqJO1v+CPq7iWAOPjCRgGeTHfHuVnKhMUO ESKFVjhglV1S/+YlnMJIxRe1Ymnn2soaXjiJnIwFYFm8d0zpu5vFCREpJidLH6bmzEtn fyzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689835196; x=1690439996; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NqyBwYcbKCWrE+16W+dtidCOefNt3yC66lUvAXJt1+s=; b=BsyiNEk+SANkAT37RsewH5mgi/wMQh7QfhT72uvPmMsBv6kK8oDoL/OLiTnjLyBWQx 89JkU8I/X/kynX8Bb/9pD9iIg7JNfJDWR83JIlecQEICwVSrBUlNbvi5RLMfmmcF+oPd BHeOFL4bX9krb1XMLf9MUY1iXkbAIXWPHfVQL9pt7U2F5IRLGwT5xpRXU3w2c+0AiEws EVn8RcNlxPX/JcVOCNgTLsxva0MxBd2R/jO/5ZiBAPGukebJ6Em4L/sjeuDbjeCVRZtu jdCfVFBMgVcEpkHsEG1k5g4s4tmD5HPHHx2UQ7ys3YnMnsg882RI0xT8o67Jc0tq1YKT uc6Q== X-Gm-Message-State: ABy/qLbgPR9E/XFx+MF8c5Tlz1cRzcedKF+4mUf5r8k01h6h6n7sJiIC vVOau/rhyxemJDUFzEqKh7+LluM4mgHuEA== X-Google-Smtp-Source: APBJJlHuCryQlJRgEDCsmhuDwUEpc3ubbxZJg09h56NXa82UbZtshW62Nzs3H+ZzSMedGeoEDaCwoP1RGJuacw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:dd2:b0:577:6462:24c3 with SMTP id db18-20020a05690c0dd200b00577646224c3mr84157ywb.4.1689835195982; Wed, 19 Jul 2023 23:39:55 -0700 (PDT) Date: Thu, 20 Jul 2023 14:38:51 +0800 Mime-Version: 1.0 X-Mailer: b4 0.13-dev-099c9 Message-ID: <20230720-rustbind-v1-0-c80db349e3b5@google.com> Subject: [PATCH 0/3] rust: kunit: Support KUnit tests with a user-space like syntax From: David Gow To: Brendan Higgins , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Benno Lossin Cc: David Gow , " =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= " , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This series was originally written by José Expósito, and can be found here: https://github.com/Rust-for-Linux/linux/pull/950 Add support for writing KUnit tests in Rust. While Rust doctests are already converted to KUnit tests and run, they're really better suited for examples, rather than as first-class unit tests. This series implements a series of direct Rust bindings for KUnit tests, as well as a new macro which allows KUnit tests to be written using a close variant of normal Rust unit test syntax. The only change required is replacing '#[cfg(test)]' with '#[kunit_tests(kunit_test_suite_name)]' An example test would look like: #[kunit_tests(rust_kernel_hid_driver)] mod tests { use super::*; use crate::{c_str, driver, hid, prelude::*}; use core::ptr; struct SimpleTestDriver; impl Driver for SimpleTestDriver { type Data = (); } #[test] fn rust_test_hid_driver_adapter() { let mut hid = bindings::hid_driver::default(); let name = c_str!("SimpleTestDriver"); static MODULE: ThisModule = unsafe { ThisModule::from_ptr(ptr::null_mut()) }; let res = unsafe { as driver::DriverOps>::register(&mut hid, name, &MODULE) }; assert_eq!(res, Err(ENODEV)); // The mock returns -19 } } Changes since the GitHub PR: - Rebased on top of kselftest/kunit - Add const_mut_refs feature This may conflict with https://lore.kernel.org/lkml/20230503090708.2524310-6-nmi@metaspace.dk/ - Add rust/macros/kunit.rs to the KUnit MAINTAINERS entry Signed-off-by: David Gow --- José Expósito (3): rust: kunit: add KUnit case and suite macros rust: macros: add macro to easily run KUnit tests rust: kunit: allow to know if we are in a test MAINTAINERS | 1 + rust/kernel/kunit.rs | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/macros/kunit.rs | 149 ++++++++++++++++++++++++++++++++++++++++++ rust/macros/lib.rs | 29 +++++++++ 5 files changed, 361 insertions(+) --- base-commit: 64bd4641310c41a1ecf07c13c67bc0ed61045dfd change-id: 20230720-rustbind-477964954da5 Best regards,