2013-09-05 06:58:51 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""IPython Test Process Controller
|
|
|
|
|
|
|
|
This module runs one or more subprocesses which will actually run the IPython
|
|
|
|
test suite.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Copyright (C) 2009-2011 The IPython Development Team
|
|
|
|
#
|
|
|
|
# Distributed under the terms of the BSD License. The full license is in
|
|
|
|
# the file COPYING, distributed as part of this software.
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Imports
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
import argparse
|
2013-09-05 06:58:51 +08:00
|
|
|
import multiprocessing.pool
|
|
|
|
import os
|
2013-09-07 02:02:41 +08:00
|
|
|
import shutil
|
2013-09-05 06:58:51 +08:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
from .iptest import have, test_group_names, test_sections
|
2013-09-07 07:54:06 +08:00
|
|
|
from IPython.utils.py3compat import bytes_to_str
|
2013-10-15 07:19:36 +08:00
|
|
|
from IPython.utils.sysinfo import brief_sys_info
|
2013-09-05 06:58:51 +08:00
|
|
|
from IPython.utils.tempdir import TemporaryDirectory
|
|
|
|
|
|
|
|
|
2013-09-07 07:43:10 +08:00
|
|
|
class TestController(object):
|
|
|
|
"""Run tests in a subprocess
|
2013-09-05 06:58:51 +08:00
|
|
|
"""
|
2013-09-06 06:37:30 +08:00
|
|
|
#: str, IPython test suite to be executed.
|
|
|
|
section = None
|
|
|
|
#: list, command line arguments to be executed
|
|
|
|
cmd = None
|
|
|
|
#: dict, extra environment variables to set for the subprocess
|
|
|
|
env = None
|
|
|
|
#: list, TemporaryDirectory instances to clear up when the process finishes
|
|
|
|
dirs = None
|
|
|
|
#: subprocess.Popen instance
|
|
|
|
process = None
|
2013-09-07 07:43:10 +08:00
|
|
|
#: str, process stdout+stderr
|
|
|
|
stdout = None
|
|
|
|
#: bool, whether to capture process stdout & stderr
|
2013-09-05 06:58:51 +08:00
|
|
|
buffer_output = False
|
|
|
|
|
2013-09-07 07:43:10 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.cmd = []
|
2013-09-06 06:37:30 +08:00
|
|
|
self.env = {}
|
|
|
|
self.dirs = []
|
|
|
|
|
2013-09-07 07:30:30 +08:00
|
|
|
@property
|
|
|
|
def will_run(self):
|
2013-09-07 07:43:10 +08:00
|
|
|
"""Override in subclasses to check for dependencies."""
|
|
|
|
return False
|
2013-09-06 06:37:30 +08:00
|
|
|
|
|
|
|
def launch(self):
|
|
|
|
# print('*** ENV:', self.env) # dbg
|
|
|
|
# print('*** CMD:', self.cmd) # dbg
|
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(self.env)
|
|
|
|
output = subprocess.PIPE if self.buffer_output else None
|
2013-09-06 07:24:36 +08:00
|
|
|
stdout = subprocess.STDOUT if self.buffer_output else None
|
2013-09-06 06:37:30 +08:00
|
|
|
self.process = subprocess.Popen(self.cmd, stdout=output,
|
2013-09-06 07:24:36 +08:00
|
|
|
stderr=stdout, env=env)
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-06 07:24:36 +08:00
|
|
|
def wait(self):
|
|
|
|
self.stdout, _ = self.process.communicate()
|
|
|
|
return self.process.returncode
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-06 06:44:31 +08:00
|
|
|
def cleanup_process(self):
|
2013-09-05 06:58:51 +08:00
|
|
|
"""Cleanup on exit by killing any leftover processes."""
|
2013-09-06 06:37:30 +08:00
|
|
|
subp = self.process
|
|
|
|
if subp is None or (subp.poll() is not None):
|
|
|
|
return # Process doesn't exist, or is already dead.
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
try:
|
|
|
|
print('Cleaning up stale PID: %d' % subp.pid)
|
|
|
|
subp.kill()
|
|
|
|
except: # (OSError, WindowsError) ?
|
|
|
|
# This is just a best effort, if we fail or the process was
|
|
|
|
# really gone, ignore it.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for i in range(10):
|
|
|
|
if subp.poll() is None:
|
|
|
|
time.sleep(0.1)
|
|
|
|
else:
|
|
|
|
break
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
if subp.poll() is None:
|
|
|
|
# The process did not die...
|
|
|
|
print('... failed. Manual cleanup may be required.')
|
2013-09-06 06:44:31 +08:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"Kill process if it's still alive, and clean up temporary directories"
|
|
|
|
self.cleanup_process()
|
2013-09-06 06:37:30 +08:00
|
|
|
for td in self.dirs:
|
|
|
|
td.cleanup()
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
__del__ = cleanup
|
|
|
|
|
2013-09-07 07:43:10 +08:00
|
|
|
class PyTestController(TestController):
|
|
|
|
"""Run Python tests using IPython.testing.iptest"""
|
|
|
|
#: str, Python command to execute in subprocess
|
|
|
|
pycmd = None
|
|
|
|
|
|
|
|
def __init__(self, section):
|
|
|
|
"""Create new test runner."""
|
|
|
|
TestController.__init__(self)
|
|
|
|
self.section = section
|
|
|
|
# pycmd is put into cmd[2] in PyTestController.launch()
|
|
|
|
self.cmd = [sys.executable, '-c', None, section]
|
|
|
|
self.pycmd = "from IPython.testing.iptest import run_iptest; run_iptest()"
|
|
|
|
ipydir = TemporaryDirectory()
|
|
|
|
self.dirs.append(ipydir)
|
|
|
|
self.env['IPYTHONDIR'] = ipydir.name
|
|
|
|
self.workingdir = workingdir = TemporaryDirectory()
|
|
|
|
self.dirs.append(workingdir)
|
|
|
|
self.env['IPTEST_WORKING_DIR'] = workingdir.name
|
|
|
|
# This means we won't get odd effects from our own matplotlib config
|
|
|
|
self.env['MPLCONFIGDIR'] = workingdir.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def will_run(self):
|
2013-09-20 06:07:11 +08:00
|
|
|
try:
|
|
|
|
return test_sections[self.section].will_run
|
|
|
|
except KeyError:
|
|
|
|
return True
|
2013-09-07 07:43:10 +08:00
|
|
|
|
|
|
|
def add_xunit(self):
|
|
|
|
xunit_file = os.path.abspath(self.section + '.xunit.xml')
|
|
|
|
self.cmd.extend(['--with-xunit', '--xunit-file', xunit_file])
|
|
|
|
|
|
|
|
def add_coverage(self):
|
2013-09-20 06:07:11 +08:00
|
|
|
try:
|
|
|
|
sources = test_sections[self.section].includes
|
|
|
|
except KeyError:
|
|
|
|
sources = ['IPython']
|
|
|
|
|
2013-09-07 07:43:10 +08:00
|
|
|
coverage_rc = ("[run]\n"
|
|
|
|
"data_file = {data_file}\n"
|
|
|
|
"source =\n"
|
|
|
|
" {source}\n"
|
|
|
|
).format(data_file=os.path.abspath('.coverage.'+self.section),
|
2013-09-20 06:07:11 +08:00
|
|
|
source="\n ".join(sources))
|
2013-09-07 07:43:10 +08:00
|
|
|
config_file = os.path.join(self.workingdir.name, '.coveragerc')
|
|
|
|
with open(config_file, 'w') as f:
|
|
|
|
f.write(coverage_rc)
|
|
|
|
|
|
|
|
self.env['COVERAGE_PROCESS_START'] = config_file
|
|
|
|
self.pycmd = "import coverage; coverage.process_startup(); " + self.pycmd
|
|
|
|
|
|
|
|
def launch(self):
|
|
|
|
self.cmd[2] = self.pycmd
|
|
|
|
super(PyTestController, self).launch()
|
|
|
|
|
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
def prepare_py_test_controllers(inc_slow=False):
|
2013-09-07 07:43:10 +08:00
|
|
|
"""Returns an ordered list of PyTestController instances to be run."""
|
2013-09-07 07:30:30 +08:00
|
|
|
to_run, not_run = [], []
|
2013-09-06 06:37:30 +08:00
|
|
|
if not inc_slow:
|
|
|
|
test_sections['parallel'].enabled = False
|
2013-09-06 07:24:36 +08:00
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
for name in test_group_names:
|
2013-09-07 07:43:10 +08:00
|
|
|
controller = PyTestController(name)
|
2013-09-07 07:30:30 +08:00
|
|
|
if controller.will_run:
|
|
|
|
to_run.append(controller)
|
|
|
|
else:
|
|
|
|
not_run.append(controller)
|
|
|
|
return to_run, not_run
|
2013-09-06 06:37:30 +08:00
|
|
|
|
2013-09-21 07:55:09 +08:00
|
|
|
def configure_controllers(controllers, xunit=False, coverage=False, extra_args=()):
|
2013-09-20 06:07:11 +08:00
|
|
|
"""Apply options for a collection of TestController objects."""
|
|
|
|
for controller in controllers:
|
|
|
|
if xunit:
|
|
|
|
controller.add_xunit()
|
|
|
|
if coverage:
|
|
|
|
controller.add_coverage()
|
2013-09-21 07:55:09 +08:00
|
|
|
controller.cmd.extend(extra_args)
|
2013-09-20 06:07:11 +08:00
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
def do_run(controller):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
controller.launch()
|
|
|
|
except Exception:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
return controller, 1 # signal failure
|
|
|
|
|
2013-09-06 07:24:36 +08:00
|
|
|
exitcode = controller.wait()
|
2013-09-06 06:37:30 +08:00
|
|
|
return controller, exitcode
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return controller, -signal.SIGINT
|
2013-09-06 07:24:36 +08:00
|
|
|
finally:
|
|
|
|
controller.cleanup()
|
2013-09-05 06:58:51 +08:00
|
|
|
|
|
|
|
def report():
|
|
|
|
"""Return a string with a summary report of test-related variables."""
|
|
|
|
|
2013-10-15 07:19:36 +08:00
|
|
|
out = [ brief_sys_info(), '\n']
|
2013-09-05 06:58:51 +08:00
|
|
|
|
|
|
|
avail = []
|
|
|
|
not_avail = []
|
|
|
|
|
|
|
|
for k, is_avail in have.items():
|
|
|
|
if is_avail:
|
|
|
|
avail.append(k)
|
|
|
|
else:
|
|
|
|
not_avail.append(k)
|
|
|
|
|
|
|
|
if avail:
|
|
|
|
out.append('\nTools and libraries available at test time:\n')
|
|
|
|
avail.sort()
|
|
|
|
out.append(' ' + ' '.join(avail)+'\n')
|
|
|
|
|
|
|
|
if not_avail:
|
|
|
|
out.append('\nTools and libraries NOT available at test time:\n')
|
|
|
|
not_avail.sort()
|
|
|
|
out.append(' ' + ' '.join(not_avail)+'\n')
|
|
|
|
|
|
|
|
return ''.join(out)
|
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
def run_iptestall(options):
|
2013-09-05 06:58:51 +08:00
|
|
|
"""Run the entire IPython test suite by calling nose and trial.
|
|
|
|
|
|
|
|
This function constructs :class:`IPTester` instances for all IPython
|
|
|
|
modules and package and then runs each of them. This causes the modules
|
|
|
|
and packages of IPython to be tested each in their own subprocess using
|
|
|
|
nose.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2013-09-20 06:07:11 +08:00
|
|
|
|
|
|
|
All parameters are passed as attributes of the options object.
|
|
|
|
|
|
|
|
testgroups : list of str
|
|
|
|
Run only these sections of the test suite. If empty, run all the available
|
|
|
|
sections.
|
|
|
|
|
|
|
|
fast : int or None
|
|
|
|
Run the test suite in parallel, using n simultaneous processes. If None
|
|
|
|
is passed, one process is used per CPU core. Default 1 (i.e. sequential)
|
|
|
|
|
|
|
|
inc_slow : bool
|
2013-09-05 06:58:51 +08:00
|
|
|
Include slow tests, like IPython.parallel. By default, these tests aren't
|
|
|
|
run.
|
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
xunit : bool
|
|
|
|
Produce Xunit XML output. This is written to multiple foo.xunit.xml files.
|
|
|
|
|
|
|
|
coverage : bool or str
|
|
|
|
Measure code coverage from tests. True will store the raw coverage data,
|
|
|
|
or pass 'html' or 'xml' to get reports.
|
2013-09-21 07:55:09 +08:00
|
|
|
|
|
|
|
extra_args : list
|
|
|
|
Extra arguments to pass to the test subprocesses, e.g. '-v'
|
2013-09-05 06:58:51 +08:00
|
|
|
"""
|
2013-09-20 06:07:11 +08:00
|
|
|
if options.fast != 1:
|
|
|
|
# If running in parallel, capture output so it doesn't get interleaved
|
2013-09-07 07:43:10 +08:00
|
|
|
TestController.buffer_output = True
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
if options.testgroups:
|
|
|
|
to_run = [PyTestController(name) for name in options.testgroups]
|
|
|
|
not_run = []
|
|
|
|
else:
|
|
|
|
to_run, not_run = prepare_py_test_controllers(inc_slow=options.all)
|
|
|
|
|
2013-09-21 07:55:09 +08:00
|
|
|
configure_controllers(to_run, xunit=options.xunit, coverage=options.coverage,
|
|
|
|
extra_args=options.extra_args)
|
2013-09-05 06:58:51 +08:00
|
|
|
|
2013-09-07 07:30:30 +08:00
|
|
|
def justify(ltext, rtext, width=70, fill='-'):
|
|
|
|
ltext += ' '
|
|
|
|
rtext = (' ' + rtext).rjust(width - len(ltext), fill)
|
|
|
|
return ltext + rtext
|
|
|
|
|
2013-09-05 06:58:51 +08:00
|
|
|
# Run all test runners, tracking execution time
|
|
|
|
failed = []
|
|
|
|
t_start = time.time()
|
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
print()
|
|
|
|
if options.fast == 1:
|
|
|
|
# This actually means sequential, i.e. with 1 job
|
2013-09-07 07:30:30 +08:00
|
|
|
for controller in to_run:
|
2013-09-06 07:24:36 +08:00
|
|
|
print('IPython test group:', controller.section)
|
2013-09-25 04:19:26 +08:00
|
|
|
sys.stdout.flush() # Show in correct order when output is piped
|
2013-09-06 07:24:36 +08:00
|
|
|
controller, res = do_run(controller)
|
|
|
|
if res:
|
|
|
|
failed.append(controller)
|
|
|
|
if res == -signal.SIGINT:
|
|
|
|
print("Interrupted")
|
|
|
|
break
|
|
|
|
print()
|
|
|
|
|
|
|
|
else:
|
2013-09-20 06:07:11 +08:00
|
|
|
# Run tests concurrently
|
2013-09-06 07:24:36 +08:00
|
|
|
try:
|
2013-09-20 06:07:11 +08:00
|
|
|
pool = multiprocessing.pool.ThreadPool(options.fast)
|
2013-09-07 07:30:30 +08:00
|
|
|
for (controller, res) in pool.imap_unordered(do_run, to_run):
|
|
|
|
res_string = 'OK' if res == 0 else 'FAILED'
|
|
|
|
print(justify('IPython test group: ' + controller.section, res_string))
|
2013-09-06 07:24:36 +08:00
|
|
|
if res:
|
2013-09-07 07:54:06 +08:00
|
|
|
print(bytes_to_str(controller.stdout))
|
2013-09-06 07:24:36 +08:00
|
|
|
failed.append(controller)
|
|
|
|
if res == -signal.SIGINT:
|
|
|
|
print("Interrupted")
|
|
|
|
break
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
2013-09-06 06:37:30 +08:00
|
|
|
|
2013-09-07 07:30:30 +08:00
|
|
|
for controller in not_run:
|
|
|
|
print(justify('IPython test group: ' + controller.section, 'NOT RUN'))
|
|
|
|
|
2013-09-05 06:58:51 +08:00
|
|
|
t_end = time.time()
|
|
|
|
t_tests = t_end - t_start
|
2013-09-07 07:30:30 +08:00
|
|
|
nrunners = len(to_run)
|
2013-09-05 06:58:51 +08:00
|
|
|
nfail = len(failed)
|
|
|
|
# summarize results
|
2013-09-20 06:07:11 +08:00
|
|
|
print('_'*70)
|
2013-09-05 06:58:51 +08:00
|
|
|
print('Test suite completed for system with the following information:')
|
|
|
|
print(report())
|
2013-10-15 07:19:36 +08:00
|
|
|
took = "Took %.3fs." % t_tests
|
2013-09-20 06:07:11 +08:00
|
|
|
print('Status: ', end='')
|
2013-09-05 06:58:51 +08:00
|
|
|
if not failed:
|
2013-10-15 07:22:19 +08:00
|
|
|
print('OK (%d test groups).' % nrunners, took)
|
2013-09-05 06:58:51 +08:00
|
|
|
else:
|
|
|
|
# If anything went wrong, point out what command to rerun manually to
|
|
|
|
# see the actual errors and individual summary
|
2013-09-20 06:07:11 +08:00
|
|
|
failed_sections = [c.section for c in failed]
|
|
|
|
print('ERROR - {} out of {} test groups failed ({}).'.format(nfail,
|
2013-10-15 07:19:36 +08:00
|
|
|
nrunners, ', '.join(failed_sections)), took)
|
2013-09-20 06:07:11 +08:00
|
|
|
print()
|
|
|
|
print('You may wish to rerun these, with:')
|
|
|
|
print(' iptest', *failed_sections)
|
|
|
|
print()
|
|
|
|
|
|
|
|
if options.coverage:
|
2013-09-07 02:02:41 +08:00
|
|
|
from coverage import coverage
|
|
|
|
cov = coverage(data_file='.coverage')
|
|
|
|
cov.combine()
|
|
|
|
cov.save()
|
|
|
|
|
|
|
|
# Coverage HTML report
|
2013-09-20 06:07:11 +08:00
|
|
|
if options.coverage == 'html':
|
2013-09-07 02:02:41 +08:00
|
|
|
html_dir = 'ipy_htmlcov'
|
|
|
|
shutil.rmtree(html_dir, ignore_errors=True)
|
|
|
|
print("Writing HTML coverage report to %s/ ... " % html_dir, end="")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
# Custom HTML reporter to clean up module names.
|
|
|
|
from coverage.html import HtmlReporter
|
|
|
|
class CustomHtmlReporter(HtmlReporter):
|
|
|
|
def find_code_units(self, morfs):
|
|
|
|
super(CustomHtmlReporter, self).find_code_units(morfs)
|
|
|
|
for cu in self.code_units:
|
|
|
|
nameparts = cu.name.split(os.sep)
|
|
|
|
if 'IPython' not in nameparts:
|
|
|
|
continue
|
|
|
|
ix = nameparts.index('IPython')
|
|
|
|
cu.name = '.'.join(nameparts[ix:])
|
|
|
|
|
|
|
|
# Reimplement the html_report method with our custom reporter
|
|
|
|
cov._harvest_data()
|
|
|
|
cov.config.from_args(omit='*%stests' % os.sep, html_dir=html_dir,
|
|
|
|
html_title='IPython test coverage',
|
|
|
|
)
|
|
|
|
reporter = CustomHtmlReporter(cov, cov.config)
|
|
|
|
reporter.report(None)
|
|
|
|
print('done.')
|
|
|
|
|
|
|
|
# Coverage XML report
|
2013-09-20 06:07:11 +08:00
|
|
|
elif options.coverage == 'xml':
|
2013-09-07 02:02:41 +08:00
|
|
|
cov.xml_report(outfile='ipy_coverage.xml')
|
|
|
|
|
|
|
|
if failed:
|
2013-09-05 06:58:51 +08:00
|
|
|
# Ensure that our exit code indicates failure
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-09-21 07:55:09 +08:00
|
|
|
# Arguments after -- should be passed through to nose. Argparse treats
|
|
|
|
# everything after -- as regular positional arguments, so we separate them
|
|
|
|
# first.
|
|
|
|
try:
|
|
|
|
ix = sys.argv.index('--')
|
|
|
|
except ValueError:
|
|
|
|
to_parse = sys.argv[1:]
|
|
|
|
extra_args = []
|
|
|
|
else:
|
|
|
|
to_parse = sys.argv[1:ix]
|
|
|
|
extra_args = sys.argv[ix+1:]
|
|
|
|
|
2013-09-06 06:37:30 +08:00
|
|
|
parser = argparse.ArgumentParser(description='Run IPython test suite')
|
2013-09-20 06:07:11 +08:00
|
|
|
parser.add_argument('testgroups', nargs='*',
|
|
|
|
help='Run specified groups of tests. If omitted, run '
|
|
|
|
'all tests.')
|
2013-09-06 06:37:30 +08:00
|
|
|
parser.add_argument('--all', action='store_true',
|
|
|
|
help='Include slow tests not run by default.')
|
2013-09-20 06:12:50 +08:00
|
|
|
parser.add_argument('-j', '--fast', nargs='?', const=None, default=1, type=int,
|
2013-09-06 06:37:30 +08:00
|
|
|
help='Run test sections in parallel.')
|
|
|
|
parser.add_argument('--xunit', action='store_true',
|
|
|
|
help='Produce Xunit XML results')
|
2013-09-07 02:02:41 +08:00
|
|
|
parser.add_argument('--coverage', nargs='?', const=True, default=False,
|
|
|
|
help="Measure test coverage. Specify 'html' or "
|
|
|
|
"'xml' to get reports.")
|
2013-09-06 06:37:30 +08:00
|
|
|
|
2013-09-21 07:55:09 +08:00
|
|
|
options = parser.parse_args(to_parse)
|
|
|
|
options.extra_args = extra_args
|
2013-09-06 06:37:30 +08:00
|
|
|
|
2013-09-20 06:07:11 +08:00
|
|
|
run_iptestall(options)
|
2013-09-05 06:58:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|