| 1 |
""" |
| 2 |
Tests for copying from pathlib.types._ReadablePath to _WritablePath. |
| 3 |
""" |
| 4 |
|
| 5 |
import contextlib |
| 6 |
import unittest |
| 7 |
|
| 8 |
from .support import is_pypi |
| 9 |
from .support.local_path import LocalPathGround |
| 10 |
from .support.zip_path import ZipPathGround, ReadableZipPath, WritableZipPath |
| 11 |
|
| 12 |
|
| 13 |
class CopyTestBase: |
| 14 |
def setUp(self): |
| 15 |
self.source_root = self.source_ground.setup() |
| 16 |
self.source_ground.create_hierarchy(self.source_root) |
| 17 |
self.target_root = self.target_ground.setup(local_suffix="_target") |
| 18 |
|
| 19 |
def tearDown(self): |
| 20 |
self.source_ground.teardown(self.source_root) |
| 21 |
self.target_ground.teardown(self.target_root) |
| 22 |
|
| 23 |
def test_copy_file(self): |
| 24 |
source = self.source_root / 'fileA' |
| 25 |
target = self.target_root / 'copyA' |
| 26 |
result = source.copy(target) |
| 27 |
self.assertEqual(result, target) |
| 28 |
self.assertTrue(self.target_ground.isfile(target)) |
| 29 |
self.assertEqual(self.source_ground.readbytes(source), |
| 30 |
self.target_ground.readbytes(result)) |
| 31 |
|
| 32 |
def test_copy_file_empty(self): |
| 33 |
source = self.source_root / 'empty' |
| 34 |
target = self.target_root / 'copyA' |
| 35 |
self.source_ground.create_file(source, b'') |
| 36 |
result = source.copy(target) |
| 37 |
self.assertEqual(result, target) |
| 38 |
self.assertTrue(self.target_ground.isfile(target)) |
| 39 |
self.assertEqual(self.target_ground.readbytes(result), b'') |
| 40 |
|
| 41 |
def test_copy_file_to_existing_file(self): |
| 42 |
source = self.source_root / 'fileA' |
| 43 |
target = self.target_root / 'copyA' |
| 44 |
self.target_ground.create_file(target, b'this is a copy\n') |
| 45 |
with contextlib.ExitStack() as stack: |
| 46 |
if isinstance(target, WritableZipPath): |
| 47 |
stack.enter_context(self.assertWarns(UserWarning)) |
| 48 |
result = source.copy(target) |
| 49 |
self.assertEqual(result, target) |
| 50 |
self.assertTrue(self.target_ground.isfile(target)) |
| 51 |
self.assertEqual(self.source_ground.readbytes(source), |
| 52 |
self.target_ground.readbytes(result)) |
| 53 |
|
| 54 |
def test_copy_file_to_directory(self): |
| 55 |
if isinstance(self.target_root, WritableZipPath): |
| 56 |
self.skipTest('needs local target') |
| 57 |
source = self.source_root / 'fileA' |
| 58 |
target = self.target_root / 'copyA' |
| 59 |
self.target_ground.create_dir(target) |
| 60 |
self.assertRaises(OSError, source.copy, target) |
| 61 |
|
| 62 |
def test_copy_file_to_itself(self): |
| 63 |
source = self.source_root / 'fileA' |
| 64 |
self.assertRaises(OSError, source.copy, source) |
| 65 |
self.assertRaises(OSError, source.copy, source, follow_symlinks=False) |
| 66 |
|
| 67 |
def test_copy_dir(self): |
| 68 |
source = self.source_root / 'dirC' |
| 69 |
target = self.target_root / 'copyC' |
| 70 |
result = source.copy(target) |
| 71 |
self.assertEqual(result, target) |
| 72 |
self.assertTrue(self.target_ground.isdir(target)) |
| 73 |
self.assertTrue(self.target_ground.isfile(target / 'fileC')) |
| 74 |
self.assertEqual(self.target_ground.readtext(target / 'fileC'), 'this is file C\n') |
| 75 |
self.assertTrue(self.target_ground.isdir(target / 'dirD')) |
| 76 |
self.assertTrue(self.target_ground.isfile(target / 'dirD' / 'fileD')) |
| 77 |
self.assertEqual(self.target_ground.readtext(target / 'dirD' / 'fileD'), 'this is file D\n') |
| 78 |
|
| 79 |
def test_copy_dir_follow_symlinks_true(self): |
| 80 |
if not self.source_ground.can_symlink: |
| 81 |
self.skipTest('needs symlink support on source') |
| 82 |
source = self.source_root / 'dirC' |
| 83 |
target = self.target_root / 'copyC' |
| 84 |
self.source_ground.create_symlink(source / 'linkC', 'fileC') |
| 85 |
self.source_ground.create_symlink(source / 'linkD', 'dirD') |
| 86 |
result = source.copy(target) |
| 87 |
self.assertEqual(result, target) |
| 88 |
self.assertTrue(self.target_ground.isdir(target)) |
| 89 |
self.assertFalse(self.target_ground.islink(target / 'linkC')) |
| 90 |
self.assertTrue(self.target_ground.isfile(target / 'linkC')) |
| 91 |
self.assertEqual(self.target_ground.readtext(target / 'linkC'), 'this is file C\n') |
| 92 |
self.assertFalse(self.target_ground.islink(target / 'linkD')) |
| 93 |
self.assertTrue(self.target_ground.isdir(target / 'linkD')) |
| 94 |
self.assertTrue(self.target_ground.isfile(target / 'linkD' / 'fileD')) |
| 95 |
self.assertEqual(self.target_ground.readtext(target / 'linkD' / 'fileD'), 'this is file D\n') |
| 96 |
|
| 97 |
def test_copy_dir_follow_symlinks_false(self): |
| 98 |
if not self.source_ground.can_symlink: |
| 99 |
self.skipTest('needs symlink support on source') |
| 100 |
if not self.target_ground.can_symlink: |
| 101 |
self.skipTest('needs symlink support on target') |
| 102 |
source = self.source_root / 'dirC' |
| 103 |
target = self.target_root / 'copyC' |
| 104 |
self.source_ground.create_symlink(source / 'linkC', 'fileC') |
| 105 |
self.source_ground.create_symlink(source / 'linkD', 'dirD') |
| 106 |
result = source.copy(target, follow_symlinks=False) |
| 107 |
self.assertEqual(result, target) |
| 108 |
self.assertTrue(self.target_ground.isdir(target)) |
| 109 |
self.assertTrue(self.target_ground.islink(target / 'linkC')) |
| 110 |
self.assertEqual(self.target_ground.readlink(target / 'linkC'), 'fileC') |
| 111 |
self.assertTrue(self.target_ground.islink(target / 'linkD')) |
| 112 |
self.assertEqual(self.target_ground.readlink(target / 'linkD'), 'dirD') |
| 113 |
|
| 114 |
def test_copy_dir_to_existing_directory(self): |
| 115 |
if isinstance(self.target_root, WritableZipPath): |
| 116 |
self.skipTest('needs local target') |
| 117 |
source = self.source_root / 'dirC' |
| 118 |
target = self.target_root / 'copyC' |
| 119 |
self.target_ground.create_dir(target) |
| 120 |
self.assertRaises(FileExistsError, source.copy, target) |
| 121 |
|
| 122 |
def test_copy_dir_to_itself(self): |
| 123 |
source = self.source_root / 'dirC' |
| 124 |
self.assertRaises(OSError, source.copy, source) |
| 125 |
self.assertRaises(OSError, source.copy, source, follow_symlinks=False) |
| 126 |
|
| 127 |
def test_copy_dir_into_itself(self): |
| 128 |
source = self.source_root / 'dirC' |
| 129 |
target = self.source_root / 'dirC' / 'dirD' / 'copyC' |
| 130 |
self.assertRaises(OSError, source.copy, target) |
| 131 |
self.assertRaises(OSError, source.copy, target, follow_symlinks=False) |
| 132 |
|
| 133 |
def test_copy_into(self): |
| 134 |
source = self.source_root / 'fileA' |
| 135 |
target_dir = self.target_root / 'dirA' |
| 136 |
self.target_ground.create_dir(target_dir) |
| 137 |
result = source.copy_into(target_dir) |
| 138 |
self.assertEqual(result, target_dir / 'fileA') |
| 139 |
self.assertTrue(self.target_ground.isfile(result)) |
| 140 |
self.assertEqual(self.source_ground.readbytes(source), |
| 141 |
self.target_ground.readbytes(result)) |
| 142 |
|
| 143 |
def test_copy_into_empty_name(self): |
| 144 |
source = self.source_root.with_segments() |
| 145 |
target_dir = self.target_root / 'dirA' |
| 146 |
self.target_ground.create_dir(target_dir) |
| 147 |
self.assertRaises(ValueError, source.copy_into, target_dir) |
| 148 |
|
| 149 |
|
| 150 |
class ZipToZipPathCopyTest(CopyTestBase, unittest.TestCase): |
| 151 |
source_ground = ZipPathGround(ReadableZipPath) |
| 152 |
target_ground = ZipPathGround(WritableZipPath) |
| 153 |
|
| 154 |
|
| 155 |
if not is_pypi: |
| 156 |
from pathlib import Path |
| 157 |
|
| 158 |
class ZipToLocalPathCopyTest(CopyTestBase, unittest.TestCase): |
| 159 |
source_ground = ZipPathGround(ReadableZipPath) |
| 160 |
target_ground = LocalPathGround(Path) |
| 161 |
|
| 162 |
|
| 163 |
class LocalToZipPathCopyTest(CopyTestBase, unittest.TestCase): |
| 164 |
source_ground = LocalPathGround(Path) |
| 165 |
target_ground = ZipPathGround(WritableZipPath) |
| 166 |
|
| 167 |
|
| 168 |
class LocalToLocalPathCopyTest(CopyTestBase, unittest.TestCase): |
| 169 |
source_ground = LocalPathGround(Path) |
| 170 |
target_ground = LocalPathGround(Path) |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == "__main__": |
| 174 |
unittest.main() |