sapling/scripts/utils.py
Jun Wu 2698cf1d22 scripts: unify spwaning run-test logic
Summary:
Previously, lint.py and unit.py have different logic spawning
run-tests.py.

The logic in `unit.py` is more robust: it sets `PYTHONPATH` and
`cwd`. It sets `-j` according to CPU cores. It can find `run-tests.py`
even if `MERCURIALRUNTESTS` is not set. You can run the script
from any directory (not only reporoot).

Test Plan: Run `lint.py` and `unit.py`, from the `scripts` directory and reporoot.

Reviewers: #sourcecontrol, stash

Reviewed By: stash

Subscribers: stash, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D4095437

Signature: t1:4095437:1477663516:13a7ac4270435c272077132915f9d02cc98a5afb
2016-10-28 13:58:48 +01:00

59 lines
2.1 KiB
Python

import multiprocessing
import os
import subprocess
reporoot = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def getrunner():
"""return the path of run-tests.py. best-effort"""
runner = 'run-tests.py'
# Try MERCURIALRUNTEST
candidate = os.environ.get('MERCURIALRUNTEST')
if candidate and os.access(candidate, os.X_OK):
return candidate
# Search in PATH
for d in os.environ.get('PATH').split(os.path.pathsep):
candidate = os.path.abspath(os.path.join(d, runner))
if os.access(candidate, os.X_OK):
return candidate
# Search some common places for run-tests.py, as a nice default
# if we cannot find it otherwise.
for prefix in [os.path.dirname(reporoot), os.path.expanduser('~')]:
for hgrepo in ['hg', 'hg-crew', 'hg-committed']:
path = os.path.abspath(os.path.join(prefix, hgrepo,
'tests', runner))
if os.access(path, os.X_OK):
return path
return runner
def reporequires():
"""return a list of string, which are the requirements of the hg repo"""
requirespath = os.path.join(reporoot, '.hg', 'requires')
if os.path.exists(requirespath):
return [s.rstrip() for s in open(requirespath, 'r')]
return []
def spawnruntests(args, **kwds):
cpucount = multiprocessing.cpu_count()
cmd = [getrunner(), '-j%d' % cpucount]
# Enable lz4revlog extension for lz4revlog repo.
if 'lz4revlog' in reporequires():
cmd += ['--extra-config-opt=extensions.lz4revlog=']
# Include the repository root in PYTHONPATH so the unit tests will find
# the extensions from the local repository, rather than the versions
# already installed on the system.
env = os.environ.copy()
if 'PYTHONPATH' in env:
existing_pypath = [env['PYTHONPATH']]
else:
existing_pypath = []
env['PYTHONPATH'] = os.path.pathsep.join([reporoot] + existing_pypath)
# Spawn the run-tests.py process.
cmd += args
cwd = os.path.join(reporoot, 'tests')
proc = subprocess.Popen(cmd, cwd=cwd, env=env, **kwds)
return proc