@@ -80,6 +80,30 @@ def __repr__(self):
80
80
return "TestIterableMember(%r)" % self .name
81
81
82
82
83
+ @contextlib .contextmanager
84
+ def _tmpdir_to_force_permission_error ():
85
+ """Context manager to test permission errors in situations where we do not fix them."""
86
+ if sys .platform == "cygwin" :
87
+ raise SkipTest ("Cygwin can't set the permissions that make the test meaningful." )
88
+ if sys .version_info < (3 , 8 ):
89
+ raise SkipTest ("In 3.7, TemporaryDirectory doesn't clean up after weird permissions." )
90
+
91
+ with tempfile .TemporaryDirectory () as parent :
92
+ td = pathlib .Path (parent , "testdir" )
93
+ td .mkdir ()
94
+ (td / "x" ).write_bytes (b"" )
95
+ (td / "x" ).chmod (stat .S_IRUSR ) # Set up PermissionError on Windows.
96
+ td .chmod (stat .S_IRUSR | stat .S_IXUSR ) # Set up PermissionError on Unix.
97
+ yield td
98
+
99
+
100
+ @contextlib .contextmanager
101
+ def _tmpdir_for_file_not_found ():
102
+ """Context manager to test errors deleting a directory that are not due to permissions."""
103
+ with tempfile .TemporaryDirectory () as parent :
104
+ yield pathlib .Path (parent , "testdir" ) # It is deliberately never created.
105
+
106
+
83
107
@ddt .ddt
84
108
class TestUtils (TestBase ):
85
109
def setup (self ):
@@ -107,6 +131,7 @@ def test_rmtree_deletes_nested_dir_with_files(self):
107
131
@skipIf (sys .platform == "cygwin" , "Cygwin can't set the permissions that make the test meaningful." )
108
132
def test_rmtree_deletes_dir_with_readonly_files (self ):
109
133
# Automatically works on Unix, but requires special handling on Windows.
134
+ # Not to be confused with _tmpdir_to_force_permission_error (which is used below).
110
135
with tempfile .TemporaryDirectory () as parent :
111
136
td = pathlib .Path (parent , "testdir" )
112
137
for d in td , td / "sub" :
@@ -122,31 +147,9 @@ def test_rmtree_deletes_dir_with_readonly_files(self):
122
147
123
148
self .assertFalse (td .exists ())
124
149
125
- @staticmethod
126
- @contextlib .contextmanager
127
- def _tmpdir_to_force_permission_error ():
128
- if sys .platform == "cygwin" :
129
- raise SkipTest ("Cygwin can't set the permissions that make the test meaningful." )
130
- if sys .version_info < (3 , 8 ):
131
- raise SkipTest ("In 3.7, TemporaryDirectory doesn't clean up after weird permissions." )
132
-
133
- with tempfile .TemporaryDirectory () as parent :
134
- td = pathlib .Path (parent , "testdir" )
135
- td .mkdir ()
136
- (td / "x" ).write_bytes (b"" )
137
- (td / "x" ).chmod (stat .S_IRUSR ) # Set up PermissionError on Windows.
138
- td .chmod (stat .S_IRUSR | stat .S_IXUSR ) # Set up PermissionError on Unix.
139
- yield td
140
-
141
- @staticmethod
142
- @contextlib .contextmanager
143
- def _tmpdir_for_file_not_found ():
144
- with tempfile .TemporaryDirectory () as parent :
145
- yield pathlib .Path (parent , "testdir" ) # It is deliberately never created.
146
-
147
150
def test_rmtree_can_wrap_exceptions (self ):
148
151
"""Our rmtree wraps PermissionError when HIDE_WINDOWS_KNOWN_ERRORS is true."""
149
- with self . _tmpdir_to_force_permission_error () as td :
152
+ with _tmpdir_to_force_permission_error () as td :
150
153
# Access the module through sys.modules so it is unambiguous which module's
151
154
# attribute we patch: the original git.util, not git.index.util even though
152
155
# git.index.util "replaces" git.util and is what "import git.util" gives us.
0 commit comments