tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

wptreport.py (2487B)


      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            "time": time(),
     31            "run_info": {},
     32        })
     33 
     34    def suite_end(self):
     35        """
     36        Produce the "suite_end" message at the present time and write the
     37        results to the file path given in the constructor.
     38        """
     39        result = self.formatter.suite_end({
     40            "time": time(),
     41        })
     42        with open(self.out, "w") as fp:
     43            fp.write(result)
     44 
     45    def test(self, result, duration):
     46        """
     47        Produce the "test_start", "test_status" and "test_end" messages, as
     48        appropriate.
     49 
     50        :param dict result: a dictionary with the test results. It should
     51                            include the following keys:
     52                            * "name": the ID of the test;
     53                            * "status": the actual status of the whole test;
     54                            * "expected": the expected status of the whole test;
     55                            * "subtests": a list of dicts with keys "test",
     56                              "subtest", "status" and "expected".
     57        :param float duration: the runtime of the test
     58        """
     59        testname = result["name"]
     60 
     61        end_time = time()
     62        start_time = end_time - duration
     63 
     64        self.formatter.test_start({
     65            "test": testname,
     66            "time": start_time,
     67        })
     68 
     69        for subtest in result["subtests"]:
     70            self.formatter.test_status(subtest)
     71 
     72        self.formatter.test_end({
     73            "test": testname,
     74            "time": end_time,
     75            "status": result["status"],
     76            "expected": result["expected"],
     77        })