From nobody Tue Jun 23 21:32:01 2026 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 D2AA0C433EF for ; Sat, 26 Feb 2022 21:23:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229729AbiBZVYL (ORCPT ); Sat, 26 Feb 2022 16:24:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229708AbiBZVYI (ORCPT ); Sat, 26 Feb 2022 16:24:08 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E080D28EBB5 for ; Sat, 26 Feb 2022 13:23:33 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id e129-20020a25d387000000b006245d830ca6so6820569ybf.13 for ; Sat, 26 Feb 2022 13:23:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=J6j2inEmM2G/VjC4yVLo68KdKRLj/4RdQFdUNasgbc4=; b=p1UQxaHkIXrxFewg80mViEE+sNxpZrJ5BXIUBsMp5MBkAfULz3wEdzO1SWTfpqxAeb AovBR03HS/YvV8v59t+R4ro/HK1Lgb/A11zoIm2jVSKCJpk1Omff8DZdBVun3eNSh99P qiCP88AYXqZRg0F26UMFh6tFAqD0T9MKB9RM7BQSfgF135w2ToWvF/BiaNuKT/cJiHlb eVuWdEi7IHrmaSR0DHzYjNtCtM0wmSLwQ0MGU7BbIvYtJgYrgrzomRs5EKkfRLu2UoXC pJhBv0VrFeLJqqzrK2bfPLNZfkqayO7vj26sTHNFTTyV0ST1Hhk++WcAnCi0btUZA/km wXZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=J6j2inEmM2G/VjC4yVLo68KdKRLj/4RdQFdUNasgbc4=; b=JrYUw9wXtBWRYnsGwo7XafDGmaK1qI6B9diwMrGdgr1DbrtY36GCnEdKJCnv4CVffn 30HEgLsyshJBIiqfvcrjlVaqQgh71ynvgYxmcWWlKmQoGCHXxljJqSd5uMEKyAo/Zzoe WEz+O1HJzxtl49wjqWC6jCBWfAaMVl7MC1g0BP1FaXBGT07GS3HDYpwAwgDzD+UxcU3V 0AxNpUwlhUgvcWKm0z/TLVvmA517Mq8dpSXoVyVzF/uZO80COilMD7g0nL4xF8mvMzkz fibNxgjRsrYLCUMNiYlWBVKbyjEu4/z4sWw42BV3g3iAa5MY2fPZdzq9mGTjSUVxLBMY 977g== X-Gm-Message-State: AOAM532fRd7Q/b6q+R4ngBZun5DrR/ZwZ0H52vvuV6zDCrKQTuj1WQ/Q tseD8GZMJu3TTSLeyHiYhbncJ+qWwaXBWA== X-Google-Smtp-Source: ABdhPJx2ZmaY96X8Esy5Q6/6g/Hm/z+/jdCTYF1vj/idV3IZqOvcTd6IY6lC1+gMb6jOOMuBFGu2ezJ9MXER7A== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:3a80:7413:c995:5fb1]) (user=dlatypov job=sendgmr) by 2002:a05:6902:1027:b0:619:3e74:ab5b with SMTP id x7-20020a056902102700b006193e74ab5bmr13207007ybt.136.1645910613153; Sat, 26 Feb 2022 13:23:33 -0800 (PST) Date: Sat, 26 Feb 2022 13:23:25 -0800 Message-Id: <20220226212325.2984807-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v2] kunit: tool: more descriptive metavars/--help output From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Before, our help output contained lines like --kconfig_add KCONFIG_ADD --qemu_config qemu_config --jobs jobs They're not very helpful. The former kind come from the automatic 'metavar' we get from argparse, the uppercase version of the flag name. The latter are where we manually specified metavar as the flag name. After: --build_dir DIR --make_options X=3DY --kunitconfig PATH --kconfig_add CONFIG_X=3DY --arch ARCH --cross_compile PREFIX --qemu_config FILE --jobs N --timeout SECONDS --raw_output [{all,kunit}] --json [FILE] This patch tries to make the code more clear by specifying the _type_ of input we expect, e.g. --build_dir is a DIR, --qemu_config is a FILE. I also switched it to uppercase since it looked more clearly like placeholder text that way. This patch also changes --raw_output to specify `choices` to make it more clear what the options are, and this way argparse can validate it for us, as shown by the added test case. Signed-off-by: Daniel Latypov Reviewed-by: David Gow Reviewed-by: Brendan Higgins --- tools/testing/kunit/kunit.py | 26 ++++++++++++-------------- tools/testing/kunit/kunit_tool_test.py | 5 +++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py index 9274c6355809..63d2c4bb9503 100755 --- a/tools/testing/kunit/kunit.py +++ b/tools/testing/kunit/kunit.py @@ -206,8 +206,6 @@ def parse_tests(request: KunitParseRequest, input_data:= Iterable[str]) -> Tuple[ pass elif request.raw_output =3D=3D 'kunit': output =3D kunit_parser.extract_tap_lines(output) - else: - print(f'Unknown --raw_output option "{request.raw_output}"', file=3Dsys= .stderr) for line in output: print(line.rstrip()) =20 @@ -281,10 +279,10 @@ def add_common_opts(parser) -> None: parser.add_argument('--build_dir', help=3D'As in the make command, it specifies the build ' 'directory.', - type=3Dstr, default=3D'.kunit', metavar=3D'build_dir') + type=3Dstr, default=3D'.kunit', metavar=3D'DIR') parser.add_argument('--make_options', help=3D'X=3DY make option, can be repeated.', - action=3D'append') + action=3D'append', metavar=3D'X=3DY') parser.add_argument('--alltests', help=3D'Run all KUnit tests through allyesconfig', action=3D'store_true') @@ -292,11 +290,11 @@ def add_common_opts(parser) -> None: help=3D'Path to Kconfig fragment that enables KUnit tests.' ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" ' 'will get automatically appended.', - metavar=3D'kunitconfig') + metavar=3D'PATH') parser.add_argument('--kconfig_add', help=3D'Additional Kconfig options to append to the ' '.kunitconfig, e.g. CONFIG_KASAN=3Dy. Can be repeated.', - action=3D'append') + action=3D'append', metavar=3D'CONFIG_X=3DY') =20 parser.add_argument('--arch', help=3D('Specifies the architecture to run tests under. ' @@ -304,7 +302,7 @@ def add_common_opts(parser) -> None: 'string passed to the ARCH make param, ' 'e.g. i386, x86_64, arm, um, etc. Non-UML ' 'architectures run on QEMU.'), - type=3Dstr, default=3D'um', metavar=3D'arch') + type=3Dstr, default=3D'um', metavar=3D'ARCH') =20 parser.add_argument('--cross_compile', help=3D('Sets make\'s CROSS_COMPILE variable; it should ' @@ -316,18 +314,18 @@ def add_common_opts(parser) -> None: 'if you have downloaded the microblaze toolchain ' 'from the 0-day website to a directory in your ' 'home directory called `toolchains`).'), - metavar=3D'cross_compile') + metavar=3D'PREFIX') =20 parser.add_argument('--qemu_config', help=3D('Takes a path to a path to a file containing ' 'a QemuArchParams object.'), - type=3Dstr, metavar=3D'qemu_config') + type=3Dstr, metavar=3D'FILE') =20 def add_build_opts(parser) -> None: parser.add_argument('--jobs', help=3D'As in the make command, "Specifies the number of ' 'jobs (commands) to run simultaneously."', - type=3Dint, default=3Dget_default_jobs(), metavar=3D'jobs') + type=3Dint, default=3Dget_default_jobs(), metavar=3D'N') =20 def add_exec_opts(parser) -> None: parser.add_argument('--timeout', @@ -336,7 +334,7 @@ def add_exec_opts(parser) -> None: 'tests.', type=3Dint, default=3D300, - metavar=3D'timeout') + metavar=3D'SECONDS') parser.add_argument('filter_glob', help=3D'Filter which KUnit test suites/tests run at ' 'boot-time, e.g. list* or list*.*del_test', @@ -346,7 +344,7 @@ def add_exec_opts(parser) -> None: metavar=3D'filter_glob') parser.add_argument('--kernel_args', help=3D'Kernel command-line parameters. Maybe be repeated', - action=3D'append') + action=3D'append', metavar=3D'') parser.add_argument('--run_isolated', help=3D'If set, boot the kernel for= each ' 'individual suite/test. This is can be useful for debugging ' 'a non-hermetic test, one that might pass/fail based on ' @@ -357,13 +355,13 @@ def add_exec_opts(parser) -> None: def add_parse_opts(parser) -> None: parser.add_argument('--raw_output', help=3D'If set don\'t format output f= rom kernel. ' 'If set to --raw_output=3Dkunit, filters to just KUnit output.', - type=3Dstr, nargs=3D'?', const=3D'all', default=3DNone) + type=3Dstr, nargs=3D'?', const=3D'all', default=3DNone, choices=3D= ['all', 'kunit']) parser.add_argument('--json', nargs=3D'?', help=3D'Stores test results in a JSON, and either ' 'prints to stdout or saves to file if a ' 'filename is specified', - type=3Dstr, const=3D'stdout', default=3DNone) + type=3Dstr, const=3D'stdout', default=3DNone, metavar=3D'FILE') =20 def main(argv, linux=3DNone): parser =3D argparse.ArgumentParser( diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/k= unit_tool_test.py index 352369dffbd9..eb2011d12c78 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -595,6 +595,11 @@ class KUnitMainTest(unittest.TestCase): self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) =20 + def test_run_raw_output_invalid(self): + self.linux_source_mock.run_kernel =3D mock.Mock(return_value=3D[]) + with self.assertRaises(SystemExit) as e: + kunit.main(['run', '--raw_output=3Dinvalid'], self.linux_source_mock) + def test_run_raw_output_does_not_take_positional_args(self): # --raw_output is a string flag, but we don't want it to consume # any positional arguments, only ones after an '=3D' base-commit: 5debe5bfa02c4c8922bd2d0f82c9c3a70bec8944 --=20 2.35.1.574.g5d30c73bfb-goog