Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1107938pxu; Wed, 2 Dec 2020 11:12:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJzMc8RrcP16yzs2sSePkrwXDTRjSNX9RqQpGiuq79app/cA/sVBfiOv6N+lr856JjP34c/e X-Received: by 2002:a50:a6c9:: with SMTP id f9mr1438614edc.158.1606936366940; Wed, 02 Dec 2020 11:12:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606936366; cv=none; d=google.com; s=arc-20160816; b=Bq4g1v+bp+ZH+T5uQM6KKlVJ4JkAFdTcIDbMiH5GvTVIYyGZOnYpV4C63AWyEmINOd hveZxVfpUnMKu7ffgY1FpD/DrgxujFTWwGTw6bA1LF7VZIzTbouj0rMDTbtsSb0mUnON IIY6yz/s1yLmdbXX07KEfwu2gpwvVCrLfyjpNNKyguyUkx60kFPEN6CLd8soSx+adoDG 0eecLLzpe8IEOdpHZh/l+8gChXJfK10Fl6gnJIlRuHioIqnIv+GhE2OlDwQ1CtBrnmE0 qziwN58aOUP9mdu1+tT2vYjPavehLpCsgbSV3iX0NrGulGmiU9Qv+BIXLhZhrYz6qyrM F8qw== 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:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=d9ODsGy71UxS1scEvGTz4NCozRGDYn/BsJFydzZu0V0=; b=FfLsmOUu2HKDq3HlKaPnsgp4Qatc8kjhAYc+1Hon2nhrQdL/cLPjRguTypBWZUIHJI AVxRoss/7XBwNXQE+7RAao50GGUWdQsXK/3owEFHX17kU5yaRJr8yY1yf1GqF+kgghre GT3oNpfUxJvRNz0Q2+m8AKNYaVjv03FOEHUlWHVMCT3lIMPCY7RbA+ZarxYHDxW9Yh8R 9IE6EZnjhQ+FMTiwKhOqFGu7U5BtMcwztKRvdjSVSc4afh72qZMhRHTmRbu1YFN00af7 yCgCQEHeaPmyJcpQZKctm0H/BN769UDc0uqUDym9ESVza4oUSYaQp8zRdis0ZnCrlW5e i2+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dm5FdKlS; 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 g3si551235edu.11.2020.12.02.11.12.23; Wed, 02 Dec 2020 11:12:46 -0800 (PST) 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=20161025 header.b=dm5FdKlS; 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 S2389730AbgLBTK3 (ORCPT + 99 others); Wed, 2 Dec 2020 14:10:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389706AbgLBTK2 (ORCPT ); Wed, 2 Dec 2020 14:10:28 -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 E8D3FC061A04 for ; Wed, 2 Dec 2020 11:09:05 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id z29so2930801ybi.23 for ; Wed, 02 Dec 2020 11:09:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=d9ODsGy71UxS1scEvGTz4NCozRGDYn/BsJFydzZu0V0=; b=dm5FdKlSIuxUi4iLenNKFWMujqwqankdTmdkODUJuiHoLawGQEiQYIlCsPA196Zy5s rJ03obABD3u8Q3lABqlHBI08yEr3pL8Hwdv1f+NnXPVmFwdZn0VdbbpsxCKacL58cOsf 2K9qn3WjkeW/Vxhz6Z3LMNidcFYPXIh/q8TfQ8aBjP4Cgb+LmgppN4kyjCuo0Gp6ZZ9u 3fuLgKBkULfSfv42PD05FLsXvhPJGUSSex8Sk2qXGJgjhARC6jrne1bB09543UR464Kr Y1nO8tsaSexhUXeMwgV8AVn9NlC53AwAtGqznDMKxTRRTr0hEuzel7fqh67UJxVi+Aw4 D8vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=d9ODsGy71UxS1scEvGTz4NCozRGDYn/BsJFydzZu0V0=; b=T5lFMfpaAiqqHMqUi3SvwtDrrSzwzOL/1JT2nce1683SaveH/sql6HrmzLkggyN9uK PKXiyaORcuenUGlkWbJeFufCGc/BKHKdjSfD56cIz6pflRK5/qV/d9+K+dVPjKHUfMEF dYvRvt++e9udL78tFIeQcVqBDcXSxM5JbKDvGkCI58WsEmNohozOxL6H9k8N3y3kP/VG q7cqRhYcSO12scQsFWwcd+lYvKrEFkXZqB3TRjMFLQ6PyupzT6U/uI+HxPEzDzqd5FTv lM2EOSuCE1/A4SrAeQ9ZIapmril+Dp3llP/pN/F3y0ThP2i6BerJzIN1FOkxIix7yPtn /uOQ== X-Gm-Message-State: AOAM531UslhiZ4P6ruXHMX2oFcFxneYskbWgkQIKlXteoER32qnPKRsy EIQxaaQcGFqj3GinPtnjf+KgQcmEzs8LXg== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a25:d251:: with SMTP id j78mr5578186ybg.193.1606936145223; Wed, 02 Dec 2020 11:09:05 -0800 (PST) Date: Wed, 2 Dec 2020 11:08:23 -0800 In-Reply-To: <20201202190824.1309398-1-dlatypov@google.com> Message-Id: <20201202190824.1309398-3-dlatypov@google.com> Mime-Version: 1.0 References: <20201202190824.1309398-1-dlatypov@google.com> X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog Subject: [PATCH v2 3/4] kunit: tool: use `with open()` in unit test From: Daniel Latypov To: davidgow@google.com Cc: brendanhiggins@google.com, linux-kernel@vger.kernel.org, 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 The use of manual open() and .close() calls seems to be an attempt to keep the contents in scope. But Python doesn't restrict variables like that, so we can introduce new variables inside of a `with` and use them outside. Do so to make the code more Pythonic. Signed-off-by: Daniel Latypov --- tools/testing/kunit/kunit_tool_test.py | 33 +++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index 3a74e5612cf9..cf160914bc55 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -100,15 +100,14 @@ class KUnitParserTest(unittest.TestCase): def test_output_isolated_correctly(self): log_path = get_absolute_path( 'test_data/test_output_isolated_correctly.log') - file = open(log_path) - result = kunit_parser.isolate_kunit_output(file.readlines()) + with open(log_path) as file: + result = kunit_parser.isolate_kunit_output(file.readlines()) self.assertContains('TAP version 14', result) self.assertContains(' # Subtest: example', result) self.assertContains(' 1..2', result) self.assertContains(' ok 1 - example_simple_test', result) self.assertContains(' ok 2 - example_mock_test', result) self.assertContains('ok 1 - example', result) - file.close() def test_output_with_prefix_isolated_correctly(self): log_path = get_absolute_path( @@ -143,42 +142,39 @@ class KUnitParserTest(unittest.TestCase): def test_parse_successful_test_log(self): all_passed_log = get_absolute_path( 'test_data/test_is_test_passed-all_passed.log') - file = open(all_passed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(all_passed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.SUCCESS, result.status) - file.close() def test_parse_failed_test_log(self): failed_log = get_absolute_path( 'test_data/test_is_test_passed-failure.log') - file = open(failed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(failed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.FAILURE, result.status) - file.close() def test_no_tests(self): empty_log = get_absolute_path( 'test_data/test_is_test_passed-no_tests_run.log') - file = open(empty_log) - result = kunit_parser.parse_run_tests( - kunit_parser.isolate_kunit_output(file.readlines())) + with open(empty_log) as file: + result = kunit_parser.parse_run_tests( + kunit_parser.isolate_kunit_output(file.readlines())) self.assertEqual(0, len(result.suites)) self.assertEqual( kunit_parser.TestStatus.NO_TESTS, result.status) - file.close() def test_no_kunit_output(self): crash_log = get_absolute_path( 'test_data/test_insufficient_memory.log') - file = open(crash_log) print_mock = mock.patch('builtins.print').start() - result = kunit_parser.parse_run_tests( - kunit_parser.isolate_kunit_output(file.readlines())) + with open(crash_log) as file: + result = kunit_parser.parse_run_tests( + kunit_parser.isolate_kunit_output(file.readlines())) print_mock.assert_any_call(StrContains('no tests run!')) print_mock.stop() file.close() @@ -186,12 +182,11 @@ class KUnitParserTest(unittest.TestCase): def test_crashed_test(self): crashed_log = get_absolute_path( 'test_data/test_is_test_passed-crash.log') - file = open(crashed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(crashed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.TEST_CRASHED, result.status) - file.close() def test_ignores_prefix_printk_time(self): prefix_log = get_absolute_path( -- 2.29.2.576.ga3fc446d84-goog