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