Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp643005pxb; Wed, 22 Sep 2021 09:41:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUY3/pIGoU57Pppx6pLnUmgwyLXS7YVLenKnqj/QbmWzw8OuYsRWxSzraxDiaVxrm2x0Fl X-Received: by 2002:a02:3213:: with SMTP id j19mr666474jaa.17.1632328893451; Wed, 22 Sep 2021 09:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632328893; cv=none; d=google.com; s=arc-20160816; b=l5t2RL95sLjibkHxo94nVr/QvImQYOOh9TbzyP4OT1+mBxMeidXKgooRJyLB1dcL8w 5ohYigsNhBPxM7GFSGZ8vK0kiEUWmnq5koP3r3sPCvhKs5+yqS4CYi1J7U2xMp7TPiKr 8o5dUyyo9bsZhs/YXDUuauD18L9I/2a62ZzTt4glPvjDIRCbwRde2QetWL5XLz3DV1ZK BHsjdkMeOWTwxWbcxk3KZLU5rSZ9J976s4K+fOjmw8H1Rbf2+kai5/IWivlsnuKygy6q hXDKsNG2zHX2xyBkbdDSVKH0jQfuWYMeqtGOmmp2bGTakJ9BRbFS+cqrV4BEkG//9QZW vnYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=7/iG3SA02r1f60QR6SCCZYUVHBWkS+SuM1mbU3JpT68=; b=Mr2mA0Goopoxr6rIcYbWbNDPl2qHeYUdQxgr74Xv+h6o/9onc2yNs92ZNLCutbcHd9 XJm2xRl8j6EnnxV0H8YxKxiU+lkuH6x8FHvKbq3G+EkbPkIj79w9GhYnvJ4FPM8qlQYa 6LQypLfEwUPKnwY650EDg67b3nuFD1t9ksmPXHmTc2oktWkf593YtiCRhj5otwcDf+XI ah53bOsTwJWJxSARQh/+zW0VXabk4RXcPdiTCGyVTj6/nPJ8jSEk/6ki9m3X6iBVoZFl hv3ugxaoOlS/zSFahHw4wJ0e5uksDecwffV8+Mj33AR/N4XrDutAFJynhnFvSwWYaLY6 05wQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KoUB2StG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y15si671883ilu.118.2021.09.22.09.40.42; Wed, 22 Sep 2021 09:41:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KoUB2StG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231925AbhIVQlB (ORCPT + 99 others); Wed, 22 Sep 2021 12:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236586AbhIVQlA (ORCPT ); Wed, 22 Sep 2021 12:41:00 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF86EC061756 for ; Wed, 22 Sep 2021 09:39:29 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id u6-20020ad449a6000000b003798010ad14so12938469qvx.10 for ; Wed, 22 Sep 2021 09:39:29 -0700 (PDT) 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=7/iG3SA02r1f60QR6SCCZYUVHBWkS+SuM1mbU3JpT68=; b=KoUB2StG9c+YM8RcINzTvM1NepgTmSE2dbP62dqS4qE7aGKsSfDmT+DA0/PkUW0jM0 SCBnxfFMjUAJsV4Jg1O25r4jJ6bjlcmPbczCs4LQP5YySXbrxWGItZg9WvVvmubDwYfW gJJLgjIT00iIvwo0U573dXCqBkhQjz5ciuxDAyOzIm+i6x8cxfp40CddeatBv35CpQ/S In2mhmmmlRETvrsh6s3FSJcmmfByeiNdbzJCYwyaOfy8ISwLMsJTwM2bU7EmJr0Ro1z0 cv+8+4ayeNXo52WsMgljNUwzWIt0dMxfYrq52G9yATg44vIPK9XcdTBWvc70ZLEapiwl 3tLQ== 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=7/iG3SA02r1f60QR6SCCZYUVHBWkS+SuM1mbU3JpT68=; b=1bYlnnTrZ52o/XqUM3HyOly1YgOnXgBALN/ZEeqGl+rnNel14zrHfcV/v+RdPf6E6+ YB9sEXccVgb6kw73S4aN0CvPw9GixxFj5y9cM+5NfRCuNcSGR+NYJskCtA6vSP37KQBu Lwn22uVNguVT/7vWY+VyUhBcEDDApzxqyCvIpwXNLdmJpgufy3jRioVBXnDNOruboNTU vsySfLcbpJSj4MtQKbtvsviYtuPg3auUojwUI2NW9pgNBFxRyOI1xooojmgTMEWoG144 iydAbe6sqAXdYV9BFbLpy6UR5FwldGXPYtGx3cVJEpzC62Vj6CgRjK6oprXWrzgHQo4m NdFQ== X-Gm-Message-State: AOAM532SfDUIuwpmyXlAflMPWSYoIP0zSh7wO7Pv81Ze5Y59QFa5clq0 D5aX4tc+beW8yrDUtw+DELDqj9W46yg4Cg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:5ad1:21fa:bca6:a843]) (user=dlatypov job=sendgmr) by 2002:a25:1b08:: with SMTP id b8mr113845ybb.24.1632328768925; Wed, 22 Sep 2021 09:39:28 -0700 (PDT) Date: Wed, 22 Sep 2021 09:39:21 -0700 Message-Id: <20210922163921.2462565-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH v2] kunit: tool: better handling of quasi-bool args (--json, --raw_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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Problem: What does this do? $ kunit.py run --json Well, it runs all the tests and prints test results out as JSON. And next is $ kunit.py run my-test-suite --json This runs just `my-test-suite` and prints results out as JSON. But what about? $ kunit.py run --json my-test-suite This runs all the tests and stores the json results in a "my-test-suite" file. Why: --json, and now --raw_output are actually string flags. They just have a default value. --json in particular takes the name of an output file. It was intended that you'd do $ kunit.py run --json=my_output_file my-test-suite if you ever wanted to specify the value. Workaround: It doesn't seem like there's a way to make https://docs.python.org/3/library/argparse.html only accept arg values after a '='. I believe that `--json` should "just work" regardless of where it is. So this patch automatically rewrites a bare `--json` to `--json=stdout`. That makes the examples above work the same way. Add a regression test that can catch this for --raw_output. Fixes: 6a499c9c42d0 ("kunit: tool: make --raw_output support only showing kunit output") Signed-off-by: Daniel Latypov Tested-by: David Gow --- v1 -> v2: fix mypy error by converting mapped argv to a list. --- tools/testing/kunit/kunit.py | 24 ++++++++++++++++++++++-- tools/testing/kunit/kunit_tool_test.py | 8 ++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py index 5a931456e718..ac35c61f65f5 100755 --- a/tools/testing/kunit/kunit.py +++ b/tools/testing/kunit/kunit.py @@ -16,7 +16,7 @@ assert sys.version_info >= (3, 7), "Python version is too old" from collections import namedtuple from enum import Enum, auto -from typing import Iterable +from typing import Iterable, Sequence import kunit_config import kunit_json @@ -186,6 +186,26 @@ def run_tests(linux: kunit_kernel.LinuxSourceTree, exec_result.elapsed_time)) return parse_result +# Problem: +# $ kunit.py run --json +# works as one would expect and prints the parsed test results as JSON. +# $ kunit.py run --json suite_name +# would *not* pass suite_name as the filter_glob and print as json. +# argparse will consider it to be another way of writing +# $ kunit.py run --json=suite_name +# i.e. it would run all tests, and dump the json to a `suite_name` file. +# So we hackily automatically rewrite --json => --json=stdout +pseudo_bool_flag_defaults = { + '--json': 'stdout', + '--raw_output': 'kunit', +} +def massage_argv(argv: Sequence[str]) -> Sequence[str]: + def massage_arg(arg: str) -> str: + if arg not in pseudo_bool_flag_defaults: + return arg + return f'{arg}={pseudo_bool_flag_defaults[arg]}' + return list(map(massage_arg, argv)) + def add_common_opts(parser) -> None: parser.add_argument('--build_dir', help='As in the make command, it specifies the build ' @@ -303,7 +323,7 @@ def main(argv, linux=None): help='Specifies the file to read results from.', type=str, nargs='?', metavar='input_file') - cli_args = parser.parse_args(argv) + cli_args = parser.parse_args(massage_argv(argv)) if get_kernel_root_path(): os.chdir(get_kernel_root_path()) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index 619c4554cbff..1edcc8373b4e 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -408,6 +408,14 @@ class KUnitMainTest(unittest.TestCase): self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) + 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 '=' + self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) + kunit.main(['run', '--raw_output', 'filter_glob'], self.linux_source_mock) + self.linux_source_mock.run_kernel.assert_called_once_with( + args=None, build_dir='.kunit', filter_glob='filter_glob', timeout=300) + def test_exec_timeout(self): timeout = 3453 kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) base-commit: 4c17ca27923c16fd73bbb9ad033c7d749c3bcfcc -- 2.33.0.464.g1972c5931b-goog