Skip to content

Commit cfd607e

Browse files
dlatypovshuahkh
authored andcommitted
kunit: tool: fix unit test cleanup handling
* 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 <[email protected]> Reviewed-by: David Gow <[email protected]> Tested-by: Brendan Higgins <[email protected]> Acked-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 92bf226 commit cfd607e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

tools/testing/kunit/kunit_tool_test.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,17 @@ def __eq__(self, other):
288288
class KUnitMainTest(unittest.TestCase):
289289
def setUp(self):
290290
path = get_absolute_path('test_data/test_is_test_passed-all_passed.log')
291-
file = open(path)
292-
all_passed_log = file.readlines()
293-
self.print_patch = mock.patch('builtins.print')
294-
self.print_mock = self.print_patch.start()
291+
with open(path) as file:
292+
all_passed_log = file.readlines()
293+
294+
self.print_mock = mock.patch('builtins.print').start()
295+
self.addCleanup(mock.patch.stopall)
296+
295297
self.linux_source_mock = mock.Mock()
296298
self.linux_source_mock.build_reconfig = mock.Mock(return_value=True)
297299
self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True)
298300
self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log)
299301

300-
def tearDown(self):
301-
self.print_patch.stop()
302-
pass
303-
304302
def test_config_passes_args_pass(self):
305303
kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock)
306304
assert self.linux_source_mock.build_reconfig.call_count == 1

0 commit comments

Comments
 (0)