diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-02 08:38:39 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-02 08:38:39 +0200 |
commit | a40c3187a491d22826a1cbcf576f94e00c9a7cc2 (patch) | |
tree | f7dd47b4f422fda8c0eab992deee4d5e1380d97e | |
parent | c3f4f8999158cf8a0ede96ff886c5b69adf46522 (diff) | |
download | psutil-a40c3187a491d22826a1cbcf576f94e00c9a7cc2.tar.gz |
move some functions out of the main test util module as they don't belong in there
-rw-r--r-- | psutil/tests/__init__.py | 87 | ||||
-rwxr-xr-x | psutil/tests/__main__.py | 76 |
2 files changed, 77 insertions, 86 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 3cd3398c..771a3cc3 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -20,7 +20,6 @@ import random import re import shutil import socket -import ssl import stat import subprocess import sys @@ -34,11 +33,6 @@ from socket import AF_INET6 from socket import SOCK_DGRAM from socket import SOCK_STREAM -try: - from urllib.request import urlopen # py3 -except ImportError: - from urllib2 import urlopen - import psutil from psutil import POSIX from psutil import WINDOWS @@ -82,7 +76,10 @@ __all__ = [ "HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT", "HAS_SENSORS_BATTERY", "HAS_BATTERY""HAS_SENSORS_FANS", "HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", - # classes + # subprocesses + 'pyrun', 'reap_children', 'get_test_subprocess', + 'create_proc_children_pair', + # threads 'ThreadTask' # test utils 'unittest', 'cleanup', 'skip_on_access_denied', 'skip_on_not_implemented', @@ -92,9 +89,6 @@ __all__ = [ # fs utils 'chdir', 'safe_rmpath', 'create_exe', 'decode_path', 'encode_path', 'unique_filename', - # subprocesses - 'pyrun', 'reap_children', 'get_test_subprocess', - 'create_proc_children_pair', # os 'get_winver', 'get_kernel_version', # sync primitives @@ -149,7 +143,6 @@ ASCII_FS = sys.getfilesystemencoding().lower() in ('ascii', 'us-ascii') # --- paths -HERE = os.path.abspath(os.path.dirname(__file__)) ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts') @@ -175,25 +168,16 @@ PYTHON = os.path.realpath(sys.executable) DEVNULL = open(os.devnull, 'r+') VALID_PROC_STATUSES = [getattr(psutil, x) for x in dir(psutil) if x.startswith('STATUS_')] -GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" AF_UNIX = getattr(socket, "AF_UNIX", object()) SOCK_SEQPACKET = getattr(socket, "SOCK_SEQPACKET", object()) -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"]) -elif sys.version_info[:2] == (3, 3): - TEST_DEPS.extend(["ipaddress"]) - _subprocesses_started = set() _pids_started = set() _testfiles_created = set() # =================================================================== -# --- classes +# --- threads # =================================================================== @@ -647,25 +631,6 @@ class TestCase(unittest.TestCase): unittest.TestCase = TestCase -def get_suite(): - testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE) - if x.endswith('.py') and x.startswith('test_') and not - x.startswith('test_memory_leaks')] - suite = unittest.TestSuite() - for tm in testmodules: - # ...so that the full test paths are printed on screen - tm = "psutil.tests.%s" % tm - suite.addTest(unittest.defaultTestLoader.loadTestsFromName(tm)) - return suite - - -def run_suite(): - """Run unit tests.""" - result = unittest.TextTestRunner(verbosity=VERBOSITY).run(get_suite()) - success = result.wasSuccessful() - sys.exit(0 if success else 1) - - def run_test_module_by_name(name): # testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE) # if x.endswith('.py') and x.startswith('test_')] @@ -736,48 +701,6 @@ atexit.register(lambda: DEVNULL.close()) # =================================================================== -# --- install -# =================================================================== - - -def install_pip(): - """Install pip. Returns the exit code of the subprocess.""" - try: - import pip # NOQA - except ImportError: - 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' % (sys.executable, 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' % ( - sys.executable, opts, " ".join(deps))) - return code - - -# =================================================================== # --- network # =================================================================== diff --git a/psutil/tests/__main__.py b/psutil/tests/__main__.py index b57914d4..896b00cc 100755 --- a/psutil/tests/__main__.py +++ b/psutil/tests/__main__.py @@ -10,17 +10,85 @@ Run unit tests. This is invoked by: $ python -m psutil.tests """ +import contextlib import optparse import os +import ssl import sys +import tempfile +try: + from urllib.request import urlopen # py3 +except ImportError: + from urllib2 import urlopen -from psutil.tests import install_pip -from psutil.tests import install_test_deps -from psutil.tests import run_suite -from psutil.tests import TEST_DEPS +from psutil.tests import unittest +from psutil.tests import VERBOSITY +HERE = os.path.abspath(os.path.dirname(__file__)) PYTHON = os.path.basename(sys.executable) +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"]) +elif sys.version_info[:2] == (3, 3): + TEST_DEPS.extend(["ipaddress"]) + + +def install_pip(): + try: + import pip # NOQA + except ImportError: + 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' % (sys.executable, 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' % ( + sys.executable, opts, " ".join(deps))) + return code + + +def get_suite(): + testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE) + if x.endswith('.py') and x.startswith('test_') and not + x.startswith('test_memory_leaks')] + suite = unittest.TestSuite() + for tm in testmodules: + # ...so that the full test paths are printed on screen + tm = "psutil.tests.%s" % tm + suite.addTest(unittest.defaultTestLoader.loadTestsFromName(tm)) + return suite + + +def run_suite(): + result = unittest.TextTestRunner(verbosity=VERBOSITY).run(get_suite()) + success = result.wasSuccessful() + sys.exit(0 if success else 1) def main(): |