services/common/tests/run_server.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/common/tests/run_server.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 +# You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +from argparse import ArgumentParser
    1.11 +from shutil import rmtree
    1.12 +from subprocess import Popen
    1.13 +from sys import argv
    1.14 +from sys import exit
    1.15 +from tempfile import mkdtemp
    1.16 +
    1.17 +DEFAULT_PORT = 8080
    1.18 +DEFAULT_HOSTNAME = 'localhost'
    1.19 +
    1.20 +def run_server(srcdir, objdir, js_file, hostname=DEFAULT_HOSTNAME,
    1.21 +        port=DEFAULT_PORT):
    1.22 +
    1.23 +    dist_dir = '%s/dist' % objdir
    1.24 +    head_dir = '%s/services/common/tests/unit' % srcdir
    1.25 +
    1.26 +    head_paths = [
    1.27 +        'head_global.js',
    1.28 +        'head_helpers.js',
    1.29 +        'head_http.js',
    1.30 +    ]
    1.31 +
    1.32 +    head_paths = ['"%s/%s"' % (head_dir, path) for path in head_paths]
    1.33 +
    1.34 +    args = [
    1.35 +        '%s/bin/xpcshell' % dist_dir,
    1.36 +        '-g', '%s/bin' % dist_dir,
    1.37 +        '-a', '%s/bin' % dist_dir,
    1.38 +        '-r', '%s/bin/components/httpd.manifest' % dist_dir,
    1.39 +        '-m',
    1.40 +        '-n',
    1.41 +        '-s',
    1.42 +        '-f', '%s/testing/xpcshell/head.js' % srcdir,
    1.43 +        '-e', 'const _SERVER_ADDR = "%s";' % hostname,
    1.44 +        '-e', 'const _TESTING_MODULES_DIR = "%s/_tests/modules";' % objdir,
    1.45 +        '-e', 'const SERVER_PORT = "%s";' % port,
    1.46 +        '-e', 'const INCLUDE_FILES = [%s];' % ', '.join(head_paths),
    1.47 +        '-e', '_register_protocol_handlers();',
    1.48 +        '-e', 'for each (let name in INCLUDE_FILES) load(name);',
    1.49 +        '-e', '_fakeIdleService.activate();',
    1.50 +        '-f', '%s/services/common/tests/%s' % (srcdir, js_file)
    1.51 +    ]
    1.52 +
    1.53 +    profile_dir = mkdtemp()
    1.54 +    print 'Created profile directory: %s' % profile_dir
    1.55 +
    1.56 +    try:
    1.57 +        env = {'XPCSHELL_TEST_PROFILE_DIR': profile_dir}
    1.58 +        proc = Popen(args, env=env)
    1.59 +
    1.60 +        return proc.wait()
    1.61 +
    1.62 +    finally:
    1.63 +        print 'Removing profile directory %s' % profile_dir
    1.64 +        rmtree(profile_dir)
    1.65 +
    1.66 +if __name__ == '__main__':
    1.67 +    parser = ArgumentParser(description="Run a standalone JS server.")
    1.68 +    parser.add_argument('srcdir',
    1.69 +        help="Root directory of Firefox source code.")
    1.70 +    parser.add_argument('objdir',
    1.71 +        help="Root directory object directory created during build.")
    1.72 +    parser.add_argument('js_file',
    1.73 +        help="JS file (in this directory) to execute.")
    1.74 +    parser.add_argument('--port', default=DEFAULT_PORT, type=int,
    1.75 +        help="Port to run server on.")
    1.76 +    parser.add_argument('--address', default=DEFAULT_HOSTNAME,
    1.77 +        help="Hostname to bind server to.")
    1.78 +
    1.79 +    args = parser.parse_args()
    1.80 +
    1.81 +    exit(run_server(args.srcdir, args.objdir, args.js_file, args.address,
    1.82 +        args.port))

mercurial