diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-18 14:56:47 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-18 14:56:47 +0100 |
commit | f4734c80203023458cb05b1499db611ed4916af2 (patch) | |
tree | dae751369416df8231f19752d3c54bbb266795b7 | |
parent | 14aaec9353ee16b5cfeb4c1dc8673a173eff002d (diff) | |
download | psutil-f4734c80203023458cb05b1499db611ed4916af2.tar.gz |
imorove test coverage
-rw-r--r-- | psutil/_common.py | 22 | ||||
-rw-r--r-- | psutil/_psposix.py | 2 | ||||
-rw-r--r-- | psutil/tests/test_linux.py | 4 | ||||
-rw-r--r-- | psutil/tests/test_misc.py | 2 | ||||
-rw-r--r-- | psutil/tests/test_posix.py | 2 | ||||
-rw-r--r-- | psutil/tests/test_system.py | 13 |
6 files changed, 23 insertions, 22 deletions
diff --git a/psutil/_common.py b/psutil/_common.py index 92b3b5e1..13e6a828 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -187,6 +187,8 @@ def parse_environ_block(data): ret = {} pos = 0 + # localize global variable to speed up access. + WINDOWS_ = WINDOWS while True: next_pos = data.find("\0", pos) # nul byte at the beginning or double nul byte means finish @@ -198,7 +200,7 @@ def parse_environ_block(data): key = data[pos:equal_pos] value = data[equal_pos+1:next_pos] # Windows expects environment variables to be uppercase only - if os.name == "nt": + if WINDOWS_: key = key.upper() ret[key] = value pos = next_pos + 1 @@ -212,10 +214,11 @@ def sockfam_to_enum(num): """ if enum is None: return num - try: - return socket.AddressFamily(num) - except (ValueError, AttributeError): - return num + else: + try: + return socket.AddressFamily(num) + except (ValueError, AttributeError): + return num def socktype_to_enum(num): @@ -224,10 +227,11 @@ def socktype_to_enum(num): """ if enum is None: return num - try: - return socket.AddressType(num) - except (ValueError, AttributeError): - return num + else: + try: + return socket.AddressType(num) + except (ValueError, AttributeError): + return num def deprecated_method(replacement): diff --git a/psutil/_psposix.py b/psutil/_psposix.py index 02bbfcec..88aed919 100644 --- a/psutil/_psposix.py +++ b/psutil/_psposix.py @@ -113,7 +113,7 @@ def wait_pid(pid, timeout=None): return os.WEXITSTATUS(status) else: # should never happen - raise RuntimeError("unknown process exit status") + raise ValueError("unknown process exit status %r" % status) def disk_usage(path): diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index b96832b7..7bf753b2 100644 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -690,7 +690,9 @@ class TestMisc(unittest.TestCase): # Test SECTOR_SIZE fallback in case 'hw_sector_size' file # does not exist. def open_mock(name, *args, **kwargs): - if name.startswith(b"/sys/block/sda/queue/hw_sector_size"): + if PY3 and isinstance(name, bytes): + name = name.decode() + if name.startswith("/sys/block/sda/queue/hw_sector_size"): flag.append(None) raise IOError(errno.ENOENT, '') else: diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 1fad807b..77584022 100644 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -320,7 +320,7 @@ class TestMisc(unittest.TestCase): "psutil._psplatform.cext.version", return_value="0.0.0"): with self.assertRaises(ImportError) as cm: importlib.reload(psutil) - self.assertIn("version conflict", cm.exception.message.lower()) + self.assertIn("version conflict", str(cm.exception).lower()) finally: importlib.reload(psutil) diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index 6184ea6a..83125426 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. # Use of this source code is governed by a BSD-style license that can be @@ -275,6 +276,5 @@ class TestSystemAPIs(unittest.TestCase): self.assertTrue(u.name in users, u.name) self.assertTrue(u.terminal in terminals, u.terminal) - if __name__ == '__main__': run_test_module_by_name(__file__) diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index 48a2bed7..4f73e181 100644 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -419,15 +419,10 @@ class TestSystemAPIs(unittest.TestCase): "os.statvfs() function not available on this platform") def test_disk_usage_unicode(self): # see: https://github.com/giampaolo/psutil/issues/416 - # XXX this test is not really reliable as it always fails on - # Python 3.X (2.X is fine) - try: - safe_rmdir(TESTFN_UNICODE) - os.mkdir(TESTFN_UNICODE) - psutil.disk_usage(TESTFN_UNICODE) - safe_rmdir(TESTFN_UNICODE) - except UnicodeEncodeError: - pass + safe_rmdir(TESTFN_UNICODE) + self.addCleanup(safe_rmdir, TESTFN_UNICODE) + os.mkdir(TESTFN_UNICODE) + psutil.disk_usage(TESTFN_UNICODE) @unittest.skipIf(POSIX and not hasattr(os, 'statvfs'), "os.statvfs() function not available on this platform") |