Skip to content

Commit 2619a22

Browse files
committed
Force file mode when testing permission
Signed-off-by: Uilian Ries <uilianries@gmail.com>
1 parent bd3ed65 commit 2619a22

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

patch_ng.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from __future__ import print_function
3232

3333
__author__ = "Conan.io <info@conan.io>"
34-
__version__ = "1.17.3"
34+
__version__ = "1.17.4"
3535
__license__ = "MIT"
3636
__url__ = "https://github.com/conan-io/python-patch"
3737

@@ -1107,13 +1107,13 @@ def apply(self, strip=0, root=None, fuzz=False):
11071107
shutil.move(filenamen, backupname)
11081108
if self.write_hunks(backupname if filenameo == filenamen else filenameo, filenamen, p.hunks):
11091109
info("successfully patched %d/%d:\t %s" % (i+1, total, filenamen))
1110-
os.chmod(backupname, stat.S_IWRITE)
1110+
os.chmod(backupname, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
11111111
os.unlink(backupname)
11121112
if new == b'/dev/null':
11131113
# check that filename is of size 0 and delete it.
11141114
if os.path.getsize(filenamen) > 0:
11151115
warning("expected patched file to be empty as it's marked as deletion:\t %s" % filenamen)
1116-
os.chmod(filenamen, stat.S_IWRITE)
1116+
os.chmod(backupname, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
11171117
os.unlink(filenamen)
11181118
else:
11191119
errors += 1

tests/run_tests.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
import shutil
3838
import unittest
3939
import copy
40-
from os import listdir
40+
import stat
41+
from os import listdir, chmod
4142
from os.path import abspath, dirname, exists, join, isdir, isfile
4243
from tempfile import mkdtemp
4344
try:
@@ -171,9 +172,7 @@ def _run_test(self, testname):
171172
self._assert_dirs_equal(join(basepath, "[result]"),
172173
tmpdir,
173174
ignore=["%s.patch" % testname, ".svn", ".gitkeep", "[result]"])
174-
175-
176-
shutil.rmtree(tmpdir)
175+
remove_tree_force(tmpdir)
177176
return 0
178177

179178

@@ -362,7 +361,7 @@ def setUp(self):
362361

363362
def tearDown(self):
364363
os.chdir(self.save_cwd)
365-
shutil.rmtree(self.tmpdir)
364+
remove_tree_force(self.tmpdir)
366365

367366
def tmpcopy(self, filenames):
368367
"""copy file(s) from test_dir to self.tmpdir"""
@@ -447,10 +446,15 @@ def test_fuzzy_patch_after(self):
447446
self.assertFalse(pto.apply(root=treeroot, fuzz=False))
448447

449448
def test_unlink_backup_windows(self):
449+
""" Apply patch to a read-only file and don't change its filemode
450+
"""
450451
treeroot = join(self.tmpdir, 'rootparent')
451452
shutil.copytree(join(TESTS, '11permission'), treeroot)
452-
pto = patch_ng.fromfile(join(TESTS, '11permission/11permission.patch'))
453+
pto = patch_ng.fromfile(join(TESTS, '11permission', '11permission.patch'))
454+
some_file = join(TESTS, '11permission', 'some_file')
455+
chmod(some_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
453456
self.assertTrue(pto.apply(root=treeroot))
457+
self.assertTrue(os.stat(some_file).st_mode, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
454458

455459

456460
class TestHelpers(unittest.TestCase):
@@ -484,6 +488,14 @@ def test_pathstrip(self):
484488
self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 1), b'name.diff')
485489
self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 0), b'path/name.diff')
486490

491+
def remove_tree_force(folder):
492+
for root, dirs, files in os.walk(folder):
493+
for it in dirs:
494+
chmod(os.path.join(root, it), stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
495+
for it in files:
496+
chmod(os.path.join(root, it), stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
497+
shutil.rmtree(folder)
498+
487499
# ----------------------------------------------------------------------------
488500

489501
if __name__ == '__main__':

0 commit comments

Comments
 (0)