GH-89812: Miscellaneous pathlib test improvements (GH-106063)

- Split out dedicated test for unbuffered `open()`
- Split out dedicated test for `is_mount()` at the filesystem root
- Avoid `os.stat()` when checking that empty paths point to '.'
- Remove unnecessary `rmtree()` call
- Remove unused `assertSame()` method
This commit is contained in:
Barney Gale 2023-07-01 13:29:02 +01:00 committed by GitHub
parent 6e01055e15
commit cbc33e4aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 18 deletions

View File

@ -1628,11 +1628,6 @@ class PathTest(unittest.TestCase):
# Broken symlink (pointing to itself). # Broken symlink (pointing to itself).
os.symlink('brokenLinkLoop', join('brokenLinkLoop')) os.symlink('brokenLinkLoop', join('brokenLinkLoop'))
def assertSame(self, path_a, path_b):
self.assertTrue(os.path.samefile(str(path_a), str(path_b)),
"%r and %r don't point to the same file" %
(path_a, path_b))
def assertFileNotFound(self, func, *args, **kwargs): def assertFileNotFound(self, func, *args, **kwargs):
with self.assertRaises(FileNotFoundError) as cm: with self.assertRaises(FileNotFoundError) as cm:
func(*args, **kwargs) func(*args, **kwargs)
@ -1664,7 +1659,7 @@ class PathTest(unittest.TestCase):
def test_empty_path(self): def test_empty_path(self):
# The empty path points to '.' # The empty path points to '.'
p = self.cls('') p = self.cls('')
self.assertEqual(p.stat(), os.stat('.')) self.assertEqual(str(p), '.')
def test_exists(self): def test_exists(self):
P = self.cls P = self.cls
@ -1693,9 +1688,6 @@ class PathTest(unittest.TestCase):
with (p / 'fileA').open('rb') as f: with (p / 'fileA').open('rb') as f:
self.assertIsInstance(f, io.BufferedIOBase) self.assertIsInstance(f, io.BufferedIOBase)
self.assertEqual(f.read().strip(), b"this is file A") self.assertEqual(f.read().strip(), b"this is file A")
with (p / 'fileA').open('rb', buffering=0) as f:
self.assertIsInstance(f, io.RawIOBase)
self.assertEqual(f.read().strip(), b"this is file A")
def test_read_write_bytes(self): def test_read_write_bytes(self):
p = self.cls(BASE) p = self.cls(BASE)
@ -2017,7 +2009,6 @@ class PathTest(unittest.TestCase):
P = self.cls P = self.cls
base = P(BASE) / 'permissions' base = P(BASE) / 'permissions'
base.mkdir() base.mkdir()
self.addCleanup(os_helper.rmtree, base)
for i in range(100): for i in range(100):
link = base / f"link{i}" link = base / f"link{i}"
@ -2045,8 +2036,8 @@ class PathTest(unittest.TestCase):
recursion_limit = 50 recursion_limit = 50
# directory_depth > recursion_limit # directory_depth > recursion_limit
directory_depth = recursion_limit + 10 directory_depth = recursion_limit + 10
base = pathlib.Path(os_helper.TESTFN, 'deep') base = self.cls(BASE, 'deep')
path = pathlib.Path(base, *(['d'] * directory_depth)) path = self.cls(base, *(['d'] * directory_depth))
path.mkdir(parents=True) path.mkdir(parents=True)
with set_recursion_limit(recursion_limit): with set_recursion_limit(recursion_limit):
@ -2242,18 +2233,12 @@ class PathTest(unittest.TestCase):
def test_is_mount(self): def test_is_mount(self):
P = self.cls(BASE) P = self.cls(BASE)
if os.name == 'nt':
R = self.cls('c:\\')
else:
R = self.cls('/')
self.assertFalse((P / 'fileA').is_mount()) self.assertFalse((P / 'fileA').is_mount())
self.assertFalse((P / 'dirA').is_mount()) self.assertFalse((P / 'dirA').is_mount())
self.assertFalse((P / 'non-existing').is_mount()) self.assertFalse((P / 'non-existing').is_mount())
self.assertFalse((P / 'fileA' / 'bah').is_mount()) self.assertFalse((P / 'fileA' / 'bah').is_mount())
self.assertTrue(R.is_mount())
if self.can_symlink: if self.can_symlink:
self.assertFalse((P / 'linkA').is_mount()) self.assertFalse((P / 'linkA').is_mount())
self.assertIs((R / '\udfff').is_mount(), False)
def test_is_symlink(self): def test_is_symlink(self):
P = self.cls(BASE) P = self.cls(BASE)
@ -2496,6 +2481,12 @@ class PathTest(unittest.TestCase):
for dirpath, dirnames, filenames in p.walk(): for dirpath, dirnames, filenames in p.walk():
self.assertEqual(42, dirpath.session_id) self.assertEqual(42, dirpath.session_id)
def test_open_unbuffered(self):
p = self.cls(BASE)
with (p / 'fileA').open('rb', buffering=0) as f:
self.assertIsInstance(f, io.RawIOBase)
self.assertEqual(f.read().strip(), b"this is file A")
def test_resolve_nonexist_relative_issue38671(self): def test_resolve_nonexist_relative_issue38671(self):
p = self.cls('non', 'exist') p = self.cls('non', 'exist')
@ -2896,6 +2887,14 @@ class PathTest(unittest.TestCase):
self.assertIs(self.cls('/dev/null\udfff').is_char_device(), False) self.assertIs(self.cls('/dev/null\udfff').is_char_device(), False)
self.assertIs(self.cls('/dev/null\x00').is_char_device(), False) self.assertIs(self.cls('/dev/null\x00').is_char_device(), False)
def test_is_mount_root(self):
if os.name == 'nt':
R = self.cls('c:\\')
else:
R = self.cls('/')
self.assertTrue(R.is_mount())
self.assertFalse((R / '\udfff').is_mount())
def test_passing_kwargs_deprecated(self): def test_passing_kwargs_deprecated(self):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
self.cls(foo="bar") self.cls(foo="bar")