|
37 | 37 | import shutil |
38 | 38 | import unittest |
39 | 39 | import copy |
40 | | -from os import listdir |
| 40 | +import stat |
| 41 | +from os import listdir, chmod |
41 | 42 | from os.path import abspath, dirname, exists, join, isdir, isfile |
42 | 43 | from tempfile import mkdtemp |
43 | 44 | try: |
@@ -171,9 +172,7 @@ def _run_test(self, testname): |
171 | 172 | self._assert_dirs_equal(join(basepath, "[result]"), |
172 | 173 | tmpdir, |
173 | 174 | ignore=["%s.patch" % testname, ".svn", ".gitkeep", "[result]"]) |
174 | | - |
175 | | - |
176 | | - shutil.rmtree(tmpdir) |
| 175 | + remove_tree_force(tmpdir) |
177 | 176 | return 0 |
178 | 177 |
|
179 | 178 |
|
@@ -362,7 +361,7 @@ def setUp(self): |
362 | 361 |
|
363 | 362 | def tearDown(self): |
364 | 363 | os.chdir(self.save_cwd) |
365 | | - shutil.rmtree(self.tmpdir) |
| 364 | + remove_tree_force(self.tmpdir) |
366 | 365 |
|
367 | 366 | def tmpcopy(self, filenames): |
368 | 367 | """copy file(s) from test_dir to self.tmpdir""" |
@@ -446,6 +445,17 @@ def test_fuzzy_patch_after(self): |
446 | 445 | self.assertTrue(pto.apply(root=treeroot, fuzz=True)) |
447 | 446 | self.assertFalse(pto.apply(root=treeroot, fuzz=False)) |
448 | 447 |
|
| 448 | + def test_unlink_backup_windows(self): |
| 449 | + """ Apply patch to a read-only file and don't change its filemode |
| 450 | + """ |
| 451 | + treeroot = join(self.tmpdir, 'rootparent') |
| 452 | + shutil.copytree(join(TESTS, '11permission'), treeroot) |
| 453 | + pto = patch_ng.fromfile(join(TESTS, '11permission', '11permission.patch')) |
| 454 | + some_file = join(treeroot, 'some_file') |
| 455 | + chmod(some_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 456 | + self.assertTrue(pto.apply(root=treeroot)) |
| 457 | + self.assertTrue(os.stat(some_file).st_mode, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 458 | + |
449 | 459 |
|
450 | 460 | class TestHelpers(unittest.TestCase): |
451 | 461 | # unittest setting |
@@ -478,6 +488,12 @@ def test_pathstrip(self): |
478 | 488 | self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 1), b'name.diff') |
479 | 489 | self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 0), b'path/name.diff') |
480 | 490 |
|
| 491 | +def remove_tree_force(folder): |
| 492 | + for root, _, files in os.walk(folder): |
| 493 | + for it in files: |
| 494 | + chmod(os.path.join(root, it), stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 495 | + shutil.rmtree(folder, ignore_errors=True) |
| 496 | + |
481 | 497 | # ---------------------------------------------------------------------------- |
482 | 498 |
|
483 | 499 | if __name__ == '__main__': |
|
0 commit comments