|
| 1 | +""" |
| 2 | +Tests for copying from pathlib.types._ReadablePath to _WritablePath. |
| 3 | +""" |
| 4 | + |
| 5 | +importcontextlib |
| 6 | +importunittest |
| 7 | + |
| 8 | +frompathlibimportPath |
| 9 | + |
| 10 | +fromtest.test_pathlib.support.local_pathimportLocalPathGround,WritableLocalPath |
| 11 | +fromtest.test_pathlib.support.zip_pathimportZipPathGround,ReadableZipPath,WritableZipPath |
| 12 | + |
| 13 | + |
| 14 | +classCopyTestBase: |
| 15 | +defsetUp(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 | +deftearDown(self): |
| 21 | +self.source_ground.teardown(self.source_root) |
| 22 | +self.target_ground.teardown(self.target_root) |
| 23 | + |
| 24 | +deftest_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 | +deftest_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 | +deftest_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 | +withcontextlib.ExitStack()asstack: |
| 47 | +ifisinstance(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 | +deftest_copy_file_to_directory(self): |
| 56 | +ifnotisinstance(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 | +deftest_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 | +deftest_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 | +deftest_copy_dir_follow_symlinks_true(self): |
| 81 | +ifnotself.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 | +deftest_copy_dir_follow_symlinks_false(self): |
| 99 | +ifnotself.source_ground.can_symlink: |
| 100 | +self.skipTest('needs symlink support on source') |
| 101 | +ifnotself.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 | +deftest_copy_dir_to_existing_directory(self): |
| 116 | +ifnotisinstance(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 | +deftest_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 | +deftest_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 | +deftest_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 | +deftest_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 | +classZipToZipPathCopyTest(CopyTestBase,unittest.TestCase): |
| 152 | +source_ground=ZipPathGround(ReadableZipPath) |
| 153 | +target_ground=ZipPathGround(WritableZipPath) |
| 154 | + |
| 155 | + |
| 156 | +classZipToLocalPathCopyTest(CopyTestBase,unittest.TestCase): |
| 157 | +source_ground=ZipPathGround(ReadableZipPath) |
| 158 | +target_ground=LocalPathGround(Path) |
| 159 | + |
| 160 | + |
| 161 | +classLocalToZipPathCopyTest(CopyTestBase,unittest.TestCase): |
| 162 | +source_ground=LocalPathGround(Path) |
| 163 | +target_ground=ZipPathGround(WritableZipPath) |
| 164 | + |
| 165 | + |
| 166 | +classLocalToLocalPathCopyTest(CopyTestBase,unittest.TestCase): |
| 167 | +source_ground=LocalPathGround(Path) |
| 168 | +target_ground=LocalPathGround(Path) |
| 169 | + |
| 170 | + |
| 171 | +if__name__=="__main__": |
| 172 | +unittest.main() |