Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.12] gh-119064: Use os_helper.FakePath instead of pathlib.Path in tests (GH-119065)#119088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
6 changes: 3 additions & 3 deletionsLib/test/_test_multiprocessing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,6 @@
import subprocess
import struct
import operator
import pathlib
import pickle
import weakref
import warnings
Expand DownExpand Up@@ -324,8 +323,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 deletionsLib/test/test_asyncio/test_unix_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,6 @@
import multiprocessing
from multiprocessing.util import _cleanup_tests as multiprocessing_cleanup_tests
import os
import pathlib
import signal
import socket
import stat
Expand DownExpand Up@@ -304,20 +303,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
deftest_create_unix_server_pathlib(self):
deftest_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())

deftest_create_unix_connection_pathlib(self):
deftest_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):
# Ifpathlib.Path wasn't supported, the exception would be
# Ifpath-like object weren't supported, the exception would be
# different.
self.loop.run_until_complete(coro)

Expand Down
26 changes: 13 additions & 13 deletionsLib/test/test_compileall.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionsLib/test/test_configparser.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,6 @@
import configparser
import io
import os
import pathlib
import textwrap
import unittest
import warnings
Expand DownExpand 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
5 changes: 1 addition & 4 deletionsLib/test/test_ctypes/test_loading.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,10 +38,7 @@ def test_load(self):
self.skipTest('could not find library to load')
CDLL(test_lib)
CDLL(os.path.basename(test_lib))
class CTypesTestPathLikeCls:
def __fspath__(self):
return test_lib
CDLL(CTypesTestPathLikeCls())
CDLL(os_helper.FakePath(test_lib))
self.assertRaises(OSError, CDLL, self.unknowndll)

def test_load_version(self):
Expand Down
17 changes: 8 additions & 9 deletionsLib/test/test_fileinput.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -478,23 +477,23 @@ def test_iteration_buffering(self):
self.assertRaises(StopIteration, next, fi)
self.assertEqual(src.linesread, [])

deftest_pathlib_file(self):
t1 =Path(self.writeTmp("Pathlib file."))
deftest_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))

deftest_pathlib_file_inplace(self):
t1 =Path(self.writeTmp('Pathlib file.'))
deftest_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(), 'ModifiedPathlib file.\n')
self.assertEqual(f.read(), 'ModifiedPath-like file.\n')


class MockFileInput:
Expand Down
7 changes: 3 additions & 4 deletionsLib/test/test_http_cookiejar.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,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 DownExpand Up@@ -337,9 +336,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 deletionsLib/test/test_logging.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -662,15 +662,15 @@ def test_builtin_handlers(self):
self.assertFalse(h.shouldFlush(r))
h.close()

deftest_path_objects(self):
deftest_pathlike_objects(self):
"""
Test thatPath objects are accepted as filename arguments to handlers.
Test thatpath-like objects are accepted as filename arguments to handlers.

See Issue #27493.
"""
fn = make_temp_file()
os.unlink(fn)
pfn =pathlib.Path(fn)
pfn =os_helper.FakePath(fn)
cases = (
(logging.FileHandler, (pfn, 'w')),
(logging.handlers.RotatingFileHandler, (pfn, 'a')),
Expand Down
21 changes: 14 additions & 7 deletionsLib/test/test_mimetypes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
import io
import mimetypes
import os
import pathlib
import sys
import unittest.mock

Expand DownExpand 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 DownExpand Up@@ -241,10 +248,10 @@ def test_init_stability(self):

def test_path_like_ob(self):
filename = "LICENSE.txt"
filepath =pathlib.Path(filename)
filepath_with_abs_dir =pathlib.Path('/dir/'+filename)
filepath_relative =pathlib.Path('../dir/'+filename)
path_dir =pathlib.Path('./')
filepath =os_helper.FakePath(filename)
filepath_with_abs_dir =os_helper.FakePath('/dir/'+filename)
filepath_relative =os_helper.FakePath('../dir/'+filename)
path_dir =os_helper.FakePath('./')

expected = self.db.guess_type(filename)

Expand Down
3 changes: 2 additions & 1 deletionLib/test/test_pkgutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
importzipfile

fromtest.support.import_helperimportDirsOnSysPath
fromtest.support.os_helperimportFakePath
fromtest.test_importlib.utilimportuncache

# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
Expand DownExpand Up@@ -121,7 +122,7 @@ def test_issue44061_iter_modules(self):

# make sure iter_modules accepts Path objects
names= []
formoduleinfoinpkgutil.iter_modules([Path(zip_file)]):
formoduleinfoinpkgutil.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 deletionsLib/test/test_runpy.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -656,11 +656,10 @@ def test_basic_script(self):
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)

deftest_basic_script_with_path_object(self):
deftest_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 deletionsLib/test/test_shutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -859,7 +859,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 DownExpand Up@@ -2055,7 +2055,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 DownExpand Up@@ -2587,12 +2587,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 deletionsLib/test/test_subprocess.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,7 +25,6 @@
import gc
import textwrap
import json
import pathlib
from test.support.os_helper import FakePath

try:
Expand DownExpand Up@@ -1522,17 +1521,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

[8]ページ先頭

©2009-2025 Movatter.jp