1
1
import logging
2
2
import subprocess # noqa: S404
3
- from typing import Iterable , List , Optional , Tuple
4
-
3
+ from typing import Iterable , List , Optional
5
4
from flask_babel import gettext as _ # type: ignore
6
5
import junitparser
7
6
from junitparser .junitparser import TestCase
@@ -51,7 +50,6 @@ def _run_tests_on_solution(self):
51
50
python_file = 'test_checks.py'
52
51
test_output_path = 'output.xml'
53
52
54
- junit_results = None
55
53
try :
56
54
with executers .get_executor (self ._executor_name ) as executor :
57
55
executor .write_file (python_file , python_code )
@@ -78,12 +76,17 @@ def _generate_python_code(self) -> str:
78
76
test_code = self ._exercise_auto_test .code
79
77
return f'{ test_code } \n \n { user_code } '
80
78
81
- def _get_parsed_suites (
79
+ def _get_test_cases (
82
80
self , raw_results : bytes ,
83
- ) -> Optional [Iterable [junitparser .TestSuite ]]:
81
+ ) -> Optional [Iterable [junitparser .TestCase ]]:
84
82
try :
85
83
parsed_string = junitparser .TestSuite .fromstring (raw_results )
86
- return parsed_string .testsuites ()
84
+ test_suites = tuple (parsed_string .testsuites ())
85
+ single_test_suite = not test_suites
86
+ if single_test_suite :
87
+ yield from tuple (parsed_string )
88
+ for test_suite in test_suites :
89
+ yield from tuple (test_suite )
87
90
except SyntaxError : # importing xml make the lint go arrrr
88
91
self ._logger .exception ('Failed to parse junit result' )
89
92
return None
@@ -98,17 +101,15 @@ def _populate_junit_results(self, raw_results: bytes) -> None:
98
101
if not raw_results :
99
102
return None
100
103
101
- suites = self ._get_parsed_suites (raw_results )
102
- if not suites :
104
+ test_cases = self ._get_test_cases (raw_results )
105
+ if not test_cases :
103
106
return None
104
107
105
108
tests_ran = False
106
109
number_of_failures = 0
107
- for test_suite in suites :
108
- failures , ran = self ._handle_test_suite (test_suite )
109
- number_of_failures += failures
110
- if ran and not tests_ran :
111
- tests_ran = ran
110
+ for test_case in test_cases :
111
+ number_of_failures += int (self ._handle_test_case (test_case ))
112
+ tests_ran = True
112
113
113
114
if not tests_ran :
114
115
self ._handle_failed_to_execute_tests (raw_results )
@@ -181,13 +182,3 @@ def _handle_test_case(self, case: TestCase) -> NumberOfErrors:
181
182
self ._handle_result (case .name , result )
182
183
number_of_failures += 1
183
184
return number_of_failures
184
-
185
- def _handle_test_suite (
186
- self , test_suite : junitparser .TestSuite ,
187
- ) -> Tuple [int , bool ]:
188
- number_of_failures = 0
189
- tests_ran = False
190
- for case in test_suite :
191
- tests_ran = True
192
- number_of_failures += int (self ._handle_test_case (case ))
193
- return number_of_failures , tests_ran
0 commit comments