Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Aug 11, 2024
1 parent 5de26e5 commit 3c1f3fe
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import io
import os
import sys
Expand All @@ -22,10 +23,18 @@
from test.test_pathlib import test_pathlib_abc
from test.test_pathlib.test_pathlib_abc import needs_posix, needs_windows, needs_symlinks

try:
import fcntl
except ImportError:
fcntl = None
try:
import grp, pwd
except ImportError:
grp = pwd = None
try:
import posix
except ImportError:
posix = None


root_in_posix = False
Expand Down Expand Up @@ -707,6 +716,45 @@ def test_copy_link_preserve_metadata(self):
if hasattr(source_st, 'st_flags'):
self.assertEqual(source_st.st_flags, target_st.st_flags)

def test_copy_error_handling(self):
def make_raiser(err):
def raiser(*args, **kwargs):
raise OSError(err, os.strerror(err))
return raiser

base = self.cls(self.base)
source = base / 'fileA'
target = base / 'copyA'

# Raise non-fatal OSError from all available fast copy functions.
with contextlib.ExitStack() as ctx:
if fcntl and hasattr(fcntl, 'FICLONE'):
ctx.enter_context(mock.patch('fcntl.ioctl', make_raiser(errno.EXDEV)))
if posix and hasattr(posix, '_fcopyfile'):
ctx.enter_context(mock.patch('posix._copyfile', make_raiser(errno.ENOTSUP)))
if hasattr(os, 'copy_file_range'):
ctx.enter_context(mock.patch('os.copy_file_range', make_raiser(errno.EXDEV)))
if hasattr(os, 'sendfile'):
ctx.enter_context(mock.patch('os.sendfile', make_raiser(errno.ENOTSOCK)))

source.copy(target)
self.assertTrue(target.exists())
self.assertEqual(source.read_text(), target.read_text())

# Raise fatal OSError from first available fast copy function.
if fcntl and hasattr(fcntl, 'FICLONE'):
patchpoint = 'fcntl.ioctl'
elif posix and hasattr(posix, '_fcopyfile'):
patchpoint = 'posix._fcopyfile'
elif hasattr(os, 'copy_file_range'):
patchpoint = 'os.copy_file_range'
elif hasattr(os, 'sendfile'):
patchpoint = 'os.sendfile'
else:
return
with mock.patch(patchpoint, make_raiser(errno.ENOENT)):
self.assertRaises(FileNotFoundError, source.copy, target)

@unittest.skipIf(sys.platform == "win32" or sys.platform == "wasi", "directories are always readable on Windows and WASI")
@unittest.skipIf(root_in_posix, "test fails with root privilege")
def test_copytree_no_read_permission(self):
Expand Down

0 comments on commit 3c1f3fe

Please sign in to comment.