Re: [PATCH 1/5] kunit: tool: fix unit test cleanup handling

From: David Gow
Date: Tue Dec 01 2020 - 02:33:35 EST


On Tue, Dec 1, 2020 at 7:32 AM Daniel Latypov <dlatypov@xxxxxxxxxx> wrote:
>
> * 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 <dlatypov@xxxxxxxxxx>
> ---

I won't pretend to be an expert on Python, but this seems good to me.
I tested it on my machine and it works fine.

So,
Reviewed-by: David Gow <davidgow@xxxxxxxxxx>

-- Davkd

> 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 497ab51bc170..3fbe1acd531a 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
>
> base-commit: b65054597872ce3aefbc6a666385eabdf9e288da
> --
> 2.29.2.454.gaff20da3a2-goog
>