File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -158,13 +158,14 @@ def cmd_output(*cmd, **kwargs):
158158
159159def rmtree (path ):
160160 """On windows, rmtree fails for readonly dirs."""
161- def handle_remove_readonly (func , path , exc ): # pragma: no cover (windows)
161+ def handle_remove_readonly (func , path , exc ):
162162 excvalue = exc [1 ]
163163 if (
164164 func in (os .rmdir , os .remove , os .unlink ) and
165165 excvalue .errno == errno .EACCES
166166 ):
167- os .chmod (path , stat .S_IRWXU | stat .S_IRWXG | stat .S_IRWXO )
167+ for p in (path , os .path .dirname (path )):
168+ os .chmod (p , os .stat (p ).st_mode | stat .S_IWUSR )
168169 func (path )
169170 else :
170171 raise
Original file line number Diff line number Diff line change 11from __future__ import unicode_literals
22
33import os .path
4+ import stat
45
56import pytest
67
78from pre_commit .util import CalledProcessError
89from pre_commit .util import clean_path_on_failure
910from pre_commit .util import cmd_output
1011from pre_commit .util import parse_version
12+ from pre_commit .util import rmtree
1113from pre_commit .util import tmpdir
1214
1315
@@ -90,3 +92,14 @@ def test_parse_version():
9092 assert parse_version ('0.0' ) == parse_version ('0.0' )
9193 assert parse_version ('0.1' ) > parse_version ('0.0' )
9294 assert parse_version ('2.1' ) >= parse_version ('2' )
95+
96+
97+ def test_rmtree_read_only_directories (tmpdir ):
98+ """Simulates the go module tree. See #1042"""
99+ tmpdir .join ('x/y/z' ).ensure_dir ().join ('a' ).ensure ()
100+ mode = os .stat (str (tmpdir .join ('x' ))).st_mode
101+ mode_no_w = mode & ~ (stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
102+ tmpdir .join ('x/y/z' ).chmod (mode_no_w )
103+ tmpdir .join ('x/y/z' ).chmod (mode_no_w )
104+ tmpdir .join ('x/y/z' ).chmod (mode_no_w )
105+ rmtree (str (tmpdir .join ('x' )))
You can’t perform that action at this time.
0 commit comments