services/common/tests/run_server.py

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:86c5f078b6ec
1 #!/usr/bin/python
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 from argparse import ArgumentParser
8 from shutil import rmtree
9 from subprocess import Popen
10 from sys import argv
11 from sys import exit
12 from tempfile import mkdtemp
13
14 DEFAULT_PORT = 8080
15 DEFAULT_HOSTNAME = 'localhost'
16
17 def run_server(srcdir, objdir, js_file, hostname=DEFAULT_HOSTNAME,
18 port=DEFAULT_PORT):
19
20 dist_dir = '%s/dist' % objdir
21 head_dir = '%s/services/common/tests/unit' % srcdir
22
23 head_paths = [
24 'head_global.js',
25 'head_helpers.js',
26 'head_http.js',
27 ]
28
29 head_paths = ['"%s/%s"' % (head_dir, path) for path in head_paths]
30
31 args = [
32 '%s/bin/xpcshell' % dist_dir,
33 '-g', '%s/bin' % dist_dir,
34 '-a', '%s/bin' % dist_dir,
35 '-r', '%s/bin/components/httpd.manifest' % dist_dir,
36 '-m',
37 '-n',
38 '-s',
39 '-f', '%s/testing/xpcshell/head.js' % srcdir,
40 '-e', 'const _SERVER_ADDR = "%s";' % hostname,
41 '-e', 'const _TESTING_MODULES_DIR = "%s/_tests/modules";' % objdir,
42 '-e', 'const SERVER_PORT = "%s";' % port,
43 '-e', 'const INCLUDE_FILES = [%s];' % ', '.join(head_paths),
44 '-e', '_register_protocol_handlers();',
45 '-e', 'for each (let name in INCLUDE_FILES) load(name);',
46 '-e', '_fakeIdleService.activate();',
47 '-f', '%s/services/common/tests/%s' % (srcdir, js_file)
48 ]
49
50 profile_dir = mkdtemp()
51 print 'Created profile directory: %s' % profile_dir
52
53 try:
54 env = {'XPCSHELL_TEST_PROFILE_DIR': profile_dir}
55 proc = Popen(args, env=env)
56
57 return proc.wait()
58
59 finally:
60 print 'Removing profile directory %s' % profile_dir
61 rmtree(profile_dir)
62
63 if __name__ == '__main__':
64 parser = ArgumentParser(description="Run a standalone JS server.")
65 parser.add_argument('srcdir',
66 help="Root directory of Firefox source code.")
67 parser.add_argument('objdir',
68 help="Root directory object directory created during build.")
69 parser.add_argument('js_file',
70 help="JS file (in this directory) to execute.")
71 parser.add_argument('--port', default=DEFAULT_PORT, type=int,
72 help="Port to run server on.")
73 parser.add_argument('--address', default=DEFAULT_HOSTNAME,
74 help="Hostname to bind server to.")
75
76 args = parser.parse_args()
77
78 exit(run_server(args.srcdir, args.objdir, args.js_file, args.address,
79 args.port))

mercurial