summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-17 20:28:09 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-17 20:28:09 +0200
commit3de7b673a08d3931beb03af28d471b984f411d23 (patch)
tree2220d3320686a0d817983b6c7f53ecf29ae1390f
parentc630eff0991116c036723a80531e85d8d599e6c8 (diff)
downloadpsutil-testing-envvar.tar.gz
Introduce PSUTIL_TESTING env varpsutil-testing-envvar
...so that we can make stricter assertions in C and py code during tests only. * define a C function in _common.c which returns whether the var is set * set PSUTIL_TESTING from the Makefile
-rw-r--r--Makefile24
-rw-r--r--psutil/_psutil_bsd.c6
-rw-r--r--psutil/_psutil_common.c27
-rw-r--r--psutil/_psutil_common.h4
-rw-r--r--psutil/_psutil_linux.c4
-rw-r--r--psutil/_psutil_osx.c5
-rw-r--r--psutil/_psutil_sunos.c5
-rw-r--r--psutil/_psutil_windows.c5
-rw-r--r--psutil/tests/__init__.py34
-rwxr-xr-xpsutil/tests/__main__.py21
-rwxr-xr-xpsutil/tests/test_process.py2
11 files changed, 97 insertions, 40 deletions
diff --git a/Makefile b/Makefile
index 8b40d8c2..c44c6c02 100644
--- a/Makefile
+++ b/Makefile
@@ -118,65 +118,65 @@ setup-dev-env:
# Run all tests.
test:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) $(TSCRIPT)
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) $(TSCRIPT)
# Run process-related API tests.
test-process:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) -m unittest -v psutil.tests.test_process
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) -m unittest -v psutil.tests.test_process
# Run system-related API tests.
test-system:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) -m unittest -v psutil.tests.test_system
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) -m unittest -v psutil.tests.test_system
# Run miscellaneous tests.
test-misc:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_misc.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_misc.py
# Test APIs dealing with strings.
test-unicode:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_unicode.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_unicode.py
# APIs sanity tests.
test-contracts:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_contracts.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_contracts.py
# Test net_connections() and Process.connections().
test-connections:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_connections.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_connections.py
# POSIX specific tests.
test-posix:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_posix.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_posix.py
# Run specific platform tests only.
test-platform:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS") if getattr(psutil, x)][0])'`.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS") if getattr(psutil, x)][0])'`.py
# Memory leak tests.
test-memleaks:
${MAKE} install
- PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_memory_leaks.py
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) psutil/tests/test_memory_leaks.py
# Run a specific test by name, e.g.
# make test-by-name psutil.tests.test_system.TestSystemAPIs.test_cpu_times
test-by-name:
${MAKE} install
- @PYTHONWARNINGS=all $(PYTHON) -m unittest -v $(ARGS)
+ @PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) -m unittest -v $(ARGS)
# Run test coverage.
coverage:
${MAKE} install
# Note: coverage options are controlled by .coveragerc file
rm -rf .coverage htmlcov
- PYTHONWARNINGS=all $(PYTHON) -m coverage run $(TSCRIPT)
+ PSUTIL_TESTING=1 PYTHONWARNINGS=all $(PYTHON) -m coverage run $(TSCRIPT)
$(PYTHON) -m coverage report
@echo "writing results to htmlcov/index.html"
$(PYTHON) -m coverage html
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index 217a95de..3527b666 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -908,7 +908,6 @@ error:
*/
static PyMethodDef
PsutilMethods[] = {
-
// --- per-process functions
{"proc_oneshot_info", psutil_proc_oneshot_info, METH_VARARGS,
@@ -983,6 +982,11 @@ PsutilMethods[] = {
{"sensors_battery", psutil_sensors_battery, METH_VARARGS,
"Return battery information."},
#endif
+
+ // --- others
+ {"py_psutil_testing", py_psutil_testing, METH_VARARGS,
+ "Return True if PSUTIL_TESTING env var is set"},
+
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index bcbd623b..37cc2f16 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -7,6 +7,7 @@
*/
#include <Python.h>
+#include <stdio.h>
/*
* Set OSError(errno=ESRCH, strerror="No such process") Python exception.
@@ -37,6 +38,30 @@ AccessDenied(void) {
/*
+ * Return 1 if PSUTIL_TESTING env var is set else 0.
+ */
+int
+psutil_testing(void) {
+ if (getenv("PSUTIL_TESTING") != NULL)
+ return 1;
+ else
+ return 0;
+}
+
+
+/*
+ * Return True if PSUTIL_TESTING env var is set else False.
+ */
+PyObject *
+py_psutil_testing(PyObject *self, PyObject *args) {
+ PyObject *res;
+ res = psutil_testing() ? Py_True : Py_False;
+ Py_INCREF(res);
+ return res;
+}
+
+
+/*
* Backport of unicode FS APIs from Python 3.
* On Python 2 we just return a plain byte string
* which is never supposed to raise decoding errors.
@@ -52,4 +77,4 @@ PyObject *
PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size) {
return PyString_FromStringAndSize(s, size);
}
-#endif \ No newline at end of file
+#endif
diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h
index aa634ad3..13404532 100644
--- a/psutil/_psutil_common.h
+++ b/psutil/_psutil_common.h
@@ -11,7 +11,9 @@ static const int PSUTIL_CONN_NONE = 128;
PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
+int psutil_testing(void);
+PyObject* py_psutil_testing(PyObject *self, PyObject *args);
#if PY_MAJOR_VERSION < 3
PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
-#endif \ No newline at end of file
+#endif
diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c
index 1a96fea0..a15ebe5c 100644
--- a/psutil/_psutil_linux.c
+++ b/psutil/_psutil_linux.c
@@ -574,7 +574,6 @@ error:
*/
static PyMethodDef
PsutilMethods[] = {
-
// --- per-process functions
#if PSUTIL_HAVE_IOPRIO
@@ -607,6 +606,9 @@ PsutilMethods[] = {
"Get or set process resource limits."},
#endif
+ // --- others
+ {"py_psutil_testing", py_psutil_testing, METH_VARARGS,
+ "Return True if PSUTIL_TESTING env var is set"},
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index 20ece694..7d762a1c 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -1779,7 +1779,6 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
*/
static PyMethodDef
PsutilMethods[] = {
-
// --- per-process functions
{"proc_kinfo_oneshot", psutil_proc_kinfo_oneshot, METH_VARARGS,
@@ -1841,6 +1840,10 @@ PsutilMethods[] = {
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},
+ // --- others
+ {"py_psutil_testing", py_psutil_testing, METH_VARARGS,
+ "Return True if PSUTIL_TESTING env var is set"},
+
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c
index 6c152eed..ddfcbea8 100644
--- a/psutil/_psutil_sunos.c
+++ b/psutil/_psutil_sunos.c
@@ -1447,7 +1447,6 @@ error:
*/
static PyMethodDef
PsutilMethods[] = {
-
// --- process-related functions
{"proc_basic_info", psutil_proc_basic_info, METH_VARARGS,
"Return process ppid, rss, vms, ctime, nice, nthreads, status and tty"},
@@ -1490,6 +1489,10 @@ PsutilMethods[] = {
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},
+ // --- others
+ {"py_psutil_testing", py_psutil_testing, METH_VARARGS,
+ "Return True if PSUTIL_TESTING env var is set"},
+
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 436dd76b..795ee9cd 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -3479,7 +3479,6 @@ psutil_sensors_battery(PyObject *self, PyObject *args) {
static PyMethodDef
PsutilMethods[] = {
-
// --- per-process functions
{"proc_cmdline", psutil_proc_cmdline, METH_VARARGS,
@@ -3602,6 +3601,10 @@ PsutilMethods[] = {
{"win32_QueryDosDevice", psutil_win32_QueryDosDevice, METH_VARARGS,
"QueryDosDevice binding"},
+ // --- others
+ {"py_psutil_testing", py_psutil_testing, METH_VARARGS,
+ "Return True if PSUTIL_TESTING env var is set"},
+
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index f14b1c98..6f2dd030 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -77,7 +77,8 @@ __all__ = [
'ThreadTask'
# test utils
'unittest', 'skip_on_access_denied', 'skip_on_not_implemented',
- 'retry_before_failing', 'run_test_module_by_name',
+ 'retry_before_failing', 'run_test_module_by_name', 'get_suite',
+ 'run_suite',
# install utils
'install_pip', 'install_test_deps',
# fs utils
@@ -141,6 +142,7 @@ ASCII_FS = sys.getfilesystemencoding().lower() in ('ascii', 'us-ascii')
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts')
+HERE = os.path.abspath(os.path.dirname(__file__))
# --- support
@@ -699,9 +701,39 @@ class TestCase(unittest.TestCase):
unittest.TestCase = TestCase
+def _setup_tests():
+ c_testing = psutil._psplatform.cext.py_psutil_testing
+ if 'PSUTIL_TESTING' in os.environ:
+ assert c_testing()
+ else:
+ assert not c_testing()
+ os.environ['PSUTIL_TESTING'] = '1'
+ assert c_testing()
+
+
+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():
+ _setup_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_')]
+ _setup_tests()
name = os.path.splitext(os.path.basename(name))[0]
suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(name))
diff --git a/psutil/tests/__main__.py b/psutil/tests/__main__.py
index 896b00cc..475e6b81 100755
--- a/psutil/tests/__main__.py
+++ b/psutil/tests/__main__.py
@@ -21,8 +21,7 @@ try:
except ImportError:
from urllib2 import urlopen
-from psutil.tests import unittest
-from psutil.tests import VERBOSITY
+from psutil.tests import run_suite
HERE = os.path.abspath(os.path.dirname(__file__))
@@ -73,24 +72,6 @@ def install_test_deps(deps=None):
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():
usage = "%s -m psutil.tests [opts]" % PYTHON
parser = optparse.OptionParser(usage=usage, description="run unit tests")
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index b1f2508f..3410ec0b 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -1391,6 +1391,8 @@ class TestProcess(unittest.TestCase):
d2 = os.environ.copy()
removes = []
+ if 'PSUTIL_TESTING' in os.environ:
+ removes.append('PSUTIL_TESTING')
if OSX:
removes.extend([
"__CF_USER_TEXT_ENCODING",