Skip to content

Commit

Permalink
#655: provide preliminary unicode tests for all process APIs returnin…
Browse files Browse the repository at this point in the history
…g strings (except username)
  • Loading branch information
giampaolo committed Aug 1, 2015
1 parent 919282c commit 21da6e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
55 changes: 55 additions & 0 deletions test/_windows.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,14 +10,18 @@
import errno
import os
import platform
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import traceback

from psutil._compat import u
from test_psutil import APPVEYOR, WINDOWS
from test_psutil import get_test_subprocess, reap_children, unittest
from test_psutil import safe_remove, safe_rmdir, chdir

import mock
try:
Expand Down Expand Up @@ -452,13 +457,63 @@ def test_zombies(self):
self.assertRaises(psutil.NoSuchProcess, meth, ZOMBIE_PID)


class TestUnicode(unittest.TestCase):

@classmethod
def setUpClass(cls):
with tempfile.NamedTemporaryFile() as f:
tdir = os.path.dirname(f.name)
cls.uexe = os.path.join(tdir, "psutil-è.exe")

def setUp(self):
safe_remove(self.uexe)
reap_children()

tearDown = setUp

def test_proc_exe(self):
shutil.copyfile(sys.executable, self.uexe)
subp = get_test_subprocess(cmd=[self.uexe])
p = psutil.Process(subp.pid)
self.assertEqual(os.path.basename(p.name()), u("psutil-è.exe"))

def test_proc_name(self):
shutil.copyfile(sys.executable, self.uexe)
subp = get_test_subprocess(cmd=[self.uexe])
self.assertEqual(psutil._psplatform.cext.proc_name(subp.pid),
u("psutil-è.exe"))

def test_proc_cmdline(self):
shutil.copyfile(sys.executable, self.uexe)
subp = get_test_subprocess(cmd=[self.uexe])
p = psutil.Process(subp.pid)
self.assertEqual(p.cmdline(), [self.uexe])

def test_proc_cwd(self):
tdir = tempfile.mkdtemp(prefix="psutil-è-")
self.addCleanup(safe_rmdir, tdir)
with chdir(tdir):
p = psutil.Process()
self.assertEqual(p.cwd(), tdir)

def test_proc_open_files(self):
p = psutil.Process()
start = set(p.open_files())
with open(self.uexe, 'w'):
new = set(p.open_files())
path = (new - start).pop().path
self.assertEqual(path, self.uexe)


def main():
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(WindowsSpecificTestCase))
test_suite.addTest(unittest.makeSuite(TestDualProcessImplementation))
test_suite.addTest(unittest.makeSuite(TestUnicode))
result = unittest.TextTestRunner(verbosity=2).run(test_suite)
return result.wasSuccessful()


if __name__ == '__main__':
if not main():
sys.exit(1)
12 changes: 11 additions & 1 deletion test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,17 @@ def safe_rmdir(dir):
raise


@contextlib.contextmanager
def chdir(dirname):
"""Context manager which temporarily changes the current directory."""
curdir = os.getcwd()
try:
os.chdir(dirname)
yield
finally:
os.chdir(curdir)


def call_until(fun, expr, timeout=GLOBAL_TIMEOUT):
"""Keep calling function for timeout secs and exit if eval()
expression is True.
Expand Down Expand Up @@ -2130,7 +2141,6 @@ def test_halfway_terminated_process(self):
sproc = get_test_subprocess()
p = psutil.Process(sproc.pid)
p.terminate()
p.wait()
if WINDOWS:
wait_for_pid(p.pid)
self.assertFalse(p.is_running())
Expand Down

0 comments on commit 21da6e7

Please sign in to comment.