summaryrefslogtreecommitdiffstats
path: root/testing/tps/tps/cli.py
blob: 9ef6186b4da192f5fe572ca029024b5aac00c27b (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import json
import optparse
import os
import re
import sys
from threading import RLock

from tps import TPSTestRunner


def main():
    parser = optparse.OptionParser()
    parser.add_option(
        "--binary",
        action="store",
        type="string",
        dest="binary",
        default=None,
        help="path to the Firefox binary, specified either as "
        "a local file or a url; if omitted, the PATH "
        "will be searched;",
    )
    parser.add_option(
        "--configfile",
        action="store",
        type="string",
        dest="configfile",
        default=None,
        help="path to the config file to use default: %default]",
    )
    parser.add_option(
        "--debug",
        action="store_true",
        dest="debug",
        default=False,
        help="run in debug mode",
    )
    parser.add_option(
        "--ignore-unused-engines",
        default=False,
        action="store_true",
        dest="ignore_unused_engines",
        help="If defined, do not load unused engines in individual tests."
        " Has no effect for pulse monitor.",
    )
    parser.add_option(
        "--logfile",
        action="store",
        type="string",
        dest="logfile",
        default="tps.log",
        help="path to the log file [default: %default]",
    )
    parser.add_option(
        "--mobile",
        action="store_true",
        dest="mobile",
        default=False,
        help="run with mobile settings",
    )
    parser.add_option(
        "--pulsefile",
        action="store",
        type="string",
        dest="pulsefile",
        default=None,
        help="path to file containing a pulse message in "
        "json format that you want to inject into the monitor",
    )
    parser.add_option(
        "--resultfile",
        action="store",
        type="string",
        dest="resultfile",
        default="tps_result.json",
        help="path to the result file [default: %default]",
    )
    parser.add_option(
        "--testfile",
        action="store",
        type="string",
        dest="testfile",
        default="all_tests.json",
        help="path to the test file to run [default: %default]",
    )
    parser.add_option(
        "--stop-on-error",
        action="store_true",
        dest="stop_on_error",
        help="stop running tests after the first failure",
    )
    (options, args) = parser.parse_args()

    configfile = options.configfile
    if configfile is None:
        virtual_env = os.environ.get("VIRTUAL_ENV")
        if virtual_env:
            configfile = os.path.join(virtual_env, "config.json")
        if configfile is None or not os.access(configfile, os.F_OK):
            raise Exception(
                "Unable to find config.json in a VIRTUAL_ENV; you must "
                "specify a config file using the --configfile option"
            )

    # load the config file
    f = open(configfile, "r")
    configcontent = f.read()
    f.close()
    config = json.loads(configcontent)
    testfile = os.path.join(config.get("testdir", ""), options.testfile)

    rlock = RLock()

    print("using result file", options.resultfile)

    extensionDir = config.get("extensiondir")
    if not extensionDir or extensionDir == "__EXTENSIONDIR__":
        extensionDir = os.path.join(
            os.getcwd(), "..", "..", "services", "sync", "tps", "extensions"
        )
    else:
        if sys.platform == "win32":
            # replace msys-style paths with proper Windows paths
            m = re.match("^\/\w\/", extensionDir)
            if m:
                extensionDir = "%s:/%s" % (m.group(0)[1:2], extensionDir[3:])
                extensionDir = extensionDir.replace("/", "\\")
    if sys.platform == "darwin":
        # Needed to avoid tab crashes on mac due to level 3 sandboxing
        sourceRoot = os.path.join(extensionDir, "..", "..", "..", "..")
        os.environ["MOZ_DEVELOPER_REPO_DIR"] = os.path.abspath(sourceRoot)

    TPS = TPSTestRunner(
        extensionDir,
        binary=options.binary,
        config=config,
        debug=options.debug,
        ignore_unused_engines=options.ignore_unused_engines,
        logfile=options.logfile,
        mobile=options.mobile,
        resultfile=options.resultfile,
        rlock=rlock,
        testfile=testfile,
        stop_on_error=options.stop_on_error,
    )
    TPS.run_tests()

    if TPS.numfailed > 0 or TPS.numpassed == 0:
        sys.exit(1)


if __name__ == "__main__":
    main()