Skip to content

Commit

Permalink
[3.11] pythongh-119064: Use os_helper.FakePath instead of pathlib.Pat…
Browse files Browse the repository at this point in the history
…h in tests (pythonGH-119065)

(cherry picked from commit 0152dc4)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka committed May 16, 2024
1 parent ba43157 commit b1d7360
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 105 deletions.
6 changes: 3 additions & 3 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import subprocess
import struct
import operator
import pathlib
import pickle
import weakref
import warnings
Expand Down Expand Up @@ -323,8 +322,9 @@ def test_set_executable(self):
self.skipTest(f'test not appropriate for {self.TYPE}')
paths = [
sys.executable, # str
sys.executable.encode(), # bytes
pathlib.Path(sys.executable) # os.PathLike
os.fsencode(sys.executable), # bytes
os_helper.FakePath(sys.executable), # os.PathLike
os_helper.FakePath(os.fsencode(sys.executable)), # os.PathLike bytes
]
for path in paths:
self.set_executable(path)
Expand Down
11 changes: 5 additions & 6 deletions Lib/test/test_asyncio/test_unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import errno
import io
import os
import pathlib
import signal
import socket
import stat
Expand Down Expand Up @@ -297,20 +296,20 @@ def test_create_unix_server_existing_path_sock(self):
self.loop.run_until_complete(srv.wait_closed())

@socket_helper.skip_unless_bind_unix_socket
def test_create_unix_server_pathlib(self):
def test_create_unix_server_pathlike(self):
with test_utils.unix_socket_path() as path:
path = pathlib.Path(path)
path = os_helper.FakePath(path)
srv_coro = self.loop.create_unix_server(lambda: None, path)
srv = self.loop.run_until_complete(srv_coro)
srv.close()
self.loop.run_until_complete(srv.wait_closed())

def test_create_unix_connection_pathlib(self):
def test_create_unix_connection_pathlike(self):
with test_utils.unix_socket_path() as path:
path = pathlib.Path(path)
path = os_helper.FakePath(path)
coro = self.loop.create_unix_connection(lambda: None, path)
with self.assertRaises(FileNotFoundError):
# If pathlib.Path wasn't supported, the exception would be
# If path-like object weren't supported, the exception would be
# different.
self.loop.run_until_complete(coro)

Expand Down
26 changes: 13 additions & 13 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import importlib.util
import io
import os
import pathlib
import py_compile
import shutil
import struct
Expand All @@ -31,6 +30,7 @@
from test.support import script_helper
from test.test_py_compile import without_source_date_epoch
from test.test_py_compile import SourceDateEpochTestMeta
from test.support.os_helper import FakePath


def get_pyc(script, opt):
Expand Down Expand Up @@ -156,28 +156,28 @@ def test_compile_file_pathlike(self):
self.assertFalse(os.path.isfile(self.bc_path))
# we should also test the output
with support.captured_stdout() as stdout:
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
self.assertTrue(compileall.compile_file(FakePath(self.source_path)))
self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
self.assertTrue(os.path.isfile(self.bc_path))

def test_compile_file_pathlike_ddir(self):
self.assertFalse(os.path.isfile(self.bc_path))
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
ddir=pathlib.Path('ddir_path'),
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
ddir=FakePath('ddir_path'),
quiet=2))
self.assertTrue(os.path.isfile(self.bc_path))

def test_compile_file_pathlike_stripdir(self):
self.assertFalse(os.path.isfile(self.bc_path))
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
stripdir=pathlib.Path('stripdir_path'),
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
stripdir=FakePath('stripdir_path'),
quiet=2))
self.assertTrue(os.path.isfile(self.bc_path))

def test_compile_file_pathlike_prependdir(self):
self.assertFalse(os.path.isfile(self.bc_path))
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
prependdir=pathlib.Path('prependdir_path'),
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
prependdir=FakePath('prependdir_path'),
quiet=2))
self.assertTrue(os.path.isfile(self.bc_path))

Expand Down Expand Up @@ -228,22 +228,22 @@ def test_optimize(self):
def test_compile_dir_pathlike(self):
self.assertFalse(os.path.isfile(self.bc_path))
with support.captured_stdout() as stdout:
compileall.compile_dir(pathlib.Path(self.directory))
compileall.compile_dir(FakePath(self.directory))
line = stdout.getvalue().splitlines()[0]
self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
self.assertTrue(os.path.isfile(self.bc_path))

def test_compile_dir_pathlike_stripdir(self):
self.assertFalse(os.path.isfile(self.bc_path))
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
stripdir=pathlib.Path('stripdir_path'),
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
stripdir=FakePath('stripdir_path'),
quiet=2))
self.assertTrue(os.path.isfile(self.bc_path))

def test_compile_dir_pathlike_prependdir(self):
self.assertFalse(os.path.isfile(self.bc_path))
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
prependdir=pathlib.Path('prependdir_path'),
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
prependdir=FakePath('prependdir_path'),
quiet=2))
self.assertTrue(os.path.isfile(self.bc_path))

Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import configparser
import io
import os
import pathlib
import textwrap
import unittest
import warnings
Expand Down Expand Up @@ -746,12 +745,12 @@ def test_read_returns_file_list(self):
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we pass only a Path object:
cf = self.newconfig()
parsed_files = cf.read(pathlib.Path(file1), encoding="utf-8")
parsed_files = cf.read(os_helper.FakePath(file1), encoding="utf-8")
self.assertEqual(parsed_files, [file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we passed both a filename and a Path object:
cf = self.newconfig()
parsed_files = cf.read([pathlib.Path(file1), file1], encoding="utf-8")
parsed_files = cf.read([os_helper.FakePath(file1), file1], encoding="utf-8")
self.assertEqual(parsed_files, [file1, file1])
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
# check when we pass only missing files:
Expand Down
17 changes: 8 additions & 9 deletions Lib/test/test_fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@

from io import BytesIO, StringIO
from fileinput import FileInput, hook_encoded
from pathlib import Path

from test.support import verbose
from test.support.os_helper import TESTFN
from test.support.os_helper import TESTFN, FakePath
from test.support.os_helper import unlink as safe_unlink
from test.support import os_helper
from test import support
Expand Down Expand Up @@ -478,23 +477,23 @@ def test_iteration_buffering(self):
self.assertRaises(StopIteration, next, fi)
self.assertEqual(src.linesread, [])

def test_pathlib_file(self):
t1 = Path(self.writeTmp("Pathlib file."))
def test_pathlike_file(self):
t1 = FakePath(self.writeTmp("Path-like file."))
with FileInput(t1, encoding="utf-8") as fi:
line = fi.readline()
self.assertEqual(line, 'Pathlib file.')
self.assertEqual(line, 'Path-like file.')
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 1)
self.assertEqual(fi.filename(), os.fspath(t1))

def test_pathlib_file_inplace(self):
t1 = Path(self.writeTmp('Pathlib file.'))
def test_pathlike_file_inplace(self):
t1 = FakePath(self.writeTmp('Path-like file.'))
with FileInput(t1, inplace=True, encoding="utf-8") as fi:
line = fi.readline()
self.assertEqual(line, 'Pathlib file.')
self.assertEqual(line, 'Path-like file.')
print('Modified %s' % line)
with open(t1, encoding="utf-8") as f:
self.assertEqual(f.read(), 'Modified Pathlib file.\n')
self.assertEqual(f.read(), 'Modified Path-like file.\n')


class MockFileInput:
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import time
import unittest
import urllib.request
import pathlib

from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
parse_ns_headers, join_header_words, split_header_words, Cookie,
Expand Down Expand Up @@ -338,9 +337,9 @@ def test_constructor_with_str(self):
self.assertEqual(c.filename, filename)

def test_constructor_with_path_like(self):
filename = pathlib.Path(os_helper.TESTFN)
c = LWPCookieJar(filename)
self.assertEqual(c.filename, os.fspath(filename))
filename = os_helper.TESTFN
c = LWPCookieJar(os_helper.FakePath(filename))
self.assertEqual(c.filename, filename)

def test_constructor_with_none(self):
c = LWPCookieJar(None)
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,16 @@ def test_builtin_handlers(self):
self.assertFalse(h.shouldFlush(r))
h.close()

def test_path_objects(self):
def test_pathlike_objects(self):
"""
Test that Path objects are accepted as filename arguments to handlers.
Test that path-like objects are accepted as filename arguments to handlers.
See Issue #27493.
"""
fd, fn = tempfile.mkstemp()
os.close(fd)
os.unlink(fn)
pfn = pathlib.Path(fn)
pfn = os_helper.FakePath(fn)
cases = (
(logging.FileHandler, (pfn, 'w')),
(logging.handlers.RotatingFileHandler, (pfn, 'a')),
Expand Down
13 changes: 10 additions & 3 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import io
import mimetypes
import os
import pathlib
import sys
import unittest.mock

Expand Down Expand Up @@ -75,11 +74,19 @@ def test_read_mime_types(self):

with os_helper.temp_dir() as directory:
data = "x-application/x-unittest pyunit\n"
file = pathlib.Path(directory, "sample.mimetype")
file.write_text(data, encoding="utf-8")
file = os.path.join(directory, "sample.mimetype")
with open(file, 'w', encoding="utf-8") as f:
f.write(data)
mime_dict = mimetypes.read_mime_types(file)
eq(mime_dict[".pyunit"], "x-application/x-unittest")

data = "x-application/x-unittest2 pyunit2\n"
file = os.path.join(directory, "sample2.mimetype")
with open(file, 'w', encoding="utf-8") as f:
f.write(data)
mime_dict = mimetypes.read_mime_types(os_helper.FakePath(file))
eq(mime_dict[".pyunit2"], "x-application/x-unittest2")

# bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
# Not with locale encoding. _bootlocale has been imported because io.open(...)
# uses it.
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import shutil
import zipfile

from test.support.os_helper import FakePath

# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
# a hack to get a major issue resolved for 3.3b2. Longer term, it should
# be moved back here, perhaps by factoring out the helper code for
Expand Down Expand Up @@ -118,7 +120,7 @@ def test_issue44061_iter_modules(self):

# make sure iter_modules accepts Path objects
names = []
for moduleinfo in pkgutil.iter_modules([Path(zip_file)]):
for moduleinfo in pkgutil.iter_modules([FakePath(zip_file)]):
self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
names.append(moduleinfo.name)
self.assertEqual(names, [pkg])
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import warnings
from test.support import no_tracing, verbose, requires_subprocess, requires_resource
from test.support.import_helper import forget, make_legacy_pyc, unload
from test.support.os_helper import create_empty_file, temp_dir
from test.support.os_helper import create_empty_file, temp_dir, FakePath
from test.support.script_helper import make_script, make_zip_script


Expand Down Expand Up @@ -656,11 +656,10 @@ def test_basic_script(self):
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)

def test_basic_script_with_path_object(self):
def test_basic_script_with_pathlike_object(self):
with temp_dir() as script_dir:
mod_name = 'script'
script_name = pathlib.Path(self._make_test_script(script_dir,
mod_name))
script_name = FakePath(self._make_test_script(script_dir, mod_name))
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)

Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def _ignore(src, names):
'test.txt')))

dst_dir = join(self.mkdtemp(), 'destination')
shutil.copytree(pathlib.Path(src_dir), dst_dir, ignore=_ignore)
shutil.copytree(FakePath(src_dir), dst_dir, ignore=_ignore)
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
'test.txt')))

Expand Down Expand Up @@ -1744,7 +1744,7 @@ def check_unpack_archive(self, format, **kwargs):
self.check_unpack_archive_with_converter(
format, lambda path: path, **kwargs)
self.check_unpack_archive_with_converter(
format, pathlib.Path, **kwargs)
format, FakePath, **kwargs)
self.check_unpack_archive_with_converter(format, FakePath, **kwargs)

def check_unpack_archive_with_converter(self, format, converter, **kwargs):
Expand Down Expand Up @@ -2137,12 +2137,12 @@ def test_move_file_to_dir(self):

def test_move_file_to_dir_pathlike_src(self):
# Move a pathlike file to another location on the same filesystem.
src = pathlib.Path(self.src_file)
src = FakePath(self.src_file)
self._check_move_file(src, self.dst_dir, self.dst_file)

def test_move_file_to_dir_pathlike_dst(self):
# Move a file to another pathlike location on the same filesystem.
dst = pathlib.Path(self.dst_dir)
dst = FakePath(self.dst_dir)
self._check_move_file(self.src_file, dst, self.dst_file)

@mock_rename
Expand Down
7 changes: 2 additions & 5 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import gc
import textwrap
import json
import pathlib
from test.support.os_helper import FakePath

try:
Expand Down Expand Up @@ -1520,17 +1519,15 @@ def test_communicate_epipe(self):
p.communicate(b"x" * 2**20)

def test_repr(self):
path_cmd = pathlib.Path("my-tool.py")
pathlib_cls = path_cmd.__class__.__name__

cases = [
("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"),
('a' * 100, True, 0,
"<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"),
(["ls"], False, None, "<Popen: returncode: None args: ['ls']>"),
(["ls", '--my-opts', 'a' * 100], False, None,
"<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"),
(path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>")
(os_helper.FakePath("my-tool.py"), False, 7,
"<Popen: returncode: 7 args: <FakePath 'my-tool.py'>>")
]
with unittest.mock.patch.object(subprocess.Popen, '_execute_child'):
for cmd, shell, code, sx in cases:
Expand Down
Loading

0 comments on commit b1d7360

Please sign in to comment.