From ef0826994952a6bf2086e857f636c4f91a2391d7 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 24 Jun 2023 19:48:54 +0100 Subject: [PATCH 1/4] GH-89812: Miscellaneous pathlib test improvements - 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 --- Lib/test/test_pathlib.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index f9356909cb0982..5b2044d88e0ceb 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1671,7 +1671,7 @@ def test_samefile(self): def test_empty_path(self): # The empty path points to '.' p = self.cls('') - self.assertEqual(p.stat(), os.stat('.')) + self.assertEqual(str(p), '.') def test_exists(self): P = self.cls @@ -1700,9 +1700,6 @@ def test_open_common(self): with (p / 'fileA').open('rb') as f: self.assertIsInstance(f, io.BufferedIOBase) 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): p = self.cls(BASE) @@ -2017,7 +2014,6 @@ def test_glob_permissions(self): P = self.cls base = P(BASE) / 'permissions' base.mkdir() - self.addCleanup(os_helper.rmtree, base) for i in range(100): link = base / f"link{i}" @@ -2210,18 +2206,10 @@ def test_is_file(self): def test_is_mount(self): 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 / 'dirA').is_mount()) self.assertFalse((P / 'non-existing').is_mount()) self.assertFalse((P / 'fileA' / 'bah').is_mount()) - self.assertTrue(R.is_mount()) - if os_helper.can_symlink(): - self.assertFalse((P / 'linkA').is_mount()) - self.assertIs((R / '\udfff').is_mount(), False) def test_is_symlink(self): P = self.cls(BASE) @@ -2854,6 +2842,14 @@ def test_is_char_device_true(self): self.assertIs(self.cls('/dev/null\udfff').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): with self.assertWarns(DeprecationWarning): self.cls(foo="bar") @@ -3121,6 +3117,12 @@ def test_open_mode(self): st = os.stat(join('other_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) + 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_root(self): current_directory = os.getcwd() try: From 436fa3bace9c856ec2c02f47ea9fccd4564d5134 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 24 Jun 2023 20:33:18 +0100 Subject: [PATCH 2/4] Remove unused `assertSame()` method --- Lib/test/test_pathlib.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 5b2044d88e0ceb..800e95750da7b1 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1635,11 +1635,6 @@ def dirlink(self, src, dest): def dirlink(self, src, dest): os.symlink(src, dest) - 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): with self.assertRaises(FileNotFoundError) as cm: func(*args, **kwargs) From 50967966a7554e647c90501d8f3f2b7fb3d6af37 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 24 Jun 2023 20:39:18 +0100 Subject: [PATCH 3/4] Fix misplaced `test_open_unbuffered()` method. --- Lib/test/test_pathlib.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 800e95750da7b1..008c0f1cc8eaac 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2438,6 +2438,12 @@ def with_segments(self, *pathsegments): for dirpath, dirnames, filenames in p.walk(): 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): p = self.cls('non', 'exist') @@ -3112,12 +3118,6 @@ def test_open_mode(self): st = os.stat(join('other_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) - 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_root(self): current_directory = os.getcwd() try: From 9c37a53a62c0bf620a46ac07d8b8a4b8084937a9 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 24 Jun 2023 22:23:21 +0100 Subject: [PATCH 4/4] Fix direct reference to `pathlib.Path` in `test_glob_above_recursion_limit()` --- Lib/test/test_pathlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 008c0f1cc8eaac..1facd5f04b23af 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2035,8 +2035,8 @@ def test_glob_above_recursion_limit(self): recursion_limit = 50 # directory_depth > recursion_limit directory_depth = recursion_limit + 10 - base = pathlib.Path(os_helper.TESTFN, 'deep') - path = pathlib.Path(base, *(['d'] * directory_depth)) + base = self.cls(BASE, 'deep') + path = self.cls(base, *(['d'] * directory_depth)) path.mkdir(parents=True) with set_recursion_limit(recursion_limit):