bpo-40275: Adding filesystem_helper submodule in test.support by shihai1991 · Pull Request #20625 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Doc/library/test.rst
7 changes: 5 additions & 2 deletions Lib/ctypes/test/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import os.path
import sys
import test.support
from test.support import filesystem_helper
from ctypes import *
from ctypes.util import find_library


# On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
class Test_OpenGL_libs(unittest.TestCase):
@classmethod
Expand Down Expand Up @@ -65,8 +67,9 @@ def test_gle(self):
self.gle.gleGetJoinStyle

def test_shell_injection(self):
result = find_library('; echo Hello shell > ' + test.support.TESTFN)
self.assertFalse(os.path.lexists(test.support.TESTFN))
result = find_library('; echo Hello shell > ' +
filesystem_helper.TESTFN)
self.assertFalse(os.path.lexists(filesystem_helper.TESTFN))
self.assertIsNone(result)


Expand Down
9 changes: 5 additions & 4 deletions Lib/distutils/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import test.support
from test.support import captured_stdout, run_unittest
from test.support import filesystem_helper
import unittest
from distutils.tests import support
from distutils import log
Expand Down Expand Up @@ -62,13 +63,13 @@ def tearDown(self):
super(CoreTestCase, self).tearDown()

def cleanup_testfn(self):
path = test.support.TESTFN
path = filesystem_helper.TESTFN
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)

def write_setup(self, text, path=test.support.TESTFN):
def write_setup(self, text, path=filesystem_helper.TESTFN):
f = open(path, "w")
try:
f.write(text)
Expand Down Expand Up @@ -105,8 +106,8 @@ def test_run_setup_uses_current_dir(self):
cwd = os.getcwd()

# Create a directory and write the setup.py file there:
os.mkdir(test.support.TESTFN)
setup_py = os.path.join(test.support.TESTFN, "setup.py")
os.mkdir(filesystem_helper.TESTFN)
setup_py = os.path.join(filesystem_helper.TESTFN, "setup.py")
distutils.core.run_setup(
self.write_setup(setup_prints_cwd, path=setup_py))

Expand Down
3 changes: 2 additions & 1 deletion Lib/distutils/tests/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from distutils.cmd import Command

from test.support import (
TESTFN, captured_stdout, captured_stderr, run_unittest
captured_stdout, captured_stderr, run_unittest
)
from test.support.filesystem_helper import TESTFN
from distutils.tests import support
from distutils import log

Expand Down
3 changes: 2 additions & 1 deletion Lib/distutils/tests/test_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest.mock
from test.support import run_unittest, unix_shell
from test import support as test_support
from test.support import filesystem_helper

from distutils.spawn import find_executable
from distutils.spawn import spawn
Expand Down Expand Up @@ -46,7 +47,7 @@ def test_spawn(self):
def test_find_executable(self):
with test_support.temp_dir() as tmp_dir:
# use TESTFN to get a pseudo-unique filename
program_noeext = test_support.TESTFN
program_noeext = filesystem_helper.TESTFN
# Give the temporary program an ".exe" suffix for all.
# It's needed on Windows and not harmful on other platforms.
program = program_noeext + ".exe"
Expand Down
4 changes: 3 additions & 1 deletion Lib/distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from distutils import sysconfig
from distutils.ccompiler import get_default_compiler
from distutils.tests import support
from test.support import TESTFN, run_unittest, check_warnings, swap_item
from test.support import run_unittest, check_warnings, swap_item
from test.support.filesystem_helper import TESTFN


class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
def setUp(self):
Expand Down
3 changes: 2 additions & 1 deletion Lib/sqlite3/test/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import unittest
import sqlite3 as sqlite

from test.support import TESTFN, unlink
from test.support import unlink
from test.support.filesystem_helper import TESTFN


class ModuleTests(unittest.TestCase):
Expand Down
4 changes: 3 additions & 1 deletion Lib/sqlite3/test/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import unittest
import sqlite3 as sqlite

from test.support import TESTFN, unlink
from test.support import unlink
from test.support.filesystem_helper import TESTFN


class CollationTests(unittest.TestCase):
def CheckCreateCollationNotString(self):
Expand Down
17 changes: 9 additions & 8 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import test.support
import test.support.script_helper
from test import support
from test.support import filesystem_helper
from test.support import hashlib_helper
from test.support import socket_helper
from test.support import threading_helper
Expand Down Expand Up @@ -818,7 +819,7 @@ def test_stderr_flush(self):
if self.TYPE == "threads":
self.skipTest('test not appropriate for {}'.format(self.TYPE))

testfn = test.support.TESTFN
testfn = filesystem_helper.TESTFN
self.addCleanup(test.support.unlink, testfn)
proc = self.Process(target=self._test_stderr_flush, args=(testfn,))
proc.start()
Expand Down Expand Up @@ -848,7 +849,7 @@ def test_sys_exit(self):
if self.TYPE == 'threads':
self.skipTest('test not appropriate for {}'.format(self.TYPE))

testfn = test.support.TESTFN
testfn = filesystem_helper.TESTFN
self.addCleanup(test.support.unlink, testfn)

for reason in (
Expand Down Expand Up @@ -3198,14 +3199,14 @@ def test_fd_transfer(self):
p = self.Process(target=self._writefd, args=(child_conn, b"foo"))
p.daemon = True
p.start()
self.addCleanup(test.support.unlink, test.support.TESTFN)
with open(test.support.TESTFN, "wb") as f:
self.addCleanup(test.support.unlink, filesystem_helper.TESTFN)
with open(filesystem_helper.TESTFN, "wb") as f:
fd = f.fileno()
if msvcrt:
fd = msvcrt.get_osfhandle(fd)
reduction.send_handle(conn, fd, p.pid)
p.join()
with open(test.support.TESTFN, "rb") as f:
with open(filesystem_helper.TESTFN, "rb") as f:
self.assertEqual(f.read(), b"foo")

@unittest.skipUnless(HAS_REDUCTION, "test needs multiprocessing.reduction")
Expand All @@ -3224,8 +3225,8 @@ def test_large_fd_transfer(self):
p = self.Process(target=self._writefd, args=(child_conn, b"bar", True))
p.daemon = True
p.start()
self.addCleanup(test.support.unlink, test.support.TESTFN)
with open(test.support.TESTFN, "wb") as f:
self.addCleanup(test.support.unlink, filesystem_helper.TESTFN)
with open(filesystem_helper.TESTFN, "wb") as f:
fd = f.fileno()
for newfd in range(256, MAXFD):
if not self._is_fd_assigned(newfd):
Expand All @@ -3238,7 +3239,7 @@ def test_large_fd_transfer(self):
finally:
os.close(newfd)
p.join()
with open(test.support.TESTFN, "rb") as f:
with open(filesystem_helper.TESTFN, "rb") as f:
self.assertEqual(f.read(), b"bar")

@classmethod
Expand Down
1 change: 1 addition & 0 deletions Lib/test/audiotests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from test.support import findfile, TESTFN, unlink
from test.support.filesystem_helper import TESTFN
import array
import io
import pickle
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/eintrdata/eintr_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest

from test import support
from test.support import filesystem_helper
from test.support import socket_helper

@contextlib.contextmanager
Expand Down Expand Up @@ -314,7 +315,7 @@ def test_accept(self):
@support.requires_freebsd_version(10, 3)
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
def _test_open(self, do_open_close_reader, do_open_close_writer):
filename = support.TESTFN
filename = filesystem_helper.TESTFN

# Use a fifo: until the child opens it for reading, the parent will
# block when trying to open it for writing.
Expand Down Expand Up @@ -486,16 +487,16 @@ def test_devpoll(self):

class FNTLEINTRTest(EINTRBaseTest):
def _lock(self, lock_func, lock_name):
self.addCleanup(support.unlink, support.TESTFN)
self.addCleanup(support.unlink, filesystem_helper.TESTFN)
code = '\n'.join((
"import fcntl, time",
"with open('%s', 'wb') as f:" % support.TESTFN,
"with open('%s', 'wb') as f:" % filesystem_helper.TESTFN,
" fcntl.%s(f, fcntl.LOCK_EX)" % lock_name,
" time.sleep(%s)" % self.sleep_time))
start_time = time.monotonic()
proc = self.subprocess(code)
with kill_on_error(proc):
with open(support.TESTFN, 'wb') as f:
with open(filesystem_helper.TESTFN, 'wb') as f:
while True: # synchronize the subprocess
dt = time.monotonic() - start_time
if dt > 60.0:
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import unittest

from test import support
from test.support import filesystem_helper
from test.libregrtest.refleak import dash_R, clear_caches
from test.libregrtest.save_env import saved_test_environment
from test.libregrtest.utils import format_duration, print_warning
Expand Down Expand Up @@ -313,7 +314,7 @@ def cleanup_test_droppings(test_name, verbose):
# since if a test leaves a file open, it cannot be deleted by name (while
# there's nothing we can do about that here either, we can display the
# name of the offending test, which is a real help).
for name in (support.TESTFN,):
for name in (filesystem_helper.TESTFN,):
if not os.path.exists(name):
continue

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import urllib.request
import warnings
from test import support
from test.support import filesystem_helper
from test.libregrtest.utils import print_warning
try:
import _multiprocessing, multiprocessing.process
Expand Down Expand Up @@ -241,7 +242,7 @@ def get_files(self):
return sorted(fn + ('/' if os.path.isdir(fn) else '')
for fn in os.listdir())
def restore_files(self, saved_value):
fn = support.TESTFN
fn = filesystem_helper.TESTFN
if fn not in saved_value and (fn + '/') not in saved_value:
if os.path.isfile(fn):
support.unlink(fn)
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from functools import cmp_to_key

from test import support, seq_tests
from test.support import filesystem_helper
from test.support import ALWAYS_EQ, NEVER_EQ


Expand Down Expand Up @@ -73,12 +74,12 @@ def test_print(self):
d.append(d)
d.append(400)
try:
with open(support.TESTFN, "w") as fo:
with open(filesystem_helper.TESTFN, "w") as fo:
fo.write(str(d))
with open(support.TESTFN, "r") as fo:
with open(filesystem_helper.TESTFN, "r") as fo:
self.assertEqual(fo.read(), repr(d))
finally:
os.remove(support.TESTFN)
os.remove(filesystem_helper.TESTFN)

def test_set_subscript(self):
a = self.type2test(range(20))
Expand Down
1 change: 1 addition & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
TestFailed, TESTFN, run_with_locale, no_tracing,
_2G, _4G, bigmemtest, forget,
)
from test.support.filesystem_helper import TESTFN
from test.support import threading_helper

from pickle import bytes_types
Expand Down
14 changes: 2 additions & 12 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest
import warnings

from test.support.filesystem_helper import TESTFN
from .testresult import get_test_runner

__all__ = [
Expand All @@ -37,7 +38,7 @@
"record_original_stdout", "get_original_stdout", "captured_stdout",
"captured_stdin", "captured_stderr",
# filesystem
"TESTFN", "SAVEDCWD", "unlink", "rmtree", "temp_cwd", "findfile",
"SAVEDCWD", "unlink", "rmtree", "temp_cwd", "findfile",
"create_empty_file", "can_symlink", "fs_is_case_insensitive",
# unittest
"is_resource_enabled", "requires", "requires_freebsd_version",
Expand Down Expand Up @@ -714,17 +715,6 @@ def requires_lzma(reason='requires lzma'):
else:
unix_shell = None

# Filename used for testing
if os.name == 'java':
# Jython disallows @ in module names
TESTFN = '$test'
else:
TESTFN = '@test'

# Disambiguate TESTFN for parallel testing, while letting it remain a valid
# module name.
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())

# Define the URL of a dedicated HTTP server for the network tests.
# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
TEST_HTTP_URL = "http://www.pythontest.net"
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/support/filesystem_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os


# Filename used for testing
if os.name == 'java':
# Jython disallows @ in module names
TESTFN = '$test'
else:
TESTFN = '@test'

# Disambiguate TESTFN for parallel testing, while letting it remain a valid
# module name.
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
3 changes: 2 additions & 1 deletion Lib/test/support/socket_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def skip_unless_bind_unix_socket(test):
return unittest.skip('No UNIX Sockets')(test)
global _bind_nix_socket_error
if _bind_nix_socket_error is None:
from test.support import TESTFN, unlink
from test.support import unlink
from test.support.filesystem_helper import TESTFN
path = TESTFN + "can_bind_unix_socket"
with socket.socket(socket.AF_UNIX) as sock:
try:
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_aifc.py
Loading