author Gregory Szorc <>
Tue, 01 Jul 2014 19:53:35 -0700
changeset 358889 fbb4bd1952831fa6fa882c879e6a035fe3ff7c8e
parent 358852 27a1f27cb0acf1a234ed1d3b2599c879f9d9dba9
child 358986 824d08326fe7a676947cacefbbf8116d7e1fcee3
permissions -rwxr-xr-x
support code coverage during tests

#!/usr/bin/env python

# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

# This file is used to run all Mercurial-related tests in this repository.

import imp
import os
import sys

from coverage import coverage

# Mercurial's isn't meant to be loaded as a module. We do it
# anyway.
HERE = os.path.dirname(os.path.abspath(__file__))
RUNTESTS = os.path.join(HERE, 'pylib', 'mercurial-support', '')
EXTDIR = os.path.join(HERE, 'hgext')

sys.path.insert(0, os.path.join(HERE, 'pylib', 'mercurial-support'))
runtestsmod = imp.load_source('runtests', RUNTESTS)

def find_test_files():
    """Find all test files in this repository."""
    for d in os.listdir(EXTDIR):
        if d.startswith('.'):

        test_dir = os.path.join(EXTDIR, d, 'tests')
        if not os.path.isdir(test_dir):

        for f in os.listdir(test_dir):
            if f.startswith('.'):

            if f.startswith('test-') and f.endswith(('.py', '.t')):
                yield os.path.join(test_dir, f)

if __name__ == '__main__':
    if not hasattr(sys, 'real_prefix'):
        raise Exception('You are not running inside the virtualenv. Please '
                'create one and `pip install -r test-requirements.txt`')

    hg = os.path.join(os.path.dirname(sys.executable), 'hg')
    sys.argv.extend(['--with-hg', hg])

    coveragerc = os.path.join(HERE, '.coveragerc')
    coverdir = os.path.join(HERE, 'coverage')
    if not os.path.exists(coverdir):

    docoverage = '--cover' in sys.argv
    sys.argv = [a for a in sys.argv if a != '--cover']

    #'s coverage options don't work for us... yet. So, we hack
    # in code coverage manually.
    if docoverage:
        os.environ['COVERAGE_DIR'] = coverdir
        os.environ['CODE_COVERAGE'] = '1'

    runner = runtestsmod.TestRunner()

    res =[1:])

    if docoverage:
        cov = coverage(data_file=os.path.join(coverdir, 'coverage'))
        cov.html_report(directory='coverage/html', ignore_errors=True,
                os.path.join(HERE, 'venv', '*'),
                os.path.join(HERE, 'pylib', 'flake8', '*'),
                os.path.join(HERE, 'pylib', 'mccabe', '*'),
                os.path.join(HERE, 'pylib', 'mercurial-support', '*'),
                os.path.join(HERE, 'pylib', 'pep8', '*'),
                os.path.join(HERE, 'pylib', 'pyflakes', '*'),