8000 GH-73991: Add `pathlib.Path.copytree()` by barneygale · Pull Request #120718 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73991: Add pathlib.Path.copytree() #120718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve tests
  • Loading branch information
barneygale committed Jun 23, 2024
commit 8c453bbcc320f4352a080af5de4aaa9590c6f5ce
7 changes: 2 additions & 5 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,14 @@ def setUp(self):
os.chmod(self.parser.join(self.base, 'dirE'), 0)

def tearDown(self):
try:
os.chmod(self.parser.join(self.base, 'dirE'), 0o777)
except FileNotFoundError:
pass
os.chmod(self.parser.join(self.base, 'dirE'), 0o777)
os_helper.rmtree(self.base)

def tempdir(self):
d = os_helper._longpath(tempfile.mkdtemp(suffix='-dirD',
dir=os.getcwd()))
self.addCleanup(os_helper.rmtree, d)
return self.cls(d)
return d

def test_matches_pathbase_api(self):
our_names = {name for name in dir(self.cls) if name[0] != '_'}
Expand Down
57 changes: 25 additions & 32 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,24 +1836,23 @@ def test_copytree_simple(self):
self.assertTrue(target.joinpath('fileC').read_text(),
"this is file C\n")

def test_copytree_complex(self):
def test_copytree_complex(self, follow_symlinks=True):
def ordered_walk(path):
for dirpath, dirnames, filenames in path.walk(follow_symlinks=True):
for dirpath, dirnames, filenames in path.walk(follow_symlinks=follow_symlinks):
dirnames.sort()
filenames.sort()
yield dirpath, dirnames, filenames
source = self.cls(self.base)
target = self.tempdir() / 'target'
base = self.cls(self.base)
source = base / 'dirC'

# Special cases tested elsehwere
source.joinpath('dirE').rmdir()
if self.can_symlink:
source.joinpath('brokenLink').unlink()
source.joinpath('brokenLinkLoop').unlink()
source.joinpath('dirB', 'linkD').unlink()
# Add some symlinks
source.joinpath('linkC').symlink_to('fileC')
source.joinpath('linkD').symlink_to('dirD')

# Perform the copy
source.copytree(target)
target = base / 'copyC'
source.copytree(target, follow_symlinks=follow_symlinks)

# Compare the source and target trees
source_walk = ordered_walk(source)
Expand All @@ -1863,30 +1862,24 @@ def ordered_walk(path):
target_item[0].relative_to(target)) # 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_copytree_complex_follow_symlinks_false(self):
def ordered_walk(path):
for dirpath, dirnames, filenames in path.walk(follow_symlinks=False):
dirnames.sort()
filenames.sort()
yield dirpath, dirnames, filenames
source = self.cls(self.base)
target = self.tempdir() / 'target'

# Special cases tested elsewhere
source.joinpath('dirE').rmdir()

# Perform the copy
source.copytree(target, follow_symlinks=False)

# 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].relative_to(source),
target_item[0].relative_to(target)) # dirpath
self.assertEqual(source_item[1], target_item[1]) # dirnames
self.assertEqual(source_item[2], target_item[2]) # filenames
self.test_copytree_complex(follow_symlinks=False)

def test_copytree_to_existing_directory(self):
base = self.cls(self.base)
Expand Down
Loading
0