| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
# Integration between the jstests harness and `WptreportFormatter`. |
| 6 |
# |
| 7 |
# `WptreportFormatter` uses the data format specified in |
| 8 |
# <https://firefox-source-docs.mozilla.org/mozbase/mozlog.html>. |
| 9 |
|
| 10 |
from time import time |
| 11 |
|
| 12 |
from wptrunner.formatters.wptreport import WptreportFormatter |
| 13 |
|
| 14 |
|
| 15 |
class WptreportHandler: |
| 16 |
def __init__(self, out): |
| 17 |
""" |
| 18 |
Initialize the WptreportHandler handler. |
| 19 |
|
| 20 |
:param str out: path to a file to write output to. |
| 21 |
""" |
| 22 |
self.out = out |
| 23 |
self.formatter = WptreportFormatter() |
| 24 |
|
| 25 |
def suite_start(self): |
| 26 |
""" |
| 27 |
Produce the "suite_start" message at the present time. |
| 28 |
""" |
| 29 |
self.formatter.suite_start( |
| 30 |
{ |
| 31 |
"time": time(), |
| 32 |
"run_info": {}, |
| 33 |
} |
| 34 |
) |
| 35 |
|
| 36 |
def suite_end(self): |
| 37 |
""" |
| 38 |
Produce the "suite_end" message at the present time and write the |
| 39 |
results to the file path given in the constructor. |
| 40 |
""" |
| 41 |
result = self.formatter.suite_end( |
| 42 |
{ |
| 43 |
"time": time(), |
| 44 |
} |
| 45 |
) |
| 46 |
with open(self.out, "w") as fp: |
| 47 |
fp.write(result) |
| 48 |
|
| 49 |
def test(self, result, duration): |
| 50 |
""" |
| 51 |
Produce the "test_start", "test_status" and "test_end" messages, as |
| 52 |
appropriate. |
| 53 |
|
| 54 |
:param dict result: a dictionary with the test results. It should |
| 55 |
include the following keys: |
| 56 |
* "name": the ID of the test; |
| 57 |
* "status": the actual status of the whole test; |
| 58 |
* "expected": the expected status of the whole test; |
| 59 |
* "subtests": a list of dicts with keys "test", |
| 60 |
"subtest", "status" and "expected". |
| 61 |
:param float duration: the runtime of the test |
| 62 |
""" |
| 63 |
testname = result["name"] |
| 64 |
|
| 65 |
end_time = time() |
| 66 |
start_time = end_time - duration |
| 67 |
|
| 68 |
self.formatter.test_start( |
| 69 |
{ |
| 70 |
"test": testname, |
| 71 |
"time": start_time, |
| 72 |
} |
| 73 |
) |
| 74 |
|
| 75 |
for subtest in result["subtests"]: |
| 76 |
self.formatter.test_status(subtest) |
| 77 |
|
| 78 |
self.formatter.test_end( |
| 79 |
{ |
| 80 |
"test": testname, |
| 81 |
"time": end_time, |
| 82 |
"status": result["status"], |
| 83 |
"expected": result["expected"], |
| 84 |
} |
| 85 |
) |