Commit a3ece079 authored by Daniel Latypov's avatar Daniel Latypov Committed by Shuah Khan

kunit: tool: use `with open()` in unit test

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: default avatarDaniel Latypov <dlatypov@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Tested-by: default avatarBrendan Higgins <brendanhiggins@google.com>
Acked-by: default avatarBrendan Higgins <brendanhiggins@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 0b3e6807
...@@ -100,15 +100,14 @@ class KUnitParserTest(unittest.TestCase): ...@@ -100,15 +100,14 @@ class KUnitParserTest(unittest.TestCase):
def test_output_isolated_correctly(self): def test_output_isolated_correctly(self):
log_path = get_absolute_path( log_path = get_absolute_path(
'test_data/test_output_isolated_correctly.log') 'test_data/test_output_isolated_correctly.log')
file = open(log_path) with open(log_path) as file:
result = kunit_parser.isolate_kunit_output(file.readlines()) result = kunit_parser.isolate_kunit_output(file.readlines())
self.assertContains('TAP version 14', result) self.assertContains('TAP version 14', result)
self.assertContains(' # Subtest: example', result) self.assertContains(' # Subtest: example', result)
self.assertContains(' 1..2', result) self.assertContains(' 1..2', result)
self.assertContains(' ok 1 - example_simple_test', result) self.assertContains(' ok 1 - example_simple_test', result)
self.assertContains(' ok 2 - example_mock_test', result) self.assertContains(' ok 2 - example_mock_test', result)
self.assertContains('ok 1 - example', result) self.assertContains('ok 1 - example', result)
file.close()
def test_output_with_prefix_isolated_correctly(self): def test_output_with_prefix_isolated_correctly(self):
log_path = get_absolute_path( log_path = get_absolute_path(
...@@ -143,42 +142,39 @@ class KUnitParserTest(unittest.TestCase): ...@@ -143,42 +142,39 @@ class KUnitParserTest(unittest.TestCase):
def test_parse_successful_test_log(self): def test_parse_successful_test_log(self):
all_passed_log = get_absolute_path( all_passed_log = get_absolute_path(
'test_data/test_is_test_passed-all_passed.log') 'test_data/test_is_test_passed-all_passed.log')
file = open(all_passed_log) with open(all_passed_log) as file:
result = kunit_parser.parse_run_tests(file.readlines()) result = kunit_parser.parse_run_tests(file.readlines())
self.assertEqual( self.assertEqual(
kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SUCCESS,
result.status) result.status)
file.close()
def test_parse_failed_test_log(self): def test_parse_failed_test_log(self):
failed_log = get_absolute_path( failed_log = get_absolute_path(
'test_data/test_is_test_passed-failure.log') 'test_data/test_is_test_passed-failure.log')
file = open(failed_log) with open(failed_log) as file:
result = kunit_parser.parse_run_tests(file.readlines()) result = kunit_parser.parse_run_tests(file.readlines())
self.assertEqual( self.assertEqual(
kunit_parser.TestStatus.FAILURE, kunit_parser.TestStatus.FAILURE,
result.status) result.status)
file.close()
def test_no_tests(self): def test_no_tests(self):
empty_log = get_absolute_path( empty_log = get_absolute_path(
'test_data/test_is_test_passed-no_tests_run.log') 'test_data/test_is_test_passed-no_tests_run.log')
file = open(empty_log) with open(empty_log) as file:
result = kunit_parser.parse_run_tests( result = kunit_parser.parse_run_tests(
kunit_parser.isolate_kunit_output(file.readlines())) kunit_parser.isolate_kunit_output(file.readlines()))
self.assertEqual(0, len(result.suites)) self.assertEqual(0, len(result.suites))
self.assertEqual( self.assertEqual(
kunit_parser.TestStatus.NO_TESTS, kunit_parser.TestStatus.NO_TESTS,
result.status) result.status)
file.close()
def test_no_kunit_output(self): def test_no_kunit_output(self):
crash_log = get_absolute_path( crash_log = get_absolute_path(
'test_data/test_insufficient_memory.log') 'test_data/test_insufficient_memory.log')
file = open(crash_log)
print_mock = mock.patch('builtins.print').start() print_mock = mock.patch('builtins.print').start()
result = kunit_parser.parse_run_tests( with open(crash_log) as file:
kunit_parser.isolate_kunit_output(file.readlines())) result = kunit_parser.parse_run_tests(
kunit_parser.isolate_kunit_output(file.readlines()))
print_mock.assert_any_call(StrContains('no tests run!')) print_mock.assert_any_call(StrContains('no tests run!'))
print_mock.stop() print_mock.stop()
file.close() file.close()
...@@ -186,12 +182,11 @@ class KUnitParserTest(unittest.TestCase): ...@@ -186,12 +182,11 @@ class KUnitParserTest(unittest.TestCase):
def test_crashed_test(self): def test_crashed_test(self):
crashed_log = get_absolute_path( crashed_log = get_absolute_path(
'test_data/test_is_test_passed-crash.log') 'test_data/test_is_test_passed-crash.log')
file = open(crashed_log) with open(crashed_log) as file:
result = kunit_parser.parse_run_tests(file.readlines()) result = kunit_parser.parse_run_tests(file.readlines())
self.assertEqual( self.assertEqual(
kunit_parser.TestStatus.TEST_CRASHED, kunit_parser.TestStatus.TEST_CRASHED,
result.status) result.status)
file.close()
def test_ignores_prefix_printk_time(self): def test_ignores_prefix_printk_time(self):
prefix_log = get_absolute_path( prefix_log = get_absolute_path(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment