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