Report: Do not show failed tests in "Detailed Test Results"
[csit.git] / resources / tools / presentation / generator_files.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Algorithms to generate files.
15 """
16
17
18 import logging
19
20 from pal_utils import get_files, get_rst_title_char
21
22
23 RST_INCLUDE_TABLE = (u"\n.. only:: html\n\n"
24                      u"    .. csv-table::\n"
25                      u"        :header-rows: 1\n"
26                      u"        :widths: auto\n"
27                      u"        :align: center\n"
28                      u"        :file: {file_html}\n"
29                      u"\n.. only:: latex\n\n"
30                      u"\n  .. raw:: latex\n\n"
31                      u"      \\csvautolongtable{{{file_latex}}}\n\n")
32
33
34 def generate_files(spec, data):
35     """Generate all files specified in the specification file.
36
37     :param spec: Specification read from the specification file.
38     :param data: Data to process.
39     :type spec: Specification
40     :type data: InputData
41     """
42
43     generator = {
44         u"file_test_results": file_test_results,
45         u"file_test_results_html": file_test_results_html
46     }
47
48     logging.info(u"Generating the files ...")
49     for file_spec in spec.files:
50         try:
51             generator[file_spec[u"algorithm"]](file_spec, data)
52         except (NameError, KeyError) as err:
53             logging.error(
54                 f"Probably algorithm {file_spec[u'algorithm']} is not defined: "
55                 f"{repr(err)}"
56             )
57     logging.info(u"Done.")
58
59
60 def _tests_in_suite(suite_name, tests):
61     """Check if the suite includes tests.
62
63     :param suite_name: Name of the suite to be checked.
64     :param tests: Set of tests
65     :type suite_name: str
66     :type tests: pandas.Series
67     :returns: True if the suite includes tests.
68     :rtype: bool
69     """
70
71     for key in tests.keys():
72         if suite_name == tests[key][u"parent"]:
73             return True
74     return False
75
76
77 def file_test_results(file_spec, input_data, frmt=u"rst"):
78     """Generate the file(s) with algorithms
79     - file_test_results
80     specified in the specification file.
81
82     :param file_spec: File to generate.
83     :param input_data: Data to process.
84     :param frmt: Format can be: rst or html
85     :type file_spec: pandas.Series
86     :type input_data: InputData
87     :type frmt: str
88     """
89
90     base_file_name = f"{file_spec[u'output-file']}"
91     rst_header = (
92         u"\n"
93         u".. |br| raw:: html\n\n    <br />\n\n\n"
94         u".. |prein| raw:: html\n\n    <pre>\n\n\n"
95         u".. |preout| raw:: html\n\n    </pre>\n\n"
96     )
97     start_lvl = file_spec.get(u"data-start-level", 4)
98
99     logging.info(f"  Generating the file {base_file_name} ...")
100
101     if frmt == u"html":
102         table_lst = get_files(file_spec[u"dir-tables"], u".rst", full_path=True)
103     elif frmt == u"rst":
104         table_lst = get_files(file_spec[u"dir-tables"], u".csv", full_path=True)
105     else:
106         return
107     if not table_lst:
108         logging.error(
109             f"  No tables to include in {file_spec[u'dir-tables']}. Skipping."
110         )
111         return
112
113     logging.info(
114         f"    Creating the tests data set for the "
115         f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
116     )
117
118     tests = input_data.filter_data(
119         file_spec,
120         params=[u"name", u"parent", u"doc", u"type", u"level"],
121         continue_on_error=True
122     )
123     if tests.empty:
124         return
125     tests = input_data.merge_data(tests)
126
127     suites = input_data.filter_data(
128         file_spec,
129         continue_on_error=True,
130         data_set=u"suites"
131     )
132     if suites.empty:
133         return
134     suites = input_data.merge_data(suites)
135     suites.sort_index(inplace=True)
136
137     file_name = u""
138     for suite_longname, suite in suites.items():
139
140         suite_lvl = len(suite_longname.split(u"."))
141         if suite_lvl < start_lvl:
142             # Not interested in this suite
143             continue
144
145         if suite_lvl == start_lvl:
146             # Our top-level suite
147             chapter = suite_longname.split(u'.')[-1]
148             file_name = f"{base_file_name}/{chapter}.rst"
149             logging.info(f"    Writing file {file_name}")
150             with open(f"{base_file_name}/index.rst", u"a") as file_handler:
151                 file_handler.write(f"    {chapter}\n")
152             with open(file_name, u"a") as file_handler:
153                 file_handler.write(rst_header)
154
155         title_line = get_rst_title_char(suite[u"level"] - start_lvl + 2) * \
156             len(suite[u"name"])
157         with open(file_name, u"a") as file_handler:
158             if not (u"-ndrpdr" in suite[u"name"] or
159                     u"-mrr" in suite[u"name"] or
160                     u"-dev" in suite[u"name"]):
161                 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
162
163             if _tests_in_suite(suite[u"name"], tests):
164                 for tbl_file in table_lst:
165                     if suite[u"name"] in tbl_file:
166                         file_handler.write(
167                             f"\n{suite[u'name']}\n{title_line}\n"
168                         )
169                         file_handler.write(
170                             f"\n{suite[u'doc']}\n".replace(u'|br|', u'\n\n -')
171                         )
172                         if frmt == u"html":
173                             file_handler.write(
174                                 f"\n.. include:: {tbl_file.split(u'/')[-1]}\n"
175                             )
176                         elif frmt == u"rst":
177                             file_handler.write(
178                                 RST_INCLUDE_TABLE.format(
179                                     file_latex=tbl_file,
180                                     file_html=tbl_file.split(u"/")[-1])
181                             )
182                         break
183
184     logging.info(u"  Done.")
185
186
187 def file_test_results_html(file_spec, input_data):
188     """Generate the file(s) with algorithms
189     - file_test_results_html
190     specified in the specification file.
191
192     :param file_spec: File to generate.
193     :param input_data: Data to process.
194     :type file_spec: pandas.Series
195     :type input_data: InputData
196     """
197     file_test_results(file_spec, input_data, frmt=u"html")