diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 19:17:51 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 19:17:51 +0200 |
commit | d1500241a4ebb860d59c56e091200e4777b36b00 (patch) | |
tree | 9062389ac2475c981786fd6c81d8887053d20cef | |
parent | 5580428550896c66a9972682be0997035382d468 (diff) | |
download | psutil-d1500241a4ebb860d59c56e091200e4777b36b00.tar.gz |
refactor tests
-rwxr-xr-x | psutil/tests/test_misc.py | 3 | ||||
-rwxr-xr-x | psutil/tests/test_posix.py | 9 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 23 | ||||
-rwxr-xr-x | psutil/tests/test_windows.py | 5 | ||||
-rwxr-xr-x | scripts/internal/winmake.py | 7 |
5 files changed, 25 insertions, 22 deletions
diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 272253b6..6bc2e28c 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -37,6 +37,7 @@ from psutil.tests import create_proc_children_pair from psutil.tests import create_sockets from psutil.tests import get_free_port from psutil.tests import get_test_subprocess +from psutil.tests import HAS_BATTERY from psutil.tests import HAS_MEMORY_FULL_INFO from psutil.tests import HAS_MEMORY_MAPS from psutil.tests import HAS_SENSORS_BATTERY @@ -503,7 +504,7 @@ class TestScripts(unittest.TestCase): self.assert_stdout('fans.py') @unittest.skipIf(not HAS_SENSORS_BATTERY, "not supported") - @unittest.skipIf(TRAVIS, "not battery on TRAVIS") + @unittest.skipIf(not HAS_BATTERY, "no battery") def test_battery(self): self.assert_stdout('battery.py') diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index 819da0d2..3274c02c 100755 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -92,6 +92,15 @@ class TestProcess(unittest.TestCase): username_psutil = psutil.Process(self.pid).username() self.assertEqual(username_ps, username_psutil) + def test_username_no_resolution(self): + # Emulate a case where the system can't resolve the uid to + # a username in which case psutil is supposed to return + # the stringified uid. + p = psutil.Process() + with mock.patch("psutil.pwd.getpwuid", side_effect=KeyError) as fun: + self.assertEqual(p.username(), str(p.uids().real)) + assert fun.called + @skip_on_access_denied() @retry_before_failing() def test_rss_memory(self): diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 7bb83b63..d1cb9657 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -9,6 +9,7 @@ import collections import contextlib import errno +import getpass import os import select import signal @@ -836,22 +837,14 @@ class TestProcess(unittest.TestCase): def test_username(self): sproc = get_test_subprocess() p = psutil.Process(sproc.pid) - if POSIX: - import pwd - self.assertEqual(p.username(), pwd.getpwuid(os.getuid()).pw_name) - with mock.patch("psutil.pwd.getpwuid", - side_effect=KeyError) as fun: - self.assertEqual(p.username(), str(p.uids().real)) - assert fun.called - - elif WINDOWS and 'USERNAME' in os.environ: - expected_username = os.environ['USERNAME'] - expected_domain = os.environ['USERDOMAIN'] - domain, username = p.username().split('\\') - self.assertEqual(domain, expected_domain) - self.assertEqual(username, expected_username) + username = p.username() + if WINDOWS: + domain, username = username.split('\\') + self.assertEqual(username, getpass.getuser()) + if 'USERDOMAIN' in os.environ: + self.assertEqual(domain, os.environ['USERDOMAIN']) else: - p.username() + self.assertEqual(username, getpass.getuser()) def test_cwd(self): sproc = get_test_subprocess() diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py index 2a883132..2433849f 100755 --- a/psutil/tests/test_windows.py +++ b/psutil/tests/test_windows.py @@ -369,9 +369,8 @@ class TestProcess(unittest.TestCase): self.assertEqual(a, b) def test_username(self): - sys_value = win32api.GetUserName() - psutil_value = psutil.Process().username() - self.assertEqual(sys_value, psutil_value.split('\\')[1]) + self.assertEqual(psutil.Process().username(), + win32api.GetUserNameEx(win32con.NameSamCompatible)) def test_cmdline(self): sys_value = re.sub(' +', ' ', win32api.GetCommandLine()).strip() diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index 69d4d972..b8d111a4 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -73,8 +73,9 @@ def safe_print(text, file=sys.stdout, flush=False): file.write("\n") -def sh(cmd): - safe_print("cmd: " + cmd) +def sh(cmd, nolog=False): + if not nolog: + safe_print("cmd: " + cmd) code = os.system(cmd) if code: raise SystemExit @@ -320,7 +321,7 @@ def flake8(): py_files = py_files.decode() py_files = [x for x in py_files.split() if x.endswith('.py')] py_files = ' '.join(py_files) - sh("%s -m flake8 %s" % (PYTHON, py_files)) + sh("%s -m flake8 %s" % (PYTHON, py_files), nolog=True) @cmd |