From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CFC3C4332B for ; Tue, 2 Mar 2021 12:36:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DACFF64EF0 for ; Tue, 2 Mar 2021 12:36:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444357AbhCBMdl (ORCPT ); Tue, 2 Mar 2021 07:33:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:40130 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244585AbhCBMBx (ORCPT ); Tue, 2 Mar 2021 07:01:53 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4566064F11; Tue, 2 Mar 2021 11:56:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614686163; bh=CkUjxBqINKXBee+vKVstay9nMedck3j/qJAs/WeWVUE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ie+S0w45kgoXbrCejCZzmRZxCRPwlvfMaIPEZAvOhTCNVGmFmuQVF+oQo5xutu25U ovB7IZ2+lyPMJX3O9zW810qZ4nvkFN6YW0EC/x2+Tsk6i+nZqS8B4Idz7/VUMyI+d9 5cnfpMGE1Y6Xv0QxkfkLGwnQGBqzd1Hk7Sh2oz7QIKFnh4GoYyJNK/k1rwQA6BIxf+ HgFyTkf4U4KZacSzkb8q77CdIG+M2Cwf8pIMeeqrX+sJr+FssCh3tZCmVtWuP0Hvgx 8gKtGWyL5e4tH7ErwYRdCLQuHQmyNmfgWDGLerVUzYBcKLN4xBjgaexD6i+5I0EyvW gy1aLc+lbcRWw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Daniel Latypov , David Gow , Brendan Higgins , Shuah Khan , Sasha Levin Subject: [PATCH AUTOSEL 5.11 21/52] kunit: tool: fix unit test cleanup handling Date: Tue, 2 Mar 2021 06:55:02 -0500 Message-Id: <20210302115534.61800-21-sashal@kernel.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210302115534.61800-1-sashal@kernel.org> References: <20210302115534.61800-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Latypov [ Upstream commit cfd607e43da4a20753744f134e201310262b827a ] * Stop leaking file objects. * Use self.addCleanup() to ensure we call cleanup functions even if setUp() fails. * use mock.patch.stopall instead of more error-prone manual approach Signed-off-by: Daniel Latypov Reviewed-by: David Gow Tested-by: Brendan Higgins Acked-by: Brendan Higgins Signed-off-by: Shuah Khan Signed-off-by: Sasha Levin --- tools/testing/kunit/kunit_tool_test.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index b593f4448e83..9a036e9d4455 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -288,19 +288,17 @@ class StrContains(str): class KUnitMainTest(unittest.TestCase): def setUp(self): path = get_absolute_path('test_data/test_is_test_passed-all_passed.log') - file = open(path) - all_passed_log = file.readlines() - self.print_patch = mock.patch('builtins.print') - self.print_mock = self.print_patch.start() + with open(path) as file: + all_passed_log = file.readlines() + + self.print_mock = mock.patch('builtins.print').start() + self.addCleanup(mock.patch.stopall) + self.linux_source_mock = mock.Mock() self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True) self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) - def tearDown(self): - self.print_patch.stop() - pass - def test_config_passes_args_pass(self): kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) assert self.linux_source_mock.build_reconfig.call_count == 1 -- 2.30.1