Skip to content

Commit

Permalink
get rid of pip_install() code for py2; move everything in runner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 15, 2020
1 parent 3424a1a commit 766541f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 91 deletions.
4 changes: 2 additions & 2 deletions .ci/travis/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ python setup.py develop

# run tests (with coverage)
if [[ $PYVER == '2.7' ]] && [[ "$(uname -s)" != 'Darwin' ]]; then
PSUTIL_TESTING=1 python -Wa -m coverage run psutil/tests/__main__.py
PSUTIL_TESTING=1 python -Wa -m coverage run psutil/tests/runner.py
else
PSUTIL_TESTING=1 python -Wa psutil/tests/__main__.py
PSUTIL_TESTING=1 python -Wa psutil/tests/runner.py
fi

if [ "$PYVER" == "2.7" ] || [ "$PYVER" == "3.6" ]; then
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# You can set the variables below from the command line.

PYTHON = python3
TSCRIPT = psutil/tests/__main__.py
TSCRIPT = psutil/tests/runner.py
ARGS =
# List of nice-to-have dev libs.
DEPS = \
Expand Down Expand Up @@ -155,7 +155,7 @@ test-by-name: ## e.g. make test-by-name ARGS=psutil.tests.test_system.TestSyste

test-failed: ## Re-run tests which failed on last run
${MAKE} install
$(TEST_PREFIX) $(PYTHON) -c "import psutil.tests.runner as r; r.run(last_failed=True)"
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) --last-failed

test-coverage: ## Run test coverage.
${MAKE} install
Expand Down
2 changes: 1 addition & 1 deletion make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if "%PYTHON%" == "" (
)

if "%TSCRIPT%" == "" (
set TSCRIPT=psutil\tests\__main__.py
set TSCRIPT=psutil\tests\runner.py
)

rem Needed to locate the .pypirc file and upload exes on PyPI.
Expand Down
3 changes: 1 addition & 2 deletions psutil/tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ Instructions for running tests
* There are two ways of running tests. As a "user", if psutil is already
installed and you just want to test it works::

python -m psutil.tests --install-deps # install test deps
python -m psutil.tests

As a "developer", if you have a copy of the source code and you wish to hack
on psutil::

make setup-dev-env # install test deps (+ other things)
make setup-dev-env # install missing third-party deps
make test

* To run tests on all supported Python versions install tox
Expand Down
83 changes: 1 addition & 82 deletions psutil/tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,8 @@

"""
Run unit tests. This is invoked by:
$ python -m psutil.tests
"""

import contextlib
import optparse
import os
import sys
import tempfile
try:
from urllib.request import urlopen # py3
except ImportError:
from urllib2 import urlopen

from psutil.tests import PYTHON_EXE
from psutil.tests.runner import run


HERE = os.path.abspath(os.path.dirname(__file__))
GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py"
TEST_DEPS = []
if sys.version_info[:2] == (2, 6):
TEST_DEPS.extend(["ipaddress", "unittest2", "argparse", "mock==1.0.1"])
elif sys.version_info[:2] == (2, 7) or sys.version_info[:2] <= (3, 2):
TEST_DEPS.extend(["ipaddress", "mock"])


def install_pip():
try:
import pip # NOQA
except ImportError:
import ssl
f = tempfile.NamedTemporaryFile(suffix='.py')
with contextlib.closing(f):
print("downloading %s to %s" % (GET_PIP_URL, f.name))
if hasattr(ssl, '_create_unverified_context'):
ctx = ssl._create_unverified_context()
else:
ctx = None
kwargs = dict(context=ctx) if ctx else {}
req = urlopen(GET_PIP_URL, **kwargs)
data = req.read()
f.write(data)
f.flush()

print("installing pip")
code = os.system('%s %s --user' % (PYTHON_EXE, f.name))
return code


def install_test_deps(deps=None):
"""Install test dependencies via pip."""
if deps is None:
deps = TEST_DEPS
deps = set(deps)
if deps:
is_venv = hasattr(sys, 'real_prefix')
opts = "--user" if not is_venv else ""
install_pip()
code = os.system('%s -m pip install %s --upgrade %s' % (
PYTHON_EXE, opts, " ".join(deps)))
return code


def main():
usage = "%s -m psutil.tests [opts]" % PYTHON_EXE
parser = optparse.OptionParser(usage=usage, description="run unit tests")
parser.add_option("-i", "--install-deps",
action="store_true", default=False,
help="don't print status messages to stdout")

opts, args = parser.parse_args()
if opts.install_deps:
install_pip()
install_test_deps()
else:
for dep in TEST_DEPS:
try:
__import__(dep.split("==")[0])
except ImportError:
sys.exit("%r lib is not installed; run %s -m psutil.tests "
"--install-deps" % (dep, PYTHON_EXE))
run()


from .runner import main
main()
15 changes: 15 additions & 0 deletions psutil/tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

from __future__ import print_function
import optparse
import os
import sys
import unittest
Expand Down Expand Up @@ -145,3 +146,17 @@ def run(name=None, last_failed=False):
save_failed_tests(result)
success = result.wasSuccessful()
sys.exit(0 if success else 1)


def main():
usage = "python3 -m psutil.tests [opts]"
parser = optparse.OptionParser(usage=usage, description="run unit tests")
parser.add_option("--last-failed",
action="store_true", default=False,
help="only run last failed tests")
opts, args = parser.parse_args()
run(last_failed=opts.last_failed)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion scripts/internal/winmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
PYTHON = sys.executable
else:
PYTHON = os.getenv('PYTHON', sys.executable)
TEST_SCRIPT = 'psutil\\tests\\__main__.py'
TEST_SCRIPT = 'psutil\\tests\\runner.py'
GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py"
PY3 = sys.version_info[0] == 3
HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ deps =
setenv =
TOX = 1

commands = python psutil/tests/__main__.py
commands = python psutil/tests/runner.py

usedevelop = True

Expand Down

0 comments on commit 766541f

Please sign in to comment.