1*55e87721SMatt Gilbride# Copyright 2020 Google LLC 2*55e87721SMatt Gilbride# 3*55e87721SMatt Gilbride# Licensed under the Apache License, Version 2.0 (the "License"); 4*55e87721SMatt Gilbride# you may not use this file except in compliance with the License. 5*55e87721SMatt Gilbride# You may obtain a copy of the License at 6*55e87721SMatt Gilbride# 7*55e87721SMatt Gilbride# https://www.apache.org/licenses/LICENSE-2.0 8*55e87721SMatt Gilbride# 9*55e87721SMatt Gilbride# Unless required by applicable law or agreed to in writing, software 10*55e87721SMatt Gilbride# distributed under the License is distributed on an "AS IS" BASIS, 11*55e87721SMatt Gilbride# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*55e87721SMatt Gilbride# See the License for the specific language governing permissions and 13*55e87721SMatt Gilbride# limitations under the License. 14*55e87721SMatt Gilbrideimport os 15*55e87721SMatt Gilbrideimport pathlib 16*55e87721SMatt Gilbrideimport typing 17*55e87721SMatt Gilbride 18*55e87721SMatt Gilbrideimport jinja2 19*55e87721SMatt Gilbride 20*55e87721SMatt Gilbride 21*55e87721SMatt Gilbridedef make_report( 22*55e87721SMatt Gilbride name: str, results: typing.List[typing.Dict], log_file_dir: pathlib.Path 23*55e87721SMatt Gilbride) -> None: 24*55e87721SMatt Gilbride """Write an xunit report sponge_log.xml to the specified directory. 25*55e87721SMatt Gilbride 26*55e87721SMatt Gilbride Arguments: 27*55e87721SMatt Gilbride name {str} - Name of the report 28*55e87721SMatt Gilbride results {typing.List[typing.Dict]} - List of synth results 29*55e87721SMatt Gilbride Each result has the following fields: 30*55e87721SMatt Gilbride name: str 31*55e87721SMatt Gilbride output: str 32*55e87721SMatt Gilbride error: bool 33*55e87721SMatt Gilbride skipped: bool 34*55e87721SMatt Gilbride """ 35*55e87721SMatt Gilbride with open(pathlib.Path(__file__).parent / "report.xml.j2") as fh: 36*55e87721SMatt Gilbride template = jinja2.Template(fh.read()) 37*55e87721SMatt Gilbride 38*55e87721SMatt Gilbride output = template.render( 39*55e87721SMatt Gilbride name=name, 40*55e87721SMatt Gilbride failures=len([result for result in results if result["error"]]), 41*55e87721SMatt Gilbride skips=len([result for result in results if result["skipped"]]), 42*55e87721SMatt Gilbride results=results, 43*55e87721SMatt Gilbride ) 44*55e87721SMatt Gilbride os.makedirs(log_file_dir, exist_ok=True) 45*55e87721SMatt Gilbride with open(log_file_dir / "sponge_log.xml", "w") as fh: 46*55e87721SMatt Gilbride fh.write(output) 47