summaryrefslogtreecommitdiff
path: root/.gitlab-ci/meson-html-report.py
blob: ef895a948a6fdcf927a38cc82b1c5827b0981bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3

# Turns a Mason testlog.json file into an HTML report
#
# Copyright 2019  GNOME Foundation
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Original author: Emmanuele Bassi

import argparse
import datetime
import json
import os
import sys
from jinja2 import Template

REPORT_TEMPLATE = '''
<!DOCTYPE html>
<html lang="en">
<head>
  <title>{{ report.project_name }} Test Report</title>
  <meta charset="utf-8" />
  <style type="text/css">
body {
  background: white;
  color: #333;
  font-family: 'Cantarell', sans-serif;
}

h1 {
  color: #333333;
  font-size: 1.9em;
  font-weight: normal;
  margin-bottom: 1em;
  border-bottom: 1px solid #333333;
}

header {
  position: fixed;
  padding-bottom: 12px;
  margin-bottom: 24px;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.15);
  z-index: 500;
  left: 0;
  top: 0;
  width: 100%;
  color: rgba(0, 0, 0, 0.3);
  transform: translateY(0px);
  transition: .2s background-color, color;
  box-sizing: border-box;
  display: block;
  visibility: visible;
  text-align: center;
}

article {
  padding-top: 200px;
  margin: 2em;
}

div.report-meta {
  width: auto;
  border: 1px solid #ccc;
  padding: .5em 2em;
  color: #3c3c3c;
}

span.result {
  font-weight: bold;
}

span.pass {
  color: rgb(51, 209, 122);
}

span.skip {
  color: rgb(255, 163, 72);
}

span.fail {
  color: rgb(224, 27, 36);
}

span.xfail {
  color: rgb(163, 71, 186);
}

div.result {
  border-top: 1px solid #c0c0c0;
  padding-top: 1em;
  padding-bottom: 1em;
  width: 100%;
}

div.result h4 {
  border-bottom: 1px solid #c0c0c0;
  margin-bottom: 0.7em;
}

pre {
  color: #fafafa;
  background-color: black;
  border-radius: 6px;
  box-shadow: 0px 5px 8px 0px rgba(0, 0, 0, 0.25);
  font-family: monospace;
  line-height: 1.2em;
  border: none;
  padding: 10px 1em;
  font-size: 0.9em;
  overflow: auto;
  white-space: pre;
  word-break: normal;
  word-wrap: normal;
}

ul.passed li {
  display: inline;
}

ul.passed li:after {
  content: ",";
}

ul.passed li:last-child:after {
  content: "";
}

ul.images {
  padding-bottom: 1em;
}

ul.images li {
  display: inline;
}
  </style>
</head>
<body>
  <header>
    <h1>{{ report.project_name }} :: Test Reports</h1>
    <div class="report-meta">
      <p><strong>Branch:</strong> {{ report.branch_name }}</p>
      <p><strong>Date:</strong> <time datetime="{{ report.date.isoformat() }}">{{ report.locale_date }}</time></p>
      {% if report.job_id %}<p><strong>Job ID:</strong> {{ report.job_id }}</p>{% endif %}
    </div>
  </header>

  <article>
    <section>
      <div class="summary">
        <h3><a name="summary">Summary</a></h3>
        <ul>
          <li><strong>Total units:</strong> {{ report.total_units }}</li>
          <li><strong>Passed:</strong> <a href="#passed">{{ report.total_successes }}</a></li>
          <li><strong>Failed:</strong> <a href="#failures">{{ report.total_failures }}</a></li>
        </ul>
      </div>
    </section>

    {% for suite_result in report.results_list %}
    <section>
      <div class="result">
        <h3><a name="results">Suite: {{ suite_result.suite_name }}</a></h3>
        <ul>
          <li><strong>Units:</strong> {{ suite_result.n_units }}</li>
          <li><strong>Passed:</strong> {{ suite_result.n_successes }}</li>
          <li><strong>Failed:</strong> {{ suite_result.n_failures }}</li>
        </ul>

        <div class="successes">
          <h4><a name="passed">Passed</a></h4>
          <ul class="passed">
            {% for success in suite_result.successes if success.result == 'OK' %}
            <li>{{ success.name }} - result: <span class="result pass">{{ success.result }}</li>
            {% else %}
            <li>None</li>
            {% endfor %}
          </ul>

          <h4><a name="skipped">Skipped</a></h4>
          <ul>
            {% for success in suite_result.successes if success.result == 'SKIP' %}
            <li>{{ success.name }} - result: <span class="result skip">{{ success.result }}</li>
            {% else %}
            <li>None</li>
            {% endfor %}
          </ul>

          <h4><a name="expected-fail">Expected failures</a></h4>
          <ul>
          {% for success in suite_result.successes if success.result == 'EXPECTEDFAIL' %}
            <li><a name="{{ success.name }}">{{ success.name }}</a> - result: <span class="result xfail">{{ success.result }}</span><br/>
            {% if success.stdout %}
              Output: <pre>{{ success.stdout }}</pre>
            {% endif %}
            {% if success.image_data is defined %}
              <ul class="images">
                <li><img alt="ref" src="{{ success.image_data.ref }}" /></li>
                <li><img alt="out" src="{{ success.image_data.out }}" /></li>
                <li><img alt="diff" src="{{ success.image_data.diff }}" /></li>
              </ul>
            {% endif %}
            </li>
          {% else %}
            <li>None</li>
          {% endfor %}
          </ul>
        </div>

        <div class="failures">
          <h4><a name="failed">Failed</a></h4>
          <ul class="failed">
            {% for failure in suite_result.failures if failure.result == 'FAIL' %}
            <li><a name="{{ failure.name }}">{{ failure.name }}</a> - result: <span class="result fail">{{ failure.result }}</span><br/>
              {% if failure.stdout %}
              Output: <pre>{{ failure.stdout }}</pre>
              {% endif %}
              {% if failure.image_data is defined %}
              <ul class="images">
                <li><img alt="ref" src="{{ failure.image_data.ref }}" /></li>
                <li><img alt="out" src="{{ failure.image_data.out }}" /></li>
                <li><img alt="diff" src="{{ failure.image_data.diff }}" /></li>
              </ul>
              {% endif %}
            </li>
            {% else %}
            <li>None</li>
            {% endfor %}
          </ul>

          <h4><a name="timed-out">Timed out</a></h4>
          <ul class="failed">
            {% for failure in suite_result.failures if failure.result == 'TIMEOUT' %}
            <li><a name="{{ failure.name }}">{{ failure.name }}</a> - result: <span class="result fail">{{ failure.result }}</span><br/>
              {% if failure.stdout %}
              Output: <pre>{{ failure.stdout }}</pre>
              {% endif %}
            </li>
            {% else %}
            <li>None</li>
            {% endfor %}
          </ul>
        </div>

      </div>
    </section>
    {% endfor %}

  </article>
</body>
</html>
'''

aparser = argparse.ArgumentParser(description='Turns a Meson test log into an HTML report')
aparser.add_argument('--project-name', metavar='NAME',
                     help='The project name',
                     default='Unknown')
aparser.add_argument('--job-id', metavar='ID',
                     help='The job ID for the report',
                     default=None)
aparser.add_argument('--branch', metavar='NAME',
                     help='Branch of the project being tested',
                     default='master')
aparser.add_argument('--output', metavar='FILE',
                     help='The output HTML file, stdout by default',
                     type=argparse.FileType('w', encoding='UTF-8'),
                     default=sys.stdout)
aparser.add_argument('--reftest-suite', metavar='NAME',
                     help='The name of the reftests suite',
                     default='reftest')
aparser.add_argument('--reftest-output-dir', metavar='DIR',
                     help='The output directory for reftests data',
                     default=None)
aparser.add_argument('infile', metavar='FILE',
                     help='The input testlog.json, stdin by default',
                     type=argparse.FileType('r', encoding='UTF-8'),
                     default=sys.stdin)

args = aparser.parse_args()

outfile = args.output

suites = {}
for line in args.infile:
    data = json.loads(line)
    (full_suite, unit_name) = data['name'].split(' / ')
    (project_name, suite_name) = full_suite.split(':')

    unit = {
        'project-name': project_name,
        'suite': suite_name,
        'name': unit_name,
        'duration': data['duration'],
        'returncode': data['returncode'],
        'result': data['result'],
        'stdout': data['stdout'],
    }

    if args.reftest_output_dir is not None and suite_name == args.reftest_suite:
        filename = unit_name.split(' ')[1]
        basename = os.path.splitext(filename)[0]

        image_data = {
            'ref': os.path.join(args.reftest_output_dir, '{}.ref.png'.format(basename)),
            'out': os.path.join(args.reftest_output_dir, '{}.out.png'.format(basename)),
            'diff': os.path.join(args.reftest_output_dir, '{}.diff.png'.format(basename)),
        }

        unit['image_data'] = image_data

    units = suites.setdefault(full_suite, [])
    units.append(unit)

report = {}
report['date'] = datetime.datetime.utcnow()
report['locale_date'] = report['date'].strftime("%c")
report['project_name'] = args.project_name
report['job_id'] = args.job_id
report['branch_name'] = args.branch
report['total_successes'] = 0
report['total_failures'] = 0
report['total_units'] = 0
report['results_list'] = []

for name, units in suites.items():
    (project_name, suite_name) = name.split(':')
    print('Processing {} suite {}:'.format(project_name, suite_name))

    def if_failed(unit):
        if unit['result'] in ['FAIL', 'TIMEOUT']:
            return True
        return False

    def if_succeded(unit):
        if unit['result'] in ['OK', 'EXPECTEDFAIL', 'SKIP']:
            return True
        return False

    successes = list(filter(if_succeded, units))
    failures = list(filter(if_failed, units))

    n_units = len(units)
    n_successes = len(successes)
    n_failures = len(failures)

    report['total_units'] += n_units
    report['total_successes'] += n_successes
    report['total_failures'] += n_failures
    print(' - {}: {} total, {} pass, {} fail'.format(suite_name, n_units, n_successes, n_failures))

    suite_report = {
        'suite_name': suite_name,
        'n_units': n_units,
        'successes': successes,
        'n_successes': n_successes,
        'failures': failures,
        'n_failures': n_failures,
    }
    report['results_list'].append(suite_report)

template = Template(REPORT_TEMPLATE)
outfile.write(template.render({'report': report}))