summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-04-30 18:39:45 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-04-30 18:39:45 +0200
commit8a9588cb90ebbf41ca743e6e7a28386ef6bae056 (patch)
treec67a4ebc4f3cd7334339992d8eef176264598db3
parent11507969839adf5359e2e6397a5d600ba669f15d (diff)
downloadpsutil-8a9588cb90ebbf41ca743e6e7a28386ef6bae056.tar.gz
get rid of global created files
-rw-r--r--psutil/tests/__init__.py16
-rwxr-xr-xpsutil/tests/test_connections.py13
-rwxr-xr-xpsutil/tests/test_linux.py5
-rwxr-xr-xpsutil/tests/test_process.py3
-rw-r--r--psutil/tests/test_unicode.py3
5 files changed, 10 insertions, 30 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index d25d1a44..8ce76304 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -30,7 +30,6 @@ import tempfile
import textwrap
import threading
import time
-import traceback
import warnings
from socket import AF_INET
from socket import AF_INET6
@@ -220,7 +219,6 @@ AF_UNIX = getattr(socket, "AF_UNIX", object())
_subprocesses_started = set()
_pids_started = set()
-_testfiles_created = set()
# ===================================================================
@@ -791,7 +789,6 @@ def chdir(dirname):
def create_exe(outpath, c_code=None):
"""Creates an executable file in the given location."""
assert not os.path.exists(outpath), outpath
- _testfiles_created.add(outpath)
if c_code:
if not which("gcc"):
raise ValueError("gcc is not installed")
@@ -830,7 +827,6 @@ def get_testfn(suffix="", dir=None):
prefix = "%s%.9f-" % (TESTFN_PREFIX, timer())
name = tempfile.mktemp(prefix=prefix, suffix=suffix, dir=dir)
if not os.path.exists(name): # also include dirs
- _testfiles_created.add(name)
return os.path.realpath(name) # needed for OSX
@@ -1169,8 +1165,6 @@ def create_sockets():
fname2 = get_testfn()
s1, s2 = unix_socketpair(fname1)
s3 = bind_unix_socket(fname2, type=socket.SOCK_DGRAM)
- # self.addCleanup(safe_rmpath, fname1)
- # self.addCleanup(safe_rmpath, fname2)
for s in (s1, s2, s3):
socks.append(s)
yield socks
@@ -1332,16 +1326,6 @@ else:
atexit.register(DEVNULL.close)
-@atexit.register
-def cleanup_test_files():
- while _testfiles_created:
- path = _testfiles_created.pop()
- try:
- safe_rmpath(path)
- except Exception:
- traceback.print_exc()
-
-
# this is executed first
@atexit.register
def cleanup_test_procs():
diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py
index 42333a84..7fcc2f98 100755
--- a/psutil/tests/test_connections.py
+++ b/psutil/tests/test_connections.py
@@ -37,7 +37,6 @@ from psutil.tests import CIRRUS
from psutil.tests import create_sockets
from psutil.tests import enum
from psutil.tests import get_free_port
-from psutil.tests import get_testfn
from psutil.tests import HAS_CONNECTIONS_UNIX
from psutil.tests import PsutilTestCase
from psutil.tests import serialrun
@@ -267,14 +266,16 @@ class TestUnconnectedSockets(_ConnTestCase):
@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix_tcp(self):
- with closing(bind_unix_socket(get_testfn(), type=SOCK_STREAM)) as sock:
+ testfn = self.get_testfn()
+ with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
conn = self.check_socket(sock)
assert not conn.raddr
self.assertEqual(conn.status, psutil.CONN_NONE)
@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix_udp(self):
- with closing(bind_unix_socket(get_testfn(), type=SOCK_STREAM)) as sock:
+ testfn = self.get_testfn()
+ with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
conn = self.check_socket(sock)
assert not conn.raddr
self.assertEqual(conn.status, psutil.CONN_NONE)
@@ -433,7 +434,7 @@ class TestFilters(_ConnTestCase):
""")
# must be relative on Windows
- testfile = os.path.basename(get_testfn(dir=os.getcwd()))
+ testfile = os.path.basename(self.get_testfn(dir=os.getcwd()))
tcp4_template = string.Template(tcp_template).substitute(
family=int(AF_INET), addr="127.0.0.1", testfn=testfile)
udp4_template = string.Template(udp_template).substitute(
@@ -586,12 +587,10 @@ class TestSystemWideConnections(_ConnTestCase):
fnames.append(fname)
src = textwrap.dedent("""\
import time, os
- from psutil.tests import create_sockets, cleanup_test_files
+ from psutil.tests import create_sockets
with create_sockets():
with open(r'%s', 'w') as f:
f.write("hello")
- # 2 UNIX test socket files are created
- cleanup_test_files()
time.sleep(60)
""" % fname)
sproc = self.pyrun(src)
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 3e64b849..fcc9d5db 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -28,7 +28,6 @@ from psutil._compat import FileNotFoundError
from psutil._compat import PY3
from psutil._compat import u
from psutil.tests import call_until
-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
@@ -1741,7 +1740,7 @@ class TestProcess(PsutilTestCase):
# execution
p = psutil.Process()
files = p.open_files()
- with open(get_testfn(), 'w'):
+ with open(self.get_testfn(), 'w'):
# give the kernel some time to see the new file
call_until(p.open_files, "len(ret) != %i" % len(files))
with mock.patch('psutil._pslinux.os.readlink',
@@ -1762,7 +1761,7 @@ class TestProcess(PsutilTestCase):
# https://travis-ci.org/giampaolo/psutil/jobs/225694530
p = psutil.Process()
files = p.open_files()
- with open(get_testfn(), 'w'):
+ with open(self.get_testfn(), 'w'):
# give the kernel some time to see the new file
call_until(p.open_files, "len(ret) != %i" % len(files))
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 25dc9480..4bae76e5 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -40,7 +40,6 @@ from psutil.tests import CIRRUS
from psutil.tests import copyload_shared_lib
from psutil.tests import create_exe
from psutil.tests import enum
-from psutil.tests import get_testfn
from psutil.tests import HAS_CPU_AFFINITY
from psutil.tests import HAS_ENVIRON
from psutil.tests import HAS_IONICE
@@ -316,7 +315,7 @@ class TestProcess(PsutilTestCase):
# test writes
io1 = p.io_counters()
- with open(get_testfn(), 'wb') as f:
+ with open(self.get_testfn(), 'wb') as f:
if PY3:
f.write(bytes("x" * 1000000, 'ascii'))
else:
diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py
index b808a920..926acfb7 100644
--- a/psutil/tests/test_unicode.py
+++ b/psutil/tests/test_unicode.py
@@ -114,7 +114,6 @@ import psutil
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:
@@ -138,8 +137,8 @@ def subprocess_supports_unicode(suffix):
"""
if PY3:
return True
- testfn = get_testfn(suffix=suffix)
sproc = None
+ testfn = get_testfn(suffix=suffix)
try:
safe_rmpath(testfn)
create_exe(testfn)