@@ -109,7 +109,7 @@ def test_deletes_dir_with_readonly_files(self, tmp_path):
109
109
sys .platform == "cygwin" ,
110
110
reason = "Cygwin can't set the permissions that make the test meaningful." ,
111
111
)
112
- def test_avoids_changing_permissions_outside_tree (self , tmp_path : pathlib . Path ):
112
+ def test_avoids_changing_permissions_outside_tree (self , tmp_path ):
113
113
# Automatically works on Windows, but on Unix requires either special handling
114
114
# or refraining from attempting to fix PermissionError by making chmod calls.
115
115
@@ -134,22 +134,24 @@ def test_avoids_changing_permissions_outside_tree(self, tmp_path: pathlib.Path):
134
134
new_mode = (dir1 / "file" ).stat ().st_mode
135
135
assert old_mode == new_mode , f"Should stay { old_mode :#o} , became { new_mode :#o} ."
136
136
137
- @pytest .mark .skipif (
138
- os .name != "nt" ,
139
- reason = "PermissionError is only ever wrapped on Windows" ,
140
- )
141
- def test_wraps_perm_error_if_enabled (self , mocker , permission_error_tmpdir ):
142
- """rmtree wraps PermissionError on Windows when HIDE_WINDOWS_KNOWN_ERRORS is true."""
137
+ def _patch_for_wrapping_test (self , mocker , hide_windows_known_errors ):
143
138
# Access the module through sys.modules so it is unambiguous which module's
144
139
# attribute we patch: the original git.util, not git.index.util even though
145
140
# git.index.util "replaces" git.util and is what "import git.util" gives us.
146
- mocker .patch .object (sys .modules ["git.util" ], "HIDE_WINDOWS_KNOWN_ERRORS" , True )
141
+ mocker .patch .object (sys .modules ["git.util" ], "HIDE_WINDOWS_KNOWN_ERRORS" , hide_windows_known_errors )
147
142
148
- # Disable common chmod functions so the callback can never fix the problem .
143
+ # Disable common chmod functions so the callback can't fix a PermissionError .
149
144
mocker .patch .object (os , "chmod" )
150
145
mocker .patch .object (pathlib .Path , "chmod" )
151
146
152
- # Now we can see how an intractable PermissionError is treated.
147
+ @pytest .mark .skipif (
148
+ os .name != "nt" ,
149
+ reason = "PermissionError is only ever wrapped on Windows" ,
150
+ )
151
+ def test_wraps_perm_error_if_enabled (self , mocker , permission_error_tmpdir ):
152
+ """rmtree wraps PermissionError on Windows when HIDE_WINDOWS_KNOWN_ERRORS is true."""
153
+ self ._patch_for_wrapping_test (mocker , True )
154
+
153
155
with pytest .raises (SkipTest ):
154
156
rmtree (permission_error_tmpdir )
155
157
@@ -166,10 +168,7 @@ def test_wraps_perm_error_if_enabled(self, mocker, permission_error_tmpdir):
166
168
)
167
169
def test_does_not_wrap_perm_error_unless_enabled (self , mocker , permission_error_tmpdir , hide_windows_known_errors ):
168
170
"""rmtree does not wrap PermissionError on non-Windows systems or when HIDE_WINDOWS_KNOWN_ERRORS is false."""
169
- # See comments in test_wraps_perm_error_if_enabled for details about patching.
170
- mocker .patch .object (sys .modules ["git.util" ], "HIDE_WINDOWS_KNOWN_ERRORS" , hide_windows_known_errors )
171
- mocker .patch .object (os , "chmod" )
172
- mocker .patch .object (pathlib .Path , "chmod" )
171
+ self ._patch_for_wrapping_test (mocker , hide_windows_known_errors )
173
172
174
173
with pytest .raises (PermissionError ):
175
174
try :
@@ -180,11 +179,7 @@ def test_does_not_wrap_perm_error_unless_enabled(self, mocker, permission_error_
180
179
@pytest .mark .parametrize ("hide_windows_known_errors" , [False , True ])
181
180
def test_does_not_wrap_other_errors (self , tmp_path , mocker , hide_windows_known_errors ):
182
181
file_not_found_tmpdir = tmp_path / "testdir" # It is deliberately never created.
183
-
184
- # See comments in test_wraps_perm_error_if_enabled for details about patching.
185
- mocker .patch .object (sys .modules ["git.util" ], "HIDE_WINDOWS_KNOWN_ERRORS" , hide_windows_known_errors )
186
- mocker .patch .object (os , "chmod" )
187
- mocker .patch .object (pathlib .Path , "chmod" )
182
+ self ._patch_for_wrapping_test (mocker , hide_windows_known_errors )
188
183
189
184
with pytest .raises (FileNotFoundError ):
190
185
try :
0 commit comments