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

Appearance settings

Commit b1d7360

Browse filesBrowse files
[3.11] pythongh-119064: Use os_helper.FakePath instead of pathlib.Path in tests (pythonGH-119065)
(cherry picked from commit 0152dc4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent ba43157 commit b1d7360
Copy full SHA for b1d7360

17 files changed

+105
-105
lines changed

‎Lib/test/_test_multiprocessing.py

Copy file name to clipboardExpand all lines: Lib/test/_test_multiprocessing.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import subprocess
2323
import struct
2424
import operator
25-
import pathlib
2625
import pickle
2726
import weakref
2827
import warnings
@@ -323,8 +322,9 @@ def test_set_executable(self):
323322
self.skipTest(f'test not appropriate for {self.TYPE}')
324323
paths = [
325324
sys.executable, # str
326-
sys.executable.encode(), # bytes
327-
pathlib.Path(sys.executable) # os.PathLike
325+
os.fsencode(sys.executable), # bytes
326+
os_helper.FakePath(sys.executable), # os.PathLike
327+
os_helper.FakePath(os.fsencode(sys.executable)), # os.PathLike bytes
328328
]
329329
for path in paths:
330330
self.set_executable(path)

‎Lib/test/test_asyncio/test_unix_events.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_unix_events.py
+5-6Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import errno
55
import io
66
import os
7-
import pathlib
87
import signal
98
import socket
109
import stat
@@ -297,20 +296,20 @@ def test_create_unix_server_existing_path_sock(self):
297296
self.loop.run_until_complete(srv.wait_closed())
298297

299298
@socket_helper.skip_unless_bind_unix_socket
300-
def test_create_unix_server_pathlib(self):
299+
def test_create_unix_server_pathlike(self):
301300
with test_utils.unix_socket_path() as path:
302-
path = pathlib.Path(path)
301+
path = os_helper.FakePath(path)
303302
srv_coro = self.loop.create_unix_server(lambda: None, path)
304303
srv = self.loop.run_until_complete(srv_coro)
305304
srv.close()
306305
self.loop.run_until_complete(srv.wait_closed())
307306

308-
def test_create_unix_connection_pathlib(self):
307+
def test_create_unix_connection_pathlike(self):
309308
with test_utils.unix_socket_path() as path:
310-
path = pathlib.Path(path)
309+
path = os_helper.FakePath(path)
311310
coro = self.loop.create_unix_connection(lambda: None, path)
312311
with self.assertRaises(FileNotFoundError):
313-
# If pathlib.Path wasn't supported, the exception would be
312+
# If path-like object weren't supported, the exception would be
314313
# different.
315314
self.loop.run_until_complete(coro)
316315

‎Lib/test/test_compileall.py

Copy file name to clipboardExpand all lines: Lib/test/test_compileall.py
+13-13Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import importlib.util
55
import io
66
import os
7-
import pathlib
87
import py_compile
98
import shutil
109
import struct
@@ -31,6 +30,7 @@
3130
from test.support import script_helper
3231
from test.test_py_compile import without_source_date_epoch
3332
from test.test_py_compile import SourceDateEpochTestMeta
33+
from test.support.os_helper import FakePath
3434

3535

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

163163
def test_compile_file_pathlike_ddir(self):
164164
self.assertFalse(os.path.isfile(self.bc_path))
165-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
166-
ddir=pathlib.Path('ddir_path'),
165+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
166+
ddir=FakePath('ddir_path'),
167167
quiet=2))
168168
self.assertTrue(os.path.isfile(self.bc_path))
169169

170170
def test_compile_file_pathlike_stripdir(self):
171171
self.assertFalse(os.path.isfile(self.bc_path))
172-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
173-
stripdir=pathlib.Path('stripdir_path'),
172+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
173+
stripdir=FakePath('stripdir_path'),
174174
quiet=2))
175175
self.assertTrue(os.path.isfile(self.bc_path))
176176

177177
def test_compile_file_pathlike_prependdir(self):
178178
self.assertFalse(os.path.isfile(self.bc_path))
179-
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
180-
prependdir=pathlib.Path('prependdir_path'),
179+
self.assertTrue(compileall.compile_file(FakePath(self.source_path),
180+
prependdir=FakePath('prependdir_path'),
181181
quiet=2))
182182
self.assertTrue(os.path.isfile(self.bc_path))
183183

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

236236
def test_compile_dir_pathlike_stripdir(self):
237237
self.assertFalse(os.path.isfile(self.bc_path))
238-
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
239-
stripdir=pathlib.Path('stripdir_path'),
238+
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
239+
stripdir=FakePath('stripdir_path'),
240240
quiet=2))
241241
self.assertTrue(os.path.isfile(self.bc_path))
242242

243243
def test_compile_dir_pathlike_prependdir(self):
244244
self.assertFalse(os.path.isfile(self.bc_path))
245-
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
246-
prependdir=pathlib.Path('prependdir_path'),
245+
self.assertTrue(compileall.compile_dir(FakePath(self.directory),
246+
prependdir=FakePath('prependdir_path'),
247247
quiet=2))
248248
self.assertTrue(os.path.isfile(self.bc_path))
249249

‎Lib/test/test_configparser.py

Copy file name to clipboardExpand all lines: Lib/test/test_configparser.py
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import configparser
33
import io
44
import os
5-
import pathlib
65
import textwrap
76
import unittest
87
import warnings
@@ -746,12 +745,12 @@ def test_read_returns_file_list(self):
746745
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
747746
# check when we pass only a Path object:
748747
cf = self.newconfig()
749-
parsed_files = cf.read(pathlib.Path(file1), encoding="utf-8")
748+
parsed_files = cf.read(os_helper.FakePath(file1), encoding="utf-8")
750749
self.assertEqual(parsed_files, [file1])
751750
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
752751
# check when we passed both a filename and a Path object:
753752
cf = self.newconfig()
754-
parsed_files = cf.read([pathlib.Path(file1), file1], encoding="utf-8")
753+
parsed_files = cf.read([os_helper.FakePath(file1), file1], encoding="utf-8")
755754
self.assertEqual(parsed_files, [file1, file1])
756755
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
757756
# check when we pass only missing files:

‎Lib/test/test_fileinput.py

Copy file name to clipboardExpand all lines: Lib/test/test_fileinput.py
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
from io import BytesIO, StringIO
2525
from fileinput import FileInput, hook_encoded
26-
from pathlib import Path
2726

2827
from test.support import verbose
29-
from test.support.os_helper import TESTFN
28+
from test.support.os_helper import TESTFN, FakePath
3029
from test.support.os_helper import unlink as safe_unlink
3130
from test.support import os_helper
3231
from test import support
@@ -478,23 +477,23 @@ def test_iteration_buffering(self):
478477
self.assertRaises(StopIteration, next, fi)
479478
self.assertEqual(src.linesread, [])
480479

481-
def test_pathlib_file(self):
482-
t1 = Path(self.writeTmp("Pathlib file."))
480+
def test_pathlike_file(self):
481+
t1 = FakePath(self.writeTmp("Path-like file."))
483482
with FileInput(t1, encoding="utf-8") as fi:
484483
line = fi.readline()
485-
self.assertEqual(line, 'Pathlib file.')
484+
self.assertEqual(line, 'Path-like file.')
486485
self.assertEqual(fi.lineno(), 1)
487486
self.assertEqual(fi.filelineno(), 1)
488487
self.assertEqual(fi.filename(), os.fspath(t1))
489488

490-
def test_pathlib_file_inplace(self):
491-
t1 = Path(self.writeTmp('Pathlib file.'))
489+
def test_pathlike_file_inplace(self):
490+
t1 = FakePath(self.writeTmp('Path-like file.'))
492491
with FileInput(t1, inplace=True, encoding="utf-8") as fi:
493492
line = fi.readline()
494-
self.assertEqual(line, 'Pathlib file.')
493+
self.assertEqual(line, 'Path-like file.')
495494
print('Modified %s' % line)
496495
with open(t1, encoding="utf-8") as f:
497-
self.assertEqual(f.read(), 'Modified Pathlib file.\n')
496+
self.assertEqual(f.read(), 'Modified Path-like file.\n')
498497

499498

500499
class MockFileInput:

‎Lib/test/test_http_cookiejar.py

Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import time
1111
import unittest
1212
import urllib.request
13-
import pathlib
1413

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

340339
def test_constructor_with_path_like(self):
341-
filename = pathlib.Path(os_helper.TESTFN)
342-
c = LWPCookieJar(filename)
343-
self.assertEqual(c.filename, os.fspath(filename))
340+
filename = os_helper.TESTFN
341+
c = LWPCookieJar(os_helper.FakePath(filename))
342+
self.assertEqual(c.filename, filename)
344343

345344
def test_constructor_with_none(self):
346345
c = LWPCookieJar(None)

‎Lib/test/test_logging.py

Copy file name to clipboardExpand all lines: Lib/test/test_logging.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,16 +610,16 @@ def test_builtin_handlers(self):
610610
self.assertFalse(h.shouldFlush(r))
611611
h.close()
612612

613-
def test_path_objects(self):
613+
def test_pathlike_objects(self):
614614
"""
615-
Test that Path objects are accepted as filename arguments to handlers.
615+
Test that path-like objects are accepted as filename arguments to handlers.
616616
617617
See Issue #27493.
618618
"""
619619
fd, fn = tempfile.mkstemp()
620620
os.close(fd)
621621
os.unlink(fn)
622-
pfn = pathlib.Path(fn)
622+
pfn = os_helper.FakePath(fn)
623623
cases = (
624624
(logging.FileHandler, (pfn, 'w')),
625625
(logging.handlers.RotatingFileHandler, (pfn, 'a')),

‎Lib/test/test_mimetypes.py

Copy file name to clipboardExpand all lines: Lib/test/test_mimetypes.py
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io
22
import mimetypes
33
import os
4-
import pathlib
54
import sys
65
import unittest.mock
76

@@ -75,11 +74,19 @@ def test_read_mime_types(self):
7574

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

83+
data = "x-application/x-unittest2 pyunit2\n"
84+
file = os.path.join(directory, "sample2.mimetype")
85+
with open(file, 'w', encoding="utf-8") as f:
86+
f.write(data)
87+
mime_dict = mimetypes.read_mime_types(os_helper.FakePath(file))
88+
eq(mime_dict[".pyunit2"], "x-application/x-unittest2")
89+
8390
# bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
8491
# Not with locale encoding. _bootlocale has been imported because io.open(...)
8592
# uses it.

‎Lib/test/test_pkgutil.py

Copy file name to clipboardExpand all lines: Lib/test/test_pkgutil.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import shutil
1313
import zipfile
1414

15+
from test.support.os_helper import FakePath
16+
1517
# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
1618
# a hack to get a major issue resolved for 3.3b2. Longer term, it should
1719
# be moved back here, perhaps by factoring out the helper code for
@@ -118,7 +120,7 @@ def test_issue44061_iter_modules(self):
118120

119121
# make sure iter_modules accepts Path objects
120122
names = []
121-
for moduleinfo in pkgutil.iter_modules([Path(zip_file)]):
123+
for moduleinfo in pkgutil.iter_modules([FakePath(zip_file)]):
122124
self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
123125
names.append(moduleinfo.name)
124126
self.assertEqual(names, [pkg])

‎Lib/test/test_runpy.py

Copy file name to clipboardExpand all lines: Lib/test/test_runpy.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import warnings
1515
from test.support import no_tracing, verbose, requires_subprocess, requires_resource
1616
from test.support.import_helper import forget, make_legacy_pyc, unload
17-
from test.support.os_helper import create_empty_file, temp_dir
17+
from test.support.os_helper import create_empty_file, temp_dir, FakePath
1818
from test.support.script_helper import make_script, make_zip_script
1919

2020

@@ -656,11 +656,10 @@ def test_basic_script(self):
656656
self._check_script(script_name, "<run_path>", script_name,
657657
script_name, expect_spec=False)
658658

659-
def test_basic_script_with_path_object(self):
659+
def test_basic_script_with_pathlike_object(self):
660660
with temp_dir() as script_dir:
661661
mod_name = 'script'
662-
script_name = pathlib.Path(self._make_test_script(script_dir,
663-
mod_name))
662+
script_name = FakePath(self._make_test_script(script_dir, mod_name))
664663
self._check_script(script_name, "<run_path>", script_name,
665664
script_name, expect_spec=False)
666665

‎Lib/test/test_shutil.py

Copy file name to clipboardExpand all lines: Lib/test/test_shutil.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def _ignore(src, names):
690690
'test.txt')))
691691

692692
dst_dir = join(self.mkdtemp(), 'destination')
693-
shutil.copytree(pathlib.Path(src_dir), dst_dir, ignore=_ignore)
693+
shutil.copytree(FakePath(src_dir), dst_dir, ignore=_ignore)
694694
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
695695
'test.txt')))
696696

@@ -1744,7 +1744,7 @@ def check_unpack_archive(self, format, **kwargs):
17441744
self.check_unpack_archive_with_converter(
17451745
format, lambda path: path, **kwargs)
17461746
self.check_unpack_archive_with_converter(
1747-
format, pathlib.Path, **kwargs)
1747+
format, FakePath, **kwargs)
17481748
self.check_unpack_archive_with_converter(format, FakePath, **kwargs)
17491749

17501750
def check_unpack_archive_with_converter(self, format, converter, **kwargs):
@@ -2137,12 +2137,12 @@ def test_move_file_to_dir(self):
21372137

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

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

21482148
@mock_rename

‎Lib/test/test_subprocess.py

Copy file name to clipboardExpand all lines: Lib/test/test_subprocess.py
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import gc
2525
import textwrap
2626
import json
27-
import pathlib
2827
from test.support.os_helper import FakePath
2928

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

15221521
def test_repr(self):
1523-
path_cmd = pathlib.Path("my-tool.py")
1524-
pathlib_cls = path_cmd.__class__.__name__
1525-
15261522
cases = [
15271523
("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"),
15281524
('a' * 100, True, 0,
15291525
"<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"),
15301526
(["ls"], False, None, "<Popen: returncode: None args: ['ls']>"),
15311527
(["ls", '--my-opts', 'a' * 100], False, None,
15321528
"<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"),
1533-
(path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>")
1529+
(os_helper.FakePath("my-tool.py"), False, 7,
1530+
"<Popen: returncode: 7 args: <FakePath 'my-tool.py'>>")
15341531
]
15351532
with unittest.mock.patch.object(subprocess.Popen, '_execute_child'):
15361533
for cmd, shell, code, sx in cases:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.