55
66from . import structures as test_structures
77from planemo .io import error , info , warn , shell_join
8+ from planemo .exit_codes import (
9+ EXIT_CODE_OK ,
10+ EXIT_CODE_GENERIC_FAILURE ,
11+ EXIT_CODE_NO_SUCH_TARGET ,
12+ )
813from planemo .galaxy .run import (
914 run_galaxy_command ,
1015 setup_venv ,
@@ -91,31 +96,52 @@ def run_in_config(ctx, config, run=run_galaxy_command, **kwds):
9196 shell ('cp -r "%s"/* "%s"' % update_cp_args )
9297
9398 _check_test_outputs (xunit_report_file_tracker , structured_report_file_tracker )
94-
9599 test_results = test_structures .GalaxyTestResults (
96100 structured_report_file ,
97101 xunit_report_file ,
98102 html_report_file ,
99103 return_code ,
100104 )
101105
102- test_data = test_results .structured_data
103- handle_reports (ctx , test_data , kwds )
104- _handle_summary (
105- test_results ,
106- ** kwds
106+ structured_data = test_results .structured_data
107+ return handle_reports_and_summary (
108+ ctx ,
109+ structured_data ,
110+ exit_code = test_results .exit_code ,
111+ kwds = kwds
107112 )
108113
109- return return_code
114+
115+ def handle_reports_and_summary (ctx , structured_data , exit_code = None , kwds = {}):
116+ """Produce reports and print summary, return 0 if tests passed.
117+
118+ If ``exit_code`` is set - use underlying test source for return
119+ code and test success determination, otherwise infer from supplied
120+ test data.
121+ """
122+ handle_reports (ctx , structured_data , kwds )
123+ summary_exit_code = _handle_summary (
124+ structured_data ,
125+ ** kwds
126+ )
127+ return exit_code if exit_code is not None else summary_exit_code
110128
111129
112- def handle_reports (ctx , test_data , kwds ):
130+ def handle_reports (ctx , structured_data , kwds ):
113131 """Write reports based on user specified kwds."""
114132 exceptions = []
133+ structured_report_file = kwds .get ("test_output_json" , None )
134+ if structured_report_file and not os .path .exists (structured_report_file ):
135+ try :
136+ with open (structured_report_file , "w" ) as f :
137+ json .dump (structured_report_file , f )
138+ except Exception as e :
139+ exceptions .append (e )
140+
115141 for report_type in ["html" , "markdown" , "text" ]:
116142 try :
117143 _handle_test_output_file (
118- ctx , report_type , test_data , kwds
144+ ctx , report_type , structured_data , kwds
119145 )
120146 except Exception as e :
121147 exceptions .append (e )
@@ -159,48 +185,48 @@ def _handle_test_output_file(ctx, report_type, test_data, kwds):
159185
160186
161187def _handle_summary (
162- test_results ,
188+ structured_data ,
163189 ** kwds
164190):
191+ summary_dict = _get_dict_value ("summary" , structured_data )
192+ num_tests = _get_dict_value ("num_tests" , summary_dict )
193+ num_failures = _get_dict_value ("num_failures" , summary_dict )
194+ num_errors = _get_dict_value ("num_errors" , summary_dict )
195+ num_problems = num_failures + num_errors
196+
197+ summary_exit_code = EXIT_CODE_OK
198+ if num_problems > 0 :
199+ summary_exit_code = EXIT_CODE_GENERIC_FAILURE
200+ elif num_tests == 0 :
201+ summary_exit_code = EXIT_CODE_NO_SUCH_TARGET
202+
165203 summary_style = kwds .get ("summary" )
166- if summary_style == "none" :
167- return
204+ if summary_style != "none" :
205+ if num_tests == 0 :
206+ warn (NO_TESTS_MESSAGE )
207+ elif num_problems == 0 :
208+ info (ALL_TESTS_PASSED_MESSAGE % num_tests )
209+ elif num_problems :
210+ html_report_file = kwds .get ("test_output" )
211+ message_args = (num_problems , num_tests , html_report_file )
212+ message = PROBLEM_COUNT_MESSAGE % message_args
213+ warn (message )
168214
169- if test_results .has_details :
170215 _summarize_tests_full (
171- test_results ,
216+ structured_data ,
172217 ** kwds
173218 )
174- else :
175- if test_results .exit_code :
176- warn (GENERIC_PROBLEMS_MESSAGE % test_results .output_html_path )
177- else :
178- info (GENERIC_TESTS_PASSED_MESSAGE )
219+
220+ return summary_exit_code
179221
180222
181223def _summarize_tests_full (
182- test_results ,
224+ structured_data ,
183225 ** kwds
184226):
185- num_tests = test_results .num_tests
186- num_problems = test_results .num_problems
187-
188- if num_tests == 0 :
189- warn (NO_TESTS_MESSAGE )
190- return
191-
192- if num_problems == 0 :
193- info (ALL_TESTS_PASSED_MESSAGE % num_tests )
194-
195- if num_problems :
196- html_report_file = test_results .output_html_path
197- message_args = (num_problems , num_tests , html_report_file )
198- message = PROBLEM_COUNT_MESSAGE % message_args
199- warn (message )
200-
201- for testcase_el in test_results .xunit_testcase_elements :
202- structured_data_tests = test_results .structured_data_tests
203- _summarize_test_case (structured_data_tests , testcase_el , ** kwds )
227+ tests = _get_dict_value ("tests" , structured_data )
228+ for test_case_data in tests :
229+ _summarize_test_case (test_case_data , ** kwds )
204230
205231
206232def passed (xunit_testcase_el ):
@@ -211,10 +237,16 @@ def passed(xunit_testcase_el):
211237 return did_pass
212238
213239
214- def _summarize_test_case (structured_data , testcase_el , ** kwds ):
240+ def _summarize_test_case (structured_data , ** kwds ):
215241 summary_style = kwds .get ("summary" )
216- test_id = test_structures .case_id (testcase_el )
217- if not passed (testcase_el ):
242+ test_id = test_structures .case_id (
243+ raw_id = _get_dict_value ("id" , structured_data )
244+ )
245+ status = _get_dict_value (
246+ "status" ,
247+ _get_dict_value ("data" , structured_data )
248+ )
249+ if status != "success" :
218250 state = click .style ("failed" , bold = True , fg = 'red' )
219251 else :
220252 state = click .style ("passed" , bold = True , fg = 'green' )
@@ -223,14 +255,7 @@ def _summarize_test_case(structured_data, testcase_el, **kwds):
223255 _print_command_line (structured_data , test_id )
224256
225257
226- def _print_command_line (structured_data , test_id ):
227- try :
228- test = [d for d in structured_data if d ["id" ] == test_id .id ][0 ]["data" ]
229- except (KeyError , IndexError ):
230- # Failed to find structured data for this test - likely targetting
231- # and older Galaxy version.
232- return
233-
258+ def _print_command_line (test , test_id ):
234259 execution_problem = test .get ("execution_problem" , None )
235260 if execution_problem :
236261 click .echo ("| command: *could not execute job, no command generated* " )
@@ -245,6 +270,13 @@ def _print_command_line(structured_data, test_id):
245270 click .echo ("| command: %s" % command )
246271
247272
273+ def _get_dict_value (key , data ):
274+ try :
275+ return data [key ]
276+ except (KeyError , TypeError ):
277+ raise KeyError ("No key [%s] in [%s]" % (key , data ))
278+
279+
248280def _check_test_outputs (
249281 xunit_report_file_tracker ,
250282 structured_report_file_tracker
@@ -321,4 +353,5 @@ def changed(self):
321353__all__ = [
322354 "run_in_config" ,
323355 "handle_reports" ,
356+ "handle_reports_and_summary" ,
324357]
0 commit comments