Bug 841126: Update DataChannels to use new stack option to ignore port numbers over...
[gecko.git] / testing / runcppunittests.py
bloba24e86c301fd995e8fe425d1b4d9c08322af70d6
1 #!/usr/bin/env python
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
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 from __future__ import with_statement
8 import sys, os, tempfile, shutil
9 from optparse import OptionParser
10 import mozprocess, mozinfo, mozlog, mozcrash
11 from contextlib import contextmanager
13 log = mozlog.getLogger('cppunittests')
15 @contextmanager
16 def TemporaryDirectory():
17 tempdir = tempfile.mkdtemp()
18 yield tempdir
19 shutil.rmtree(tempdir)
21 class CPPUnitTests(object):
22 # Time (seconds) to wait for test process to complete
23 TEST_PROC_TIMEOUT = 1200
24 # Time (seconds) in which process will be killed if it produces no output.
25 TEST_PROC_NO_OUTPUT_TIMEOUT = 300
27 def run_one_test(self, prog, env, symbols_path=None):
28 """
29 Run a single C++ unit test program.
31 Arguments:
32 * prog: The path to the test program to run.
33 * env: The environment to use for running the program.
34 * symbols_path: A path to a directory containing Breakpad-formatted
35 symbol files for producing stack traces on crash.
37 Return True if the program exits with a zero status, False otherwise.
38 """
39 basename = os.path.basename(prog)
40 log.info("Running test %s", basename)
41 with TemporaryDirectory() as tempdir:
42 proc = mozprocess.ProcessHandler([prog],
43 cwd=tempdir,
44 env=env)
45 #TODO: After bug 811320 is fixed, don't let .run() kill the process,
46 # instead use a timeout in .wait() and then kill to get a stack.
47 proc.run(timeout=CPPUnitTests.TEST_PROC_TIMEOUT,
48 outputTimeout=CPPUnitTests.TEST_PROC_NO_OUTPUT_TIMEOUT)
49 proc.wait()
50 if proc.timedOut:
51 log.testFail("%s | timed out after %d seconds",
52 basename, CPPUnitTests.TEST_PROC_TIMEOUT)
53 return False
54 if mozcrash.check_for_crashes(tempdir, symbols_path,
55 test_name=basename):
56 log.testFail("%s | test crashed", basename)
57 return False
58 result = proc.proc.returncode == 0
59 if not result:
60 log.testFail("%s | test failed with return code %d",
61 basename, proc.proc.returncode)
62 return result
64 def build_core_environment(self, env = {}):
65 """
66 Add environment variables likely to be used across all platforms, including remote systems.
67 """
68 env["MOZILLA_FIVE_HOME"] = self.xre_path
69 env["MOZ_XRE_DIR"] = self.xre_path
70 #TODO: switch this to just abort once all C++ unit tests have
71 # been fixed to enable crash reporting
72 env["XPCOM_DEBUG_BREAK"] = "stack-and-abort"
73 env["MOZ_CRASHREPORTER_NO_REPORT"] = "1"
74 env["MOZ_CRASHREPORTER"] = "1"
75 return env
77 def build_environment(self):
78 """
79 Create and return a dictionary of all the appropriate env variables and values.
80 On a remote system, we overload this to set different values and are missing things like os.environ and PATH.
81 """
82 if not os.path.isdir(self.xre_path):
83 raise Exception("xre_path does not exist: %s", self.xre_path)
84 env = dict(os.environ)
85 env = self.build_core_environment(env)
86 pathvar = ""
87 if mozinfo.os == "linux":
88 pathvar = "LD_LIBRARY_PATH"
89 elif mozinfo.os == "mac":
90 pathvar = "DYLD_LIBRARY_PATH"
91 elif mozinfo.os == "win":
92 pathvar = "PATH"
93 if pathvar:
94 if pathvar in env:
95 env[pathvar] = "%s%s%s" % (self.xre_path, os.pathsep, env[pathvar])
96 else:
97 env[pathvar] = self.xre_path
98 return env
100 def run_tests(self, programs, xre_path, symbols_path=None):
102 Run a set of C++ unit test programs.
104 Arguments:
105 * programs: An iterable containing paths to test programs.
106 * xre_path: A path to a directory containing a XUL Runtime Environment.
107 * symbols_path: A path to a directory containing Breakpad-formatted
108 symbol files for producing stack traces on crash.
110 Returns True if all test programs exited with a zero status, False
111 otherwise.
113 self.xre_path = xre_path
114 env = self.build_environment()
115 result = True
116 for prog in programs:
117 single_result = self.run_one_test(prog, env, symbols_path)
118 result = result and single_result
119 return result
121 class CPPUnittestOptions(OptionParser):
122 def __init__(self):
123 OptionParser.__init__(self)
124 self.add_option("--xre-path",
125 action = "store", type = "string", dest = "xre_path",
126 default = None,
127 help = "absolute path to directory containing XRE (probably xulrunner)")
128 self.add_option("--symbols-path",
129 action = "store", type = "string", dest = "symbols_path",
130 default = None,
131 help = "absolute path to directory containing breakpad symbols, or the URL of a zip file containing symbols")
133 def main():
134 parser = CPPUnittestOptions()
135 options, args = parser.parse_args()
136 if not args:
137 print >>sys.stderr, """Usage: %s <test binary> [<test binary>...]""" % sys.argv[0]
138 sys.exit(1)
139 if not options.xre_path:
140 print >>sys.stderr, """Error: --xre-path is required"""
141 sys.exit(1)
142 progs = [os.path.abspath(p) for p in args]
143 options.xre_path = os.path.abspath(options.xre_path)
144 tester = CPPUnitTests()
145 try:
146 result = tester.run_tests(progs, options.xre_path, options.symbols_path)
147 except Exception, e:
148 log.error(str(e))
149 result = False
150 sys.exit(0 if result else 1)
152 if __name__ == '__main__':
153 main()