summaryrefslogtreecommitdiff
path: root/buildscripts/combine_reports.py
blob: d7fe0eb5ecee70064ebe0d9039f30fbc05b6dcc7 (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
#!/usr/bin/env python

"""
Combines JSON report files used in Evergreen
"""

from __future__ import absolute_import
from __future__ import print_function

import errno
import json
import os
import sys
from optparse import OptionParser

# Get relative imports to work when the package is not installed on the PYTHONPATH.
if __name__ == "__main__" and __package__ is None:
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    from buildscripts.resmokelib.testing import report


def read_json_file(json_file):
    with open(json_file) as json_data:
        return json.load(json_data)


def report_exit(combined_test_report):
    """The exit code of this script is based on the following:
        0:  All tests have status "pass", or only non-dynamic tests have status "silentfail".
        31: At least one test has status "fail" or "timeout".
       Note: A test can be considered dynamic if its name contains a ":" character."""

    ret = 0
    for test in combined_test_report.test_infos:
        if test.status in ["fail", "timeout"]:
            return 31
    return ret


def main():

    usage = "usage: %prog [options] report1.json report2.json ..."
    parser = OptionParser(description=__doc__, usage=usage)
    parser.add_option("-o", "--output-file",
                      dest="outfile",
                      default="-",
                      help="If '-', then the combined report file is written to stdout."
                           " Any other value is treated as the output file name. By default,"
                           " output is written to stdout.")
    parser.add_option("-m", "--ignore-missing-reports",
                      dest="ignore_missing",
                      action="store_true",
                      help="Ignore any input report file that does not exist.")
    parser.add_option("-x", "--no-report-exit",
                      dest="report_exit",
                      default=True,
                      action="store_false",
                      help="Do not exit with a non-zero code if any test in the report fails.")

    (options, args) = parser.parse_args()

    if not args:
        sys.exit("No report files were specified")

    report_files = args
    test_reports = []
    for report_file in report_files:
        try:
            report_file_json = read_json_file(report_file)
            test_reports.append(report.TestReport.from_dict(report_file_json))
        except IOError as e:
            # errno.ENOENT is the error code for "No such file or directory".
            if options.ignore_missing and e.errno == errno.ENOENT:
                print("Ignoring missing file {}".format(report_file))
                continue
            raise

    combined_test_report = report.TestReport.combine(*test_reports)
    combined_report = combined_test_report.as_dict()
    if options.outfile != "-":
        with open(options.outfile, "w") as fstream:
            json.dump(combined_report, fstream)
    else:
        print(json.dumps(combined_report))

    if options.report_exit:
        sys.exit(report_exit(combined_test_report))
    else:
        sys.exit(0)


if __name__ == "__main__":
    main()