Skip to content

Commit a3ece07

Browse files
dlatypovshuahkh
authored andcommitted
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: 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 0b3e680 commit a3ece07

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

tools/testing/kunit/kunit_tool_test.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ def assertContains(self, needle, haystack):
100100
def test_output_isolated_correctly(self):
101101
log_path = get_absolute_path(
102102
'test_data/test_output_isolated_correctly.log')
103-
file = open(log_path)
104-
result = kunit_parser.isolate_kunit_output(file.readlines())
103+
with open(log_path) as file:
104+
result = kunit_parser.isolate_kunit_output(file.readlines())
105105
self.assertContains('TAP version 14', result)
106106
self.assertContains(' # Subtest: example', result)
107107
self.assertContains(' 1..2', result)
108108
self.assertContains(' ok 1 - example_simple_test', result)
109109
self.assertContains(' ok 2 - example_mock_test', result)
110110
self.assertContains('ok 1 - example', result)
111-
file.close()
112111

113112
def test_output_with_prefix_isolated_correctly(self):
114113
log_path = get_absolute_path(
@@ -143,55 +142,51 @@ def test_output_with_prefix_isolated_correctly(self):
143142
def test_parse_successful_test_log(self):
144143
all_passed_log = get_absolute_path(
145144
'test_data/test_is_test_passed-all_passed.log')
146-
file = open(all_passed_log)
147-
result = kunit_parser.parse_run_tests(file.readlines())
145+
with open(all_passed_log) as file:
146+
result = kunit_parser.parse_run_tests(file.readlines())
148147
self.assertEqual(
149148
kunit_parser.TestStatus.SUCCESS,
150149
result.status)
151-
file.close()
152150

153151
def test_parse_failed_test_log(self):
154152
failed_log = get_absolute_path(
155153
'test_data/test_is_test_passed-failure.log')
156-
file = open(failed_log)
157-
result = kunit_parser.parse_run_tests(file.readlines())
154+
with open(failed_log) as file:
155+
result = kunit_parser.parse_run_tests(file.readlines())
158156
self.assertEqual(
159157
kunit_parser.TestStatus.FAILURE,
160158
result.status)
161-
file.close()
162159

163160
def test_no_tests(self):
164161
empty_log = get_absolute_path(
165162
'test_data/test_is_test_passed-no_tests_run.log')
166-
file = open(empty_log)
167-
result = kunit_parser.parse_run_tests(
168-
kunit_parser.isolate_kunit_output(file.readlines()))
163+
with open(empty_log) as file:
164+
result = kunit_parser.parse_run_tests(
165+
kunit_parser.isolate_kunit_output(file.readlines()))
169166
self.assertEqual(0, len(result.suites))
170167
self.assertEqual(
171168
kunit_parser.TestStatus.NO_TESTS,
172169
result.status)
173-
file.close()
174170

175171
def test_no_kunit_output(self):
176172
crash_log = get_absolute_path(
177173
'test_data/test_insufficient_memory.log')
178-
file = open(crash_log)
179174
print_mock = mock.patch('builtins.print').start()
180-
result = kunit_parser.parse_run_tests(
181-
kunit_parser.isolate_kunit_output(file.readlines()))
175+
with open(crash_log) as file:
176+
result = kunit_parser.parse_run_tests(
177+
kunit_parser.isolate_kunit_output(file.readlines()))
182178
print_mock.assert_any_call(StrContains('no tests run!'))
183179
print_mock.stop()
184180
file.close()
185181

186182
def test_crashed_test(self):
187183
crashed_log = get_absolute_path(
188184
'test_data/test_is_test_passed-crash.log')
189-
file = open(crashed_log)
190-
result = kunit_parser.parse_run_tests(file.readlines())
185+
with open(crashed_log) as file:
186+
result = kunit_parser.parse_run_tests(file.readlines())
191187
self.assertEqual(
192188
kunit_parser.TestStatus.TEST_CRASHED,
193189
result.status)
194-
file.close()
195190

196191
def test_ignores_prefix_printk_time(self):
197192
prefix_log = get_absolute_path(

0 commit comments

Comments
 (0)