mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-03 23:30:46 +00:00
Bug 1175708 - Eliminate some horrid action-at-a-distance global state in jstests.py; r=sfink
This commit is contained in:
parent
63671ec61f
commit
bc6c310e53
@ -9,6 +9,7 @@ from __future__ import print_function
|
||||
|
||||
import os, sys, textwrap
|
||||
from os.path import abspath, dirname, isfile, realpath
|
||||
from contextlib import contextmanager
|
||||
from copy import copy
|
||||
from subprocess import list2cmdline, call
|
||||
|
||||
@ -22,15 +23,6 @@ if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
|
||||
else:
|
||||
from lib.tasks_win import run_all_tests
|
||||
|
||||
def run_tests(options, tests, results):
|
||||
"""Run the given tests, sending raw results to the given results
|
||||
accumulator."""
|
||||
try:
|
||||
completed = run_all_tests(tests, results, options)
|
||||
except KeyboardInterrupt:
|
||||
completed = False
|
||||
|
||||
results.finish(completed)
|
||||
|
||||
def get_cpu_count():
|
||||
"""
|
||||
@ -62,6 +54,17 @@ def get_cpu_count():
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
@contextmanager
|
||||
def changedir(dirname):
|
||||
pwd = os.getcwd()
|
||||
os.chdir(dirname)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
os.chdir(pwd)
|
||||
|
||||
|
||||
def parse_args():
|
||||
"""
|
||||
Parse command line arguments.
|
||||
@ -191,14 +194,14 @@ def parse_args():
|
||||
op.error("--valgrind, --debug, and --rr are mutually exclusive.")
|
||||
|
||||
# Fill the debugger field, as needed.
|
||||
prefix = options.debugger.split() if options.debug else []
|
||||
debugger_prefix = options.debugger.split() if options.debug else []
|
||||
if options.valgrind:
|
||||
prefix = ['valgrind'] + options.valgrind_args.split()
|
||||
debugger_prefix = ['valgrind'] + options.valgrind_args.split()
|
||||
if os.uname()[0] == 'Darwin':
|
||||
prefix.append('--dsymutil=yes')
|
||||
debugger_prefix.append('--dsymutil=yes')
|
||||
options.show_output = True
|
||||
if options.rr:
|
||||
prefix = ['rr', 'record']
|
||||
debugger_prefix = ['rr', 'record']
|
||||
|
||||
js_cmd_args = options.shell_args.split()
|
||||
if options.jorendb:
|
||||
@ -209,7 +212,8 @@ def parse_args():
|
||||
abspath(dirname(abspath(__file__))),
|
||||
'..', '..', 'examples', 'jorendb.js'))
|
||||
js_cmd_args.extend(['-d', '-f', debugger_path, '--'])
|
||||
TestCase.set_js_cmd_prefix(options.js_shell, js_cmd_args, prefix)
|
||||
prefix = TestCase.build_js_cmd_prefix(options.js_shell, js_cmd_args,
|
||||
debugger_prefix)
|
||||
|
||||
# If files with lists of tests to run were specified, add them to the
|
||||
# requested tests set.
|
||||
@ -248,7 +252,8 @@ def parse_args():
|
||||
not ProgressBar.conservative_isatty() or
|
||||
options.hide_progress)
|
||||
|
||||
return (options, requested_paths, excluded_paths)
|
||||
return (options, prefix, requested_paths, excluded_paths)
|
||||
|
||||
|
||||
def load_tests(options, requested_paths, excluded_paths):
|
||||
"""
|
||||
@ -324,8 +329,9 @@ def load_tests(options, requested_paths, excluded_paths):
|
||||
|
||||
return skip_list, test_list
|
||||
|
||||
|
||||
def main():
|
||||
options, requested_paths, excluded_paths = parse_args()
|
||||
options, prefix, requested_paths, excluded_paths = parse_args()
|
||||
if options.js_shell is not None and not isfile(options.js_shell):
|
||||
print('Could not find shell at given path.')
|
||||
return 1
|
||||
@ -348,31 +354,26 @@ def main():
|
||||
cmd = test_list[0].get_command(TestCase.js_cmd_prefix)
|
||||
if options.show_cmd:
|
||||
print(list2cmdline(cmd))
|
||||
if test_dir not in ('', '.'):
|
||||
os.chdir(test_dir)
|
||||
call(cmd)
|
||||
with changedir(test_dir):
|
||||
call(cmd)
|
||||
return 0
|
||||
|
||||
curdir = os.getcwd()
|
||||
if test_dir not in ('', '.'):
|
||||
os.chdir(test_dir)
|
||||
with changedir(test_dir):
|
||||
# Force Pacific time zone to avoid failures in Date tests.
|
||||
os.environ['TZ'] = 'PST8PDT'
|
||||
# Force date strings to English.
|
||||
os.environ['LC_TIME'] = 'en_US.UTF-8'
|
||||
|
||||
# Force Pacific time zone to avoid failures in Date tests.
|
||||
os.environ['TZ'] = 'PST8PDT'
|
||||
# Force date strings to English.
|
||||
os.environ['LC_TIME'] = 'en_US.UTF-8'
|
||||
|
||||
results = None
|
||||
try:
|
||||
results = ResultsSink(options, len(skip_list) + len(test_list))
|
||||
for t in skip_list:
|
||||
results.push(NullTestOutput(t))
|
||||
run_tests(options, test_list, results)
|
||||
finally:
|
||||
os.chdir(curdir)
|
||||
try:
|
||||
for t in skip_list:
|
||||
results.push(NullTestOutput(t))
|
||||
ok = run_all_tests(test_list, prefix, results, options)
|
||||
except KeyboardInterrupt:
|
||||
ok = False
|
||||
|
||||
if results is None or not results.all_passed():
|
||||
return 1
|
||||
results.finish(ok)
|
||||
return 0 if results.all_passed() else 1
|
||||
|
||||
return 0
|
||||
|
||||
|
@ -7,9 +7,9 @@ from datetime import datetime, timedelta
|
||||
from results import TestOutput
|
||||
|
||||
class Task(object):
|
||||
def __init__(self, test, pid, stdout, stderr):
|
||||
def __init__(self, test, prefix, pid, stdout, stderr):
|
||||
self.test = test
|
||||
self.cmd = test.get_command(test.js_cmd_prefix)
|
||||
self.cmd = test.get_command(prefix)
|
||||
self.pid = pid
|
||||
self.stdout = stdout
|
||||
self.stderr = stderr
|
||||
@ -17,7 +17,7 @@ class Task(object):
|
||||
self.out = []
|
||||
self.err = []
|
||||
|
||||
def spawn_test(test, passthrough=False):
|
||||
def spawn_test(test, prefix, passthrough=False):
|
||||
"""Spawn one child, return a task struct."""
|
||||
if not passthrough:
|
||||
(rout, wout) = os.pipe()
|
||||
@ -29,7 +29,7 @@ def spawn_test(test, passthrough=False):
|
||||
if rv:
|
||||
os.close(wout)
|
||||
os.close(werr)
|
||||
return Task(test, rv, rout, rerr)
|
||||
return Task(test, prefix, rv, rout, rerr)
|
||||
|
||||
# Child.
|
||||
os.close(rout)
|
||||
@ -38,7 +38,7 @@ def spawn_test(test, passthrough=False):
|
||||
os.dup2(wout, 1)
|
||||
os.dup2(werr, 2)
|
||||
|
||||
cmd = test.get_command(test.js_cmd_prefix)
|
||||
cmd = test.get_command(prefix)
|
||||
os.execvp(cmd[0], cmd)
|
||||
|
||||
def total_seconds(td):
|
||||
@ -180,7 +180,7 @@ def kill_undead(tasks, results, timeout):
|
||||
if timed_out(task, timeout):
|
||||
os.kill(task.pid, 9)
|
||||
|
||||
def run_all_tests(tests, results, options):
|
||||
def run_all_tests(tests, prefix, results, options):
|
||||
# Copy and reverse for fast pop off end.
|
||||
tests = tests[:]
|
||||
tests.reverse()
|
||||
@ -190,7 +190,7 @@ def run_all_tests(tests, results, options):
|
||||
|
||||
while len(tests) or len(tasks):
|
||||
while len(tests) and len(tasks) < options.worker_count:
|
||||
tasks.append(spawn_test(tests.pop(), options.passthrough))
|
||||
tasks.append(spawn_test(tests.pop(), prefix, options.passthrough))
|
||||
|
||||
timeout = get_max_wait(tasks, results, options.timeout)
|
||||
read_input(tasks, timeout)
|
||||
|
@ -18,10 +18,10 @@ def _do_work(qTasks, qResults, timeout):
|
||||
if test is EndMarker:
|
||||
qResults.put(EndMarker)
|
||||
return
|
||||
qResults.put(test.run(test.js_cmd_prefix, timeout))
|
||||
qResults.put(test.run(prefix, timeout))
|
||||
|
||||
|
||||
def run_all_tests_gen(tests, results, options):
|
||||
def run_all_tests_gen(tests, prefix, results, options):
|
||||
"""
|
||||
Uses scatter-gather to a thread-pool to manage children.
|
||||
"""
|
||||
@ -29,7 +29,8 @@ def run_all_tests_gen(tests, results, options):
|
||||
|
||||
workers = []
|
||||
for _ in range(options.worker_count):
|
||||
worker = Thread(target=_do_work, args=(qTasks, qResults, options.timeout))
|
||||
worker = Thread(target=_do_work, args=(qTasks, qResults, prefix,
|
||||
options.timeout))
|
||||
worker.setDaemon(True)
|
||||
worker.start()
|
||||
workers.append(worker)
|
||||
|
@ -118,21 +118,19 @@ class Test(object):
|
||||
return Test.prefix_command(head) \
|
||||
+ ['-f', os.path.join(path, 'shell.js')]
|
||||
|
||||
def get_command(self, js_cmd_prefix):
|
||||
def get_command(self, prefix):
|
||||
dirname, filename = os.path.split(self.path)
|
||||
cmd = js_cmd_prefix + self.jitflags + self.options \
|
||||
cmd = prefix + self.jitflags + self.options \
|
||||
+ Test.prefix_command(dirname) + ['-f', self.path]
|
||||
return cmd
|
||||
|
||||
def run(self, js_cmd_prefix, timeout=30.0):
|
||||
cmd = self.get_command(js_cmd_prefix)
|
||||
def run(self, prefix, timeout=30.0):
|
||||
cmd = self.get_command(prefix)
|
||||
out, err, rc, dt, timed_out = run_cmd(cmd, timeout)
|
||||
return TestOutput(self, cmd, out, err, rc, dt, timed_out)
|
||||
|
||||
class TestCase(Test):
|
||||
"""A test case consisting of a test and an expected result."""
|
||||
js_cmd_prefix = None
|
||||
|
||||
def __init__(self, path):
|
||||
Test.__init__(self, path)
|
||||
self.enable = True # bool: True => run test, False => don't run
|
||||
@ -163,15 +161,15 @@ class TestCase(Test):
|
||||
ans += ', debugMode'
|
||||
return ans
|
||||
|
||||
@classmethod
|
||||
def set_js_cmd_prefix(self, js_path, js_args, debugger_prefix):
|
||||
@staticmethod
|
||||
def build_js_cmd_prefix(js_path, js_args, debugger_prefix):
|
||||
parts = []
|
||||
if debugger_prefix:
|
||||
parts += debugger_prefix
|
||||
parts.append(js_path)
|
||||
if js_args:
|
||||
parts += js_args
|
||||
self.js_cmd_prefix = parts
|
||||
return parts
|
||||
|
||||
def __cmp__(self, other):
|
||||
if self.path == other.path:
|
||||
|
Loading…
x
Reference in New Issue
Block a user