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

GH-130614: pathlib ABCs: delete vestigial test_pathlib_abc module #131215

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
125 changes: 115 additions & 10 deletions 125 Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from test.support import infinite_recursion
from test.support import os_helper
from test.support.os_helper import TESTFN, FakePath
from test.test_pathlib import test_pathlib_abc
from test.test_pathlib.test_pathlib_abc import needs_posix, needs_windows

try:
import fcntl
except ImportError:
Expand Down Expand Up @@ -56,7 +53,20 @@ def new_test(self):
return new_test


_tests_needing_posix = set()
_tests_needing_windows = set()
_tests_needing_symlinks = set()

def needs_posix(fn):
"""Decorator that marks a test as requiring a POSIX-flavoured path class."""
_tests_needing_posix.add(fn.__name__)
return fn

def needs_windows(fn):
"""Decorator that marks a test as requiring a Windows-flavoured path class."""
_tests_needing_windows.add(fn.__name__)
return fn

def needs_symlinks(fn):
"""Decorator that marks a test as requiring a path class that supports symlinks."""
_tests_needing_symlinks.add(fn.__name__)
Expand All @@ -74,7 +84,7 @@ def test_is_notimplemented(self):
# Tests for the pure classes.
#

class PurePathTest(test_pathlib_abc.JoinablePathTest):
class PurePathTest(unittest.TestCase):
cls = pathlib.PurePath

# Make sure any symbolic links in the base test path are resolved.
Expand All @@ -96,6 +106,72 @@ class PurePathTest(test_pathlib_abc.JoinablePathTest):
],
}

def setUp(self):
name = self.id().split('.')[-1]
if name in _tests_needing_posix and self.cls.parser is not posixpath:
self.skipTest('requires POSIX-flavoured path class')
if name in _tests_needing_windows and self.cls.parser is posixpath:
self.skipTest('requires Windows-flavoured path class')
p = self.cls('a')
self.parser = p.parser
self.sep = self.parser.sep
self.altsep = self.parser.altsep

def _check_str_subclass(self, *args):
# Issue #21127: it should be possible to construct a PurePath object
# from a str subclass instance, and it then gets converted to
# a pure str object.
class StrSubclass(str):
pass
P = self.cls
p = P(*(StrSubclass(x) for x in args))
self.assertEqual(p, P(*args))
for part in p.parts:
self.assertIs(type(part), str)

def test_str_subclass_common(self):
self._check_str_subclass('')
self._check_str_subclass('.')
self._check_str_subclass('a')
self._check_str_subclass('a/b.txt')
self._check_str_subclass('/a/b.txt')

@needs_windows
def test_str_subclass_windows(self):
self._check_str_subclass('.\\a:b')
self._check_str_subclass('c:')
self._check_str_subclass('c:a')
self._check_str_subclass('c:a\\b.txt')
self._check_str_subclass('c:\\')
self._check_str_subclass('c:\\a')
self._check_str_subclass('c:\\a\\b.txt')
self._check_str_subclass('\\\\some\\share')
self._check_str_subclass('\\\\some\\share\\a')
self._check_str_subclass('\\\\some\\share\\a\\b.txt')

def _check_str(self, expected, args):
p = self.cls(*args)
self.assertEqual(str(p), expected.replace('/', self.sep))

def test_str_common(self):
# Canonicalized paths roundtrip.
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
self._check_str(pathstr, (pathstr,))
# Other tests for str() are in test_equivalences().

@needs_windows
def test_str_windows(self):
p = self.cls('a/b/c')
self.assertEqual(str(p), 'a\\b\\c')
p = self.cls('c:/a/b/c')
self.assertEqual(str(p), 'c:\\a\\b\\c')
p = self.cls('//a/b')
self.assertEqual(str(p), '\\\\a\\b\\')
p = self.cls('//a/b/c')
self.assertEqual(str(p), '\\\\a\\b\\c')
p = self.cls('//a/b/c/d')
self.assertEqual(str(p), '\\\\a\\b\\c\\d')

def test_concrete_class(self):
if self.cls is pathlib.PurePath:
expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath
Expand Down Expand Up @@ -1032,7 +1108,7 @@ class cls(pathlib.PurePath):
# Tests for the concrete classes.
#

class PathTest(test_pathlib_abc.RWPathTest, PurePathTest):
class PathTest(PurePathTest):
"""Tests for the FS-accessing functionalities of the Path classes."""
cls = pathlib.Path
can_symlink = os_helper.can_symlink()
Expand All @@ -1042,8 +1118,6 @@ def setUp(self):
if name in _tests_needing_symlinks and not self.can_symlink:
self.skipTest('requires symlinks')
super().setUp()

def createTestHierarchy(self):
os.mkdir(self.base)
os.mkdir(os.path.join(self.base, 'dirA'))
os.mkdir(os.path.join(self.base, 'dirB'))
Expand Down Expand Up @@ -1082,6 +1156,15 @@ def tearDown(self):
os.chmod(os.path.join(self.base, 'dirE'), 0o777)
os_helper.rmtree(self.base)

def assertFileNotFound(self, func, *args, **kwargs):
with self.assertRaises(FileNotFoundError) as cm:
func(*args, **kwargs)
self.assertEqual(cm.exception.errno, errno.ENOENT)

def assertEqualNormCase(self, path_a, path_b):
normcase = self.parser.normcase
self.assertEqual(normcase(path_a), normcase(path_b))

def tempdir(self):
d = os_helper._longpath(tempfile.mkdtemp(suffix='-dirD',
dir=os.getcwd()))
Expand Down Expand Up @@ -2747,6 +2830,24 @@ def test_iterdir_symlink(self):
expected = { P(self.base, 'linkB', q) for q in ['fileB', 'linkD'] }
self.assertEqual(paths, expected)

@needs_posix
def test_glob_posix(self):
P = self.cls
p = P(self.base)
q = p / "FILEa"
given = set(p.glob("FILEa"))
expect = {q} if q.info.exists() else set()
self.assertEqual(given, expect)
self.assertEqual(set(p.glob("FILEa*")), set())

@needs_windows
def test_glob_windows(self):
P = self.cls
p = P(self.base)
self.assertEqual(set(p.glob("FILEa")), { P(self.base, "fileA") })
self.assertEqual(set(p.glob("*a\\")), { P(self.base, "dirA/") })
self.assertEqual(set(p.glob("F*a")), { P(self.base, "fileA") })

def test_glob_empty_pattern(self):
p = self.cls('')
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
Expand Down Expand Up @@ -3335,7 +3436,7 @@ def test_group_windows(self):
P('c:/').group()


class PathWalkTest(test_pathlib_abc.ReadablePathWalkTest):
class PathWalkTest(unittest.TestCase):
cls = pathlib.Path
base = PathTest.base
can_symlink = PathTest.can_symlink
Expand All @@ -3344,9 +3445,13 @@ def setUp(self):
name = self.id().split('.')[-1]
if name in _tests_needing_symlinks and not self.can_symlink:
self.skipTest('requires symlinks')
super().setUp()
self.walk_path = self.cls(self.base, "TEST1")
self.sub1_path = self.walk_path / "SUB1"
self.sub11_path = self.sub1_path / "SUB11"
self.sub2_path = self.walk_path / "SUB2"
self.link_path = self.sub2_path / "link"
self.sub2_tree = (self.sub2_path, [], ["tmp3"])

def createTestHierarchy(self):
# Build:
# TESTFN/
# TEST1/ a file kid and two directory kids
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.