summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2025-01-11 19:27:47 (GMT)
committerGitHub <noreply@github.com>2025-01-11 19:27:47 (GMT)
commit22a442181d5f1ac496dac08e24fd852de404882f (patch)
treeeba5ef2eb665ab76911750ac5286a035ff29d729 /Lib/test
parent0946ed25b53dddfa4eb040513720353b7214d71b (diff)
downloadcpython-22a442181d5f1ac496dac08e24fd852de404882f.zip
cpython-22a442181d5f1ac496dac08e24fd852de404882f.tar.gz
cpython-22a442181d5f1ac496dac08e24fd852de404882f.tar.bz2
GH-128520: Divide pathlib ABCs into three classes (#128523)
In the private pathlib ABCs, rename `PurePathBase` to `JoinablePath`, and split `PathBase` into `ReadablePath` and `WritablePath`. This improves the API fit for read-only virtual filesystems. The split of `PathBase` entails a similar split of `CopyWorker` (implements copying) and the test cases in `test_pathlib_abc`. In a later patch, we'll make `WritablePath` inherit directly from `JoinablePath` rather than `ReadablePath`. For a couple of reasons, this isn't quite possible yet.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pathlib/test_pathlib.py14
-rw-r--r--Lib/test/test_pathlib/test_pathlib_abc.py476
2 files changed, 243 insertions, 247 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py
index 6548577..ad5a9f9 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -75,7 +75,7 @@ class UnsupportedOperationTest(unittest.TestCase):
# Tests for the pure classes.
#
-class PurePathTest(test_pathlib_abc.DummyPurePathTest):
+class PurePathTest(test_pathlib_abc.DummyJoinablePathTest):
cls = pathlib.PurePath
# Make sure any symbolic links in the base test path are resolved.
@@ -924,7 +924,7 @@ class PurePathSubclassTest(PurePathTest):
# Tests for the concrete classes.
#
-class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
+class PathTest(test_pathlib_abc.DummyWritablePathTest, PurePathTest):
"""Tests for the FS-accessing functionalities of the Path classes."""
cls = pathlib.Path
can_symlink = os_helper.can_symlink()
@@ -980,15 +980,15 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.addCleanup(os_helper.rmtree, d)
return d
- def test_matches_pathbase_docstrings(self):
- path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'}
+ def test_matches_writablepath_docstrings(self):
+ path_names = {name for name in dir(pathlib._abc.WritablePath) if name[0] != '_'}
for attr_name in path_names:
if attr_name == 'parser':
- # On Windows, Path.parser is ntpath, but PathBase.parser is
+ # On Windows, Path.parser is ntpath, but WritablePath.parser is
# posixpath, and so their docstrings differ.
continue
our_attr = getattr(self.cls, attr_name)
- path_attr = getattr(pathlib._abc.PathBase, attr_name)
+ path_attr = getattr(pathlib._abc.WritablePath, attr_name)
self.assertEqual(our_attr.__doc__, path_attr.__doc__)
def test_concrete_class(self):
@@ -3019,7 +3019,7 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
P('c:/').group()
-class PathWalkTest(test_pathlib_abc.DummyPathWalkTest):
+class PathWalkTest(test_pathlib_abc.DummyReadablePathWalkTest):
cls = pathlib.Path
base = PathTest.base
can_symlink = PathTest.can_symlink
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py
index 87aef0c..6ba012e 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -4,7 +4,7 @@ import os
import errno
import unittest
-from pathlib._abc import PurePathBase, PathBase
+from pathlib._abc import JoinablePath, ReadablePath, WritablePath
from pathlib._types import Parser
import posixpath
@@ -31,8 +31,8 @@ def needs_windows(fn):
#
-class PurePathBaseTest(unittest.TestCase):
- cls = PurePathBase
+class JoinablePathTest(unittest.TestCase):
+ cls = JoinablePath
def test_magic_methods(self):
P = self.cls
@@ -51,7 +51,7 @@ class PurePathBaseTest(unittest.TestCase):
self.assertIs(self.cls.parser, posixpath)
-class DummyPurePath(PurePathBase):
+class DummyJoinablePath(JoinablePath):
__slots__ = ('_segments',)
def __init__(self, *segments):
@@ -63,7 +63,7 @@ class DummyPurePath(PurePathBase):
return ''
def __eq__(self, other):
- if not isinstance(other, DummyPurePath):
+ if not isinstance(other, DummyJoinablePath):
return NotImplemented
return str(self) == str(other)
@@ -77,8 +77,8 @@ class DummyPurePath(PurePathBase):
return type(self)(*pathsegments)
-class DummyPurePathTest(unittest.TestCase):
- cls = DummyPurePath
+class DummyJoinablePathTest(unittest.TestCase):
+ cls = DummyJoinablePath
# Use a base path that's unrelated to any real filesystem path.
base = f'/this/path/kills/fascists/{TESTFN}'
@@ -916,9 +916,9 @@ class DummyPurePathTest(unittest.TestCase):
#
-class DummyPathIO(io.BytesIO):
+class DummyWritablePathIO(io.BytesIO):
"""
- Used by DummyPath to implement `open('w')`
+ Used by DummyWritablePath to implement `open('w')`
"""
def __init__(self, files, path):
@@ -931,10 +931,10 @@ class DummyPathIO(io.BytesIO):
super().close()
-class DummyPath(PathBase):
+class DummyReadablePath(ReadablePath):
"""
- Simple implementation of PathBase that keeps files and directories in
- memory.
+ Simple implementation of DummyReadablePath that keeps files and
+ directories in memory.
"""
__slots__ = ('_segments')
@@ -950,7 +950,7 @@ class DummyPath(PathBase):
return ''
def __eq__(self, other):
- if not isinstance(other, DummyPath):
+ if not isinstance(other, DummyReadablePath):
return NotImplemented
return str(self) == str(other)
@@ -990,10 +990,11 @@ class DummyPath(PathBase):
raise FileNotFoundError(errno.ENOENT, "File not found", path)
stream = io.BytesIO(self._files[path])
elif mode == 'w':
+ # FIXME: move to DummyWritablePath
parent, name = posixpath.split(path)
if parent not in self._directories:
raise FileNotFoundError(errno.ENOENT, "File not found", parent)
- stream = DummyPathIO(self._files, path)
+ stream = DummyWritablePathIO(self._files, path)
self._files[path] = b''
self._directories[parent].add(name)
else:
@@ -1011,6 +1012,10 @@ class DummyPath(PathBase):
else:
raise FileNotFoundError(errno.ENOENT, "File not found", path)
+
+class DummyWritablePath(DummyReadablePath, WritablePath):
+ __slots__ = ()
+
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
path = str(self)
parent = str(self.parent)
@@ -1029,24 +1034,11 @@ class DummyPath(PathBase):
self.parent.mkdir(parents=True, exist_ok=True)
self.mkdir(mode, parents=False, exist_ok=exist_ok)
- def _delete(self):
- path = str(self)
- if path in self._files:
- del self._files[path]
- elif path in self._directories:
- for name in list(self._directories[path]):
- self.joinpath(name)._delete()
- del self._directories[path]
- else:
- raise FileNotFoundError(errno.ENOENT, "File not found", path)
- parent = str(self.parent)
- self._directories[parent].remove(self.name)
-
-class DummyPathTest(DummyPurePathTest):
- """Tests for PathBase methods that use stat(), open() and iterdir()."""
+class DummyReadablePathTest(DummyJoinablePathTest):
+ """Tests for ReadablePathTest methods that use stat(), open() and iterdir()."""
- cls = DummyPath
+ cls = DummyReadablePath
can_symlink = False
# (self.base)
@@ -1138,213 +1130,6 @@ class DummyPathTest(DummyPurePathTest):
self.assertIsInstance(f, io.BufferedIOBase)
self.assertEqual(f.read().strip(), b"this is file A")
- def test_read_write_bytes(self):
- p = self.cls(self.base)
- (p / 'fileA').write_bytes(b'abcdefg')
- self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
- # Check that trying to write str does not truncate the file.
- self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr')
- self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
-
- def test_read_write_text(self):
- p = self.cls(self.base)
- (p / 'fileA').write_text('äbcdefg', encoding='latin-1')
- self.assertEqual((p / 'fileA').read_text(
- encoding='utf-8', errors='ignore'), 'bcdefg')
- # Check that trying to write bytes does not truncate the file.
- self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
- self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
-
- def test_read_text_with_newlines(self):
- p = self.cls(self.base)
- # Check that `\n` character change nothing
- (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
- self.assertEqual((p / 'fileA').read_text(newline='\n'),
- 'abcde\r\nfghlk\n\rmnopq')
- # Check that `\r` character replaces `\n`
- (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
- self.assertEqual((p / 'fileA').read_text(newline='\r'),
- 'abcde\r\nfghlk\n\rmnopq')
- # Check that `\r\n` character replaces `\n`
- (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
- self.assertEqual((p / 'fileA').read_text(newline='\r\n'),
- 'abcde\r\nfghlk\n\rmnopq')
-
- def test_write_text_with_newlines(self):
- p = self.cls(self.base)
- # Check that `\n` character change nothing
- (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
- self.assertEqual((p / 'fileA').read_bytes(),
- b'abcde\r\nfghlk\n\rmnopq')
- # Check that `\r` character replaces `\n`
- (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
- self.assertEqual((p / 'fileA').read_bytes(),
- b'abcde\r\rfghlk\r\rmnopq')
- # Check that `\r\n` character replaces `\n`
- (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
- self.assertEqual((p / 'fileA').read_bytes(),
- b'abcde\r\r\nfghlk\r\n\rmnopq')
- # Check that no argument passed will change `\n` to `os.linesep`
- os_linesep_byte = bytes(os.linesep, encoding='ascii')
- (p / 'fileA').write_text('abcde\nfghlk\n\rmnopq')
- self.assertEqual((p / 'fileA').read_bytes(),
- b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq')
-
- def test_copy_file(self):
- base = self.cls(self.base)
- source = base / 'fileA'
- target = base / 'copyA'
- result = source.copy(target)
- self.assertEqual(result, target)
- self.assertTrue(target.exists())
- self.assertEqual(source.read_text(), target.read_text())
-
- def test_copy_file_to_existing_file(self):
- base = self.cls(self.base)
- source = base / 'fileA'
- target = base / 'dirB' / 'fileB'
- result = source.copy(target)
- self.assertEqual(result, target)
- self.assertTrue(target.exists())
- self.assertEqual(source.read_text(), target.read_text())
-
- def test_copy_file_to_existing_directory(self):
- base = self.cls(self.base)
- source = base / 'fileA'
- target = base / 'dirA'
- self.assertRaises(OSError, source.copy, target)
-
- def test_copy_file_empty(self):
- base = self.cls(self.base)
- source = base / 'empty'
- target = base / 'copyA'
- source.write_bytes(b'')
- result = source.copy(target)
- self.assertEqual(result, target)
- self.assertTrue(target.exists())
- self.assertEqual(target.read_bytes(), b'')
-
- def test_copy_file_to_itself(self):
- base = self.cls(self.base)
- source = base / 'empty'
- source.write_bytes(b'')
- self.assertRaises(OSError, source.copy, source)
- self.assertRaises(OSError, source.copy, source, follow_symlinks=False)
-
- def test_copy_dir_simple(self):
- base = self.cls(self.base)
- source = base / 'dirC'
- target = base / 'copyC'
- result = source.copy(target)
- self.assertEqual(result, target)
- self.assertTrue(target.is_dir())
- self.assertTrue(target.joinpath('dirD').is_dir())
- self.assertTrue(target.joinpath('dirD', 'fileD').is_file())
- self.assertEqual(target.joinpath('dirD', 'fileD').read_text(),
- "this is file D\n")
- self.assertTrue(target.joinpath('fileC').is_file())
- self.assertTrue(target.joinpath('fileC').read_text(),
- "this is file C\n")
-
- def test_copy_dir_complex(self, follow_symlinks=True):
- def ordered_walk(path):
- for dirpath, dirnames, filenames in path.walk(follow_symlinks=follow_symlinks):
- dirnames.sort()
- filenames.sort()
- yield dirpath, dirnames, filenames
- base = self.cls(self.base)
- source = base / 'dirC'
-
- if self.can_symlink:
- # Add some symlinks
- source.joinpath('linkC').symlink_to('fileC')
- source.joinpath('linkD').symlink_to('dirD', target_is_directory=True)
-
- # Perform the copy
- target = base / 'copyC'
- result = source.copy(target, follow_symlinks=follow_symlinks)
- self.assertEqual(result, target)
-
- # Compare the source and target trees
- source_walk = ordered_walk(source)
- target_walk = ordered_walk(target)
- for source_item, target_item in zip(source_walk, target_walk, strict=True):
- self.assertEqual(source_item[0].parts[len(source.parts):],
- target_item[0].parts[len(target.parts):]) # dirpath
- self.assertEqual(source_item[1], target_item[1]) # dirnames
- self.assertEqual(source_item[2], target_item[2]) # filenames
- # Compare files and symlinks
- for filename in source_item[2]:
- source_file = source_item[0].joinpath(filename)
- target_file = target_item[0].joinpath(filename)
- if follow_symlinks or not source_file.is_symlink():
- # Regular file.
- self.assertEqual(source_file.read_bytes(), target_file.read_bytes())
- elif source_file.is_dir():
- # Symlink to directory.
- self.assertTrue(target_file.is_dir())
- self.assertEqual(source_file.readlink(), target_file.readlink())
- else:
- # Symlink to file.
- self.assertEqual(source_file.read_bytes(), target_file.read_bytes())
- self.assertEqual(source_file.readlink(), target_file.readlink())
-
- def test_copy_dir_complex_follow_symlinks_false(self):
- self.test_copy_dir_complex(follow_symlinks=False)
-
- def test_copy_dir_to_existing_directory(self):
- base = self.cls(self.base)
- source = base / 'dirC'
- target = base / 'copyC'
- target.mkdir()
- target.joinpath('dirD').mkdir()
- self.assertRaises(FileExistsError, source.copy, target)
-
- def test_copy_dir_to_existing_directory_dirs_exist_ok(self):
- base = self.cls(self.base)
- source = base / 'dirC'
- target = base / 'copyC'
- target.mkdir()
- target.joinpath('dirD').mkdir()
- result = source.copy(target, dirs_exist_ok=True)
- self.assertEqual(result, target)
- self.assertTrue(target.is_dir())
- self.assertTrue(target.joinpath('dirD').is_dir())
- self.assertTrue(target.joinpath('dirD', 'fileD').is_file())
- self.assertEqual(target.joinpath('dirD', 'fileD').read_text(),
- "this is file D\n")
- self.assertTrue(target.joinpath('fileC').is_file())
- self.assertTrue(target.joinpath('fileC').read_text(),
- "this is file C\n")
-
- def test_copy_dir_to_itself(self):
- base = self.cls(self.base)
- source = base / 'dirC'
- self.assertRaises(OSError, source.copy, source)
- self.assertRaises(OSError, source.copy, source, follow_symlinks=False)
-
- def test_copy_dir_into_itself(self):
- base = self.cls(self.base)
- source = base / 'dirC'
- target = base / 'dirC' / 'dirD' / 'copyC'
- self.assertRaises(OSError, source.copy, target)
- self.assertRaises(OSError, source.copy, target, follow_symlinks=False)
- self.assertFalse(target.exists())
-
- def test_copy_into(self):
- base = self.cls(self.base)
- source = base / 'fileA'
- target_dir = base / 'dirA'
- result = source.copy_into(target_dir)
- self.assertEqual(result, target_dir / 'fileA')
- self.assertTrue(result.exists())
- self.assertEqual(source.read_text(), result.read_text())
-
- def test_copy_into_empty_name(self):
- source = self.cls('')
- target_dir = self.base
- self.assertRaises(ValueError, source.copy_into, target_dir)
-
def test_iterdir(self):
P = self.cls
p = P(self.base)
@@ -1574,9 +1359,220 @@ class DummyPathTest(DummyPurePathTest):
self.assertIs((P / 'linkA\x00').is_file(), False)
-class DummyPathWalkTest(unittest.TestCase):
- cls = DummyPath
- base = DummyPathTest.base
+class DummyWritablePathTest(DummyReadablePathTest):
+ cls = DummyWritablePath
+
+ def test_read_write_bytes(self):
+ p = self.cls(self.base)
+ (p / 'fileA').write_bytes(b'abcdefg')
+ self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
+ # Check that trying to write str does not truncate the file.
+ self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr')
+ self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
+
+ def test_read_write_text(self):
+ p = self.cls(self.base)
+ (p / 'fileA').write_text('äbcdefg', encoding='latin-1')
+ self.assertEqual((p / 'fileA').read_text(
+ encoding='utf-8', errors='ignore'), 'bcdefg')
+ # Check that trying to write bytes does not truncate the file.
+ self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
+ self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
+
+ def test_read_text_with_newlines(self):
+ p = self.cls(self.base)
+ # Check that `\n` character change nothing
+ (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+ self.assertEqual((p / 'fileA').read_text(newline='\n'),
+ 'abcde\r\nfghlk\n\rmnopq')
+ # Check that `\r` character replaces `\n`
+ (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+ self.assertEqual((p / 'fileA').read_text(newline='\r'),
+ 'abcde\r\nfghlk\n\rmnopq')
+ # Check that `\r\n` character replaces `\n`
+ (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+ self.assertEqual((p / 'fileA').read_text(newline='\r\n'),
+ 'abcde\r\nfghlk\n\rmnopq')
+
+ def test_write_text_with_newlines(self):
+ p = self.cls(self.base)
+ # Check that `\n` character change nothing
+ (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
+ self.assertEqual((p / 'fileA').read_bytes(),
+ b'abcde\r\nfghlk\n\rmnopq')
+ # Check that `\r` character replaces `\n`
+ (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
+ self.assertEqual((p / 'fileA').read_bytes(),
+ b'abcde\r\rfghlk\r\rmnopq')
+ # Check that `\r\n` character replaces `\n`
+ (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
+ self.assertEqual((p / 'fileA').read_bytes(),
+ b'abcde\r\r\nfghlk\r\n\rmnopq')
+ # Check that no argument passed will change `\n` to `os.linesep`
+ os_linesep_byte = bytes(os.linesep, encoding='ascii')
+ (p / 'fileA').write_text('abcde\nfghlk\n\rmnopq')
+ self.assertEqual((p / 'fileA').read_bytes(),
+ b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq')
+
+ def test_copy_file(self):
+ base = self.cls(self.base)
+ source = base / 'fileA'
+ target = base / 'copyA'
+ result = source.copy(target)
+ self.assertEqual(result, target)
+ self.assertTrue(target.exists())
+ self.assertEqual(source.read_text(), target.read_text())
+
+ def test_copy_file_to_existing_file(self):
+ base = self.cls(self.base)
+ source = base / 'fileA'
+ target = base / 'dirB' / 'fileB'
+ result = source.copy(target)
+ self.assertEqual(result, target)
+ self.assertTrue(target.exists())
+ self.assertEqual(source.read_text(), target.read_text())
+
+ def test_copy_file_to_existing_directory(self):
+ base = self.cls(self.base)
+ source = base / 'fileA'
+ target = base / 'dirA'
+ self.assertRaises(OSError, source.copy, target)
+
+ def test_copy_file_empty(self):
+ base = self.cls(self.base)
+ source = base / 'empty'
+ target = base / 'copyA'
+ source.write_bytes(b'')
+ result = source.copy(target)
+ self.assertEqual(result, target)
+ self.assertTrue(target.exists())
+ self.assertEqual(target.read_bytes(), b'')
+
+ def test_copy_file_to_itself(self):
+ base = self.cls(self.base)
+ source = base / 'empty'
+ source.write_bytes(b'')
+ self.assertRaises(OSError, source.copy, source)
+ self.assertRaises(OSError, source.copy, source, follow_symlinks=False)
+
+ def test_copy_dir_simple(self):
+ base = self.cls(self.base)
+ source = base / 'dirC'
+ target = base / 'copyC'
+ result = source.copy(target)
+ self.assertEqual(result, target)
+ self.assertTrue(target.is_dir())
+ self.assertTrue(target.joinpath('dirD').is_dir())
+ self.assertTrue(target.joinpath('dirD', 'fileD').is_file())
+ self.assertEqual(target.joinpath('dirD', 'fileD').read_text(),
+ "this is file D\n")
+ self.assertTrue(target.joinpath('fileC').is_file())
+ self.assertTrue(target.joinpath('fileC').read_text(),
+ "this is file C\n")
+
+ def test_copy_dir_complex(self, follow_symlinks=True):
+ def ordered_walk(path):
+ for dirpath, dirnames, filenames in path.walk(follow_symlinks=follow_symlinks):
+ dirnames.sort()
+ filenames.sort()
+ yield dirpath, dirnames, filenames
+ base = self.cls(self.base)
+ source = base / 'dirC'
+
+ if self.can_symlink:
+ # Add some symlinks
+ source.joinpath('linkC').symlink_to('fileC')
+ source.joinpath('linkD').symlink_to('dirD', target_is_directory=True)
+
+ # Perform the copy
+ target = base / 'copyC'
+ result = source.copy(target, follow_symlinks=follow_symlinks)
+ self.assertEqual(result, target)
+
+ # Compare the source and target trees
+ source_walk = ordered_walk(source)
+ target_walk = ordered_walk(target)
+ for source_item, target_item in zip(source_walk, target_walk, strict=True):
+ self.assertEqual(source_item[0].parts[len(source.parts):],
+ target_item[0].parts[len(target.parts):]) # dirpath
+ self.assertEqual(source_item[1], target_item[1]) # dirnames
+ self.assertEqual(source_item[2], target_item[2]) # filenames
+ # Compare files and symlinks
+ for filename in source_item[2]:
+ source_file = source_item[0].joinpath(filename)
+ target_file = target_item[0].joinpath(filename)
+ if follow_symlinks or not source_file.is_symlink():
+ # Regular file.
+ self.assertEqual(source_file.read_bytes(), target_file.read_bytes())
+ elif source_file.is_dir():
+ # Symlink to directory.
+ self.assertTrue(target_file.is_dir())
+ self.assertEqual(source_file.readlink(), target_file.readlink())
+ else:
+ # Symlink to file.
+ self.assertEqual(source_file.read_bytes(), target_file.read_bytes())
+ self.assertEqual(source_file.readlink(), target_file.readlink())
+
+ def test_copy_dir_complex_follow_symlinks_false(self):
+ self.test_copy_dir_complex(follow_symlinks=False)
+
+ def test_copy_dir_to_existing_directory(self):
+ base = self.cls(self.base)
+ source = base / 'dirC'
+ target = base / 'copyC'
+ target.mkdir()
+ target.joinpath('dirD').mkdir()
+ self.assertRaises(FileExistsError, source.copy, target)
+
+ def test_copy_dir_to_existing_directory_dirs_exist_ok(self):
+ base = self.cls(self.base)
+ source = base / 'dirC'
+ target = base / 'copyC'
+ target.mkdir()
+ target.joinpath('dirD').mkdir()
+ result = source.copy(target, dirs_exist_ok=True)
+ self.assertEqual(result, target)
+ self.assertTrue(target.is_dir())
+ self.assertTrue(target.joinpath('dirD').is_dir())
+ self.assertTrue(target.joinpath('dirD', 'fileD').is_file())
+ self.assertEqual(target.joinpath('dirD', 'fileD').read_text(),
+ "this is file D\n")
+ self.assertTrue(target.joinpath('fileC').is_file())
+ self.assertTrue(target.joinpath('fileC').read_text(),
+ "this is file C\n")
+
+ def test_copy_dir_to_itself(self):
+ base = self.cls(self.base)
+ source = base / 'dirC'
+ self.assertRaises(OSError, source.copy, source)
+ self.assertRaises(OSError, source.copy, source, follow_symlinks=False)
+
+ def test_copy_dir_into_itself(self):
+ base = self.cls(self.base)
+ source = base / 'dirC'
+ target = base / 'dirC' / 'dirD' / 'copyC'
+ self.assertRaises(OSError, source.copy, target)
+ self.assertRaises(OSError, source.copy, target, follow_symlinks=False)
+ self.assertFalse(target.exists())
+
+ def test_copy_into(self):
+ base = self.cls(self.base)
+ source = base / 'fileA'
+ target_dir = base / 'dirA'
+ result = source.copy_into(target_dir)
+ self.assertEqual(result, target_dir / 'fileA')
+ self.assertTrue(result.exists())
+ self.assertEqual(source.read_text(), result.read_text())
+
+ def test_copy_into_empty_name(self):
+ source = self.cls('')
+ target_dir = self.base
+ self.assertRaises(ValueError, source.copy_into, target_dir)
+
+
+class DummyReadablePathWalkTest(unittest.TestCase):
+ cls = DummyReadablePath
+ base = DummyReadablePathTest.base
can_symlink = False
def setUp(self):