GeezerVison is showing github.com — gh-119064: Use os_helper.FakePath instead of pathlib.Path in tests · serhiy-storchaka/cpython@aded70a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/_test_multiprocessing.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import subprocess | 23 | 23 | import struct | 24 | 24 | import operator | 25 | | -import pathlib | 26 | 25 | import pickle | 27 | 26 | import weakref | 28 | 27 | import warnings | @@ -324,8 +323,9 @@ def test_set_executable(self): | 324 | 323 | self.skipTest(f'test not appropriate for {self.TYPE}') | 325 | 324 | paths = [ | 326 | 325 | sys.executable, # str | 327 | | - sys.executable.encode(), # bytes | 328 | | - pathlib.Path(sys.executable) # os.PathLike | | 326 | + os.fsencode(sys.executable), # bytes | | 327 | + os_helper.FakePath(sys.executable), # os.PathLike | | 328 | + os_helper.FakePath(os.fsencode(sys.executable)), # os.PathLike bytes | 329 | 329 | ] | 330 | 330 | for path in paths: | 331 | 331 | self.set_executable(path) | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 6 | 6 | import multiprocessing | 7 | 7 | from multiprocessing.util import _cleanup_tests as multiprocessing_cleanup_tests | 8 | 8 | import os | 9 | | -import pathlib | 10 | 9 | import signal | 11 | 10 | import socket | 12 | 11 | import stat | @@ -304,20 +303,20 @@ def test_create_unix_server_existing_path_sock(self): | 304 | 303 | self.loop.run_until_complete(srv.wait_closed()) | 305 | 304 |
| 306 | 305 | @socket_helper.skip_unless_bind_unix_socket | 307 | | - def test_create_unix_server_pathlib(self): | | 306 | + def test_create_unix_server_pathlike(self): | 308 | 307 | with test_utils.unix_socket_path() as path: | 309 | | - path = pathlib.Path(path) | | 308 | + path = os_helper.FakePath(path) | 310 | 309 | srv_coro = self.loop.create_unix_server(lambda: None, path) | 311 | 310 | srv = self.loop.run_until_complete(srv_coro) | 312 | 311 | srv.close() | 313 | 312 | self.loop.run_until_complete(srv.wait_closed()) | 314 | 313 |
| 315 | | - def test_create_unix_connection_pathlib(self): | | 314 | + def test_create_unix_connection_pathlike(self): | 316 | 315 | with test_utils.unix_socket_path() as path: | 317 | | - path = pathlib.Path(path) | | 316 | + path = os_helper.FakePath(path) | 318 | 317 | coro = self.loop.create_unix_connection(lambda: None, path) | 319 | 318 | with self.assertRaises(FileNotFoundError): | 320 | | - # If pathlib.Path wasn't supported, the exception would be | | 319 | + # If path-like object weren't supported, the exception would be | 321 | 320 | # different. | 322 | 321 | self.loop.run_until_complete(coro) | 323 | 322 |
| |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_compileall.py +13-13Lines changed: 13 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | import importlib.util | 5 | 5 | import io | 6 | 6 | import os | 7 | | -import pathlib | 8 | 7 | import py_compile | 9 | 8 | import shutil | 10 | 9 | import struct | | 31 | 30 | from test.support import script_helper | 32 | 31 | from test.test_py_compile import without_source_date_epoch | 33 | 32 | from test.test_py_compile import SourceDateEpochTestMeta | | 33 | +from test.support.os_helper import FakePath | 34 | 34 |
| 35 | 35 |
| 36 | 36 | def get_pyc(script, opt): | @@ -156,28 +156,28 @@ def test_compile_file_pathlike(self): | 156 | 156 | self.assertFalse(os.path.isfile(self.bc_path)) | 157 | 157 | # we should also test the output | 158 | 158 | 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))) | 160 | 160 | self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') | 161 | 161 | self.assertTrue(os.path.isfile(self.bc_path)) | 162 | 162 |
| 163 | 163 | def test_compile_file_pathlike_ddir(self): | 164 | 164 | 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'), | 167 | 167 | quiet=2)) | 168 | 168 | self.assertTrue(os.path.isfile(self.bc_path)) | 169 | 169 |
| 170 | 170 | def test_compile_file_pathlike_stripdir(self): | 171 | 171 | 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'), | 174 | 174 | quiet=2)) | 175 | 175 | self.assertTrue(os.path.isfile(self.bc_path)) | 176 | 176 |
| 177 | 177 | def test_compile_file_pathlike_prependdir(self): | 178 | 178 | 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'), | 181 | 181 | quiet=2)) | 182 | 182 | self.assertTrue(os.path.isfile(self.bc_path)) | 183 | 183 |
| @@ -228,22 +228,22 @@ def test_optimize(self): | 228 | 228 | def test_compile_dir_pathlike(self): | 229 | 229 | self.assertFalse(os.path.isfile(self.bc_path)) | 230 | 230 | with support.captured_stdout() as stdout: | 231 | | - compileall.compile_dir(pathlib.Path(self.directory)) | | 231 | + compileall.compile_dir(FakePath(self.directory)) | 232 | 232 | line = stdout.getvalue().splitlines()[0] | 233 | 233 | self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') | 234 | 234 | self.assertTrue(os.path.isfile(self.bc_path)) | 235 | 235 |
| 236 | 236 | def test_compile_dir_pathlike_stripdir(self): | 237 | 237 | 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'), | 240 | 240 | quiet=2)) | 241 | 241 | self.assertTrue(os.path.isfile(self.bc_path)) | 242 | 242 |
| 243 | 243 | def test_compile_dir_pathlike_prependdir(self): | 244 | 244 | 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'), | 247 | 247 | quiet=2)) | 248 | 248 | self.assertTrue(os.path.isfile(self.bc_path)) | 249 | 249 |
| |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_configparser.py +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | import configparser | 3 | 3 | import io | 4 | 4 | import os | 5 | | -import pathlib | 6 | 5 | import textwrap | 7 | 6 | import unittest | 8 | 7 |
| @@ -745,12 +744,12 @@ def test_read_returns_file_list(self): | 745 | 744 | self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") | 746 | 745 | # check when we pass only a Path object: | 747 | 746 | cf = self.newconfig() | 748 | | - parsed_files = cf.read(pathlib.Path(file1), encoding="utf-8") | | 747 | + parsed_files = cf.read(os_helper.FakePath(file1), encoding="utf-8") | 749 | 748 | self.assertEqual(parsed_files, [file1]) | 750 | 749 | self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") | 751 | 750 | # check when we passed both a filename and a Path object: | 752 | 751 | cf = self.newconfig() | 753 | | - parsed_files = cf.read([pathlib.Path(file1), file1], encoding="utf-8") | | 752 | + parsed_files = cf.read([os_helper.FakePath(file1), file1], encoding="utf-8") | 754 | 753 | self.assertEqual(parsed_files, [file1, file1]) | 755 | 754 | self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") | 756 | 755 | # check when we pass only missing files: | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_ctypes/test_loading.py +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,10 +42,7 @@ def test_load(self): | 42 | 42 | self.skipTest('could not find library to load') | 43 | 43 | CDLL(test_lib) | 44 | 44 | CDLL(os.path.basename(test_lib)) | 45 | | - class CTypesTestPathLikeCls: | 46 | | - def __fspath__(self): | 47 | | - return test_lib | 48 | | - CDLL(CTypesTestPathLikeCls()) | | 45 | + CDLL(os_helper.FakePath(test_lib)) | 49 | 46 | self.assertRaises(OSError, CDLL, self.unknowndll) | 50 | 47 |
| 51 | 48 | def test_load_version(self): | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_fileinput.py +8-9Lines changed: 8 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 |
| 24 | 24 | from io import BytesIO, StringIO | 25 | 25 | from fileinput import FileInput, hook_encoded | 26 | | -from pathlib import Path | 27 | 26 |
| 28 | 27 | from test.support import verbose | 29 | | -from test.support.os_helper import TESTFN | | 28 | +from test.support.os_helper import TESTFN, FakePath | 30 | 29 | from test.support.os_helper import unlink as safe_unlink | 31 | 30 | from test.support import os_helper | 32 | 31 | from test import support | @@ -478,23 +477,23 @@ def test_iteration_buffering(self): | 478 | 477 | self.assertRaises(StopIteration, next, fi) | 479 | 478 | self.assertEqual(src.linesread, []) | 480 | 479 |
| 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.")) | 483 | 482 | with FileInput(t1, encoding="utf-8") as fi: | 484 | 483 | line = fi.readline() | 485 | | - self.assertEqual(line, 'Pathlib file.') | | 484 | + self.assertEqual(line, 'Path-like file.') | 486 | 485 | self.assertEqual(fi.lineno(), 1) | 487 | 486 | self.assertEqual(fi.filelineno(), 1) | 488 | 487 | self.assertEqual(fi.filename(), os.fspath(t1)) | 489 | 488 |
| 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.')) | 492 | 491 | with FileInput(t1, inplace=True, encoding="utf-8") as fi: | 493 | 492 | line = fi.readline() | 494 | | - self.assertEqual(line, 'Pathlib file.') | | 493 | + self.assertEqual(line, 'Path-like file.') | 495 | 494 | print('Modified %s' % line) | 496 | 495 | 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') | 498 | 497 |
| 499 | 498 |
| 500 | 499 | class MockFileInput: | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_http_cookiejar.py +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | import time | 10 | 10 | import unittest | 11 | 11 | import urllib.request | 12 | | -import pathlib | 13 | 12 |
| 14 | 13 | from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape, | 15 | 14 | parse_ns_headers, join_header_words, split_header_words, Cookie, | @@ -337,9 +336,9 @@ def test_constructor_with_str(self): | 337 | 336 | self.assertEqual(c.filename, filename) | 338 | 337 |
| 339 | 338 | def test_constructor_with_path_like(self): | 340 | | - filename = pathlib.Path(os_helper.TESTFN) | 341 | | - c = LWPCookieJar(filename) | 342 | | - self.assertEqual(c.filename, os.fspath(filename)) | | 339 | + filename = os_helper.TESTFN | | 340 | + c = LWPCookieJar(os_helper.FakePath(filename)) | | 341 | + self.assertEqual(c.filename, filename) | 343 | 342 |
| 344 | 343 | def test_constructor_with_none(self): | 345 | 344 | c = LWPCookieJar(None) | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_logging.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -657,15 +657,15 @@ def test_builtin_handlers(self): | 657 | 657 | self.assertFalse(h.shouldFlush(r)) | 658 | 658 | h.close() | 659 | 659 |
| 660 | | - def test_path_objects(self): | | 660 | + def test_pathlike_objects(self): | 661 | 661 | """ | 662 | | - Test that Path objects are accepted as filename arguments to handlers. | | 662 | + Test that path-like objects are accepted as filename arguments to handlers. | 663 | 663 |
| 664 | 664 | See Issue #27493. | 665 | 665 | """ | 666 | 666 | fn = make_temp_file() | 667 | 667 | os.unlink(fn) | 668 | | - pfn = pathlib.Path(fn) | | 668 | + pfn = os_helper.FakePath(fn) | 669 | 669 | cases = ( | 670 | 670 | (logging.FileHandler, (pfn, 'w')), | 671 | 671 | (logging.handlers.RotatingFileHandler, (pfn, 'a')), | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_mimetypes.py +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import io | 2 | 2 | import mimetypes | 3 | 3 | import os | 4 | | -import pathlib | 5 | 4 | import sys | 6 | 5 | import unittest.mock | 7 | 6 |
| @@ -83,11 +82,19 @@ def test_read_mime_types(self): | 83 | 82 |
| 84 | 83 | with os_helper.temp_dir() as directory: | 85 | 84 | data = "x-application/x-unittest pyunit\n" | 86 | | - file = pathlib.Path(directory, "sample.mimetype") | 87 | | - file.write_text(data, encoding="utf-8") | | 85 | + file = os.path.join(directory, "sample.mimetype") | | 86 | + with open(file, 'w', encoding="utf-8") as f: | | 87 | + f.write(data) | 88 | 88 | mime_dict = mimetypes.read_mime_types(file) | 89 | 89 | eq(mime_dict[".pyunit"], "x-application/x-unittest") | 90 | 90 |
| | 91 | + data = "x-application/x-unittest2 pyunit2\n" | | 92 | + file = os.path.join(directory, "sample2.mimetype") | | 93 | + with open(file, 'w', encoding="utf-8") as f: | | 94 | + f.write(data) | | 95 | + mime_dict = mimetypes.read_mime_types(os_helper.FakePath(file)) | | 96 | + eq(mime_dict[".pyunit2"], "x-application/x-unittest2") | | 97 | + | 91 | 98 | # bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding. | 92 | 99 | # Not with locale encoding. _bootlocale has been imported because io.open(...) | 93 | 100 | # uses it. | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_pkgutil.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | import zipfile | 14 | 14 |
| 15 | 15 | from test.support.import_helper import DirsOnSysPath | | 16 | +from test.support.os_helper import FakePath | 16 | 17 | from test.test_importlib.util import uncache | 17 | 18 |
| 18 | 19 | # Note: pkgutil.walk_packages is currently tested in test_runpy. This is | @@ -121,7 +122,7 @@ def test_issue44061_iter_modules(self): | 121 | 122 |
| 122 | 123 | # make sure iter_modules accepts Path objects | 123 | 124 | names = [] | 124 | | - for moduleinfo in pkgutil.iter_modules([Path(zip_file)]): | | 125 | + for moduleinfo in pkgutil.iter_modules([FakePath(zip_file)]): | 125 | 126 | self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) | 126 | 127 | names.append(moduleinfo.name) | 127 | 128 | self.assertEqual(names, [pkg]) | |
|