Skip to content

Commit e1c0d9e

Browse files
committed
Expand test coverage, lean into the Windows differences a bit.
1 parent 5de963e commit e1c0d9e

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

Lib/pathlib/_local.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,7 @@ def move(self, target):
861861
if not isinstance(target, PathBase):
862862
raise
863863
except OSError as err:
864-
if err.errno == errno.EXDEV:
865-
pass # target is on another filesystem.
866-
elif os.name == 'nt' and err.winerror == 5: # ERROR_ACCESS_DENIED
867-
pass # target may have the wrong type.
868-
else:
864+
if err.errno != errno.EXDEV:
869865
raise
870866
return PathBase.move(self, target)
871867

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
{os.open, os.stat, os.unlink, os.rmdir} <= os.supports_dir_fd and
3737
os.listdir in os.supports_fd and os.stat in os.supports_follow_symlinks)
3838

39+
def patch_replace(old_test):
40+
def new_replace(self, target):
41+
raise OSError(errno.EXDEV, "Cross-device link", self, target)
42+
43+
def new_test(self):
44+
old_replace = self.cls.replace
45+
self.cls.replace = new_replace
46+
try:
47+
old_test(self)
48+
finally:
49+
self.cls.replace = old_replace
50+
return new_test
51+
3952
#
4053
# Tests for the pure classes.
4154
#
@@ -751,6 +764,57 @@ def test_copytree_preserve_metadata_xattrs(self):
751764
target_file = target.joinpath('dirD', 'fileD')
752765
self.assertEqual(os.getxattr(target_file, b'user.foo'), b'42')
753766

767+
@patch_replace
768+
def test_move_file_other_fs(self):
769+
self.test_move_file()
770+
771+
@patch_replace
772+
def test_move_file_to_file_other_fs(self):
773+
self.test_move_file_to_file()
774+
775+
@patch_replace
776+
def test_move_file_to_dir_other_fs(self):
777+
self.test_move_file_to_dir()
778+
779+
@patch_replace
780+
def test_move_file_to_empty_dir_other_fs(self):
781+
self.test_move_file_to_empty_dir()
782+
783+
@patch_replace
784+
def test_move_dir_other_fs(self):
785+
self.test_move_dir()
786+
787+
@patch_replace
788+
def test_move_dir_to_file_other_fs(self):
789+
self.test_move_dir_to_file()
790+
791+
@patch_replace
792+
def test_move_dir_to_dir_other_fs(self):
793+
self.test_move_dir_to_dir()
794+
795+
@patch_replace
796+
def test_move_dir_to_empty_dir_other_fs(self):
797+
self.test_move_dir_to_empty_dir()
798+
799+
@patch_replace
800+
def test_move_dir_into_itself_other_fs(self):
801+
self.test_move_dir_into_itself()
802+
803+
@needs_symlinks
804+
@patch_replace
805+
def test_move_file_symlink_other_fs(self):
806+
self.test_move_file_symlink()
807+
808+
@needs_symlinks
809+
@patch_replace
810+
def test_move_dir_symlink_other_fs(self):
811+
self.test_move_dir_symlink()
812+
813+
@needs_symlinks
814+
@patch_replace
815+
def test_move_dangling_symlink_other_fs(self):
816+
self.test_move_dangling_symlink()
817+
754818
def test_resolve_nonexist_relative_issue38671(self):
755819
p = self.cls('non', 'exist')
756820

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,14 +2008,22 @@ def test_move_file_to_dir(self):
20082008
base = self.cls(self.base)
20092009
source = base / 'fileA'
20102010
target = base / 'dirB'
2011-
self.assertRaises(IsADirectoryError, source.move, target)
2011+
if self.cls.parser is posixpath:
2012+
exc_type = IsADirectoryError
2013+
else:
2014+
exc_type = PermissionError
2015+
self.assertRaises(exc_type, source.move, target)
20122016

20132017
def test_move_file_to_empty_dir(self):
20142018
base = self.cls(self.base)
20152019
source = base / 'fileA'
20162020
target = base / 'fileA_moved'
20172021
target.mkdir()
2018-
self.assertRaises(IsADirectoryError, source.move, target)
2022+
if self.cls.parser is posixpath:
2023+
exc_type = IsADirectoryError
2024+
else:
2025+
exc_type = PermissionError
2026+
self.assertRaises(exc_type, source.move, target)
20192027

20202028
def test_move_dir(self):
20212029
base = self.cls(self.base)
@@ -2037,15 +2045,22 @@ def test_move_dir_to_file(self):
20372045
base = self.cls(self.base)
20382046
source = base / 'dirB'
20392047
target = base / 'fileA'
2040-
self.assertRaises(NotADirectoryError, source.move, target)
2048+
if self.cls.parser is posixpath:
2049+
exc_type = NotADirectoryError
2050+
else:
2051+
exc_type = PermissionError
2052+
self.assertRaises(exc_type, source.move, target)
20412053

20422054
def test_move_dir_to_dir(self):
20432055
base = self.cls(self.base)
20442056
source = base / 'dirC'
20452057
target = base / 'dirB'
20462058
with self.assertRaises(OSError) as cm:
20472059
source.move(target)
2048-
self.assertEqual(cm.exception.errno, errno.ENOTEMPTY)
2060+
if self.cls.parser is posixpath:
2061+
self.assertEqual(cm.exception.errno, errno.ENOTEMPTY)
2062+
else:
2063+
self.assertEqual(cm.exception.winerror, 5) # ERROR_ACCESS_DENIED
20492064

20502065
def test_move_dir_to_empty_dir(self):
20512066
base = self.cls(self.base)

0 commit comments

Comments
 (0)