diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-04-30 13:02:53 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-04-30 13:02:53 +0200 |
commit | a944dc086bbc150128f351b21226ccb8b5faa184 (patch) | |
tree | 2a299db7da363aa5409486da36c077abeeb0e855 | |
parent | 497f4a9dd8302ca64d328939d91afd3560189b30 (diff) | |
download | psutil-a944dc086bbc150128f351b21226ccb8b5faa184.tar.gz |
use get_testfn() from PsutilTestCase
-rw-r--r-- | psutil/tests/__init__.py | 4 | ||||
-rwxr-xr-x | psutil/tests/test_connections.py | 4 | ||||
-rwxr-xr-x | psutil/tests/test_linux.py | 10 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 18 | ||||
-rw-r--r--[-rwxr-xr-x] | psutil/tests/test_system.py | 3 | ||||
-rw-r--r--[-rwxr-xr-x] | psutil/tests/test_testutils.py | 21 | ||||
-rw-r--r--[-rwxr-xr-x] | psutil/tests/test_unicode.py | 16 |
7 files changed, 37 insertions, 39 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 3f387c51..79f95597 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -847,8 +847,8 @@ class PsutilTestCase(TestCase): """ def get_testfn(self, suffix="", dir=None): - fname = get_testfn(suffix=suffix, dir=suffix) - self.addCleanup(safe_rmpath(fname)) + fname = get_testfn(suffix=suffix, dir=dir) + self.addCleanup(safe_rmpath, fname) return fname def get_test_subprocess(self, *args, **kwds): diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index 5092b01d..42333a84 100755 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -310,7 +310,7 @@ class TestConnectedSocket(_ConnTestCase): @unittest.skipIf(not POSIX, 'POSIX only') def test_unix(self): - testfn = get_testfn() + testfn = self.get_testfn() server, client = unix_socketpair(testfn) try: cons = thisproc.connections(kind='unix') @@ -582,7 +582,7 @@ class TestSystemWideConnections(_ConnTestCase): times = 10 fnames = [] for i in range(times): - fname = get_testfn() + fname = self.get_testfn() fnames.append(fname) src = textwrap.dedent("""\ import time, os diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 3f200fc4..3e64b849 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -1216,7 +1216,7 @@ class TestMisc(PsutilTestCase): self.assertEqual(int(vmstat_value), int(psutil_value)) def test_no_procfs_on_import(self): - my_procfs = get_testfn() + my_procfs = self.get_testfn() os.mkdir(my_procfs) with open(os.path.join(my_procfs, 'stat'), 'w') as f: @@ -1345,7 +1345,7 @@ class TestMisc(PsutilTestCase): assert m.called def test_procfs_path(self): - tdir = get_testfn() + tdir = self.get_testfn() os.mkdir(tdir) try: psutil.PROCFS_PATH = tdir @@ -1644,7 +1644,7 @@ class TestProcess(PsutilTestCase): @retry_on_failure() def test_memory_full_info(self): - testfn = get_testfn() + testfn = self.get_testfn() src = textwrap.dedent(""" import time with open("%s", "w") as f: @@ -1713,7 +1713,7 @@ class TestProcess(PsutilTestCase): raise RuntimeError("timeout looking for test file") # - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, "w"): self.assertEqual(get_test_file(testfn).mode, "w") with open(testfn, "r"): @@ -2100,7 +2100,7 @@ class TestUtils(PsutilTestCase): assert m.called def test_cat(self): - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, "wt") as f: f.write("foo ") self.assertEqual(psutil._psplatform.cat(testfn, binary=False), "foo") diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 61ef77b3..e4ffeef6 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -449,7 +449,7 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(not HAS_RLIMIT, "not supported") def test_rlimit(self): - testfn = get_testfn() + testfn = self.get_testfn() p = psutil.Process() soft, hard = p.rlimit(psutil.RLIMIT_FSIZE) try: @@ -471,7 +471,7 @@ class TestProcess(PsutilTestCase): def test_rlimit_infinity(self): # First set a limit, then re-set it by specifying INFINITY # and assume we overridden the previous limit. - testfn = get_testfn() + testfn = self.get_testfn() p = psutil.Process() soft, hard = p.rlimit(psutil.RLIMIT_FSIZE) try: @@ -718,7 +718,7 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(PYPY, "broken on PYPY") def test_long_cmdline(self): - testfn = get_testfn() + testfn = self.get_testfn() create_exe(testfn) cmdline = [testfn] + (["0123456789"] * 20) sproc = self.get_test_subprocess(cmdline) @@ -733,7 +733,7 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(PYPY, "unreliable on PYPY") def test_long_name(self): - testfn = get_testfn(suffix="0123456789" * 2) + testfn = self.get_testfn(suffix="0123456789" * 2) create_exe(testfn) sproc = self.get_test_subprocess(testfn) p = psutil.Process(sproc.pid) @@ -747,7 +747,7 @@ class TestProcess(PsutilTestCase): # Test that name(), exe() and cmdline() correctly handle programs # with funky chars such as spaces and ")", see: # https://github.com/giampaolo/psutil/issues/628 - funky_path = get_testfn(suffix='foo bar )') + funky_path = self.get_testfn(suffix='foo bar )') create_exe(funky_path) cmdline = [funky_path, "-c", "import time; [time.sleep(0.01) for x in range(3000)];" @@ -938,7 +938,7 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(APPVEYOR, "unreliable on APPVEYOR") def test_open_files(self): # current process - testfn = get_testfn() + testfn = self.get_testfn() p = psutil.Process() files = p.open_files() self.assertFalse(testfn in files) @@ -978,7 +978,7 @@ class TestProcess(PsutilTestCase): def test_open_files_2(self): # test fd and path fields normcase = os.path.normcase - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, 'w') as fileobj: p = psutil.Process() for file in p.open_files(): @@ -1001,7 +1001,7 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(not POSIX, 'POSIX only') def test_num_fds(self): - testfn = get_testfn() + testfn = self.get_testfn() p = psutil.Process() start = p.num_fds() file = open(testfn, 'w') @@ -1488,7 +1488,7 @@ class TestProcess(PsutilTestCase): return execve("/bin/cat", argv, envp); } """) - path = get_testfn() + path = self.get_testfn() create_exe(path, c_code=code) sproc = self.get_test_subprocess( [path], stdin=subprocess.PIPE, stderr=subprocess.PIPE) diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index d9fa253c..eda6db01 100755..100644 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -35,7 +35,6 @@ from psutil.tests import check_net_address from psutil.tests import CI_TESTING from psutil.tests import DEVNULL from psutil.tests import enum -from psutil.tests import get_testfn from psutil.tests import HAS_BATTERY from psutil.tests import HAS_CPU_FREQ from psutil.tests import HAS_GETLOADAVG @@ -574,7 +573,7 @@ class TestDiskAPIs(PsutilTestCase): # if path does not exist OSError ENOENT is expected across # all platforms - fname = get_testfn() + fname = self.get_testfn() with self.assertRaises(FileNotFoundError): psutil.disk_usage(fname) diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py index 299cd24c..d466dcf2 100755..100644 --- a/psutil/tests/test_testutils.py +++ b/psutil/tests/test_testutils.py @@ -32,7 +32,6 @@ from psutil.tests import call_until from psutil.tests import chdir from psutil.tests import create_sockets from psutil.tests import get_free_port -from psutil.tests import get_testfn from psutil.tests import HAS_CONNECTIONS_UNIX from psutil.tests import is_namedtuple from psutil.tests import mock @@ -134,26 +133,26 @@ class TestSyncTestUtils(PsutilTestCase): self.assertRaises(psutil.NoSuchProcess, wait_for_pid, nopid) def test_wait_for_file(self): - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, 'w') as f: f.write('foo') wait_for_file(testfn) assert not os.path.exists(testfn) def test_wait_for_file_empty(self): - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, 'w'): pass wait_for_file(testfn, empty=True) assert not os.path.exists(testfn) def test_wait_for_file_no_file(self): - testfn = get_testfn() + testfn = self.get_testfn() with mock.patch('psutil.tests.retry.__iter__', return_value=iter([0])): self.assertRaises(IOError, wait_for_file, testfn) def test_wait_for_file_no_delete(self): - testfn = get_testfn() + testfn = self.get_testfn() with open(testfn, 'w') as f: f.write('foo') wait_for_file(testfn, delete=False) @@ -175,7 +174,7 @@ class TestFSTestUtils(PsutilTestCase): self.assertEqual(f.mode, 'rb') def test_safe_mkdir(self): - testfn = get_testfn() + testfn = self.get_testfn() safe_mkdir(testfn) assert os.path.isdir(testfn) safe_mkdir(testfn) @@ -183,7 +182,7 @@ class TestFSTestUtils(PsutilTestCase): def test_safe_rmpath(self): # test file is removed - testfn = get_testfn() + testfn = self.get_testfn() open(testfn, 'w').close() safe_rmpath(testfn) assert not os.path.exists(testfn) @@ -201,7 +200,7 @@ class TestFSTestUtils(PsutilTestCase): assert m.called def test_chdir(self): - testfn = get_testfn() + testfn = self.get_testfn() base = os.getcwd() os.mkdir(testfn) with chdir(testfn): @@ -284,7 +283,7 @@ class TestNetUtils(PsutilTestCase): @unittest.skipIf(not POSIX, "POSIX only") def test_bind_unix_socket(self): - name = get_testfn() + name = self.get_testfn() sock = bind_unix_socket(name) with contextlib.closing(sock): self.assertEqual(sock.family, socket.AF_UNIX) @@ -293,7 +292,7 @@ class TestNetUtils(PsutilTestCase): assert os.path.exists(name) assert stat.S_ISSOCK(os.stat(name).st_mode) # UDP - name = get_testfn() + name = self.get_testfn() sock = bind_unix_socket(name, type=socket.SOCK_DGRAM) with contextlib.closing(sock): self.assertEqual(sock.type, socket.SOCK_DGRAM) @@ -316,7 +315,7 @@ class TestNetUtils(PsutilTestCase): p = psutil.Process() num_fds = p.num_fds() assert not p.connections(kind='unix') - name = get_testfn() + name = self.get_testfn() server, client = unix_socketpair(name) try: assert os.path.exists(name) diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py index 69e52419..cc9fa114 100755..100644 --- a/psutil/tests/test_unicode.py +++ b/psutil/tests/test_unicode.py @@ -102,7 +102,7 @@ from psutil.tests import PsutilTestCase from psutil.tests import PYPY from psutil.tests import reap_children from psutil.tests import safe_mkdir -from psutil.tests import safe_rmpath as _safe_rmpath +from psutil.tests import safe_rmpath from psutil.tests import serialrun from psutil.tests import skip_on_access_denied from psutil.tests import terminate @@ -113,8 +113,9 @@ from psutil.tests import unittest import psutil -def safe_rmpath(path): - if APPVEYOR: +if APPVEYOR: + + def safe_rmpath(path): # NOQA # TODO - this is quite random and I'm not sure why it happens, # nor I can reproduce it locally: # https://ci.appveyor.com/project/giampaolo/psutil/build/job/ @@ -126,11 +127,9 @@ def safe_rmpath(path): # 68c7a70728a31d8b8b58f4be6c4c0baa2f449eda/psutil/arch/ # windows/process_info.c#L146 try: - return _safe_rmpath(path) + return safe_rmpath(path) except WindowsError: traceback.print_exc() - else: - return _safe_rmpath(path) def subprocess_supports_unicode(suffix): @@ -171,6 +170,7 @@ class _BaseFSAPIsTests(object): @classmethod def tearDownClass(cls): reap_children() + safe_rmpath(cls.funky_name) def expect_exact_path_match(self): raise NotImplementedError("must be implemented in subclass") @@ -231,7 +231,7 @@ class _BaseFSAPIsTests(object): @unittest.skipIf(not POSIX, "POSIX only") def test_proc_connections(self): suffix = os.path.basename(self.funky_name) - name = get_testfn(suffix=suffix) + name = self.get_testfn(suffix=suffix) try: sock = bind_unix_socket(name) except UnicodeEncodeError: @@ -257,7 +257,7 @@ class _BaseFSAPIsTests(object): raise ValueError("connection not found") suffix = os.path.basename(self.funky_name) - name = get_testfn(suffix=suffix) + name = self.get_testfn(suffix=suffix) try: sock = bind_unix_socket(name) except UnicodeEncodeError: |